-
Notifications
You must be signed in to change notification settings - Fork 26
/
BatteryVoltageUnit.cpp
812 lines (675 loc) · 22.7 KB
/
BatteryVoltageUnit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
// (c) OneOfEleven 2020
//
// This code can be used on terms of WTFPL Version 2 (http://www.wtfpl.net)
#include <vcl.h>
#include <Clipbrd.hpp>
#ifdef __BORLANDC__
#if (__BORLANDC__ < 0x0600)
#include <pngimage.hpp>
#include <Jpeg.hpp>
#include <GIFImg.hpp>
#else
#include <Vcl.Imaging.pngimage.hpp>
#include <Vcl.Imaging.jpeg.hpp>
#include <Vcl.Imaging.GIFImg.hpp>
#endif
#endif
#include <math.h>
#pragma hdrstop
#include "BatteryVoltageUnit.h"
#include "common.h"
#include "Settings.h"
#include "DataUnit.h"
#pragma package(smart_init)
#pragma resource "*.dfm"
TBatteryVoltageForm *BatteryVoltageForm = NULL;
__fastcall TBatteryVoltageForm::TBatteryVoltageForm(TComponent* Owner)
: TForm(Owner)
{
}
void __fastcall TBatteryVoltageForm::FormCreate(TObject *Sender)
{
AnsiString s;
m_initialised = false;
Caption = Application->Title + " " + this->Caption;
this->DoubleBuffered = true;
//Panel1->ControlStyle = Panel1->ControlStyle << csOpaque;
PaintBox1->ControlStyle = PaintBox1->ControlStyle << csOpaque;
SaveDialog1->InitialDir = ExtractFilePath(Application->ExeName);
m_graph_mouse_x = -1;
m_graph_mouse_y = -1;
m_graph_mouse_index = -1;
m_graph_bm = NULL;
m_gdiplusToken = 0;
Gdiplus::GdiplusStartup(&m_gdiplusToken, &m_gdiplusStartupInput, NULL);
m_battery_voltage.reserve(10000);
VBATOffsetLabel->Caption = "";
s.printf("%u", m_battery_voltage.size());
StatusBar1->Panels->Items[0]->Text = s;
// move to the saved position
this->Top = settings.batteryWindowPos.top;
this->Left = settings.batteryWindowPos.left;
this->Width = settings.batteryWindowPos.width;
this->Height = settings.batteryWindowPos.height;
}
void __fastcall TBatteryVoltageForm::FormDestroy(TObject *Sender)
{
if (m_graph_bm != NULL)
{
delete m_graph_bm;
m_graph_bm = NULL;
}
if (m_gdiplusToken != 0)
Gdiplus::GdiplusShutdown(m_gdiplusToken);
m_gdiplusToken = 0;
}
void __fastcall TBatteryVoltageForm::WMWindowPosChanging(TWMWindowPosChanging &msg)
{
const int thresh = WINDOW_SNAP;
if (msg.WindowPos->flags & SWP_STATECHANGED)
{
if (msg.WindowPos->flags & SWP_FRAMECHANGED)
{
if (msg.WindowPos->x < 0 && msg.WindowPos->y < 0)
{ // Window state is about to change to MAXIMIZED
if ((msg.WindowPos->flags & (SWP_SHOWWINDOW | SWP_NOACTIVATE)) == (SWP_SHOWWINDOW | SWP_NOACTIVATE))
{ // about to minimize
return;
}
else
{ // about to maximize
return;
}
}
}
}
if (msg.WindowPos->hwnd != this->Handle || Screen == NULL)
return;
const int dtLeft = Screen->DesktopRect.left;
//const int dtRight = Screen->DesktopRect.right;
const int dtTop = Screen->DesktopRect.top;
const int dtBottom = Screen->DesktopRect.bottom;
const int dtWidth = Screen->DesktopRect.Width();
const int dtHeight = Screen->DesktopRect.Height();
//const int waLeft = Screen->WorkAreaRect.left;
//const int waRight = Screen->WorkAreaRect.right;
//const int waTop = Screen->WorkAreaRect.top;
//const int waBottom = Screen->WorkAreaRect.bottom;
//const int waWidth = Screen->WorkAreaRect.Width();
//const int waHeight = Screen->WorkAreaRect.Height();
int x = msg.WindowPos->x;
int y = msg.WindowPos->y;
int w = msg.WindowPos->cx;
int h = msg.WindowPos->cy;
for (int i = 0; i < Screen->MonitorCount; i++)
{ // sticky screen edges
const int mLeft = Screen->Monitors[i]->WorkareaRect.left;
const int mRight = Screen->Monitors[i]->WorkareaRect.right;
const int mTop = Screen->Monitors[i]->WorkareaRect.top;
const int mBottom = Screen->Monitors[i]->WorkareaRect.bottom;
const int mWidth = Screen->Monitors[i]->WorkareaRect.Width();
const int mHeight = Screen->Monitors[i]->WorkareaRect.Height();
if (ABS(x - mLeft) < thresh)
x = mLeft; // stick left to left side
else
if (ABS((x + w) - mRight) < thresh)
x = mRight - w; // stick right to right side
if (ABS(y - mTop) < thresh)
y = mTop; // stick top to top side
else
if (ABS((y + h) - mBottom) < thresh)
y = mBottom - h; // stick bottom to bottm side
// stick the right side to the right side of the screen if the left side is stuck to the left side of the screen
if (x == mLeft)
if ((w >= (mWidth - thresh)) && (w <= (mWidth + thresh)))
w = mWidth;
// stick the bottom to the bottom of the screen if the top is stuck to the top of the screen
if (y == mTop)
if ((h >= (mHeight - thresh)) && (h <= (mHeight + thresh)))
h = mHeight;
}
/*
{ // sticky screen edges
if (ABS(x - waLeft) < thresh)
x = waLeft; // stick left to left side
else
if (ABS((x + w) - waRight) < thresh)
x = waRight - w; // stick right to right side
if (ABS(y - waTop) < thresh)
y = waTop; // stick top to top side
else
if (ABS((y + h) - waBottom) < thresh)
y = waBottom - h; // stick bottom to bottm side
// stick the right side to the right side of the screen if the left side is stuck to the left side of the screen
if (x == waLeft)
if ((w >= (waWidth - thresh)) && (w <= (waWidth + thresh)))
w = waWidth;
// stick the bottom to the bottom of the screen if the top is stuck to the top of the screen
if (y == waTop)
if ((h >= (waHeight - thresh)) && (h <= (waHeight + thresh)))
h = waHeight;
}
*/
// limit minimum size
if (w < Constraints->MinWidth)
w = Constraints->MinWidth;
if (h < Constraints->MinHeight)
h = Constraints->MinHeight;
// limit maximum size
if (w > Constraints->MaxWidth && Constraints->MaxWidth > Constraints->MinWidth)
w = Constraints->MaxWidth;
if (h > Constraints->MaxHeight && Constraints->MaxHeight > Constraints->MinHeight)
h = Constraints->MaxHeight;
// limit maximum size
if (w > dtWidth)
w = dtWidth;
if (h > dtHeight)
h = dtHeight;
/*
if (Application->MainForm && this != Application->MainForm)
{ // stick to our main form sides
const TRect rect = Application->MainForm->BoundsRect;
if (ABS(x - rect.left) < thresh)
x = rect.left; // stick to left to left side
else
if (ABS((x + w) - rect.left) < thresh)
x = rect.left - w; // stick right to left side
else
if (ABS(x - rect.right) < thresh)
x = rect.right; // stick to left to right side
else
if (ABS((x + w) - rect.right) < thresh)
x = rect.right - w; // stick to right to right side
if (ABS(y - rect.top) < thresh)
y = rect.top; // stick top to top side
else
if (ABS((y + h) - rect.top) < thresh)
y = rect.top - h; // stick bottom to top side
else
if (ABS(y - rect.bottom) < thresh)
y = rect.bottom; // stick top to bottom side
else
if (ABS((y + h) - rect.bottom) < thresh)
y = rect.bottom - h; // stick bottom to bottom side
}
*/
// stop it completely leaving the desktop area
if (x < (dtLeft - Width + (dtWidth / 15)))
x = dtLeft - Width + (dtWidth / 15);
if (x > (dtWidth - (Screen->Width / 15)))
x = dtWidth - (Screen->Width / 15);
if (y < dtTop)
y = dtTop;
if (y > (dtBottom - (dtHeight / 10)))
y = dtBottom - (dtHeight / 10);
msg.WindowPos->x = x;
msg.WindowPos->y = y;
msg.WindowPos->cx = w;
msg.WindowPos->cy = h;
}
void __fastcall TBatteryVoltageForm::show()
{
// update();
this->Show();
this->BringToFront();
// this->Update();
}
void __fastcall TBatteryVoltageForm::clear()
{
m_battery_voltage.resize(0);
AnsiString s;
s.printf("%u", m_battery_voltage.size());
StatusBar1->Panels->Items[0]->Text = s;
if (this->Showing)
PaintBox1->Invalidate();
}
void __fastcall TBatteryVoltageForm::addBatteryVoltage(const uint16_t vbat_mv, const uint16_t vbat_offset_mv)
{
AnsiString s;
if (vbat_mv > 0)
{
t_battery_voltage battery_voltage;
battery_voltage.datetime = Now();
battery_voltage.mv = vbat_mv;
m_battery_voltage.push_back(battery_voltage);
s.printf("%u samples", m_battery_voltage.size());
StatusBar1->Panels->Items[0]->Text = s;
s.printf("%0.3fV", (float)vbat_mv / 1000);
StatusBar1->Panels->Items[1]->Text = s;
}
s.printf("%u", vbat_offset_mv);
VBATOffsetLabel->Caption = s;
if (this->Showing)
PaintBox1->Invalidate();
}
void __fastcall TBatteryVoltageForm::FormShow(TObject *Sender)
{
if (!m_initialised)
{
// move to the saved position
this->Top = settings.batteryWindowPos.top;
this->Left = settings.batteryWindowPos.left;
this->Width = settings.batteryWindowPos.width;
this->Height = settings.batteryWindowPos.height;
m_initialised = true;
}
}
void __fastcall TBatteryVoltageForm::PaintBox1Paint(TObject *Sender)
{
TPaintBox *pb = dynamic_cast<TPaintBox *>(Sender);
if (pb == NULL)
return;
m_graph_mouse_index = -1;
if (m_graph_bm == NULL)
{
m_graph_bm = new Graphics::TBitmap();
if (m_graph_bm == NULL)
{
pb->Canvas->Brush->Color = pb->Color;
pb->Canvas->Brush->Style = bsSolid;
pb->Canvas->FillRect(pb->Canvas->ClipRect);
return;
}
m_graph_bm->Monochrome = false;
m_graph_bm->Transparent = false;
m_graph_bm->PixelFormat = pf32bit;
}
m_graph_bm->Canvas->Font = pb->Canvas->Font;
m_graph_bm->Width = pb->Width;
m_graph_bm->Height = pb->Height;
// background colour
m_graph_bm->Canvas->Pen->Width = 1;
m_graph_bm->Canvas->Pen->Style = psSolid;
m_graph_bm->Canvas->Pen->Color = clGray;
// m_graph_bm->Canvas->Pen->Color = pb->Color;
m_graph_bm->Canvas->Brush->Color = pb->Color;
m_graph_bm->Canvas->Brush->Style = bsSolid;
m_graph_bm->Canvas->Rectangle(m_graph_bm->Canvas->ClipRect);
const TColor back_colour = pb->Color;
const TColor grid_colour = TColor(RGB(190, 190, 190));
LOGFONT lf;
::GetObject(m_graph_bm->Canvas->Font->Handle, sizeof(LOGFONT), &lf);
lf.lfEscapement = -900; // 90 degrees rotation
lf.lfOrientation = lf.lfEscapement; // " "
lf.lfOutPrecision = OUT_TT_ONLY_PRECIS;
const HFONT lf_handle = CreateFontIndirect(&lf);
// const HFONT old_handle = ::SelectObject(bm->Canvas->Handle, lf_handle);
::SetGraphicsMode(m_graph_bm->Canvas->Handle, GM_ADVANCED);
const int line_width = 1;
m_graph_x = 55;
m_graph_y = 30;
m_graph_width = m_graph_bm->Width - m_graph_x - 50;
m_graph_height = m_graph_bm->Height - m_graph_y - 30;
// find the maximum and minimum values so far
m_max_mv = m_battery_voltage.empty() ? 4300 : m_battery_voltage[0].mv;
m_min_mv = m_battery_voltage.empty() ? 3000 : m_battery_voltage[0].mv;
for (unsigned int i = 0; i < m_battery_voltage.size(); i++)
{
const uint16_t mv = m_battery_voltage[i].mv;
if (m_max_mv < mv)
m_max_mv = mv;
if (m_min_mv > mv)
m_min_mv = mv;
}
m_max_mv = ((m_max_mv + 100) / 100) * 100; // 100mV steps
if (m_min_mv >= 100)
m_min_mv = ((m_min_mv - 0) / 100) * 100; // 100mV steps
else
m_min_mv = 0;
if (m_max_mv < (m_min_mv + 100))
m_max_mv = m_min_mv + 100;
const double start_dt = m_battery_voltage.empty() ? (TDateTime)0.0 : m_battery_voltage[0].datetime;
const double end_dt = (m_battery_voltage.size() < 1) ? (TDateTime)0.0 : m_battery_voltage[m_battery_voltage.size() - 1].datetime;
const double span_dt = end_dt - start_dt;
const int text_height = m_graph_bm->Canvas->TextHeight("Hq");
const float x_scale = (span_dt > 0) ? (float)m_graph_width / span_dt : 0;
const float y_scale = (float)m_graph_height / (m_max_mv - m_min_mv);
m_gdi_points.resize(0);
{ // draw vertical lines
m_graph_bm->Canvas->Pen->Style = psDot;
m_graph_bm->Canvas->Brush->Style = bsSolid;
m_graph_bm->Canvas->Brush->Color = back_colour;
m_graph_bm->Canvas->Pen->Color = grid_colour;
const int num_lines = 19;
const int min_text_spacing = 15;
const int y1 = m_graph_y;
const int y2 = m_graph_y + m_graph_height;
if (m_battery_voltage.size() < 2)
{
const float x_scale = (float)m_graph_width / (num_lines - 1);
for (int i = 0; i < num_lines; i++)
{
// vertical line
const int x = m_graph_x + IROUND(i * x_scale);
m_graph_bm->Canvas->MoveTo(x, y1);
m_graph_bm->Canvas->LineTo(x, y2);
}
}
else
{
for (int i = 0, x2 = -min_text_spacing; i < num_lines; i++)
{
const double delta_time = (span_dt * i) / (num_lines - 1);
const double date_time = start_dt + delta_time;
const int x = m_graph_x + IROUND(delta_time * x_scale);
// vertical line
m_graph_bm->Canvas->MoveTo(x, y1);
m_graph_bm->Canvas->LineTo(x, y2);
// upper text
AnsiString s1 = FormatDateTime("hh:mm:ss", delta_time);
const int tw1 = m_graph_bm->Canvas->TextWidth(s1);
const int tx1 = x - (tw1 / 2);
const int ty1 = y1 - 10 - text_height;
// lower text
AnsiString s2 = FormatDateTime("hh:mm:ss", date_time);
const int tw2 = m_graph_bm->Canvas->TextWidth(s2);
const int tx2 = x - (tw2 / 2);
const int ty2 = y2 + 10;
// draw the text
if (tx1 >= (x2 + min_text_spacing) && tx2 >= (x2 + min_text_spacing))
{
m_graph_bm->Canvas->TextOut(tx1, ty1, s1);
m_graph_bm->Canvas->TextOut(tx2, ty2, s2);
x2 = (tx1 >= tx2) ? tx1 + tw1 : tx2 + tw2;
}
}
}
m_graph_bm->Canvas->Pen->Style = psSolid;
}
{ // draw horizontal lines
AnsiString s;
m_graph_bm->Canvas->Pen->Style = psDot;
m_graph_bm->Canvas->Brush->Style = bsSolid;
m_graph_bm->Canvas->Brush->Color = back_colour;
m_graph_bm->Canvas->Pen->Color = grid_colour;
const int x1 = m_graph_x;
const int x2 = m_graph_x + m_graph_width;
const int tx = x1 - 10 - m_graph_bm->Canvas->TextWidth("0.0");
{
s = "Battery Voltage";
const int ty = m_graph_y + ((m_graph_height - m_graph_bm->Canvas->TextWidth(s)) / 2);
const HFONT old_handle = (HFONT)::SelectObject(m_graph_bm->Canvas->Handle, lf_handle);
m_graph_bm->Canvas->TextOut(tx - 5, ty, s);
::SelectObject(m_graph_bm->Canvas->Handle, old_handle);
}
for (int mv = m_min_mv; mv <= m_max_mv; mv += 100)
{
const int y = m_graph_y + m_graph_height - IROUND((mv - m_min_mv) * y_scale);
// horizontal line
m_graph_bm->Canvas->MoveTo(x1, y);
m_graph_bm->Canvas->LineTo(x2, y);
s.printf("%0.1f", (float)mv / 1000);
const int ty = y - (text_height / 2);
// draw the text
m_graph_bm->Canvas->TextOut(tx, ty, s);
}
m_graph_bm->Canvas->Pen->Style = psSolid;
}
for (unsigned int i = 0; i < m_battery_voltage.size(); i++)
{
const uint16_t mv = m_battery_voltage[i].mv;
const TDateTime date_time = m_battery_voltage[i].datetime;
Gdiplus::PointF point;
point.X = m_graph_x + (((double)date_time - start_dt) * x_scale);
point.Y = m_graph_y + m_graph_height - ((mv - m_min_mv) * y_scale);
m_gdi_points.push_back(point);
}
// draw the curve/line
if (m_gdi_points.size() >= 2)
{
Gdiplus::Graphics g(m_graph_bm->Canvas->Handle);
// g.SetSmoothingMode(Gdiplus::SmoothingModeNone);
// g.SetSmoothingMode(Gdiplus::SmoothingModeHighSpeed); // fast
g.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias); // high quality
// g.SetSmoothingMode(Gdiplus::SmoothingModeHighQuality);
// g.SetPixelOffsetMode(Gdiplus::PixelOffsetModeNone);
g.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHalf);
const int alpha = 255;
const int red = 0;
const int grn = 0;
const int blu = 0;
Gdiplus::Pen pen(Gdiplus::Color(alpha, red, grn, blu), line_width);
pen.SetAlignment(Gdiplus::PenAlignmentCenter);
pen.SetDashStyle(Gdiplus::DashStyleSolid);
// pen.SetDashStyle(Gdiplus::DashStyleDot);
g.DrawLines(&pen, &m_gdi_points[0], m_gdi_points.size());
}
if (!m_gdi_points.empty())
{
AnsiString s;
{ // plot the graph points
const int point_size = 1 + line_width;
if (fabs(m_gdi_points[1].X - m_gdi_points[0].X) >= (point_size * 4))
{
m_graph_bm->Canvas->Pen->Color = TColor(RGB(0, 0, 0));
m_graph_bm->Canvas->Pen->Width = 1;
m_graph_bm->Canvas->Brush->Color = (point_size <= 2) ? m_graph_bm->Canvas->Pen->Color : back_colour;
for (unsigned int i = 0; i < m_gdi_points.size(); i++)
{
const int x = IROUND(m_gdi_points[i].X);
const int y = IROUND(m_gdi_points[i].Y);
m_graph_bm->Canvas->Ellipse(IROUND(x - point_size), IROUND(y - point_size), IROUND(x + point_size), IROUND(y + point_size));
}
}
}
{ // draw the voltage at the last point
const int index = m_battery_voltage.size() - 1;
const uint16_t mv = m_battery_voltage[index].mv;
s.printf("%0.3f", (float)mv / 1000);
const int tx = m_graph_x + m_graph_width + 10;
const int y = m_graph_y + m_graph_height - ((mv - m_min_mv) * y_scale);
m_graph_bm->Canvas->Brush->Color = back_colour;
m_graph_bm->Canvas->TextOut(tx, y - (text_height / 2), s);
}
if (!m_battery_voltage.empty() &&
m_graph_mouse_x >= m_graph_x &&
m_graph_mouse_y >= m_graph_y &&
m_graph_mouse_x <= m_graph_x + m_graph_width &&
m_graph_mouse_y <= m_graph_y + m_graph_height)
{ // draw the mouse
int index = -1;
{ // find the closest point
float min_dist = -1.0f;
for (unsigned int i = 0; i < m_gdi_points.size(); i++)
{
const Gdiplus::PointF point = m_gdi_points[i];
const float delta_x = (float)m_graph_mouse_x - point.X;
const float delta_y = (float)m_graph_mouse_y - point.Y;
const float dist = (delta_x * delta_x) + (delta_y * delta_y);
if (min_dist < 0.0f || min_dist > dist)
{
min_dist = dist;
index = i;
}
}
}
if (index >= 0 && index < (int)m_battery_voltage.size())
{
const int mv = m_battery_voltage[index].mv;
const TDateTime date_time = m_battery_voltage[index].datetime;
const int x = m_graph_x + IROUND(((double)date_time - start_dt) * x_scale);
const int y = m_graph_y + m_graph_height - IROUND((mv - m_min_mv) * y_scale);
// if (std::abs(m_graph_mouse_x - x) <= 20 && std::abs(m_graph_mouse_y - y) <= 30)
{
m_graph_mouse_index = index;
const int marker_size = 6;
{ // marker
TPoint points[4];
points[0].x = x; points[0].y = y - 2;
points[1].x = x - marker_size; points[1].y = y - marker_size * 2;
points[2].x = x + marker_size; points[2].y = y - marker_size * 2;
points[3].x = x; points[3].y = y - 2;
m_graph_bm->Canvas->Pen->Style = psSolid;
m_graph_bm->Canvas->Pen->Color = TColor(RGB(0, 0, 0));
m_graph_bm->Canvas->Pen->Width = 1;
// m_graph_bm->Canvas->Brush->Style = bsClear;
m_graph_bm->Canvas->Brush->Color = back_colour;
m_graph_bm->Canvas->Polygon(&points[0], 3);
}
{ // index text
s.printf("%d", index);
TSize ts = m_graph_bm->Canvas->TextExtent(s);
const int tx = x - (ts.cx / 2);
const int ty = y - marker_size - 10 - (text_height * 3);
m_graph_bm->Canvas->Brush->Color = back_colour;
m_graph_bm->Canvas->TextOut(tx, ty, s);
}
{ // time text
s = FormatDateTime("hh:mm:ss", date_time);
TSize ts = m_graph_bm->Canvas->TextExtent(s);
const int tx = x - (ts.cx / 2);
const int ty = y - marker_size - 10 - (text_height * 2);
m_graph_bm->Canvas->Brush->Color = back_colour;
m_graph_bm->Canvas->TextOut(tx, ty, s);
}
{ // voltage text
s.printf("%0.3fV", (float)mv / 1e3);
const int tx = x - (m_graph_bm->Canvas->TextWidth(s) / 2);
const int ty = y - marker_size - 10 - (text_height * 1);
m_graph_bm->Canvas->Brush->Color = back_colour;
m_graph_bm->Canvas->TextOut(tx, ty, s);
}
}
}
}
}
::DeleteObject(lf_handle);
pb->Canvas->Draw(0, 0, m_graph_bm);
}
void __fastcall TBatteryVoltageForm::saveImage(const bool to_clipboard)
{
if (m_graph_bm == NULL)
{
Application->NormalizeTopMosts();
Application->MessageBox(L"No image to save", L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
if (to_clipboard)
{
try
{
Clipboard()->Assign(m_graph_bm); // copy the image to the clipboard
}
catch (Exception &exception)
{
Application->ShowException(&exception);
//String s = exception.ToString();
}
return;
}
String filename = data_unit.m_vna_data.name.LowerCase() + "_bat_volt_" + FormatDateTime("yyyy-mm-dd_hh-nn-ss", Now()) + ".png";
filename = common.cleanFilename(filename, false, true);
SaveDialog1->FileName = filename;
Application->NormalizeTopMosts();
const bool ok = SaveDialog1->Execute();
Application->RestoreTopMosts();
if (!ok)
return;
filename = SaveDialog1->FileName;
if (filename.IsEmpty())
{
Application->NormalizeTopMosts();
Application->MessageBox(L"Invalid filename characters", L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
return;
}
String ext = ExtractFileExt(filename).LowerCase();
if (ext == ".bmp")
{
try
{
m_graph_bm->SaveToFile(filename);
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return;
}
if (ext == ".jpg" || ext == ".jpeg")
{
TJPEGImage *jpg;
try
{
jpg = new TJPEGImage;
if (jpg == NULL)
return;
jpg->Assign(m_graph_bm);
jpg->CompressionQuality = 100; // 0 to 100
jpg->SaveToFile(filename);
}
__finally
{
if (jpg != NULL)
delete jpg;
}
return;
}
if (ext == ".png")
{
TPngImage *png;
try
{
png = new TPngImage();
if (png == NULL)
return;
png->Assign(m_graph_bm);
png->CompressionLevel = 9; // 0 to 9
png->SaveToFile(filename);
}
__finally
{
if (png != NULL)
delete png;
}
return;
}
Application->NormalizeTopMosts();
Application->MessageBox(L"Only PNG, JPG, JPEG and BMP extensions are supported", L"Error", MB_ICONERROR | MB_OK);
Application->RestoreTopMosts();
}
void __fastcall TBatteryVoltageForm::PaintBox1MouseMove(TObject *Sender,
TShiftState Shift, int X, int Y)
{
m_graph_mouse_x = X;
m_graph_mouse_y = Y;
PaintBox1->Invalidate();
}
void __fastcall TBatteryVoltageForm::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
switch (Key)
{
case VK_ESCAPE:
Key = 0;
this->Hide();
break;
}
}
void __fastcall TBatteryVoltageForm::PaintBox1MouseLeave(TObject *Sender)
{
m_graph_mouse_x = -1;
m_graph_mouse_y = -1;
m_graph_mouse_index = -1;
}
void __fastcall TBatteryVoltageForm::CloseBitBtnClick(TObject *Sender)
{
Hide();
}
void __fastcall TBatteryVoltageForm::FormMouseEnter(TObject *Sender)
{
// this->SetFocus();
}
void __fastcall TBatteryVoltageForm::SaveBitBtnClick(TObject *Sender)
{
saveImage(false);
}
void __fastcall TBatteryVoltageForm::CopyBitBtnClick(TObject *Sender)
{
saveImage(true);
}
void __fastcall TBatteryVoltageForm::ClearBitBtnClick(TObject *Sender)
{
clear();
}