-
Notifications
You must be signed in to change notification settings - Fork 0
/
soarCAI.c
2675 lines (2379 loc) · 82.1 KB
/
soarCAI.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
/**
* \file soarCAI.c
* \brief SoarPilot Cambridge C302, C302A and GPSNAV support
*/
// tab size: 4
#include <PalmOS.h> // all the system toolbox headers
#include "soaring.h"
#include "soarCAI.h"
#include "soarComp.h"
#include "soaring.h"
#include "soarIO.h"
#include "soarUtil.h"
#include "soarDB.h"
#include "soarPLst.h"
#include "soarForm.h"
#include "soarUMap.h"
#include "soarMem.h"
#include "soarMath.h"
#include "soarWay.h"
#include "soarWLst.h"
/// CAI command mode prompt
#define CAI_COMMAND_PROMPT "\r\n\ncmd>"
/// CAI download mode prompt
#define CAI_DOWNLOAD_PROMPT "\r\n\ndn>"
/// CAI upload mode prompt
#define CAI_UPLOAD_PROMPT "\r\n\nup>"
#define STR2SHORT(s) ((((s)[0]&255)<<8)+((s)[1]&255))
// variables for list of flights in logger
Int16 cainumLogs = 0; ///< number of flights
Int16 currentCAIFltPage = 0; ///< current flight page number
Int16 selectedCAIFltIndex = -1; ///< selected flight index
Int16 CAInumperpage = 8; ///< number of flights per page
CAILogData *cailogdata = NULL; ///< CAI logger data
Int16 previndex = 1; ///< prev. index
Int16 appnumblocks = 0; ///< ???
CAIGenInfo *caigeninfo=NULL; ///< CAI generic info
Boolean skipmc = false; ///< skip MC
Boolean skipbugs = false; ///< skip bugs
Boolean skipballast = false; ///< skip ballast
// externals
extern UInt16 serial_ref; ///< serial port handle
extern UInt16 WayptsortType; ///< waypoint sort type
extern Int8 xfrdialog; ///< transfer dialog
extern Int8 CompCmdRes; ///< return value flight computer command
extern Int16 CompCmdData; ///< flight computer command data
extern Boolean menuopen; ///< menu open flag
/**
* \brief output a CAI C302 "G" record
* \param item type of G-record
*/
void Output302GRec(Int8 item)
{
Char output_char[81];
Char tempchar[20];
double tmpdbl;
if (data.input.basemc >= 100.0) {
tmpdbl = 99.9;
} else {
tmpdbl = data.input.basemc;
}
// Xmit C302 G Record
StrCopy(output_char, "!g,");
switch (item) {
case GMC:
StrCat(output_char, "m");
StrCat(output_char, leftpad(StrIToA(tempchar, (Int32)(pround(tmpdbl, 1)*10.0)), '0', 3));
StrCat(output_char, ",");
break;
case GBALLAST:
StrCat(output_char, "b");
StrCat(output_char, StrIToA(tempchar, (Int32)(data.config.pctwater*10.0)));
break;
case GBUGS:
StrCat(output_char, "u");
StrCat(output_char, leftpad(StrIToA(tempchar, (Int32)((data.config.bugfactor)*100.0)), '0', 3));
break;
case GALL:
StrCat(output_char, "m");
StrCat(output_char, leftpad(StrIToA(tempchar, (Int32)(pround(tmpdbl, 1)*10.0)), '0', 3));
StrCat(output_char, ",");
StrCat(output_char, "b");
StrCat(output_char, StrIToA(tempchar, (Int32)(data.config.pctwater*10.0)));
StrCat(output_char, ",");
StrCat(output_char, "u");
StrCat(output_char, leftpad(StrIToA(tempchar, (Int32)((data.config.bugfactor)*100.0)), '0', 3));
break;
}
StrCat(output_char, "\r\n");
#ifdef NMEALOG
if (device.VFSCapable) TxData(output_char, USEVFS);
#endif
TxData(output_char, USESER);
// HostTraceOutputTL(appErrorClass, "%s", output_char);
return;
}
/**
* \brief function to declare task to CAI flight computer
* \return true - task declaration successful, false - task decl. failed
*/
Boolean DeclareCAITask()
{
UInt16 TAKEOFFSET=0, LANDOFFSET=0;
UInt16 numWaypts;
Boolean retval=true;
numWaypts = data.task.numwaypts - data.task.numctlpts;
if (data.task.hastakeoff) {
TAKEOFFSET = 1;
numWaypts -= 1;
}
if (data.task.haslanding) {
LANDOFFSET = data.task.numwaypts - 1;
numWaypts -= 1;
} else {
LANDOFFSET = data.task.numwaypts;
}
HandleWaitDialog(true);
// Send the CAI Command to put it into Command Mode
if (retval) {
if (!SendCAICommandStart()) {
HandleWaitDialog(false);
retval = false;
}
}
// Send the CAI Command to put it into Download Mode
if (retval) {
if (!SendCAIDownloadStart()) {
HandleWaitDialog(false);
retval = false;
}
}
// Send All Turnpoints
// HostTraceOutputTL(appErrorClass, "Sending Taskpoints");
if (retval) {
if (!SendCAITaskpoints()) {
HandleWaitDialog(false);
retval = false;
}
}
// Send the C302 Declare End String
if (retval) {
if (!SendCAIDeclareEnd()) {
HandleWaitDialog(false);
retval = false;
}
}
if (retval) {
if (data.config.flightcomp == C302COMP ||
data.config.flightcomp == C302ACOMP) {
// Send Pilot Info Data to C302
if (!SendCAIPilotInfo()) {
HandleWaitDialog(false);
retval = false;
}
} else {
// Send Pilot & Config Info Data To GPSNAV
if (!SendGPSNAVConfigInfo()) {
HandleWaitDialog(false);
retval = false;
}
}
}
// Send Glider Info Data
// Not necessary for the GPSNAV
if (retval && (data.config.flightcomp == C302COMP || data.config.flightcomp == C302ACOMP)) {
if (!SendCAIGliderInfo()) {
HandleWaitDialog(false);
retval = false;
}
}
// Send the CAI Command it into Command Mode
if (retval) {
if (!SendCAICommandStart()) {
HandleWaitDialog(false);
retval = false;
}
}
// Put the C302/CFR into Flight PNP Mode
// Always want to do this even if error
SendCAIFlightModeStart(false);
if (retval) HandleWaitDialog(false);
// HostTraceOutputTL(appErrorClass, "------------------------");
return(retval);
}
/**
* \brief function to put CAI flight computer in command mode
* \return true - successful, else connection error
*/
Boolean SendCAICommandStart()
{
Char output_char[6];
// Put C302/CFR into Command Mode (send Ctrl-C)
StrCopy(output_char, "\003");
TxData(output_char, USESER);
// Put it here so that the Serial Flush will take effect
ClearSerial();
TxData(output_char, USESER);
ClearSerial();
TxData(output_char, USESER);
// Wait for the Command Mode Prompt
if (!(WaitFor(CAI_COMMAND_PROMPT, USESER))) {
CompCmdRes = CONNECTERR;
return(false);
}
return(true);
}
/**
* \brief function to put CAI flight computer in Pocket NAV mode
* \param dialog - true to show dialog
* \return for now always true
*/
Boolean SendCAIFlightModeStart(Boolean dialog)
{
Char output_char[6];
if (dialog) {
// pop up dialog box
if (data.config.flightcomp == C302COMP || data.config.flightcomp == C302ACOMP) {
xfrdialog = XFRC302;
} else {
xfrdialog = XFRGPSNAV;
}
HandleWaitDialog(true);
}
// Send the CAI Command it into Command Mode
// AM: wouldn't it be better to check return value?
SendCAICommandStart();
if (data.config.flightcomp == C302COMP ||
data.config.flightcomp == C302ACOMP) {
// HostTraceOutputTL(appErrorClass, "Sending CAI PNP Mode Command");
// Put C302/CFR into PNP Mode
StrCopy(output_char, "PNP\r");
} else {
// HostTraceOutputTL(appErrorClass, "Sending GPSNAV NMEA Mode Command");
// Put GPSNAV into NMEA Mode
if (!SendCAICommandStart()) {
if (!SendCAICommandStart()) {
SendCAICommandStart();
}
}
StrCopy(output_char, "NMEA\r");
// PG : repeat just in case?
// StrCopy(output_char, "NMEA\r");
}
TxData(output_char, USESER);
// close dialog box
if (dialog) {
HandleWaitDialog(false);
xfrdialog = XFRXFR;
}
return(true);
}
/**
* \brief function to send flight download command to CAI flight computer
* \return true if download prompt seen, else false
*/
Boolean SendCAIDownloadStart()
{
Char output_char[6];
// HostTraceOutputTL(appErrorClass, "Sending CAI Download Command");
// Xmit the C302 Download command to put it in Download Mode
StrCopy(output_char, "DOW 1\r");
TxData(output_char, USESER);
if (!(WaitFor(CAI_DOWNLOAD_PROMPT, USESER))) {
return(false);
}
return(true);
}
/**
* \brief function to upload data to CAI flight computer
* \param data pointer to data to upload
* \return true if upload prompt seen, else false
*/
Boolean SendCAIUploadStart(CAIData *data)
{
Char output_char[6];
// HostTraceOutputTL(appErrorClass, "Sending CAI Upload Command");
// Xmit the C302 Upload command to put it in Upload Mode
StrCopy(output_char, "UPL 1\r");
// StrCopy(output_char, "UPL\r");
putString(data, output_char);
if (!(WaitFor(CAI_UPLOAD_PROMPT, USESER))) {
return(false);
}
data->checksum = 0;
data->longChecksum = 0;
return(true);
}
/**
* \brief function to send declare end to CAI flight computer
* \return true if download prompt seen, else false
*/
Boolean SendCAIDeclareEnd()
{
Char output_char[6];
if (data.config.flightcomp == C302COMP ||
data.config.flightcomp == C302ACOMP) {
// HostTraceOutputTL(appErrorClass, "Sending 302 Declare End");
StrCopy(output_char, "D,255\r");
} else {
// HostTraceOutputTL(appErrorClass, "Sending GPSNAV Declare End");
StrCopy(output_char, "D,0\r");
}
TxData(output_char, USESER);
// HostTraceOutputTL(appErrorClass, "%s", output_char);
if (!(WaitFor(CAI_DOWNLOAD_PROMPT, USESER))) {
return(false);
}
return(true);
}
/**
* \brief function to send task points to CAI flight computer
* \return true if successful, else false
*/
Boolean SendCAITaskpoints()
{
Int16 caiidx=1, wayidx=0;
Boolean retval=true;
Char output_char[81];
Char tempchar[20];
UInt16 TAKEOFFSET=0, LANDOFFSET=0;
if (data.task.hastakeoff) {
TAKEOFFSET = 1;
}
if (data.task.haslanding) {
LANDOFFSET = data.task.numwaypts - 1;
} else {
LANDOFFSET = data.task.numwaypts;
}
if (data.config.flightcomp == C302COMP ||
data.config.flightcomp == C302ACOMP) {
caiidx = 0;
} else {
if (WayptsortType != SortByNameA) {
// Put the waypoint db into 0-9, A-Z order
DmQuickSort(waypoint_db, (DmComparF*)waypt_comparison, SortByNameA);
}
// HostTraceOutputTL(appErrorClass, "Sending GPSNAV Task-caiidx|%hu| wayidx|%hu|", caiidx, wayidx);
// Always puts task into the slot 0 (slot A on the GPSNav Display)
StrCopy(output_char, "T,0");
}
for (wayidx=(TAKEOFFSET); wayidx<LANDOFFSET; wayidx++) {
if ((data.task.waypttypes[wayidx] & CONTROL) == 0) {
if (data.config.flightcomp == C302COMP ||
data.config.flightcomp == C302ACOMP) {
if (!SendCAITaskpoint(caiidx, wayidx)) {
retval = false;
// This gets us out of the loop
wayidx = LANDOFFSET;
}
caiidx++;
} else {
caiidx = FindWayptRecordByName(data.task.wayptnames[wayidx]);
if (caiidx == CAINOTFOUND) {
CompCmdRes = WPTNOTFOUND;
CompCmdData = wayidx;
retval = false;
// This gets us out of the loop
wayidx = LANDOFFSET;
} else {
StrCat(output_char, ",");
StrCat(output_char, StrIToA(tempchar, caiidx+1));
}
}
}
}
if (data.config.flightcomp == GPSNAVCOMP) {
StrCat(output_char, "\r");
TxData(output_char, USESER);
// HostTraceOutputTL(appErrorClass, "%s", output_char);
if (!(WaitFor(CAI_DOWNLOAD_PROMPT, USESER))) {
retval = false;
}
if (WayptsortType != SortByNameA) {
// Put the waypoint db back into selected order
DmQuickSort(waypoint_db, (DmComparF*)waypt_comparison, WayptsortType);
}
}
return(retval);
}
/**
* \brief function to send single task point to CAI flight computer
* \param caiidx CAI index
* \param wayidx waypoint index
* \return true if download prompt seen (data understood and accepted), else false
*/
Boolean SendCAITaskpoint(UInt16 caiidx, UInt16 wayidx)
{
Char output_char[81];
Char tempchar[20];
// This is required for the C302
caiidx += 128;
// HostTraceOutputTL(appErrorClass, "Sending CAI Waypoint-caiidx|%hu| wayidx|%hu|", caiidx, wayidx);
StrCopy(output_char, "D,");
StrCat(output_char, StrIToA(tempchar, caiidx));
StrCat(output_char, ",");
LLToStringDM(data.task.wayptlats[wayidx], tempchar, ISLAT, false, true, 4);
StrCat(output_char, tempchar);
StrCat(output_char, ",");
LLToStringDM(data.task.wayptlons[wayidx], tempchar, ISLON, false, true, 4);
StrCat(output_char, tempchar);
StrCat(output_char, ",");
StrCat(output_char, data.task.wayptnames[wayidx]);
StrCat(output_char, "\r");
TxData(output_char, USESER);
// HostTraceOutputTL(appErrorClass, "%s", output_char);
if (!(WaitFor(CAI_DOWNLOAD_PROMPT, USESER))) {
return(false);
}
return(true);
}
/**
* \brief helper function to get unit flags for CAI flight computer
* \param caidata pointer to CAIAddData structure
* \return unit settings data
*/
static Int16 unitFlags(CAIAddData *caidata)
{
return (data.config.lftunits == METRIC ? CAI_UNIT_VARIO_MS : CAI_UNIT_VARIO_KTS)
| (data.config.altunits == METRIC ? CAI_UNIT_ALT_METERS : CAI_UNIT_ALT_FEET)
| (caidata->tempunits == CELCIUS ? CAI_UNIT_TEMP_C : CAI_UNIT_TEMP_F)
| (caidata->barounits == MILLIBARS ? CAI_UNIT_BARO_MILL : CAI_UNIT_BARO_INHG)
| (data.config.disunits == METRIC ? CAI_UNIT_DIST_KM : (data.config.disunits == NAUTICAL ? CAI_UNIT_DIST_NM : CAI_UNIT_DIST_SM))
| (data.config.spdunits == METRIC ? CAI_UNIT_SPEED_KPH : (data.config.spdunits == NAUTICAL ? CAI_UNIT_SPEED_KTS : CAI_UNIT_SPEED_MPH));
}
/**
* \brief function to send pilot info to CAI flight computer, uses IGC header settings
* \return true if download prompt seen (data understood and accepted), else false
*/
Boolean SendCAIPilotInfo()
{
Char output_char[65];
Char tempchar[65];
MemHandle record_handle;
MemPtr record_ptr;
CAIAddData *caidata;
AllocMem((void *)&caidata, sizeof(CAIAddData));
/* Retrieve IGC Header Info */
OpenDBQueryRecord(config_db, CAIINFO_REC, &record_handle, &record_ptr);
MemMove(caidata, record_ptr, sizeof(CAIAddData));
MemHandleUnlock(record_handle);
if (caidata->pilotinfo) {
// O,NAME,UNITS,TUNITS,SINK,TEFG,DIFF,DATUM,APP,ARR,SLOW,FAST,GAP,SPD,DB,R,UW,MH
// Sending Pilot Data
// HostTraceOutputTL(appErrorClass, "Sending Pilot Data");
StrCopy(output_char, "O,");
//Output the Pilot Name (24 chars)
StrNCopy(tempchar, data.igchinfo.name, 24);
if (StrLen(data.igchinfo.name) < 24) {
tempchar[StrLen(data.igchinfo.name)] = '\0';
} else {
// IGC pilot name too long, truncate
tempchar[24] = '\0';
}
StrCat(output_char, tempchar);
StrCat(output_char, ",");
// Output a "Zero" to overwrite the current Active Pilot
StrCat(output_char, "0,");
// Output the Temperature Units Constant
StrCat(output_char, StrIToA(tempchar, (Int32)caidata->tempunits));
StrCat(output_char, ",");
// Output the Sink Tone On/Off Setting
StrCat(output_char, StrIToA(tempchar, (Int32)caidata->sinktone));
StrCat(output_char, ",");
// Output the Total Energy Final Glide Setting
StrCat(output_char, StrIToA(tempchar, (Int32)caidata->tefg));
StrCat(output_char, ",");
// Output the Final Glide Altitude Mode
StrCat(output_char, StrIToA(tempchar, (Int32)caidata->dalt));
StrCat(output_char, ",");
// Output the Datum Mode
// Hardcoding it to 100 or WGS84
// It is ignored by IGC approved units
StrCat(output_char, "100,");
// Output the Approach Radius in meters
// Value in Nautical miles converted to Km and then to meters
StrCat(output_char, StrIToA(tempchar, (Int32)(pround(caidata->apprad * DISTKMCONST * 1000.0, 0))));
StrCat(output_char, ",");
// Output the Arrival Radius in meters
// Value in Nautical miles converted to Km and then to meters
StrCat(output_char, StrIToA(tempchar, (Int32)(pround(caidata->arvrad * DISTKMCONST * 1000.0, 0))));
StrCat(output_char, ",");
// Output the Enroute(Slow) Logging Interval in seconds
StrCat(output_char, StrIToA(tempchar, data.config.slowlogint));
StrCat(output_char, ",");
// Output the Close(Fast) Logging Interval in seconds
StrCat(output_char, StrIToA(tempchar, data.config.fastlogint));
StrCat(output_char, ",");
// Output the Time between Flight Logs (Minutes)
StrCat(output_char, StrIToA(tempchar, (Int32)caidata->tbtwn));
StrCat(output_char, ",");
// Output the Minimum Speed to force flight logging (Knots)
StrCat(output_char, StrIToA(tempchar, (Int32)data.config.logstartspd));
StrCat(output_char, ",");
// Output the STF Deadband in M/S
StrCat(output_char, StrIToA(tempchar, (Int32)(pround(caidata->stfdb * AIRMPSCONST, 0))));
StrCat(output_char, ",");
// This field sets the Vario type
StrCat(output_char, StrIToA(tempchar, (Int32)caidata->variotype));
StrCat(output_char, ",");
// Output the Unit Flags
StrCat(output_char, StrIToA(tempchar, (Int32)unitFlags(caidata)));
StrCat(output_char, ",");
// Output the Margin Height(10ths of Meters)
// This is Safety Height in SoaringPilot
StrCat(output_char, StrIToA(tempchar, (Int32)(pround(data.config.safealt * ALTMETCONST * 10.0, 0))));
StrCat(output_char, "\r");
TxData(output_char, USESER);
// PG moved FreeMem((void *)&caidata);
// HostTraceOutputTL(appErrorClass, "%s", output_char);
if (!(WaitFor(CAI_DOWNLOAD_PROMPT, USESER))) {
return(false);
}
}
FreeMem((void *)&caidata);
return(true);
}
/**
* \brief function to send configuration to GPSNAV flight computer
* \return true if download prompt seen (data understood and accepted), else false
*/
Boolean SendGPSNAVConfigInfo()
{
Char output_char[65];
Char tempchar[65];
MemHandle record_handle;
MemPtr record_ptr;
CAIAddData *caidata;
AllocMem((void *)&caidata, sizeof(CAIAddData));
/* Retrieve IGC Header Info */
OpenDBQueryRecord(config_db, CAIINFO_REC, &record_handle, &record_ptr);
MemMove(caidata, record_ptr, sizeof(CAIAddData));
MemHandleUnlock(record_handle);
// set pilot data flag set?
if (caidata->pilotinfo) {
// X,units,PILOTID,ACID,DATUM,APP-R,ARR-R,FAST,SLOW,USERCFG,CONFIG1,CONFIG2,MFN
// Sending Pilot, Glider & Config Data
// HostTraceOutputTL(appErrorClass, "Sending GPSNAV Config Data");
StrCopy(output_char, "X,");
//Output Units
//Distance, Height, Vertical Speed
//If NM, set all
//0 Nautical miles, feet, knots
//If KM, check Alt and set based on that
//1 Kilometers, meters, meters/sec
//2 Kilometers, feet, knots
//If SM , set all
//3 Statute miles, feet, knots
if (data.config.disunits == NAUTICAL) {
StrCat(output_char, "0,");
} else if (data.config.disunits == STATUTE) {
StrCat(output_char, "3,");
} else {
if (data.config.altunits == METRIC) {
StrCat(output_char, "1,");
} else {
StrCat(output_char, "2,");
}
}
//Output the Pilot Name (24 chars)
// Must Add Padding to 24 Chars
StrNCopy(tempchar, data.igchinfo.name, 24);
StrCat(output_char, rightpad(tempchar, ' ', 24));
// if (StrLen(data.igchinfo.name) < 24) {
// tempchar[StrLen(data.igchinfo.name)] = '\0';
// } else {
// tempchar[24] = '\0';
// }
// StrCat(output_char, tempchar);
StrCat(output_char, ",");
// Output the Glider Type
StrNCopy(tempchar, data.igchinfo.type, 9);
StrCat(output_char, rightpad(tempchar, ' ', 9));
// Output the Glider Contest ID
// Must Add Padding to 12 Chars
StrCopy(tempchar, data.igchinfo.cid);
StrCat(output_char, rightpad(tempchar, ' ', 3));
StrCat(output_char, ",");
// Output the Datum Mode
// Hardcoding it to 100 or WGS84
// It is ignored by IGC approved units
StrCat(output_char, "100,");
// Output the Approach Radius in 10ths of Km
// Value in Nautical miles converted to Km and then to meters
StrCat(output_char, StrIToA(tempchar, (Int32)(pround(caidata->apprad * DISTKMCONST * 10.0, 0))));
StrCat(output_char, ",");
// Output the Approach Radius in 10ths of Km
// Value in Nautical miles converted to Km and then to meters
StrCat(output_char, StrIToA(tempchar, (Int32)(pround(caidata->arvrad * DISTKMCONST * 10.0, 0))));
StrCat(output_char, ",");
// Logging values are one numeric value with the two combined
// Multiply Fast * 256 and then add the Slow value
// Output the Close(Fast) Logging Interval in seconds
StrCat(output_char, StrIToA(tempchar, (data.config.fastlogint*256+data.config.slowlogint)));
StrCat(output_char, "\r");
TxData(output_char, USESER);
// HostTraceOutputTL(appErrorClass, "%s", output_char);
if (!(WaitFor(CAI_DOWNLOAD_PROMPT, USESER))) {
return(false);
}
}
FreeMem((void *)&caidata);
return(true);
}
/**
* \brief function to send glider info to CAI flight computer
* \return true if download prompt seen (data understood and accepted), else false
*/
Boolean SendCAIGliderInfo()
{
Char output_char[65];
Char tempchar[65];
MemHandle record_handle;
MemPtr record_ptr;
PolarData *polar;
double a, b, c;
double V2;
double twomsinkts;
double ldspd, ldsnk, ld;
CAIAddData *caidata;
Boolean gliderinfo;
AllocMem((void *)&caidata, sizeof(CAIAddData));
/* Retrieve IGC Header Info */
OpenDBQueryRecord(config_db, CAIINFO_REC, &record_handle, &record_ptr);
MemMove(caidata, record_ptr, sizeof(CAIAddData));
MemHandleUnlock(record_handle);
gliderinfo = caidata->gliderinfo;
FreeMem((void *)&caidata);
// set glider info flag set?
if (gliderinfo) {
// HostTraceOutputTL(appErrorClass, "About to AllocMem");
AllocMem((void *)&polar, sizeof(PolarData));
// Retrieve a copy of the active polar
// HostTraceOutputTL(appErrorClass, "Getting Polar Record");
OpenDBQueryRecord(polar_db, POLAR_REC, &record_handle, &record_ptr);
MemMove(polar, record_ptr, sizeof(PolarData));
MemHandleUnlock(record_handle);
// HostTraceOutputTL(appErrorClass, "Calling CalcPolarABC");
CalcPolarABC(polar, 1.0);
a = polar->a;
// HostTraceOutputTL(appErrorClass, "a=|%s|", DblToStr(a, 6));
b = polar->b;
// HostTraceOutputTL(appErrorClass, "b=|%s|", DblToStr(b, 6));
c = polar->c;
// HostTraceOutputTL(appErrorClass, "c=|%s|", DblToStr(c, 6));
ldspd = Sqrt(c/a);
// HostTraceOutputTL(appErrorClass, "ldspd=|%s|", DblToStr(ldspd, 6));
ldsnk = (a*ldspd*ldspd) + (b*ldspd) + c;
// HostTraceOutputTL(appErrorClass, "ldsnk=|%s|", DblToStr(ldsnk, 6));
ld = ldspd / ldsnk * -1.0;
// HostTraceOutputTL(appErrorClass, "ld=|%s|", DblToStr(ld, 6));
// Must be negative value. Sink is negative.
twomsinkts = -2.0 / AIRMPSCONST;
// HostTraceOutputTL(appErrorClass, "twomsinkts=|%s|", DblToStr(twomsinkts, 6));
V2 = ((-1.0 * b) - Sqrt(b*b - (4.0*a*(c-twomsinkts)))) / (2.0*a);
// HostTraceOutputTL(appErrorClass, "V2=|%s|", DblToStr(V2, 6));
// G,GliderType,GliderID,L/D,VM,V2,Dry Weight(Kg), Ballast(Liters), Reserved, Config Word
// Sending Pilot Data
// HostTraceOutputTL(appErrorClass, "Sending Glider Data");
StrCopy(output_char, "G,");
//Output the Glider Type (12 chars)
StrNCopy(tempchar, polar->name, 12);
if (StrLen(polar->name) < 12) {
tempchar[StrLen(polar->name)] = '\0';
} else {
tempchar[12] = '\0';
}
StrCat(output_char, tempchar);
StrCat(output_char, ",");
// Output the Glider Contest ID
StrCat(output_char, data.igchinfo.cid);
StrCat(output_char, ",");
// Outputting L/D
StrCat(output_char, StrIToA(tempchar, (Int32)(pround(ld, 0))));
StrCat(output_char, ",");
// Outputting VM
StrCat(output_char, StrIToA(tempchar, (Int32)(pround(ldspd*SPDKPHCONST, 0))));
StrCat(output_char, ",");
// Outputting V2
StrCat(output_char, StrIToA(tempchar, (Int32)(pround(V2*SPDKPHCONST, 0))));
StrCat(output_char, ",");
// Outputting Dry Weight(Kg)
StrCat(output_char, StrIToA(tempchar, (Int32)(pround(polar->maxdrywt * WGTKGCONST, 0))));
StrCat(output_char, ",");
// Outputting Ballast(Liters)
StrCat(output_char, StrIToA(tempchar, (Int32)(pround(polar->maxwater * WTRLTRCONST, 0))));
StrCat(output_char, ",");
// Outputting zero (0) for the Reserved field
StrCat(output_char, "0,");
// Outputting 65535 so that the Polar config is NOT locked
StrCat(output_char, "65535");
StrCat(output_char, "\r");
TxData(output_char, USESER);
FreeMem((void *)&polar);
// HostTraceOutputTL(appErrorClass, "%s", output_char);
if (!(WaitFor(CAI_DOWNLOAD_PROMPT, USESER))) {
return(false);
}
}
return(true);
}
/**
* \brief function to upload SoarPilot waypoints to CAI flight computer
* \param ShowFinished true to show upload completed dialog
* \return true if successful, else false
*/
Boolean UploadCAIWaypoints(Boolean ShowFinished)
{
Boolean clearwaypoints = false;
Boolean retval=true;
clearwaypoints = true;
HandleWaitDialogUpdate(SHOWDIALOG, 0, 0, NULL);
if (WayptsortType != SortByNameA) {
// Sort the waypoint database into Name Ascending format
DmQuickSort(waypoint_db, (DmComparF*)waypt_comparison, SortByNameA);
}
// Send the CAI Command it into Command Mode
if (retval) {
if (!SendCAICommandStart()) {
HandleWaitDialog(false);
retval = false;
}
}
// Send the CAI Command to clear all waypoints from the unit
if (retval && clearwaypoints) {
if (!SendCAIClearWaypoints()) {
HandleWaitDialog(false);
retval = false;
}
}
// Send the CAI Command to set baud to 9600
/*
if (retval) {
if (!SendCAISetSpeed(9600)) {
HandleWaitDialog(false);
retval = false;
}
}
*/
// Send the CAI Command to put it into Download Mode
if (retval) {
if (!SendCAIDownloadStart()) {
HandleWaitDialog(false);
retval = false;
}
}
// Send All Waypoints
if (retval) {
if (!SendCAIWaypoints()) {
HandleWaitDialog(false);
retval = false;
}
}
// Send the CAI Command to end sending waypoints
if (retval && (data.config.flightcomp==C302COMP || data.config.flightcomp == C302ACOMP)) {
if (!SendCAIWaypointEnd()) {
HandleWaitDialog(false);
retval = false;
}
}
// Send the CAI Command to put it into Command Mode
if (retval) {
if (!SendCAICommandStart()) {
HandleWaitDialog(false);
retval = false;
}
}
/*
// Send the CAI Command to set baud to 4800
if (retval) {
if (!SendCAISetSpeed(4800)) {
HandleWaitDialog(false);
retval = false;
}
}
*/
// Put the C302/CFR into Flight PNP Mode
// Always want to do this even if error
SendCAIFlightModeStart(false);
if (WayptsortType != SortByNameA) {
// Put the waypoint db back into the current sort type
DmQuickSort(waypoint_db, (DmComparF*)waypt_comparison, WayptsortType);
}
HandleWaitDialogUpdate(STOPDIALOG, 0, 0, NULL);
if (retval && ShowFinished) {
if (data.config.flightcomp == C302COMP ||
data.config.flightcomp == C302ACOMP) {
FrmCustomAlert(FinishedAlert, "Sending Waypoints to C302/CFR Succeeded!"," "," ");
} else {
FrmCustomAlert(FinishedAlert, "Sending Waypoints to GPSNAV Succeeded!"," "," ");
}
}
// HostTraceOutputTL(appErrorClass, "------------------------");
return(retval);
}
/**
* \brief function to upload max. possible waypoints to CAI flight computer
* \return true if successful, else false
*/
Boolean SendCAIWaypoints()
{
Char output_char[81];
Char tempchar[20];
Int16 caiwaytype=0;
Int16 wayindex=0;
Int16 nrecs;
MemHandle output_hand;
MemPtr output_ptr;
WaypointData *waydata;
Boolean retval=true;
// HostTraceOutputTL(appErrorClass, "Sending CAI Waypoints");
nrecs = OpenDBCountRecords(waypoint_db);
// max. number of waypoints for each model
if (data.config.flightcomp == C302COMP ||
data.config.flightcomp == C302ACOMP) {
if (nrecs > 2000) {
nrecs = 2000;
}
} else {
if (nrecs > 250) {
nrecs = 250;
}
}
AllocMem((void *)&waydata, sizeof(WaypointData));
// step through the SoarPilot waypoint database
for (wayindex=0; wayindex<nrecs; wayindex++) {
OpenDBQueryRecord(waypoint_db, wayindex, &output_hand, &output_ptr);
MemMove(waydata, output_ptr, sizeof(WaypointData));
MemHandleUnlock(output_hand);
// HostTraceOutputTL(appErrorClass, "Sending CAI Waypoint-wayindex|%hd|", wayindex);
StrCopy(output_char, "C,");
StrIToA(tempchar, (Int32)wayindex+1);
StrCat(output_char, tempchar);
StrCat(output_char, ",");
LLToStringDM(waydata->lat, tempchar, ISLAT, false, true, 4);
StrCat(output_char, tempchar);
StrCat(output_char, ",");
LLToStringDM(waydata->lon, tempchar, ISLON, false, true, 4);
StrCat(output_char, tempchar);
StrCat(output_char, ",");
StrIToA(tempchar, (Int32)(waydata->elevation * ALTMETCONST));
StrCat(output_char, tempchar);
StrCat(output_char, ",");
StrIToA(tempchar, (Int32)wayindex+1);
StrCat(output_char, tempchar);
StrCat(output_char, ",");
caiwaytype = 0;
if (waydata->type & AIRPORT) caiwaytype|= CAIAIRPORT;
if (waydata->type & TURN) caiwaytype|= CAITURN;
if (waydata->type & LAND) caiwaytype|= CAILAND;
if (waydata->type & START) caiwaytype|= CAISTART;
if (waydata->type & FINISH) caiwaytype|= CAIFINISH;
if (waydata->type & MARK) caiwaytype|= CAIMARK;
if (waydata->type & HOME) caiwaytype|= CAIHOME;
if (waydata->type & THRML) caiwaytype|= CAITHRML;
if (waydata->type & AREA) caiwaytype|= CAITURN;
StrIToA(tempchar, (Int32)caiwaytype);
StrCat(output_char, tempchar);
StrCat(output_char, ",");
StrNCopy(tempchar, waydata->name, 12);
tempchar[12] = '\0';
StrCat(output_char, tempchar);
StrCat(output_char, ",");
StrNCopy(tempchar, waydata->rmks, 12);