-
Notifications
You must be signed in to change notification settings - Fork 0
/
messwertanzeige.c
1841 lines (1524 loc) · 54.7 KB
/
messwertanzeige.c
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <math.h>
#include <assert.h> /* drawRotated */
#include <errno.h>
#include <termios.h>
#include <fcntl.h>
#include <stdarg.h>
#include <locale.h>
#include <signal.h>
#include <Xm/Protocols.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <Xm/Xm.h>
#include <Xm/Form.h>
#include <Xm/PushB.h>
#include <Xm/DrawingA.h>
#include <Xm/TextF.h>
#include <Xm/Label.h>
#include <Xm/LabelG.h>
#include <Xm/Frame.h>
#include <Xm/RowColumn.h>
/* #define DEBUG */
#define BUFSIZE 240
#define COLOR_GRID 0x444444 /* farbe fuer Gitter */
#define COLOR_POINTER_MASK 0xff0000 /* Zeigerfarbe */
#define COLOR_1_SEC 0x004400 /* Farbe 1 sec Daten */
#define COLOR_10_SEC 0x00bb00 /* Farbe 10 sec Daten */
#define COLOR_30_SEC 0xff00ff /* Farbe 30 sec Daten */
#define COLOR_60_SEC 0x00ffff /* Farbe 60 sec Daten */
#define COLOR_GLAETTUNG 0xff0000 /* Farbe geglaettete Daten */
static void cleanup(int ex, void *arg);
static void updateImages(double val, int is_new_val);
static int pointerColors[] = {
0xff0000, /* Rot */
0xff1100, /* . */
0xff2200, /* . */
0xff3300, /* . */
0xff4400, /* . */
0xff5500, /* . */
0xff6600, /* . */
0xff7700, /* . */
0xff8800, /* . */
0xff9900, /* . */
0xffaa00, /* . */
0xffbb00, /* . */
0xffcc00, /* . */
0xffdd00, /* . */
0xffee00, /* . */
0xffff00, /* Gelb */
0xeeff00, /* . */
0xddff00, /* . */
0xccff00, /* . */
0xbbff00, /* . */
0xaaff00, /* . */
0x99ff00, /* . */
0x88ff00, /* . */
0x77ff00, /* . */
0x66ff00, /* . */
0x55ff00, /* . */
0x44ff00, /* . */
0x33ff00, /* . */
0x22ff00, /* . */
0x11ff00, /* . */
0x00ff00 /* Gruen */
};
struct drawStruct{
void (*backgroundFillFunc)(void);
GC gc; /* GC fuer pixmap */
XGCValues gcv; /* GCValues */
int tiefe; /* farbtiefe */
Pixmap backMap; /* hindergrund pixmap */
Pixmap foreMap; /* vordergrund pixmap */
int l_bord; /* left Border */
int r_bord; /* right Border */
int top_bord; /* top Border */
int bot_bord; /* bottom Border */
int vert_lines; /* # vertical lines */
int hor_lines; /* # horizontal lines */
Widget draw;
Dimension sizeX;
Dimension sizeY;
Display *dpy;
};
struct global{
int verbose; /* verboselevel */
int print_help; /* flag to call print_help() */
int is_test; /* flag if programtest */
int sondTyp; /* Sondentyp */
int use_stdin;
int time_base;
char *exponent_alpha;
char *port;
int graph1;
int graph2;
int graph3;
int graph4;
int graph5;
int no_table;
int no_analog;
int no_timeline;
double max_value;
GC gc_pointer; /* GC fuer pointer */
int win_sizeX; /* fenster X */
int win_sizeY; /* fenster Y */
struct drawStruct draw_point;
struct drawStruct draw_avg;
struct drawStruct draw_sens;
double val1[BUFSIZE]; /* 1sec werte */
double val2[BUFSIZE]; /* durchschnittswerte */
double val3[BUFSIZE]; /* durchschnittswerte 30sec */
double val6[BUFSIZE]; /* durchschnittswerte 60sec */
double avg_exp[BUFSIZE]; /* gleitender mittelwert*/
Widget sens_lout; /* Layout mit Sensor -Widgets */
int drawPointTo[2]; /* [0] x-Wert; [1] y-Wert; Punkt an den Zeiger laufen soll */
int pointer_radius; /* radius fuer zeigerlaenge */
double pointer_winkel_ziel_exp; /* zielwinkel fuer Glattungszeiger */
double pointer_winkel_ziel; /* zielwinkel fuer Zeiger - ungeglaettet */
} glob;
static XrmOptionDescRec options[] = {
{"-verbose", "*verbose", XrmoptionNoArg, "True"},
{"-no_table", "*no_table", XrmoptionNoArg, "True"},
{"-no_analog", "*no_analog", XrmoptionNoArg, "True"},
{"-no_timeline", "*no_timeline", XrmoptionNoArg, "True"},
{"-help", "*print_help", XrmoptionNoArg, "True"},
{"-test", "*is_test", XrmoptionNoArg, "True"},
{"-typ", "*sondTyp", XrmoptionSepArg, NULL},
{"-use_stdin", "*use_stdin", XrmoptionNoArg, "True"},
{"-timebase", "*time_base", XrmoptionSepArg, NULL},
{"-exponent", "*exponent", XrmoptionSepArg, NULL},
{"-port", "*port", XrmoptionSepArg, NULL},
{"-raw_graph", "*raw_graph", XrmoptionSepArg, "True"},
{"-graph_1", "*avg1", XrmoptionSepArg, NULL},
{"-graph_2", "*avg2", XrmoptionSepArg, NULL},
{"-graph_3", "*avg3", XrmoptionSepArg, NULL},
{"-expo_graph", "*expo_graph", XrmoptionSepArg, "True"}
};
//XLoadResources resources;
#define Offset(field) (XtOffsetOf(struct global, field))
static XtResource my_resources[] = {
{"verbose", XtCValue, XtRBoolean, sizeof(Boolean),
Offset(verbose), XtRString, "False"},
{"no_table", XtCValue, XtRBoolean, sizeof(Boolean),
Offset(no_table), XtRString, "False"},
{"no_analog", XtCValue, XtRBoolean, sizeof(Boolean),
Offset(no_analog), XtRString, "False"},
{"no_timeline", XtCValue, XtRBoolean, sizeof(Boolean),
Offset(no_timeline), XtRString, "False"},
{"print_help", XtCBoolean, XtRBoolean, sizeof(Boolean),
Offset(print_help), XtRString, "False"},
{"is_test", XtCBoolean, XtRBoolean, sizeof(Boolean),
Offset(is_test), XtRString, "False"},
{"sondTyp", XtCValue, XmRInt, sizeof(int),
Offset(sondTyp), XmRString, "7"},
{"time_base", XtCValue, XmRInt, sizeof(int),
Offset(time_base), XmRString, "1"},
{"use_stdin", XtCBoolean, XtRBoolean, sizeof(Boolean),
Offset(use_stdin), XtRString, "False"},
{"exponent", XtCValue, XmRString, sizeof(char *),
Offset(exponent_alpha), XmRString, "0.3"},
{"port", XtCValue, XmRString, sizeof(char *),
Offset(port), XmRString, "/dev/ttyS0"},
{"raw_graph", XtCValue, XmRBoolean, sizeof(Boolean),
Offset(graph1), XmRString, "1"},
{"avg1", XtCValue, XmRInt, sizeof(int),
Offset(graph2), XmRString, "10"},
{"avg2", XtCValue, XmRInt, sizeof(int),
Offset(graph3), XmRString, "30"},
{"avg3", XtCValue, XmRInt, sizeof(int),
Offset(graph4), XmRString, "60"},
{"expo_graph", XtCValue, XmRBoolean, sizeof(Boolean),
Offset(graph5), XmRString, "1"}
};
#undef Offset
char *fallback[] = {
/* GS 07 */
"*.background: black",
"*.sht11tLbl.labelString: Temperatur",
"*.sht11tValLbl.labelString: ",
"*.sht11tValLbl.foreground: green",
"*.sht11fLbl.labelString: Rel. Feuchte",
"*.sht11fValLbl.labelString: ",
"*.sht11fValLbl.foreground: green",
"*.sht11ImgLbl.labelType: XmPIXMAP",
"*.sht11ImgLbl.labelPixmap: imgs/SHT11.xpm",
"*.bmp085tLbl.labelString: Temperatur",
"*.bmp085tValLbl.labelString: ",
"*.bmp085tValLbl.foreground: green",
"*.bmp085dLbl.labelString: Luftdruck",
"*.bmp085dValLbl.labelString: ",
"*.bmp085dValLbl.foreground: green",
"*.bmp085ImgLbl.labelType: XmPIXMAP",
"*.bmp085ImgLbl.labelPixmap: imgs/BMP085.xpm",
"*.bma150tLbl.labelString: Temperatur",
"*.bma150tValLbl.labelString: ",
"*.bma150tValLbl.foreground: green",
"*.bma150xLbl.labelString: Beschleunigung X",
"*.bma150xValLbl.labelString: ",
"*.bma150xValLbl.foreground: green",
"*.bma150yLbl.labelString: Beschleunigung Y",
"*.bma150yValLbl.labelString: ",
"*.bma150yValLbl.foreground: green",
"*.bma150zLbl.labelString: Beschleunigung Z",
"*.bma150zValLbl.labelString: ",
"*.bma150zValLbl.foreground: green",
"*.bma150ImgLbl.labelType: XmPIXMAP",
"*.bma150ImgLbl.labelPixmap: imgs/BMA150.xpm",
/* GS08 */
"*.sht21tLbl.labelString: Temperatur",
"*.sht21tValLbl.labelString: ",
"*.sht21tValLbl.foreground: green",
"*.sht21fLbl.labelString: Rel. Feuchte",
"*.sht21fValLbl.labelString: ",
"*.sht21fValLbl.foreground: green",
"*.sht21ImgLbl.labelType: XmPIXMAP",
"*.sht21ImgLbl.labelPixmap: imgs/SHT21.xpm",
// "*.sht21ImgLbl.background: green",
NULL
};
/* Based on Gnuplot DrawRotated() */
static
void drawRotatedString(Display *dpy, GC gc, Drawable d,
int xdest, int ydest,
double angle,
const char *str, int len)
{
int x, y;
double src_x, src_y;
double dest_x, dest_y;
XFontStruct *font = XLoadQueryFont(dpy, "fixed");
int width = XTextWidth(font, str, len);
int height = font->ascent + font->descent;
double src_cen_x = (double)width * 0.5;
double src_cen_y = (double)height * 0.5;
static const double deg2rad = .01745329251994329576; /* atan2(1, 1) / 45.0; */
double sa = sin(angle * deg2rad);
double ca = cos(angle * deg2rad);
int dest_width = (double)height * fabs(sa) + (double)width * fabs(ca) + 2;
int dest_height = (double)width * fabs(sa) + (double)height * fabs(ca) + 2;
double dest_cen_x = (double)dest_width * 0.5;
double dest_cen_y = (double)dest_height * 0.5;
char* data = (char*) malloc(dest_width * dest_height * sizeof(char));
Pixmap pixmap_src = XCreatePixmap(dpy, DefaultRootWindow(dpy), (unsigned int)width, (unsigned int)height, 1);
XImage *image_src;
XImage *image_dest;
unsigned long fgpixel = 0;
unsigned long bgpixel = 0;
GC gcTmp;
int scr = DefaultScreen(dpy);
unsigned long gcFunctionMask = GCFunction;
XGCValues gcValues;
int gcCurrentFunction = 0;
Status s;
/* bitmapGC is static, so that is has to be initialized only once */
static GC bitmapGC = (GC) 0;
gcTmp = XCreateGC(dpy, d, 0, (XGCValues *) 0);
XCopyGC(dpy, gc, 0, gcTmp);
/* eventually initialize bitmapGC */
if ((GC)0 == bitmapGC) {
bitmapGC = XCreateGC(dpy, pixmap_src, 0, (XGCValues *) 0);
XSetForeground(dpy, bitmapGC, 1);
XSetBackground(dpy, bitmapGC, 0);
}
s = XGetGCValues(dpy, gc, gcFunctionMask|GCForeground|GCBackground, &gcValues);
if (s) {
/* success */
fgpixel = gcValues.foreground;
bgpixel = gcValues.background;
gcCurrentFunction = gcValues.function; /* save current function */
}
/* set font for the bitmap GC */
if (font)
XSetFont(dpy, bitmapGC, font->fid);
/* draw string to the source bitmap */
XDrawImageString(dpy, pixmap_src, bitmapGC, 0, font->ascent, str, len);
/* create XImage's of depth 1 */
/* source from pixmap */
image_src = XGetImage(dpy, pixmap_src, 0, 0, (unsigned int)width, (unsigned int)height,
1, XYPixmap /* ZPixmap, XYBitmap */ );
/* empty dest */
assert(data);
memset((void*)data, 0, (size_t)dest_width * dest_height);
image_dest = XCreateImage(dpy, DefaultVisual(dpy, DefaultScreen(dpy)), 1, XYBitmap,
0, data, (unsigned int)dest_width, (unsigned int)dest_height, 8, 0);
#define RotateX(_x, _y) (( (_x) * ca + (_y) * sa + dest_cen_x))
#define RotateY(_x, _y) ((-(_x) * sa + (_y) * ca + dest_cen_y))
/* copy & rotate from source --> dest */
for (y = 0, src_y = -src_cen_y; y < height; y++, src_y++) {
for (x = 0, src_x = -src_cen_x; x < width; x++, src_x++) {
/* TODO: move some operations outside the inner loop (joze) */
dest_x = rint(RotateX(src_x, src_y));
dest_y = rint(RotateY(src_x, src_y));
if (dest_x >= 0 && dest_x < dest_width && dest_y >= 0 && dest_y < dest_height)
XPutPixel(image_dest, (int)dest_x, (int)dest_y, XGetPixel(image_src, x, y));
}
}
assert(s); /* Previous success in reading XGetGCValues() */
/* Force pixels of new text to black, background unchanged */
/*gcValues.function = GXand;*/
gcValues.background = BlackPixel(dpy, scr);
gcValues.foreground = WhitePixel(dpy, scr);
XChangeGC(dpy, gcTmp, /*gcFunctionMask|*/GCBackground|GCForeground, &gcValues);
XPutImage(dpy, d, gcTmp, image_dest, 0, 0, xdest, ydest, dest_width, dest_height);
XFreePixmap(dpy, pixmap_src);
XDestroyImage(image_src);
XDestroyImage(image_dest);
}
static
void set_res(Widget w, char *res, char *buf)
{
XtVaSetValues(w, XtVaTypedArg,
res, XtRString, buf, strlen(buf) + 1, /* setze Ressource */
NULL);
}
static
Widget getWidgetByName(Widget ref, char *name)
{
return XtNameToWidget(ref, name);
}
static
void printWidget(Widget w, const char *fmt, ...){
char *cmd;
va_list ap;
va_start(ap, fmt);
vasprintf(&cmd, fmt, ap );
va_end(ap);
set_res(w, XmNlabelString, cmd); /* nur fuer Label */
free(cmd);
}
static
double calc_uSv(double count)
{
/* Calculate the ODL in uGy/h */
double diff;
double odl=0.0;
double eigen_nd= 15; /* eigene dosis des zaehlrohrs */
double empf_nd = 972; /* relative Empfindlichkeit */
double totzeit_nd = 3.3e-06; /* Totzeit vom Zaehlrohr */
double korr1_nd = -2.45e-06; /* faktor */
double korr2_nd = -1.17e-11; /* faktor */
double korr3_nd = 9.16e-17; /* faktor */
double korr4_nd = -1.64e-22; /* faktor */
double Nob= 1/totzeit_nd; /* max Impulse */
/* nur rechnen wenn count > eigeneffekt */
if (count > eigen_nd )
{
diff=count-eigen_nd;
if (count > Nob)
count = Nob;
odl = diff/(empf_nd*(1.0-korr1_nd*count
+korr2_nd*count*count
-korr3_nd*count*count*count
+korr4_nd*count*count*count*count
)
);
}
/* kann bei overflow passieren */
if (odl<0) odl=-2.0;
return odl;
}
static
void expose_cb(Widget w, XtPointer clientData, XtPointer callData)
{
struct drawStruct *str = (struct drawStruct *)clientData;
// puts(__func__);
XCopyArea( str->dpy, str->foreMap, XtWindow(str->draw), str->gc,
0, 0, str->sizeX, str->sizeY, 0, 0);
}
static
void resize_cb(Widget w, XtPointer clientData, XtPointer callData)
{
struct drawStruct *str = (struct drawStruct *)clientData;
Dimension width, height;
XtVaGetValues(w,
XmNheight, &height,
XmNwidth, &width,
NULL);
str->sizeX = width;
str->sizeY = height;
XFreePixmap(str->dpy, str->backMap);
str->backMap = XCreatePixmap(str->dpy,
RootWindowOfScreen(XtScreen(str->draw)),
str->sizeX, str->sizeY, str->tiefe);
XFreePixmap(str->dpy, str->foreMap);
str->foreMap = XCreatePixmap(str->dpy,
RootWindowOfScreen(XtScreen(str->draw)),
str->sizeX, str->sizeY, str->tiefe);
str->backgroundFillFunc();
updateImages(99, 0); // no new value
}
static
int getSondTyp()
{
FILE *fp;
double v;
int vers;
int ret;
char *call;
asprintf(&call, "sonde -F %s -V", glob.port);
fp = popen(call, "r"); /* Pipe open */
if(fp == NULL)
{
perror("popen error");
return -1;
}
free(call);
alarm(10); // 10 sec timeout
ret = fscanf(fp, "V:%lf\n", &v);
if(ret < 1)
{
perror("error reading QIS Version");
exit(1);
}
alarm(0); // reset alarm
if(v == 1.61)
vers = 7;
else if(v == 2.71)
vers = 8;
pclose(fp);
return vers;
}
static
void drawPointer(Widget w, int x, int y, int color)
{
if(XtWindow(w) == 0)
return;
/* printf("dpy : %p\n",glob.draw_point.dpy); */
/* printf("backMap: %p\n",glob.draw_point.backMap); */
/* printf("foreMap: %p\n",glob.draw_point.foreMap); */
/* printf("gc : %p\n",glob.draw_point.gc); */
/* Fenster loeschen */
XCopyArea( glob.draw_point.dpy,
glob.draw_point.backMap,
glob.draw_point.foreMap,
glob.draw_point.gc,
0, 0,
glob.draw_point.sizeX, glob.draw_point.sizeY,
0, 0);
XSetPlaneMask(glob.draw_point.dpy, glob.gc_pointer, color); /* Neue Zeigerfarbe setzten */
XDrawLine(glob.draw_point.dpy, glob.draw_point.foreMap, glob.gc_pointer,
glob.draw_point.sizeX/2, glob.draw_point.sizeY, x, y);
expose_cb(NULL, &glob.draw_point, NULL);
}
static
void setExpPointer_tm(XtPointer clientData, XtIntervalId *id)
{
static int i = 0; /* schrittzaehler */
static int x = 0; /* punkt x */
static int y = 0; /* punkt y */
static double winkel = M_PI; /* position des zeigers; am anfang links */
int anz_schritt = 50;
double winkel_ziel = glob.pointer_winkel_ziel_exp; /* sonst spiegelverkehrt */
static double winkel_schritt = 0.0; /* schrittweite */
int color;
double scl;
if(winkel_schritt == 0.0) /* immer nur am anfang */
winkel_schritt = ((winkel_ziel - winkel) /anz_schritt); /* schrittweite ausrechnen */
if(i == anz_schritt)
{
i = 0;
winkel_schritt = 0.0;
return;
}
i++;
winkel += winkel_schritt;
/* Zeigerfarbe finden */
/* erg = ( Anzahl der Farben / "laenge" der anzeige (pi) ); */
scl = (double) ( (sizeof(pointerColors) / sizeof(pointerColors[0])) / M_PI );
color = pointerColors[(int) (scl * winkel)];
/* zielposition finden */
x = glob.pointer_radius *sin( winkel +(M_PI/2)) + glob.draw_point.sizeX/2;
y = glob.pointer_radius *cos( winkel +(M_PI/2)) + glob.draw_point.sizeY;
drawPointer(glob.draw_point.draw, x, y, color);
XtAppAddTimeOut(XtWidgetToApplicationContext(glob.draw_point.draw),
500/anz_schritt, setExpPointer_tm, clientData);
}
static
double average(int elements, int cnt, double saveIn[])
{
/* elements: anzahl der elemente ueber die ein mittelwert gebildet werden soll */
/* cnt: aktuelle position im ringspeicher */
int i;
double avg = 0.0;
int anzahl = cnt%elements; /* anzahl der elemente ueber die der mittelwert gebildet wird */
int pos = cnt - anzahl; /* position des ersten werts fuer den mittelwert */
/* werte aufaddieren */
for(i = 0; i <= anzahl; i++)
avg += glob.val1[pos+i];
/* mittelwerte auffuellen */
for(i = 0; i <= anzahl; i++)
saveIn[pos+i] = avg / (anzahl +1);
return avg / (anzahl +1) ;
}
static
double calc_exp_avg(int cnt, double saveIn[])
{
double vor; /* vorganger */
double alpha = atof(glob.exponent_alpha); // glob.exponent_alpha; /* factor */
vor = saveIn[(cnt+BUFSIZE-1)%BUFSIZE];
/* ergebnis =factor * aktueller Wert+ (1-factor)* vorgaenger */
saveIn[cnt] = alpha * glob.val1[cnt] + (1-alpha) * vor;
return saveIn[cnt];
}
static
void draw_graph_on_pixmap(int len, int cnt, double *val, int color)
{
double rval;
double min = 0.0;
int factor;
int i;
int n;
XPoint points[len]; /* punkte fuer 1 sec daten */
double pnt_dist;
int draw_height;
int draw_width;
Dimension pix_width, pix_height;
XtVaGetValues(glob.draw_avg.draw,
XmNwidth, &pix_width,
XmNheight, &pix_height,
NULL);
draw_width = pix_width-glob.draw_avg.l_bord-glob.draw_avg.r_bord;
draw_height = pix_height-glob.draw_avg.top_bord-glob.draw_avg.bot_bord;
pnt_dist = (double) draw_width/len;
factor = ((draw_height - 0.0) / (glob.max_value - min));
memset(points, 0, sizeof(XPoint) * len);
n = 0;
/* beim aeltesten wert anfangen zu zeichnen */
for(i = 1; i < len+1; i++)
{
rval = factor * val[(cnt+i)%len]; // cnt= actual_val ;; cnt+1=oldest_val
points[n].x = glob.draw_avg.l_bord + i *pnt_dist;
points[n].y = glob.draw_avg.top_bord + (draw_height - rval);
n++;
}
XSetForeground(glob.draw_avg.dpy, glob.draw_avg.gc, color);
XDrawLines(glob.draw_avg.dpy, glob.draw_avg.foreMap, glob.draw_avg.gc,
points, n, CoordModeOrigin );
}
static
void draw_averages(int cnt)
{
int anz_pnt_1; /* anzazhl punkte 1 sec daten */
/* Mittelwerte Zeichnen */
XCopyArea( glob.draw_avg.dpy, /* display */
glob.draw_avg.backMap, /* src */
glob.draw_avg.foreMap, /* dest */
glob.draw_avg.gc, /* gc */
0, 0, /* src width, src height */
glob.draw_avg.sizeX, glob.draw_avg.sizeY, /* width, height */
0, 0); /* dest width, dest height */
anz_pnt_1 = 0;
if(glob.graph1 > 0)
draw_graph_on_pixmap(BUFSIZE, cnt, glob.val1, COLOR_1_SEC);
if(glob.graph2> 0)
draw_graph_on_pixmap(BUFSIZE, cnt, glob.val2, COLOR_10_SEC);
if(glob.graph3 > 0)
draw_graph_on_pixmap(BUFSIZE, cnt, glob.val3, COLOR_30_SEC);
if(glob.graph4 > 0)
draw_graph_on_pixmap(BUFSIZE, cnt, glob.val6, COLOR_60_SEC);
if(glob.graph5 > 0)
draw_graph_on_pixmap(BUFSIZE, cnt, glob.avg_exp, COLOR_GLAETTUNG);
expose_cb(NULL, (XtPointer) &glob.draw_avg , NULL);
}
static
void draw_analog()
{
XtAppAddTimeOut(XtWidgetToApplicationContext(glob.draw_point.draw), 1, setExpPointer_tm, NULL);
}
static
void updateImages(double val, int is_new_val)
{
static int cnt = -1;
double rval;
double min = 0.0;
// int x1, y1;
if(is_new_val)
{
cnt++;
cnt %= BUFSIZE;
if(val > glob.max_value) /* wert zu hoch fuer anzeige */
val = glob.max_value;
glob.val1[cnt] = val;
if(glob.graph2)
average(glob.graph2, cnt, glob.val2); /* mittelwert ausrechnen und aufaddieren */
if(glob.graph3)
average(glob.graph3, cnt, glob.val3); /* mittelwert ausrechnen und aufaddieren */
if(glob.graph4)
average(glob.graph4, cnt, glob.val6); /* mittelwert ausrechnen und aufaddieren */
rval = calc_exp_avg(cnt, glob.avg_exp); /* glaettung */
rval=M_PI*rval/(glob.max_value - min); /* zielwinkel [0...1] */
//glob.pointer_winkel_ziel = M_PI*glob.val[cnt]/(max - min); /* zielwinkel grauer zeiger */
glob.pointer_winkel_ziel_exp = M_PI - rval; /* zielwinkel farbiger zeiger */
/* x1 = glob.draw_point.sizeX/2; */
/* y1 = glob.draw_point.sizeY; */
}
if(!glob.no_analog)
draw_analog();
if(!glob.no_timeline)
draw_averages(cnt);
}
static
void updateSensors_gs08(double temp, double feuchte)
{
Widget tw; /* Temperatur Widget */
Widget fw; /* Feuchte Widget */
tw = getWidgetByName(glob.sens_lout, "SENSORION SHT21Frm"); /* Frame */
tw = getWidgetByName(tw, "sht21Row"); /* RowColumn -Widget */
fw = getWidgetByName(tw, "sht21fValLbl"); /* Feuchte Label */
tw = getWidgetByName(tw, "sht21tValLbl"); /* Temperatur Label */
printWidget(tw, "%.01lf", temp);
printWidget(fw, "%.01lf", feuchte);
/* Force Update */
XFlush(XtDisplay(tw));
XSync(XtDisplay(tw), 0);
}
static
void updateSensors_gs07(
double t1,
double t2,
double t3,
double d,
double f,
double x, double y, double z
)
{
Widget t1_w; /* Temperatur 1 Widget */
Widget t2_w; /* Temperatur 2 Widget */
Widget t3_w; /* Temperatur 3 Widget */
Widget d_w; /* Druck Widget */
Widget f_w; /* Feuchte Widget */
Widget x_w; /* Beschleunigung X Widget */
Widget y_w; /* Beschleunigung Y Widget */
Widget z_w; /* Beschleunigung Z Widget */
t1_w = getWidgetByName(glob.sens_lout, "SENSORION SHT11Frm"); /* get SHT11 Frame */
t1_w = getWidgetByName(t1_w, "sht11Row"); /* get SHT11 RowColumn -Widget */
f_w = getWidgetByName(t1_w, "sht11fValLbl"); /* get SHT11 Feuchte Label*/
t1_w = getWidgetByName(t1_w, "sht11tValLbl"); /* get SHT11 Temperatur Label */
t2_w = getWidgetByName(glob.sens_lout, "BOSCH BMP085Frm"); /* get BMP085 Frame */
t2_w = getWidgetByName(t2_w, "bmp085Row"); /* get SHT11 RowColumn -Widget */
d_w = getWidgetByName(t2_w, "bmp085dValLbl");
t2_w = getWidgetByName(t2_w, "bmp085tValLbl");
t3_w = getWidgetByName(glob.sens_lout, "BOSCH BMA150Frm");
t3_w = getWidgetByName(t3_w, "bma150Row");
x_w = getWidgetByName(t3_w, "bma150xValLbl");
y_w = getWidgetByName(t3_w, "bma150yValLbl");
z_w = getWidgetByName(t3_w, "bma150zValLbl");
t3_w = getWidgetByName(t3_w, "bma150tValLbl");
printWidget(t1_w, "%.01lf",t1);
printWidget(f_w, "%.01lf",f);
printWidget(t2_w, "%.01lf",t2);
printWidget(d_w, "%.01lf", d);
printWidget(t3_w, "%.01lf", t3);
printWidget(x_w, "%.01lf", x);
printWidget(y_w, "%.01lf", y);
printWidget(z_w, "%.01lf", z);
/* Force Update */
XFlush(XtDisplay(t1_w));
XSync(XtDisplay(t1_w), 0);
}
static
void update_ev(XtPointer clientData, int *fd, XtInputId *id)
{
static int cnt = 0;
static char buf[1024];
int rc; /* read Count */
int nd;
double t1, t2, t3; /* alle Temperaturen */
double d, f; /* Druck, Feuchte */
double x, y, z; /* Beschleunigung X,Y,Z */
double usv; /* Wert in uSv */
cnt++;
memset(buf,0,sizeof(buf));
if(glob.is_test > 1)
{
fprintf(stderr,"# # # # # # # # # # # # # # # # # # # # # # # #\n");
fprintf(stderr,"# WARNUNG: PROGRAMM IM TESTMODUS #\n");
fprintf(stderr,"# SIMULIERTE SONDENVERSION: %d #\n",glob.is_test);
fprintf(stderr,"# SONDE_SIM WIRD AUFGERUFEN UND ABGEFRAGT !!! #\n");
fprintf(stderr,"# # # # # # # # # # # # # # # # # # # # # # # #\n");
}
/* Sondendaten lesen */
rc = read(*fd, buf, sizeof(buf));
if(rc < 0)
{
fprintf(stderr,"Error reading Data: %s\n", strerror(errno));
return;
}
if(strcmp("ERR_EXIT", buf) == 0) // child told to Exit caused by Error
exit(0);
/* Daten aufbereiten */
if(buf[0] == 'V') // Version gelesen
return;
if(
sscanf(buf, "%d %lf %lf %lf %lf %lf %lf %lf %lf",
&nd, &t1, &t2, &t3, &d, &f, &x, &y, &z )
!= 9
)
return;
if(glob.is_test > 1)
{
printf("Niederdosis:\t\t%d\n", nd);
printf("Temperatur1:\t\t%03lf\nTemperatur2:\t\t%.03lf\nTemperatur3:\t\t%.03lf\n", t1, t2, t3);
printf("Druck:\t\t\t%.03lf\nFeuchte:\t\t%.03lf\n", d, f);
printf("Beschleungigung X:\t%.03lf\nBeschleungigung Y:\t%.03lf\nBeschleungigung Z:\t%.03lf\n", x, y, z);
printf("\n\n");
}
if(nd > 1000)
nd = 1000; /* zu hoch fuer 1 sek */
/* Sensordaten anzeigen */
if(!glob.no_table)
switch(glob.sondTyp)
{
case 7:
updateSensors_gs07(t1, t2, t3, d, f, x, y, z );
break;
case 8:
updateSensors_gs08(t1, f);
break;
default:
fprintf(stderr, "%s: illegal sondTyp: %d\n",__func__, glob.sondTyp);
}
/* uSv/h berechnen */
usv = calc_uSv(nd*(60/glob.time_base)); /* hochrechnen auf eine Minute */
/* GUI updaten */
updateImages(usv, 1);
}
static
void draw_legend(int x1, int y1)
{
int x2,y2; /* koordinaten */
char leg_name[100];
if(glob.graph1 > 0)
{
x2 = x1+10;
y2 = y1+10;
XSetForeground(glob.draw_avg.dpy, glob.draw_avg.gc, COLOR_1_SEC);
XDrawLine(glob.draw_avg.dpy, glob.draw_avg.backMap, glob.draw_avg.gc,
x1,y1,x2,y2);
memset(leg_name, 0, sizeof(leg_name));
snprintf(leg_name, 100, "%d sec", glob.graph1 * glob.time_base);
drawRotatedString(glob.draw_avg.dpy, glob.draw_avg.gc,
glob.draw_avg.backMap, x2+5, y1, 0, leg_name, strlen(leg_name));
}
if(glob.graph2> 0)
{
y1 += 30;
x2 = x1+10;
y2 = y1+10;
XSetForeground(glob.draw_avg.dpy, glob.draw_avg.gc, COLOR_10_SEC);
XDrawLine(glob.draw_avg.dpy, glob.draw_avg.backMap, glob.draw_avg.gc,
x1,y1,x2,y2);
memset(leg_name, 0, sizeof(leg_name));
snprintf(leg_name, 100, "%d sec", glob.graph2 * glob.time_base);
drawRotatedString(glob.draw_avg.dpy, glob.draw_avg.gc,
glob.draw_avg.backMap, x2+5, y1, 0, leg_name, strlen(leg_name));
}
if(glob.graph3> 0)
{
y1 += 30;
x2 = x1+10;
y2 = y1+10;
XSetForeground(glob.draw_avg.dpy, glob.draw_avg.gc, COLOR_30_SEC);
XDrawLine(glob.draw_avg.dpy, glob.draw_avg.backMap, glob.draw_avg.gc,
x1,y1,x2,y2);
memset(leg_name, 0, sizeof(leg_name));
snprintf(leg_name, 100, "%d sec", glob.graph3 * glob.time_base);
drawRotatedString(glob.draw_avg.dpy, glob.draw_avg.gc,
glob.draw_avg.backMap, x2+5, y1, 0, leg_name, strlen(leg_name));
}
if(glob.graph4> 0)
{
y1 += 30;
x2 = x1+10;
y2 = y1+10;
XSetForeground(glob.draw_avg.dpy, glob.draw_avg.gc, COLOR_60_SEC);
XDrawLine(glob.draw_avg.dpy, glob.draw_avg.backMap, glob.draw_avg.gc,
x1,y1,x2,y2);
memset(leg_name, 0, sizeof(leg_name));
snprintf(leg_name, 100, "%d sec", glob.graph4 * glob.time_base);
drawRotatedString(glob.draw_avg.dpy, glob.draw_avg.gc,
glob.draw_avg.backMap, x2+5, y1, 0, leg_name, strlen(leg_name));
}
if(glob.graph5> 0)
{
y1 += 30;
x2 = x1+10;
y2 = y1+10;
XSetForeground(glob.draw_avg.dpy, glob.draw_avg.gc, COLOR_GLAETTUNG);
XDrawLine(glob.draw_avg.dpy, glob.draw_avg.backMap, glob.draw_avg.gc,
x1,y1,x2,y2);
drawRotatedString(glob.draw_avg.dpy, glob.draw_avg.gc,
glob.draw_avg.backMap, x2+5, y1, 0, "Glaettung", 9);
}
}
static
void fillPointerBackground()
{
double i;
int x1,x2,y1,y2; /* Punkte fuer Striche */
int x = glob.draw_point.sizeX/2; /* Mittelpunkt X */
int y = glob.draw_point.sizeY; /* Mittelpunkt Y*/
int rot; /* Rotation */
int anz_strich = 9; /* Anzahl Unterteilstriche */
char *val;
int cnt = 0;
anz_strich++; // + letzter Strich
/* puts(__func__); */
/* printf("dpy : %p\n",glob.draw_point.dpy); */
/* printf("backMap: %p\n",glob.draw_point.backMap); */
/* printf("foreMap: %p\n",glob.draw_point.foreMap); */
/* printf("gc : %p\n",glob.draw_point.gc); */
/* * * * * * * * * * * * * * * */
/* draw_pointer Map fuellen */
/* * * * * * * * * * * * * * * */
XSetForeground(glob.draw_point.dpy, glob.draw_point.gc,
BlackPixelOfScreen(XtScreen(glob.draw_point.draw))); /* Black */