-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWingraph.cpp
More file actions
2664 lines (2127 loc) · 67.4 KB
/
Copy pathWingraph.cpp
File metadata and controls
2664 lines (2127 loc) · 67.4 KB
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
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "WinGraph.h"
#include "xlsx.h"
#include "Mouse.h"
#include "math.h"
#include "datetimeapi.h"
#include "fileapi.h"
#pragma comment (lib, "gdi32.lib")
#pragma comment (lib, "User32.lib")
#pragma comment (lib, "Opengl32.lib")
#pragma comment (lib, "Glu32.lib")
// Private declarations goes here
enum { MAX_SIGNAL_COUNT = 64 };
VOID InitGL(HGRAPH hGraph, int Width, int Height);
VOID SetGLView(int Width, int Height);
VOID CheckErr(VOID);
BOOL BuildMyFont(HGRAPH hGraph, char* FontName, int Fontsize);
void KillFont(GLvoid);
GLvoid glPrint(const char* fmt, ...);
BOOL FindGlobalMaxScale(HGRAPH hGraph, double& Xmin, double& Xmax, double& Ymin, double& Ymax);
VOID DrawWave(HGRAPH hGraph);
VOID DrawString(float x, float y, char* string);
VOID DrawGraphSquare(VOID);
VOID DrawGridLines(VOID);
VOID DrawCursor(float x, float y);
inline double TakeFiniteNumber(double x);
double FindFirstFiniteNumber(double* tab, int length);
LPSTR dtos(LPSTR str, int len, double value);
double GetStandardizedData(double X, double min, double max);
VOID normalize_data(HGRAPH hGraph, double Xmin, double Xmax, double Ymin, double Ymax);
VOID UpdateBorder(HGRAPH hGraph);
INT GetBufferSize(HGRAPH hGraph);
BOOL GetUniqueFilename(CHAR* lpFilename, CHAR* lpFileExtension);
inline long long PerformanceFrequency();
inline long long PerformanceCounter();
typedef struct {
double period_s;
double min_value;
double max_value;
INT average_value_counter;
double average_value_accumulator;
double average_value;
}DATA_STATISTIC;
typedef struct {
char signame[260];
float color[3];
bool show;
DATA_STATISTIC stat;
double*X;
double*Y;
double*Xnorm;
double*Ynorm;
double Xmin;
double Xmax;
double Ymin;
double Ymax;
double Yaverage;
}DATA;
VOID ZeroObject(DATA* pDATA, INT iBufferSize);
#pragma warning(disable : 4200) // Disable warning: DATA* signal[] -> Array size [0], See CreateGraph for signal allocation specifics
typedef struct {
HWND hParentWnd; // Parent handle of the object
HWND hGraphWnd; // Graph handle
HDC hDC; // OpenGL device context
HGLRC hRC; // OpenGL rendering context
INT totalsignalcount; // Total signals in the struct
INT signalcount; // signals in use in the struct
INT cur_nbpoints; // Current total points in the arrays
INT BufferSize; // The total amount of point to handle
BOOL bRunning; // Status of the graph
LOGGER_M Logging; // Logging type
FILTER_M Filtering; // Filtering type
BOOL bAutoscale; // Autoscale active
BOOL bDisplayCursor; // Logging active
double ymin_fix; // Fix the Y min val
double ymax_fix; // Fix the Y max val
int scale_factor; // Fix the X scale factor (zoom)
double xwindow_fix; // Fix the time windows val
DATA* signal[]; // ! (flexible array member) Array of pointers for every signal to be store by the struct - Must be last member of the struct
}GRAPHSTRUCT, * PGRAPHSTRUCT; // Declaration of the struct. To be cast from HGRAPH api
// Global access
DATA* SnapPlot; // SnapPlot: work with temp data on signals[], used to convert standard values to normalized values
RECT DispArea; // RECT struct for the OpenGL area dimensions stored in WinProc
GLuint base; // Base Display List For The Font Set
SIZE dispStringWidth; // The size in pixel of "-0.000" displayed on screen
CRITICAL_SECTION cs; // Sync purpose
FILE* logfile; // The ascii log file
HEXCEL XL; // The xlsx log file
INT runonce; // Used by UpdateBorder
// High precision time measurements
long long frequency;
long long start;
long long finish;
GLuint PixelFormat; //Defining the pixel format to display OpenGL
PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number (?)
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
32, // Select A 32Bit Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored (?)
0, // No Alpha Buffer
0, // Shift Bit Ignored (?)
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored (?)
24, // 32Bit Z-Buffer (Depth Buffer)
8, // No Stencil Buffer
0, // No Auxiliary Buffer (?)
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved (?)
0, 0, 0 // Layer Masks Ignored (?)
};
/*-------------------------------------------------------------------------
DllMain: DLL Entry point
-------------------------------------------------------------------------*/
BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return true;
}
/*-------------------------------------------------------------------------
StartGraph: Setup a new log file and zero memory
-------------------------------------------------------------------------*/
BOOL StartGraph(HGRAPH hGraph)
{
// Sanity check
if (NULL == hGraph)
{
printf("[!] Error at StartGraph() graph handle is null\n");
return FALSE;
}
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
DATA* pDATA;
// Sanity check
if (TRUE == pgraph->bRunning)
{
printf("[!] Error at StartGraph() graph already running\n");
return FALSE;
}
EnterCriticalSection(&cs);
// reset counters and data array of signals
pgraph->cur_nbpoints = 0;
for (int index = 0; index < pgraph->signalcount; index++)
{
pDATA = pgraph->signal[index];
memset(pDATA->X, 0, sizeof(double)* pgraph->BufferSize);
memset(pDATA->Y, 0, sizeof(double) * pgraph->BufferSize);
memset(pDATA->Xnorm, 0, sizeof(double) * pgraph->BufferSize);
memset(pDATA->Ynorm, 0, sizeof(double) * pgraph->BufferSize);
pDATA->Xmin = 0.0f;
pDATA->Xmax = 0.0f;
pDATA->Ymin = 0.0f;
pDATA->Ymax = 0.0f;
pDATA->show = true;
}
// Create the log file
if (pgraph->Logging == LOGGER_ASCII)
{
logfile = NULL;
// create unique filename
char lpDateStr[MAX_PATH] = "";
if (!GetUniqueFilename(lpDateStr, (char*)".lab"))
{
MessageBox(GetFocus(), "Error: impossible to generate an unique filename in the current directory", "Error", MB_ICONERROR);
LeaveCriticalSection(&cs);
return FALSE;
}
// try to open the file
fopen_s(&logfile, lpDateStr, "w+");
if (!logfile)
{
MessageBox(GetFocus(), "Error: impossible to read/write the file", "Error", MB_ICONERROR);
LeaveCriticalSection(&cs);
return FALSE;
}
// make logfile header
fprintf(logfile, "Time(s)");
for (int u = 0; u < pgraph->signalcount; u++)
{
pDATA = (DATA*)pgraph->signal[u];
fprintf_s(logfile, "\t%s", pDATA->signame);
}
fprintf_s(logfile, "\n");
}
if (pgraph->Logging == LOGGER_XLSX)
{
// create unique filename
char lpDateStr[MAX_PATH] = "";
if (!GetUniqueFilename(lpDateStr, (char*)".xlsx"))
{
MessageBox(GetFocus(), "Error: impossible to generate an unique filename in the current directory", "Error", MB_ICONERROR);
LeaveCriticalSection(&cs);
return FALSE;
}
// open Excel and load instance
XL = excel_create_instance();
// Write header
char one_line[64] = "Logger header\tAnalog0\tAnalog1\t";
//excel_addline(XL, one_line);
}
// Save the start time x=0
frequency = PerformanceFrequency();
start = PerformanceCounter();
// Update status -> Graph ON
pgraph->bRunning = TRUE;
// reset runonce flag
runonce = 0;
LeaveCriticalSection(&cs);
return TRUE;
}
/*-------------------------------------------------------------------------
StopGraph: Close the logfile if needed and
update state flags
-------------------------------------------------------------------------*/
VOID StopGraph(HGRAPH hGraph)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at StopGraph() graph handle is null\n");
return ;
}
//Close the log file properly
EnterCriticalSection(&cs);
if (pgraph->Logging == LOGGER_ASCII)
{
if (logfile)
{
fclose(logfile);
logfile = NULL;
}
}
if (pgraph->Logging == LOGGER_XLSX)
{
if (XL)
{
excel_drawgraph(XL);
excel_save(XL, "test_excel.xlsx");
excel_close(XL);
}
}
// Update status -> Graph OFF
pgraph->bRunning = FALSE;
LeaveCriticalSection(&cs);
}
/*-------------------------------------------------------------------------
FreeGraph: Free every buffer allocated by malloc
Realease the device context and delete the object
-------------------------------------------------------------------------*/
VOID FreeGraph(HGRAPH *hGraph)
{
// Sanity check
if (NULL == hGraph)
return;
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)*hGraph; // Take the reference not the value
EnterCriticalSection(&cs);
if (pgraph !=NULL)
{
DATA* pDATA;
for (int index = 0; index < pgraph->totalsignalcount; index++)
{
pDATA = pgraph->signal[index];
if (pDATA)
{
if (pDATA->X)
free(pDATA->X);
if (pDATA->Y)
free(pDATA->Y);
if (pDATA->Xnorm)
free(pDATA->Xnorm);
if (pDATA->Ynorm)
free(pDATA->Ynorm);
free(pDATA);
}
}
wglMakeCurrent(pgraph->hDC, NULL);
wglDeleteContext(pgraph->hRC);
ReleaseDC(pgraph->hParentWnd, pgraph->hDC);
free(pgraph);
if (SnapPlot)
{
if (SnapPlot->X)
free(SnapPlot->X);
if (SnapPlot->Y)
free(SnapPlot->Y);
if (SnapPlot->Xnorm)
free(SnapPlot->Xnorm);
if (SnapPlot->Ynorm)
free(SnapPlot->Ynorm);
free(SnapPlot);
}
if (logfile)
{
fclose(logfile);
logfile = NULL;
}
}
LeaveCriticalSection(&cs);
DeleteCriticalSection(&cs);
*hGraph = NULL;
}
/*-------------------------------------------------------------------------
CreateGraph: Initialize the structure, signals,
OpenGL and critical section. return HGRAPH
-------------------------------------------------------------------------*/
HGRAPH CreateGraph(HWND hWnd, RECT GraphArea, INT SignalCount, INT BufferSize )
{
int PFDID;
static GRAPHSTRUCT* pgraph = NULL;
// Sanity check
if (NULL != pgraph)
return pgraph;
if (NULL == hWnd)
{
printf("[!] No control available to load the graph in CreateGraph()\n");
return NULL;
}
if (0 == SignalCount || MAX_SIGNAL_COUNT < SignalCount || 0 >= BufferSize)
{
printf("[!] SignalCount not in range in CreateGraph()\n");
return NULL;
}
// Initialyze sync
InitializeCriticalSection(&cs);
EnterCriticalSection(&cs);
// Init struct
if (NULL == (pgraph = (GRAPHSTRUCT*)malloc(sizeof(GRAPHSTRUCT) + sizeof(void*) * SignalCount))) // Carefully taking in account signal declaration DATA*signal[], so allocate space for each new ptr on the fly
{
LeaveCriticalSection(&cs);
printf("[!] malloc() failed in CreateGraph()\n");
return NULL; // Otherwize Heap will be corrupted
}
// Struct memory zero at startup
memset(pgraph, 0, sizeof(GRAPHSTRUCT) + sizeof(void*) * SignalCount);
// Allocate each signal buffers on the Heap and fill with zero
for (int i = 0; i < SignalCount; i++)
{
if (NULL == (pgraph->signal[i] = (DATA*)malloc(sizeof(DATA))))
{
LeaveCriticalSection(&cs);
printf("[!] malloc failed to build signals buffer in CreateGraph()\n");
return NULL;
}
DATA* pDATA = pgraph->signal[i];
if (NULL == (pDATA->X = (double*)malloc(sizeof(double) * BufferSize)))
{
LeaveCriticalSection(&cs);
printf("[!] malloc failed to build signals buffer in CreateGraph()\n");
return NULL;
}
if (NULL == (pDATA->Y = (double*)malloc(sizeof(double) * BufferSize)))
{
LeaveCriticalSection(&cs);
printf("[!] malloc failed to build signals buffer in CreateGraph()\n");
return NULL;
}
if (NULL == (pDATA->Xnorm = (double*)malloc(sizeof(double) * BufferSize)))
{
LeaveCriticalSection(&cs);
printf("[!] malloc failed to build signals buffer in CreateGraph()\n");
return NULL;
}
if (NULL == (pDATA->Ynorm = (double*)malloc(sizeof(double) * BufferSize)))
{
LeaveCriticalSection(&cs);
printf("[!] malloc failed to build signals buffer in CreateGraph()\n");
return NULL;
}
memset(pDATA->X, 0, sizeof(double) * BufferSize);
memset(pDATA->Y, 0, sizeof(double) * BufferSize);
memset(pDATA->Xnorm, 0, sizeof(double) * BufferSize);
memset(pDATA->Ynorm, 0, sizeof(double) * BufferSize);
pDATA->Xmax = 0.0;
pDATA->Xmin = 0.0;
pDATA->Ymax = 0.0;
pDATA->Ymin = 0.0;
pDATA->stat.min_value = 0.0;
pDATA->stat.average_value_accumulator = 0.0;
pDATA->stat.average_value_counter = 0;
pDATA->stat.average_value = 0.0;
pDATA->stat.max_value = 0.0;
// set default signal name
snprintf(pDATA->signame, sizeof(pDATA->signame) - 1, "Analog%i", i);
// set default signal color
pDATA->color[0] = 0.5f; pDATA->color[1] = 0.5f; pDATA->color[2] = 0.01f*i;
// Update the number of signal inside object HGRAPH
pgraph->signalcount++;
}
pgraph->totalsignalcount = pgraph->signalcount;
pgraph->BufferSize = BufferSize;
// Allocate a temp struct for computing
SnapPlot = NULL;
if(NULL== (SnapPlot = (DATA*)malloc(sizeof(DATA))))
{
LeaveCriticalSection(&cs);
printf("[!] malloc failed to build temp signals buffer in CreateGraph()\n");
return NULL;
}
if (NULL == (SnapPlot->X = (double*)malloc(sizeof(double) * BufferSize)))
{
LeaveCriticalSection(&cs);
printf("[!] malloc failed to build temp signals buffer in CreateGraph()\n");
return NULL;
}
if (NULL == (SnapPlot->Y = (double*)malloc(sizeof(double) * BufferSize)))
{
LeaveCriticalSection(&cs);
printf("[!] malloc failed to build temp signals buffer in CreateGraph()\n");
return NULL;
}
if (NULL == (SnapPlot->Xnorm = (double*)malloc(sizeof(double) * BufferSize)))
{
LeaveCriticalSection(&cs);
printf("[!] malloc failed to build temp signals buffer in CreateGraph()\n");
return NULL;
}
if (NULL == (SnapPlot->Ynorm = (double*)malloc(sizeof(double) * BufferSize)))
{
LeaveCriticalSection(&cs);
printf("[!] malloc failed to build temp signals buffer in CreateGraph()\n");
return NULL;
}
memset(SnapPlot->X, 0, sizeof(double) * BufferSize);
memset(SnapPlot->Y, 0, sizeof(double) * BufferSize);
memset(SnapPlot->Xnorm, 0, sizeof(double) * BufferSize);
memset(SnapPlot->Ynorm, 0, sizeof(double) * BufferSize);
pgraph->hParentWnd = hWnd;
// Graph created in a "Static" control windows class named ""
// When redrawn the control will be painted with the graph in place of
//printf("[*] CreateGraph() of position l:%i t:%i r:%i b:%i \n", GraphArea.left, GraphArea.top, GraphArea.right, GraphArea.bottom );
pgraph->hGraphWnd = CreateWindow(
"Static", // Predefined class; Unicode assumed
"", // The text will be erased by OpenGL
WS_EX_TRANSPARENT | WS_VISIBLE | WS_CHILD, // Styles WS_EX_TRANSPARENT mandatory
GraphArea.left, // x pos
GraphArea.top, // y pos
GraphArea.right, // Graph width
GraphArea.bottom, // Graph height
hWnd, // Parent window
NULL, // No menu.
(HINSTANCE)GetWindowLongPtr(hWnd, GWLP_HINSTANCE), // HINST of the app
NULL); // No parameters
// Sanity check
if (NULL == pgraph->hGraphWnd)
{
printf("[!] CreateWindow() failed in CreateGraph()\n");
LeaveCriticalSection(&cs);
return NULL;
}
pgraph->hDC = GetDC(pgraph->hGraphWnd);
// Sanity check
if (NULL == pgraph->hDC)
{
printf("[!] GetDC() failed in CreateGraph()\n");
LeaveCriticalSection(&cs);
return NULL;
}
// Load OpenGL specific to legacy version
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cAlphaBits = 8;
pfd.cDepthBits = 24;
// To support advanced pixel format it is needed to load modern OpenGL functions
// Only the legacy version is supported here
PFDID = ChoosePixelFormat(pgraph->hDC, &pfd);
if (PFDID == 0)
{
LeaveCriticalSection(&cs);
MessageBox(0, "[!] Can't Find A Suitable PixelFormat.", "Error", MB_OK | MB_ICONERROR);
PostQuitMessage(0);
return NULL;
}
if (SetPixelFormat(pgraph->hDC, PFDID, &pfd) == false)
{
LeaveCriticalSection(&cs);
MessageBox(0, "[!] Can't Set The PixelFormat.", "Error", MB_OK | MB_ICONERROR);
PostQuitMessage(0);
return NULL;
}
// Rendering Context
pgraph->hRC = wglCreateContext(pgraph->hDC);
if (pgraph->hRC == 0)
{
LeaveCriticalSection(&cs);
MessageBox(0, "[!] Can't Create A GL Rendering Context.", "Error", MB_OK | MB_ICONERROR);
PostQuitMessage(0);
return NULL;
}
if (wglMakeCurrent(pgraph->hDC, pgraph->hRC) == false)
{
LeaveCriticalSection(&cs);
MessageBox(0, "[!] Can't activate GLRC.", "Error", MB_OK | MB_ICONERROR);
PostQuitMessage(0);
return NULL;
}
pgraph->scale_factor = 1;
pgraph->bAutoscale = true;
pgraph->bDisplayCursor = true;
pgraph->Logging = LOGGER_NONE;
pgraph->Filtering = FILTER_NONE;
GetClientRect(pgraph->hGraphWnd, &DispArea);
InitGL(pgraph, DispArea.right, DispArea.bottom);
ReshapeGraph(pgraph, DispArea.left, DispArea.top, DispArea.right, DispArea.bottom );
LeaveCriticalSection(&cs);
return pgraph;
}
/*-------------------------------------------------------------------------
SetSignalCount: set a new total signal count ;
must be in range between [0;MAX_SIGNAL_COUNT]
-------------------------------------------------------------------------*/
BOOL SetSignalCount(HGRAPH hGraph,CONST INT iSignalNumber)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SetSignalCount() graph handle is null\n");
return FALSE;
}
if (iSignalNumber > MAX_SIGNAL_COUNT)
{
printf("[!] Error at SetSignalCount()\n%i signal max is reached\n", pgraph->signalcount);
return FALSE;
}
pgraph->signalcount = iSignalNumber;
return TRUE;
}
/*-------------------------------------------------------------------------
SetSignalLabel: set a name to a signal [0;MAXSIG-1]
-------------------------------------------------------------------------*/
VOID SetSignalLabel(HGRAPH hGraph, CONST CHAR szLabel[260], INT iSignalNumber)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SetSignalLabel()\ngraph handle is null\n");
return;
}
if (iSignalNumber < 0 || iSignalNumber >= pgraph->signalcount)
{
printf("[!] Error at SetSignalLabel()\nrange must be [1;%i] and is %i\n", pgraph->signalcount, iSignalNumber);
return;
}
printf("[*] a new signal name is assigned: %s\n", szLabel);
DATA* signal = (DATA*)pgraph->signal[iSignalNumber];
strncpy_s(signal->signame, szLabel, sizeof(signal->signame)-1);
}
/*-------------------------------------------------------------------------
SetSignalColor: Specify a RGB color [0-255]
-------------------------------------------------------------------------*/
VOID SetSignalColor(HGRAPH hGraph, INT R, INT G, INT B, INT iSignalNumber)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SetSignalColor() graph handle is null\n");
return;
}
if(iSignalNumber> MAX_SIGNAL_COUNT-1)
{
printf("[!] Error at SetSignalColor() MAX_SIGNAL_COUNT reached \n");
return;
}
if (R < 0 || R> 255)
{
printf("[!] Error at SetSignalColor() R value overflow R:%i\n", R);
return;
}
if (G < 0 || G> 255)
{
printf("[!] Error at SetSignalColor() G value overflow G:%i\n", G);
return;
}
if (B < 0 || B> 255)
{
printf("[!] Error at SetSignalColor() B value overflow B:%i\n", B);
return;
}
printf("[*] a new signal color is assigned: RGBf (%i %i %i) at position: %i\n", R,G,B, iSignalNumber);
DATA* signal = (DATA*)pgraph->signal[iSignalNumber];
if (NULL == signal)
{
printf("[!] Error at SetSignalColor() graph signal %i is null\n", iSignalNumber);
return;
}
signal->color[0] = (float)R / 255.0f;
signal->color[1] = (float)G / 255.0f;
signal->color[2] = (float)B / 255.0f;
//////////////////////////////////////////////////
for (int i = 0; i < pgraph->signalcount; i++)
{
signal = (DATA*)pgraph->signal[i];
printf("[WINGRAPH]%i %s (%i %i %i)\n",i, signal->signame, (int)(signal->color[0]*255.0f), (int)(signal->color[1] * 255.0f), (int)(signal->color[2] * 255.0f));
}
}
/*-------------------------------------------------------------------------
SetSignalVisible: Enable or disable specific signal on graph
-------------------------------------------------------------------------*/
VOID SetSignalVisible(HGRAPH hGraph, BOOL bDisplay, INT iSignalNumber)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SetSignalVisible()\ngraph handle is null\n");
return;
}
if (iSignalNumber < 0 || iSignalNumber >= pgraph->signalcount)
{
printf("[!] Error at SetSignalVisible()\nrange must be [1;%i] and is %i\n", pgraph->signalcount, iSignalNumber);
return;
}
DATA* signal = (DATA*)pgraph->signal[iSignalNumber];
signal->show = bDisplay;
printf("[*] Signal visibility changed at signal number: %i\n", iSignalNumber);
}
/*-------------------------------------------------------------------------
SetRecordingMode: set the graph reccording state with bLogging
-------------------------------------------------------------------------*/
VOID SetRecordingMode(HGRAPH hGraph, LOGGER_M logging)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SetRecordingMode() graph handle is null\n");
return;
}
pgraph->Logging = logging;
}
/*-------------------------------------------------------------------------
SetAutoscaleMode: set autoscale
-------------------------------------------------------------------------*/
VOID SetAutoscaleMode(HGRAPH hGraph, BOOL mode)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SetAutoscaleMode() graph handle is null\n");
return;
}
pgraph->bAutoscale = mode;
if (mode == FALSE)
{
if (pgraph->bRunning)
{
DATA* pData = NULL;
pData = (DATA * )pgraph->signal[0];
// TODO Check every signal not just once
// only chan 1 is evaluated
pgraph->ymax_fix = pData->Ymax;
pgraph->ymin_fix = pData->Ymin;
pgraph->xwindow_fix = pData->X[pgraph->cur_nbpoints-1];
}
}
}
/*-------------------------------------------------------------------------
SetDisplayCursor: Add indicator bellow the mouse cursor with X and Y values
-------------------------------------------------------------------------*/
VOID SetDisplayCursor(HGRAPH hGraph, BOOL isActive)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SetDisplayCursor() graph handle is null\n");
return;
}
pgraph->bDisplayCursor = isActive;
}
/*-------------------------------------------------------------------------
SetYminVal: set the Ymin scale value
-------------------------------------------------------------------------*/
VOID SetYminVal(HGRAPH hGraph, double ymin)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SetYminVal() graph handle is null\n");
return;
}
pgraph->ymin_fix = ymin;
}
/*-------------------------------------------------------------------------
SetYmaxVal: set the Ymin scale value
-------------------------------------------------------------------------*/
VOID SetYmaxVal(HGRAPH hGraph, double ymax)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SetYmaxVal() graph handle is null\n");
return;
}
pgraph->ymax_fix = ymax;
}
VOID SetZoomFactor(HGRAPH hGraph, int zoom)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SetZoomFactor() graph handle is null\n");
return;
}
if (zoom < 0)
{
printf("[!] Error at SetZoomFactor() zoom can't be <0\n");
return;
}
pgraph->scale_factor = zoom;
return;
}
/*-------------------------------------------------------------------------
SetFilteringMode: set the EMA filtering state
-------------------------------------------------------------------------*/
VOID SetFilteringMode(HGRAPH hGraph, FILTER_M filtering)
{
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SetFilteringMode() graph handle is null\n");
return;
}
pgraph->Filtering = filtering;
}
VOID SetSignalMinValue(HGRAPH hGraph, INT SIGNB, DOUBLE val)
{
if (SIGNB > MAX_SIGNAL_COUNT || SIGNB < 0)
{
printf("[!] Error at SignalResetStatisticValue() signal number not in range\n");
return;
}
EnterCriticalSection(&cs);
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check
if (NULL == pgraph)
{
printf("[!] Error at SignalResetStatisticValue() graph handle is null\n");
LeaveCriticalSection(&cs);
return;
}
if (NULL == pgraph->signalcount)
{
printf("[!] Error at SignalResetStatisticValue() graph signal count is null\n");
LeaveCriticalSection(&cs);
return;
}
DATA* pData = NULL;
pData = (DATA*)pgraph->signal[SIGNB];
if (pgraph->cur_nbpoints >= 0)
{
pData->stat.min_value = val;
pData->stat.average_value_accumulator = 0.0;
pData->stat.average_value_counter = 0;
}
LeaveCriticalSection(&cs);
}
VOID SetSignalAverageValue(HGRAPH hGraph, INT SIGNB, DOUBLE val)
{
if (SIGNB > MAX_SIGNAL_COUNT || SIGNB < 0)
{
printf("[!] Error at SignalResetStatisticValue() signal number not in range\n");
return;
}
EnterCriticalSection(&cs);
PGRAPHSTRUCT pgraph = (PGRAPHSTRUCT)hGraph;
// Sanity check