-
Notifications
You must be signed in to change notification settings - Fork 0
/
soarFlrm.c
1375 lines (1237 loc) · 40.9 KB
/
soarFlrm.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
#include <PalmOS.h> // all the system toolbox headers
#include <SerialMgr.h>
#include <SerialMgrOld.h>
#include "soaring.h"
#include "soarIO.h"
#include "soarUtil.h"
#include "soarForm.h"
#include "soarMem.h"
#include "soarDB.h"
#include "soarMath.h"
#include "soarFlrm.h"
#include "soarCAI.h"
#include "soarUMap.h"
#include "AddIncs/GarminIncs/GPSLib68K.h"
// used for Flarm data and traffic display
GPSSatDataType *satDataFlarm;
Boolean Flarmpresent = false;
Boolean FlarmAlarm = false;
Int16 FlarmIdx = 1;
UInt8 FlarmRX = 0;
Int16 FlarmVSep;
Char errstring[15] = "Flarm";
Int8 FLARMxfertype = USESER;
double Flarm_HW_Version = 0.0; // to hold the flarm hardware version
double Flarm_SW_Version = 0.0; // to hold the flarm software version
// Bug in version 2.15 - 4.0 and onwards that counts the NMEA checksum in the configuration string
#define IGC_checksum_error 999.9 // This will be set to the version that corrects this bug
// external variables
extern Int8 xfrdialog;
extern CAILogData *cailogdata;
extern Int16 cainumLogs;
extern Int16 selectedCAIFltIndex;
extern UInt32 origform;
extern Boolean menuopen;
// internal variabels
UInt16 SeqNo = 0;
FlarmConfigData FlarmData;
Boolean DeclareFlarmTask(Boolean declaretoSD)
{
Boolean endResult = true;
Char output_char[50];
Char tempchar[25];
Int16 i = 0;
UInt8 flarmspeed = 255;
xfrdialog = XFRXFR;
HandleWaitDialog(true);
StrCopy(errstring, "Declare Task");
if (declaretoSD) {
if (!device.CardRW) {
HandleWaitDialog(false);
return(false);
}
// declaring to SD card
FLARMxfertype = USEVFS;
XferInit("flarmcfg.txt", IOOPENTRUNC, FLARMxfertype);
// header
StrCopy(output_char, "// SOARINGPILOT Version ");
StrCat(output_char, device.appVersion);
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
StrCopy(output_char, "// Flarm Task Declaration ");
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
StrCopy(output_char, "// Date : ");
StrCat(output_char, data.flight.declaredtg);
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
StrCopy(output_char, "// Time : ");
StrCat(output_char, data.flight.declareutc);
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
}
// IGC Info
if (data.config.FlrmCopyIGC) if (!FlarmSendIGCInfo(declaretoSD)) return(FlarmDataErr(declaretoSD));
// task name
StrCopy(errstring, "Task Name");
StrCopy(output_char, "$PFLAC,S,NEWTASK,");
StrCat(output_char, data.task.name);
if (!declaretoSD) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
if (!declaretoSD) {
output_char[7] = 'A';
// output_char[9] = 0;
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, FLARMxfertype)) return(FlarmDataErr(declaretoSD));
}
if (!data.task.hastakeoff) {
// blank take off waypoint
StrCopy(errstring, "Takeoff");
StrCopy(output_char, "$PFLAC,S,ADDWP,");
StrCat(output_char, "0000000N,00000000E,TAKEOFF");
if (!declaretoSD) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
if (!declaretoSD) {
output_char[7] = 'A';
// output_char[9] = 0;
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, FLARMxfertype)) return(FlarmDataErr(declaretoSD));
}
}
// output each waypoint
for (i = 0; i < data.task.numwaypts; i++) {
StrCopy(errstring, "WP ");
StrCat(errstring, DblToStr(i,0));
StrCopy(output_char, "$PFLAC,S,ADDWP,");
LLToStringDM(data.task.wayptlats[i], tempchar, ISLAT, false, false, 3);
StrCat(output_char, tempchar);
StrCat(output_char, ",");
LLToStringDM(data.task.wayptlons[i], tempchar, ISLON, false, false, 3);
StrCat(output_char, tempchar);
StrCat(output_char, ",");
StrCat(output_char, data.task.wayptnames[i]);
if (!declaretoSD) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
if (!declaretoSD) {
output_char[7] = 'A';
// output_char[9] = 0;
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, FLARMxfertype)) return(FlarmDataErr(declaretoSD));
}
}
if (!data.task.haslanding) {
// blank landing waypoint
StrCopy(errstring, "Landing");
StrCopy(output_char, "$PFLAC,S,ADDWP,");
StrCat(output_char, "0000000N,00000000E,LANDING");
if (!declaretoSD) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
if (!declaretoSD) {
output_char[7] = 'A';
// output_char[9] = 0;
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, FLARMxfertype)) return(FlarmDataErr(declaretoSD));
}
}
// logging interval
if (declaretoSD) {
StrCopy(errstring, "Log Interval");
StrCopy(output_char, "$PFLAC,S,LOGINT,");
StrCat(output_char, DblToStr(data.config.slowlogint,0));
if (!declaretoSD) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
}
// baud rate
switch (data.config.nmeaspeed) {
// case 0: // 1200
// case 1: // 2400
case 2: // 4800
flarmspeed = 0;
break;
case 3: // 9600
// case 4: // 14400
flarmspeed = 1;
break;
case 5: // 19200
flarmspeed = 2;
break;
// case 6: // 28880
flarmspeed = 3;
break;
case 7: // 38400
flarmspeed = 4;
break;
case 8: // 57600
// case 9: // 115200
flarmspeed = 5;
break;
}
if (flarmspeed == 255) return(FlarmDataErr(declaretoSD));
if (declaretoSD) {
StrCopy(errstring, "Baud Rate");
StrCopy(output_char, "$PFLAC,S,BAUD,");
StrCat(output_char, DblToStr(flarmspeed,0));
if (!declaretoSD) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
}
if (declaretoSD) {
// close SD file
XferClose(FLARMxfertype);
if (!VFSFileCopy("/PALM/Programs/SoarPilot/flarmcfg.txt", "/flarmcfg.txt")) {
HandleWaitDialog(false);
return(false);
}
FLARMxfertype = USESER;
}
HandleWaitDialog(false);
return(endResult);
}
Boolean ClearFlarmDeclaration(Boolean declaretoSD)
{
Boolean endResult = true;
Char output_char[50];
xfrdialog = XFRXFR;
HandleWaitDialog(true);
StrCopy(errstring, "Clear Task");
MemSet(&FlarmData, sizeof(FlarmConfigData), 0);
// New Task
StrCopy(output_char, "$PFLAC,S,NEWTASK,");
StrCat(output_char, " ");
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(FlarmDataErr(false));
// set zero waypoints to clear task declaration
// take off
StrCopy(output_char, "$PFLAC,S,ADDWP,");
StrCat(output_char, "0000000N,00000000E,TAKEOFF");
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(FlarmDataErr(false));
// landing
StrCopy(output_char, "$PFLAC,S,ADDWP,");
StrCat(output_char, "0000000N,00000000E,LANDING");
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(FlarmDataErr(false));
HandleWaitDialog(false);
return(endResult);
}
Boolean FlarmDownloadFlight()
{
Boolean endResult;
UInt16 frame_length = 0;
UInt8 *frame_data = NULL;
UInt8 cmdarray[10];
UInt16 crc = 0;
Boolean cmdOK = false;
Boolean seqOK = false;
UInt8 lastchar = 0;
Boolean initstat;
UInt32 numbytes;
if (Flarm_ping()) {
HandleWaitDialogUpdate(SHOWDIALOG, 0, -1, NULL);
HandleWaitDialogUpdate(UPDATEDIALOG, 0, -1, "%");
// select selected flight record
// HostTraceOutputTL(appErrorClass, "Flight %hu", (UInt16)selectedCAIFltIndex);
// build command
SeqNo++; // increment sequence no.
cmdarray[0] = 0x73; // start frame
cmdarray[1] = 0x09; // frame length
cmdarray[2] = 0x00; //
cmdarray[3] = 0x01; // version
cmdarray[4] = SeqNo%256; // sequence no.
cmdarray[5] = SeqNo/256; //
cmdarray[6] = 0x20; // select log record
cmdarray[7] = cailogdata[selectedCAIFltIndex].StartTape; // record to select (only here for crc check)
crc = Calc_crc(&cmdarray[1], 7); // calculate crc
cmdarray[7] = crc%256; // crc
cmdarray[8] = crc/256; //
cmdarray[9] = cailogdata[selectedCAIFltIndex].StartTape; // record to select
Send_command(&cmdarray[0], 10);
// read response
frame_length = Wait_Frame_Start();
// HostTraceOutputTL(appErrorClass, "frame length1 %hu", frame_length);
AllocMem((void *)&frame_data, frame_length+1);
if (Read_Frame(frame_data, frame_length)) {
// HostTraceOutputTL(appErrorClass, "Msg1 %hx", (UInt16)frame_data[5]);
cmdOK = (frame_data[5] == 0xA0);
seqOK = ((frame_data[8]+frame_data[9]*256) == SeqNo);
FreeMem((void *)&frame_data);
if (cmdOK && seqOK) {
// open IGC output file
initstat = XferInit(cailogdata[selectedCAIFltIndex].IGCname, IOOPENTRUNC, data.config.xfertype);
// request flight log download
do {
cmdOK = false;
seqOK = false;
do {
// build command
SeqNo++; // increment sequence no.
cmdarray[0] = 0x73; // start frame
cmdarray[1] = 0x08; // frame length
cmdarray[2] = 0x00; //
cmdarray[3] = 0x01; // version
cmdarray[4] = SeqNo%256; // sequence no.
cmdarray[5] = SeqNo/256; //
cmdarray[6] = 0x22; // get flight log
crc = Calc_crc(&cmdarray[1], 6);// calculate crc
cmdarray[7] = crc%256; // crc
cmdarray[8] = crc/256; //
Send_command(&cmdarray[0], 9);
// read response
frame_length = Wait_Frame_Start();
// HostTraceOutputTL(appErrorClass, "frame length2 %hu", frame_length);
} while (frame_length == 0);
AllocMem((void *)&frame_data, frame_length+1);
if (Read_Frame(frame_data, frame_length)) {
// HostTraceOutputTL(appErrorClass, "Msg2 %hx", (UInt16)frame_data[5]);
cmdOK = (frame_data[5] == 0xA0);
seqOK = ((frame_data[8]+frame_data[9]*256) == SeqNo);
lastchar = frame_data[frame_length-1]; // check for EOF marker
if (cmdOK && seqOK) {
// update progress
// HostTraceOutputTL(appErrorClass, "Done %hu", (UInt16)frame_data[10]);
HandleWaitDialogUpdate(UPDATEDIALOG, frame_data[10], -1, "%");
// save IGC data
numbytes = frame_length-((lastchar==0x1A)?12:11);
if (data.config.xfertype == USEVFS)
HandleVFSData(IOWRITE, &frame_data[11], &numbytes);
else if (data.config.xfertype == USEDOC)
HandleDOCData(IOWRITE, &frame_data[11], &numbytes);
}
FreeMem((void *)&frame_data);
}
} while (cmdOK && seqOK && (lastchar != 0x1A));
}
}
if (frame_data) FreeMem((void *)&frame_data);
// close IGC output file
XferClose(data.config.xfertype);
HandleWaitDialogUpdate(STOPDIALOG, 100, -1, NULL);
endResult = (lastchar == 0x1A);
if (endResult) FrmCustomAlert(FinishedAlert, "Flight Log Download Succeeded!"," "," ");
} else {
// connection in binary mode failed
endResult = false;
}
return(endResult);
}
Boolean FlarmGetFlightList(Int16 cmd)
{
UInt16 numberOfFlights = 0;
UInt16 OKFlights = 0;
UInt16 frame_length = 0;
UInt8 *frame_data = NULL;
UInt8 cmdarray[10];
UInt16 crc = 0;
CAILogData *caitemp = NULL;
Boolean cmdOK = false;
Boolean seqOK = false;
Char tempchar[82];
if (cmd == CAIFIFREE) {
if (cailogdata) { // free the previously allocated memory
MemPtrFree(cailogdata);
cailogdata = NULL;
}
cainumLogs = 0;
Flarm_reset(); // reset Flarm to put into NMEA mode.
return(true);
}
if (cmd == CAIFISTART) { // download the list of flights stored in the logger
HandleWaitDialogUpdate(SHOWDIALOG, 0, -1, NULL);
if (Flarm_binary_connect()) { // connection test
// HostTraceOutputTL(appErrorClass, "Flarm Connected");
HandleWaitDialogUpdate(UPDATEDIALOG, 0, -1, "Flights");
// loop to read flight data
do {
cmdOK = false;
seqOK = false;
// build command
SeqNo++; // increment sequence no.
cmdarray[0] = 0x73; // start frame
cmdarray[1] = 0x09; // frame length
cmdarray[2] = 0x00; //
cmdarray[3] = 0x01; // version
cmdarray[4] = SeqNo%256; // sequence no.
cmdarray[5] = SeqNo/256; //
cmdarray[6] = 0x20; // select log record
cmdarray[7] = numberOfFlights%256; // record to select (only here for crc check)
crc = Calc_crc(&cmdarray[1], 7); // calculate crc
cmdarray[7] = crc%256; // crc
cmdarray[8] = crc/256; //
cmdarray[9] = numberOfFlights%256; // record to select
Send_command(&cmdarray[0], 10);
// read response
frame_length = Wait_Frame_Start();
// HostTraceOutputTL(appErrorClass, "frame length1 %hu", frame_length);
AllocMem((void *)&frame_data, frame_length+1);
if (Read_Frame(frame_data, frame_length)) {
// HostTraceOutputTL(appErrorClass, "Msg1 %hx", (UInt16)frame_data[5]);
cmdOK = (frame_data[5] == 0xA0);
seqOK = ((frame_data[8]+frame_data[9]*256) == SeqNo);
FreeMem((void *)&frame_data);
if (cmdOK && seqOK) {
// move already read flights to temp storage
if (numberOfFlights > 0) {
AllocMem((void *)&caitemp, sizeof(CAILogData)*numberOfFlights);
MemMove(caitemp, cailogdata, sizeof(CAILogData)*numberOfFlights);
FreeMem((void *)&cailogdata);
}
// resize cailogdata
numberOfFlights++;
AllocMem((void *)&cailogdata, sizeof(CAILogData)*numberOfFlights);
// copy temp data back to cailogdata
if (caitemp) {
MemMove(cailogdata, caitemp, sizeof(CAILogData)*(numberOfFlights-1));
FreeMem((void *)&caitemp);
}
MemSet(&cailogdata[numberOfFlights-1], sizeof(CAILogData), 0);
// build command
SeqNo++; // increment sequence no. cmdarray[0] = 0x73; // start frame
cmdarray[0] = 0x73; // start frame
cmdarray[1] = 0x08; // frame length
cmdarray[2] = 0x00; //
cmdarray[3] = 0x01; // version
cmdarray[4] = SeqNo%256; // sequence no.
cmdarray[5] = SeqNo/256; //
cmdarray[6] = 0x21; // get record info
crc = Calc_crc(&cmdarray[1], 6);// calculate crc
cmdarray[7] = crc%256; // crc
cmdarray[8] = crc/256; //
Send_command(&cmdarray[0], 9);
// read response
frame_length = Wait_Frame_Start();
// HostTraceOutputTL(appErrorClass, "frame length2 %hu", frame_length);
AllocMem((void *)&frame_data, frame_length+1);
if (Read_Frame(frame_data, frame_length)) {
// HostTraceOutputTL(appErrorClass, "Msg2 %hx", (UInt16)frame_data[5]);
cmdOK = (frame_data[5] == 0xA0);
seqOK = ((frame_data[8]+frame_data[9]*256) == SeqNo);
frame_data[frame_length] = 0; // terminate data string
if (cmdOK && seqOK) {
// fill log data structure
// HostTraceOutputTL(appErrorClass, "Flight Log %hu OK", numberOfFlights%256);
// HostTraceOutputTL(appErrorClass, "Frame Data %s", &frame_data[10]);
// IGC filename
GetFieldDelim(&frame_data[10], 0, '|', '|', tempchar);
// HostTraceOutputTL(appErrorClass, "IGC %s",tempchar);
StrNCopy(cailogdata[OKFlights].IGCname, tempchar, 15);
// Pilot
GetFieldDelim(&frame_data[10], 4, '|', '|', tempchar);
// HostTraceOutputTL(appErrorClass, "Pilot %s",tempchar);
StrNCopy(cailogdata[OKFlights].pilotName, tempchar, GL_CAI_NAME_SIZE);
// Start Date
GetFieldDelim(&frame_data[10], 1, '|', '|', tempchar);
// HostTraceOutputTL(appErrorClass, "Date %s", tempchar);
cailogdata[OKFlights].startDate.year = (Int16)StrAToI(Mid(tempchar,2,3));
cailogdata[OKFlights].startDate.month = (Int16)StrAToI(Mid(tempchar,2,6));
cailogdata[OKFlights].startDate.day = (Int16)StrAToI(Right(tempchar,2));
// Start Time
GetFieldDelim(&frame_data[10], 2, '|', '|', tempchar);
// HostTraceOutputTL(appErrorClass, "Time %s",tempchar);
cailogdata[OKFlights].startTime.hour = (Int16)StrAToI(Left(tempchar,2));
cailogdata[OKFlights].startTime.min = (Int16)StrAToI(Mid(tempchar,2,4));
cailogdata[OKFlights].startTime.sec = (Int16)StrAToI(Right(tempchar,2));
// Flight Log No.
cailogdata[OKFlights].StartTape = numberOfFlights%256-1;
OKFlights++;
HandleWaitDialogUpdate(UPDATEDIALOG, OKFlights, -1, "Flights");
} else {
// HostTraceOutputTL(appErrorClass, "Flight Log %hu NG", numberOfFlights%256);
}
} else {
// HostTraceOutputTL(appErrorClass, "Flight Log %hu NG", numberOfFlights%256);
}
}
}
if (frame_data) FreeMem((void *)&frame_data);
} while (cmdOK && seqOK);
// HostTraceOutputTL(appErrorClass, "%hu Valid Flights", OKFlights);
cainumLogs = OKFlights;
HandleWaitDialogUpdate(STOPDIALOG, OKFlights, -1, NULL);
return(true);
} else {
// connection in binary mode failed
cainumLogs = 0;
cailogdata = NULL;
HandleWaitDialogUpdate(STOPDIALOG, 0, -1, NULL);
return(false);
}
}
return(false);
}
Boolean FlarmSendIGCInfo(Boolean declaretoSD)
{
Char output_char[50];
StrCopy(errstring, "IGC Info");
// Pilot
StrCopy(output_char, "$PFLAC,S,PILOT,");
StrCat(output_char, data.igchinfo.name);
if (!declaretoSD && (Flarm_SW_Version >= IGC_checksum_error)) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
if (!declaretoSD) {
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(false);
}
// Co-Pilot
StrCopy(output_char, "$PFLAC,S,COPIL,None");
if (!declaretoSD && (Flarm_SW_Version >= IGC_checksum_error)) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
if (!declaretoSD) {
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(false);
}
// Glider Type
StrCopy(output_char, "$PFLAC,S,GLIDERTYPE,");
StrCat(output_char, data.igchinfo.type);
if (!declaretoSD && (Flarm_SW_Version >= IGC_checksum_error)) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
if (!declaretoSD) {
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(false);
}
// Glider ID
StrCopy(output_char, "$PFLAC,S,GLIDERID,");
StrCat(output_char, data.igchinfo.gid);
if (!declaretoSD && (Flarm_SW_Version >= IGC_checksum_error)) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
if (!declaretoSD) {
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(false);
}
// Comp ID
StrCopy(output_char, "$PFLAC,S,COMPID,");
StrCat(output_char, data.igchinfo.cid);
if (!declaretoSD && (Flarm_SW_Version >= IGC_checksum_error)) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
if (!declaretoSD) {
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(false);
}
// Comp Class
StrCopy(output_char, "$PFLAC,S,COMPCLASS,");
StrCat(output_char, data.igchinfo.cls);
if (!declaretoSD && (Flarm_SW_Version >= IGC_checksum_error)) {
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
}
StrCat(output_char, "\r\n");
TxData(output_char, FLARMxfertype);
if (!declaretoSD) {
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(false);
}
return(true);
}
Boolean FlarmSendConfig()
{
Char output_char[50];
Boolean retval = true;
xfrdialog = XFRXFR;
HandleWaitDialog(true);
StrCopy(errstring, "Send Config");
// Device ID
StrCopy(output_char, "$PFLAC,S,ID,");
StrCat(output_char, FlarmData.DeviceID);
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(FlarmDataErr(false));
// RF Freq
StrCopy(output_char, "$PFLAC,S,FREQ,");
StrCat(output_char, DblToStr(FlarmData.RFFreq,0));
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(FlarmDataErr(false));
if (Flarm_SW_Version < 4.0) {
// Ground RF
StrCopy(output_char, "$PFLAC,S,RFTX,");
StrCat(output_char, DblToStr(FlarmData.RFGndTX,0));
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(FlarmDataErr(false));
}
// Private
StrCopy(output_char, "$PFLAC,S,PRIV,");
StrCat(output_char, DblToStr(FlarmData.Private,0));
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(FlarmDataErr(false));
// Aircraft Type
StrCopy(output_char, "$PFLAC,S,ACFT,");
StrCat(output_char, DblToStr(FlarmData.Type,0));
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(FlarmDataErr(false));
// User Interface
StrCopy(output_char, "$PFLAC,S,UI,");
StrCat(output_char, DblToStr(FlarmData.UserIF,0));
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(FlarmDataErr(false));
if (Flarm_SW_Version < 4.0) {
// Logging
StrCopy(output_char, "$PFLAC,S,LOGGING,");
StrCat(output_char, DblToStr(FlarmData.Logging,0));
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(FlarmDataErr(false));
}
// Logging Interval
StrCopy(output_char, "$PFLAC,S,LOGINT,");
StrCat(output_char, DblToStr(FlarmData.Logint,0));
StrCat(output_char, "*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
output_char[7] = 'A';
output_char[StrLen(output_char)-5] = 0;
if (!WaitFor(output_char, USESER)) return(FlarmDataErr(false));
// IGC Info
if (data.config.FlrmCopyIGC) if (!FlarmSendIGCInfo(false))return(FlarmDataErr(false));
HandleWaitDialog(false);
return(retval);
}
Boolean FlarmGetConfig()
{
Char output_char[50];
Char input_char[50];
Boolean retval = true;
StrCopy(errstring, "Get Config");
if (!Flarm_Version()) return(false);
xfrdialog = XFRXFR;
MemSet(&FlarmData, sizeof(FlarmConfigData), 0);
HandleWaitDialog(true);
// Device ID
MemSet(input_char, sizeof(input_char), 0);
StrCopy(output_char, "$PFLAC,R,ID,0*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
if (WaitFor("$PFLAC,A,ID,", USESER)) {
GetData(input_char, 6, USESER);
StrNCopy(FlarmData.DeviceID, input_char, 6);
} else {
return(FlarmDataErr(false));
}
// RF Freq
MemSet(input_char, sizeof(input_char), 0);
StrCopy(output_char, "$PFLAC,R,FREQ,0*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
if (WaitFor("$PFLAC,A,FREQ,", USESER)) {
GetData(input_char, 1, USESER);
FlarmData.RFFreq = StrAToI(input_char);
} else {
return(FlarmDataErr(false));
}
if (Flarm_SW_Version < 4.0) {
// Ground RF
MemSet(input_char, sizeof(input_char), 0);
StrCopy(output_char, "$PFLAC,R,RFTX,0*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
if (WaitFor("$PFLAC,A,RFTX,", USESER)) {
GetData(input_char, 1, USESER);
FlarmData.RFGndTX = StrAToI(input_char);
} else {
return(FlarmDataErr(false));
}
}
// Private
MemSet(input_char, sizeof(input_char), 0);
StrCopy(output_char, "$PFLAC,R,PRIV,0*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
if (WaitFor("$PFLAC,A,PRIV,", USESER)) {
GetData(input_char, 1, USESER);
FlarmData.Private = StrAToI(input_char);
} else {
return(FlarmDataErr(false));
}
// Aircraft Type
MemSet(input_char, sizeof(input_char), 0);
StrCopy(output_char, "$PFLAC,R,ACFT,0*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
if (WaitFor("$PFLAC,A,ACFT,", USESER)) {
GetData(input_char, 2, USESER);
StrCopy(input_char, trim(input_char, '*', true));
FlarmData.Type = StrAToI(input_char);
} else {
return(FlarmDataErr(false));
}
// User Interface
MemSet(input_char, sizeof(input_char), 0);
StrCopy(output_char, "$PFLAC,R,UI,0*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
if (WaitFor("$PFLAC,A,UI,", USESER)) {
GetData(input_char, 1, USESER);
FlarmData.UserIF = StrAToI(input_char);
} else {
return(FlarmDataErr(false));
}
if (Flarm_SW_Version < 4.0) {
// Logging
MemSet(input_char, sizeof(input_char), 0);
StrCopy(output_char, "$PFLAC,R,LOGGING,0*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
if (WaitFor("$PFLAC,A,LOGGING,", USESER)) {
GetData(input_char, 1, USESER);
FlarmData.Logging = StrAToI(input_char);
} else {
return(FlarmDataErr(false));
}
} else {
FlarmData.Logging = 1;
}
// Log Interval
MemSet(input_char, sizeof(input_char), 0);
StrCopy(output_char, "$PFLAC,R,LOGINT,0*");
StrCat(output_char, CalcChkSum(output_char) );
StrCat(output_char, "\r\n");
TxData(output_char, USESER);
if (WaitFor("$PFLAC,A,LOGINT,", USESER)) {
GetData(input_char, 5, USESER);
FlarmData.Logint = StrAToI(input_char);
} else {
return(FlarmDataErr(false));
}
HandleWaitDialog(false);
return(retval);
}
Boolean form_config_flarminst_event_handler(EventPtr event)
{
Boolean handled=false;
FormPtr frm;
ControlPtr ctl;
ListPtr lst;
Char tempchar[20];
Int8 i;
Boolean hexOK;
frm = FrmGetActiveForm();
switch (event->eType) {
case frmOpenEvent:
FlarmGetConfig();
case frmUpdateEvent:
FrmDrawForm(FrmGetActiveForm());
field_set_value(form_config_flarminst_id, FlarmData.DeviceID);
switch (FlarmData.RFFreq) {
case 0:
ctl_set_value(form_config_flarminst_freq_world, true);
ctl_set_value(form_config_flarminst_freq_usa, false);
ctl_set_value(form_config_flarminst_freq_nz, false);
break;
case 2:
ctl_set_value(form_config_flarminst_freq_world, false);
ctl_set_value(form_config_flarminst_freq_usa, true);
ctl_set_value(form_config_flarminst_freq_nz, false);
break;
case 3:
ctl_set_value(form_config_flarminst_freq_world, false);
ctl_set_value(form_config_flarminst_freq_usa, false);
ctl_set_value(form_config_flarminst_freq_nz, true);
break;
default:
ctl_set_value(form_config_flarminst_freq_world, false);
ctl_set_value(form_config_flarminst_freq_usa, false);
ctl_set_value(form_config_flarminst_freq_nz, false);
break;
}
ctl_set_value(form_config_flarminst_gndrf, (FlarmData.RFGndTX == 1));
ctl_set_value(form_config_flarminst_priv, (FlarmData.Private == 1));
ctl = (ControlPtr)FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, form_config_flarminst_pop1));
lst = (ListPtr)FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, form_config_flarminst_pop2));
LstSetSelection (lst, FlarmData.Type);
LstSetTopItem (lst, FlarmData.Type);
SysStringByIndex(form_config_flarminst_acrft_table, FlarmData.Type, tempchar, 19);
CtlSetLabel (ctl, tempchar);
ctl_set_value(form_config_flarminst_userif_led, ((FlarmData.UserIF == 0) || (FlarmData.UserIF == 3)));
ctl_set_value(form_config_flarminst_userif_buzz, ((FlarmData.UserIF == 0) || (FlarmData.UserIF == 2)));
ctl_set_value(form_config_flarminst_logging, (FlarmData.Logging == 1));
field_set_value(form_config_flarminst_logint, DblToStr(FlarmData.Logint,0));
ctl_set_value(form_config_flarminst_copyigchinfo, data.config.FlrmCopyIGC);
WinDrawLine(0,92,160,92);
WinDrawChars("(hex)", 5, 130, 15);
WinDrawChars("secs", 4, 130, 112);
handled=true;
break;
case winExitEvent:
// HostTraceOutputTL(appErrorClass, "menuopen = true");
menuopen = true;
handled = false;
break;
case winEnterEvent:
if (event->data.winEnter.enterWindow == (WinHandle) FrmGetFirstForm ()) {
// HostTraceOutputTL(appErrorClass, "menuopen = false");
menuopen = false;
}
handled=false;
break;
case frmCloseEvent:
handled=false;
break;
case ctlSelectEvent: // A control button was pressed and released.
PlayKeySound();
switch ( event->data.ctlEnter.controlID ) {
case form_config_flarminst_savebtn:
data.config.FlrmCopyIGC = ctl_get_value(form_config_flarminst_copyigchinfo);
StrNCopy(FlarmData.DeviceID,field_get_str(form_config_flarminst_id),6);
ConvertToUpper(FlarmData.DeviceID);
field_set_value(form_config_flarminst_id, FlarmData.DeviceID);
hexOK = true;
tempchar[1] = 0;
for (i=0; i<6; i++) {
tempchar[0] = FlarmData.DeviceID[i];
if (StrStr("0123456789ABCDEF", tempchar) == NULL) {
hexOK = false;
FrmCustomAlert(WarningAlert, "Device ID Invalid!\n\n","Must be 6 digit hex"," ");
break;
}
}
if (hexOK) {
if (ctl_get_value(form_config_flarminst_freq_world)) {
FlarmData.RFFreq = 0;
} else if (ctl_get_value(form_config_flarminst_freq_usa)) {
FlarmData.RFFreq = 2;
} else if (ctl_get_value(form_config_flarminst_freq_nz)) {
FlarmData.RFFreq = 3;
}
FlarmData.RFGndTX = (ctl_get_value(form_config_flarminst_gndrf)?1:0);
FlarmData.Private = (ctl_get_value(form_config_flarminst_priv)?1:0);
if (ctl_get_value(form_config_flarminst_userif_led)) {
if (ctl_get_value(form_config_flarminst_userif_buzz))
FlarmData.UserIF = 0;
else
FlarmData.UserIF = 3;