forked from jamesroscoe/seatalk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseatalk_datagram.c
1360 lines (1265 loc) · 42.7 KB
/
seatalk_datagram.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 "seatalk_datagram.h"
#define INITIALIZE_DATAGRAM(DATAGRAM_NUMBER, HIGH_NIBBLE_OF_SECOND_BYTE) initialize_datagram(datagram, 0x##DATAGRAM_NUMBER, DATAGRAM_##DATAGRAM_NUMBER##_LENGTH, HIGH_NIBBLE_OF_SECOND_BYTE)
static unsigned char first_nibble(unsigned char c) {
return (c & 0xf0) >> 4;
}
static unsigned char last_nibble(unsigned char c) {
return (c & 0x0f);
}
static unsigned short int from_two_bytes(unsigned char* datagram, unsigned short int index) {
unsigned char byte1, byte2;
byte1 = datagram[index];
byte2 = datagram[index + 1];
return (byte2 << 8) | byte1;
}
static void to_two_bytes(unsigned short int value, unsigned char* datagram, unsigned short int index) {
datagram[index] = value & 0xFF;
datagram[index + 1] = value >> 8;
}
unsigned char complement_checksum(unsigned char value) {
return value ^ 0xff;
}
short int fix_twos_complement_char(unsigned char value) {
if (value & 0x80) {
return -1 * ((value ^ 0xff) + 1);
}
else {
return value;
}
}
static short int fix_twos_complement_word(short int value) {
if (value & 0x8000) {
return -1 * ((value ^ 0xffff) + 1);
}
else {
return value;
}
}
bool flag(unsigned short int flags, unsigned short int mask) {
return (flags & mask) != 0;
}
unsigned char get_datagram_length(char second_byte) {
return last_nibble(second_byte);
}
static void initialize_datagram(unsigned char* datagram, unsigned char datagram_number, short int total_length, short int high_nibble_of_second_byte) {
int i;
datagram[0] = datagram_number;
datagram[1] = (total_length - 3) + (high_nibble_of_second_byte << 4);
for (i = 2; i < total_length; i++) { datagram[i] = 0; }
}
static void uvw_compass(unsigned char* u, unsigned char* vw, short int compass) {
short int temp_compass = compass;
*u = (unsigned char)(temp_compass / 90);
temp_compass = temp_compass - (*u * 90);
*vw = (unsigned char)(temp_compass / 2);
temp_compass -= *vw * 2;
*u |= (unsigned char)((temp_compass * 2) << 2);
}
static void uvw_heading_and_turning_direction(short int heading, TURN_DIRECTION turning_direction, unsigned char* u, unsigned char* vw) {
short int temp_direction, odd;
temp_direction = heading;
odd = temp_direction % 2;
*u = (unsigned char)turning_direction == TURN_DIRECTION_RIGHT ? 0x8 : 0;
if (odd) {
if (*u & 0x8) {
// high bit of u adds 1 to heading so nothing to do
}
else {
*u |= 0x4;
}
}
else {
if (*u & 0x8) {
// high bit of u adds 1 to heading so need to reduce vw component
if (temp_direction == 0) {
temp_direction = 359;
}
else {
temp_direction -= 1;
}
*u |= 0x4;
}
else {
// heading reflected accurately with no extra from u
}
}
*u |= (unsigned char)(temp_direction / 90);
temp_direction = temp_direction % 90;
*vw = (unsigned char)(temp_direction / 2);
}
static int alarm_is_active(int active_alarms, short int alarm) {
return (active_alarms & alarm) ? 0xffff : 0;
}
unsigned char build_depth_below_transducer(unsigned char* datagram, short int depth_in_feet_times_10, bool display_in_metres, unsigned short int active_alarms, bool transducer_defective) {
// 00 02 yz xx xx
// depth below transducer (in feet): xxxx/10
// anchor alarm: y & 0x8
// metric display: y & 4
// deep water alarm: z & 2
// shallow water alarm: z & 1
short int xxxx = depth_in_feet_times_10;
unsigned char y = (alarm_is_active(active_alarms, ALARM_ANCHOR) & 0x8) | (display_in_metres ? 0x4 : 0x0);
unsigned char z = (alarm_is_active(active_alarms, ALARM_DEEP_WATER) & 0x2) | (alarm_is_active(active_alarms, ALARM_SHALLOW_WATER) & 0x1) | (transducer_defective ? 0x4 : 0);;
INITIALIZE_DATAGRAM(00, 0);
datagram[2] = (y << 4) | z;
to_two_bytes(xxxx, datagram, 3);
return DATAGRAM_00_LENGTH;
}
void parse_depth_below_transducer(unsigned char* datagram, short int* depth_in_feet_times_10, bool* display_in_metres, unsigned short int* active_alarms, bool* transducer_defective) {
short int y, z, xxxx;
y = first_nibble(datagram[2]);
z = last_nibble(datagram[2]);
xxxx = from_two_bytes(datagram, 3);
*depth_in_feet_times_10 = xxxx;
*display_in_metres = flag(y, 0x4);
*active_alarms = 0;
*active_alarms |= flag(z, 0x1) ? ALARM_SHALLOW_WATER : 0;
*active_alarms |= flag(z, 0x2) ? ALARM_DEEP_WATER : 0;
*active_alarms |= flag(y, 0x8) ? ALARM_ANCHOR : 0;
*transducer_defective = flag(z, 0x4);
}
unsigned char build_engine_rpm_and_pitch(unsigned char* datagram, ENGINE_ID engine_id, short int rpm, short int pitch_percent) {
// 05 03 0x yy zz pp
// x = 0: single engine (supply starboard_indicator as -1)
// x = 1: starboard engine (supply staboard_indicator as 1)
// x = 2: port engine (supply starboard_indicator as 0)
// rpm: yy * 256 + zz (signed value)
// pp: prop pitch, signed value
unsigned char x, pp;
switch (engine_id) {
case ENGINE_ID_PORT:
x = 2;
break;
case ENGINE_ID_STARBOARD:
x = 1;
break;
default:
x = 0;
}
pp = (unsigned char) pitch_percent;
INITIALIZE_DATAGRAM(05, 0);
datagram[2] = x;
to_two_bytes(rpm, datagram, 3);
datagram[5] = pp;
return DATAGRAM_05_LENGTH;
}
void parse_engine_rpm_and_pitch(unsigned char* datagram, ENGINE_ID* engine_id, short int* rpm, short int * pitch_percent) {
unsigned char x, pp;
short int yyzz;
x = datagram[2];
yyzz = from_two_bytes(datagram, 3);
pp = datagram[5];
switch (x) {
case 1:
*engine_id = ENGINE_ID_STARBOARD;
break;
case 2:
*engine_id = ENGINE_ID_PORT;
break;
default:
*engine_id = ENGINE_ID_SINGLE;
}
*rpm = yyzz;
*pitch_percent = fix_twos_complement_char(pp);
}
unsigned char build_apparent_wind_angle(unsigned char* datagram, short int degrees_right_times_2) {
// 10 01 xx yy
// xxyy/2 = degrees right of bow
// byte order : MSB-LSB not LSB-MSB like most of the other multibytes information
degrees_right_times_2 %= 720;
if (degrees_right_times_2 < 0) {
degrees_right_times_2 += 720;
}
unsigned char xx = (unsigned char)(degrees_right_times_2 >> 8);
unsigned char yy = (unsigned char)(degrees_right_times_2 & 0xff);
INITIALIZE_DATAGRAM(10, 0);
datagram[2] = xx;
datagram[3] = yy;
return DATAGRAM_10_LENGTH;
}
void parse_apparent_wind_angle(unsigned char* datagram, short int* degrees_right_times_2) {
short int xxyy;
xxyy = (unsigned short int) (datagram[2] << 8) + datagram[3];
if (xxyy > 360) {
xxyy -= 720;
}
*degrees_right_times_2 = xxyy;
}
unsigned char build_apparent_wind_speed(unsigned char* datagram, unsigned short int knots_times_10, bool display_in_metric) {
// 11 01 xx 0y
// wind speed: (xx & 0x7f) + y/10
// units flag xx & 0x80; 0 knots/0x80 m/s
unsigned char xx, y;
xx = (unsigned char)(knots_times_10 / 10);
y = (unsigned char)(knots_times_10 % 10);
if (xx > 0x7f) {
xx = 0x7f;
}
if (display_in_metric) {
xx |= 0x80;
}
INITIALIZE_DATAGRAM(11, 0);
datagram[2] = xx;
datagram[3] = y;
return DATAGRAM_11_LENGTH;
}
void parse_apparent_wind_speed(unsigned char* datagram, unsigned short int* knots_times_10, bool* display_in_metric) {
short int xx, y;
xx = datagram[2];
y = last_nibble(datagram[3]);
*knots_times_10 = (xx & 0x7f) * 10 + y;
*display_in_metric = flag(xx, 0x80);
}
unsigned char build_water_speed(unsigned char* datagram, short int knots_times_10) {
// 20 01 xx xx
// water speed = xxxx/10 knots
INITIALIZE_DATAGRAM(20, 0);
to_two_bytes(knots_times_10, datagram, 2);
return DATAGRAM_20_LENGTH;
}
void parse_water_speed(unsigned char* datagram, short int* knots_times_10) {
short int xxxx;
xxxx = from_two_bytes(datagram, 2);
*knots_times_10 = xxxx;
}
unsigned char build_trip_mileage(unsigned char* datagram, unsigned long int nmiles_times_100) {
// 21 02 xx xx 0x
// mileage = xxxxx/100 nautical miles
unsigned char xx1;
xx1 = (unsigned char)(nmiles_times_100 >> 16);
INITIALIZE_DATAGRAM(21, 0);
to_two_bytes(nmiles_times_100 & 0xFFFF, datagram, 2);
datagram[4] = xx1;
return DATAGRAM_21_LENGTH;
}
void parse_trip_mileage(unsigned char* datagram, unsigned long int* miles_times_100) {
unsigned long int xxxxx;
xxxxx = from_two_bytes(datagram, 2) | (unsigned long int)last_nibble(datagram[4]) << 16;
*miles_times_100 = xxxxx;
}
unsigned char build_total_mileage(unsigned char* datagram, unsigned long int nmiles_times_10) {
// 22 02 x xx 0x
// mileage = xxxx/10 nautical miles
INITIALIZE_DATAGRAM(22, 0);
to_two_bytes((unsigned short)nmiles_times_10, datagram, 2);
datagram[4] = 0;
return DATAGRAM_22_LENGTH;
}
void parse_total_mileage(unsigned char* datagram, unsigned long int* miles_times_10) {
unsigned long int xxxx;
xxxx = from_two_bytes(datagram, 2);
*miles_times_10 = xxxx;
}
unsigned char build_water_temperature(unsigned char* datagram, char degrees_celcius, bool transducer_defective) {
// 23 z1 xx yy
// z & 0x4 sensor problem
// xx degrees Celsius, yy degrees Fahrenheit from -80°c to +50°c (Farenheit overflow if outside this bounds)
char xx, yy;
unsigned char z;
xx = (char)degrees_celcius;
yy = (int) (((float)degrees_celcius * 9.0 / 5) + 32);
z = transducer_defective ? 0x4 : 0;
INITIALIZE_DATAGRAM(23, z);
datagram[2] = xx;
datagram[3] = yy;
return DATAGRAM_23_LENGTH;
}
void parse_water_temperature(unsigned char* datagram, char* degrees_celcius, bool* transducer_defective) {
char xx, z;
xx = datagram[2];
z = first_nibble(datagram[1]);
*degrees_celcius = xx;
*transducer_defective = flag(z, 0x4);
}
unsigned char build_speed_distance_units(unsigned char* datagram, DISTANCE_UNITS distance_units) {
// 24 02 00 00 xx
// xx == 00 nm/kts; 06 sm/mph; 86 km/kph
unsigned char xx;
switch (distance_units) {
case DISTANCE_UNITS_NAUTICAL:
xx = 0;
break;
case DISTANCE_UNITS_STATUTE:
xx = 0x06;
break;
default:
xx = 0x86;
break;
}
INITIALIZE_DATAGRAM(24, 0);
datagram[4] = xx;
return DATAGRAM_24_LENGTH;
}
void parse_speed_distance_units(unsigned char* datagram, DISTANCE_UNITS* distance_units) {
unsigned char xx;
xx = datagram[4];
if (xx == 0) {
*distance_units = DISTANCE_UNITS_NAUTICAL;
}
else if (xx == 6) {
*distance_units = DISTANCE_UNITS_STATUTE;
}
else {
*distance_units = DISTANCE_UNITS_METRIC;
}
}
unsigned char build_total_and_trip_mileage(unsigned char* datagram, unsigned long int total_nm_times_10, unsigned long int trip_nm_times_100) {
// 25 z4 xx yy uu vv aw
// total = (z * 4096 + yy * 256 + xx) / 10 (max 104857.5 nm)
// trip = (w * 65546 + vv * 256 + uu) / 100 (max 10485.85 nm)
unsigned char z, w;
z = (unsigned char)(total_nm_times_10 >> 16);
if (z > 0x0f) {
z = 0x0f;
}
INITIALIZE_DATAGRAM(25, z);
to_two_bytes((unsigned short)total_nm_times_10, datagram, 2);
w = (unsigned char)(trip_nm_times_100 >> 16);
if (w > 0x0f) {
w = 0x0f;
}
to_two_bytes((unsigned short)trip_nm_times_100, datagram, 4);
datagram[6] = 0xa0 | w;
return DATAGRAM_25_LENGTH;
}
void parse_total_and_trip_mileage(unsigned char* datagram, unsigned long int* total_mileage_times_10, unsigned long int* trip_mileage_times_100) {
unsigned char xx, yy, uu, vv, z, w;
xx = datagram[2];
yy = datagram[3];
uu = datagram[4];
vv = datagram[5];
z = first_nibble(datagram[1]);
w = last_nibble(datagram[6]);
*total_mileage_times_10 = ((unsigned long int)z << 16) | (yy << 8) | xx;
*trip_mileage_times_100 = ((unsigned long int)w << 16) | (vv << 8) | uu;
}
unsigned char build_average_water_speed(unsigned char* datagram, short int knots_1_times_100, short int knots_2_times_100, short int speed_1_from_sensor, short int speed_2_is_average, short int average_is_stopped, bool display_in_statute_miles) {
// 26 04 xx xx yy yy de
// xxxx/100 = current speed (kts), valid if d & 0x4 == 4
// yyyy/100 = average speed (kts), d & 0x8: 8 => sensor; 0 => trip log/time
// e & 0x1 => calculation stopped
// e & 0x2 => display in statute miles
unsigned char xx1, xx2, yy1, yy2, d, e;
xx2 = knots_1_times_100 >> 8;
xx1 = knots_1_times_100 & 0xff;
yy2 = knots_2_times_100 >> 8;
yy1 = knots_2_times_100 & 0xff;
d = 0;
d |= (speed_1_from_sensor ? 0x4 : 0);
d |= (speed_2_is_average ? 0x8 : 0);
e = 0;
e |= (average_is_stopped ? 1 : 0);
e |= (display_in_statute_miles ? 2 : 0);
INITIALIZE_DATAGRAM(26, 0);
datagram[2] = xx1;
datagram[3] = xx2;
datagram[4] = yy1;
datagram[5] = yy2;
datagram[6] = (d << 4) | e;
return DATAGRAM_26_LENGTH;
}
void parse_average_water_speed(unsigned char* datagram, short int* knots_1_times_100, short int* knots_2_times_100, short int* speed_1_from_sensor, short int* speed_2_is_average, short int* average_is_stopped, bool* display_in_statute_miles) {
short int xxxx, yyyy, d, e;
xxxx = from_two_bytes(datagram, 2);
yyyy = from_two_bytes(datagram, 4);
d = first_nibble(datagram[6]);
e = last_nibble(datagram[6]);
*knots_1_times_100 = xxxx;
*knots_2_times_100 = yyyy;
*speed_1_from_sensor = flag(d, 0x4);
*speed_2_is_average = flag(d, 0x8);
*average_is_stopped = flag(e, 0x1);
*display_in_statute_miles = flag(e, 0x2);
}
unsigned char build_precise_water_temperature(unsigned char* datagram, short int degrees_celsius_times_10) {
// 27 01 xx xx
// (xxxx - 100) / 10 = temperature in Celsius
unsigned short int temp_temperature;
temp_temperature = degrees_celsius_times_10 + 100;
INITIALIZE_DATAGRAM(27, 0);
to_two_bytes(temp_temperature, datagram, 2);
return DATAGRAM_27_LENGTH;
}
void parse_precise_water_temperature(unsigned char* datagram, short int* degrees_celsius_times_10) {
short int xxxx;
xxxx = from_two_bytes(datagram, 2);
*degrees_celsius_times_10 = xxxx - 100;
}
unsigned char build_lamp_intensity(unsigned char* datagram, unsigned char intensity) {
// 30 00 0x x value/intensity 0x0/0, 0x4/1, 0x8/2, 0xc/3
unsigned char x;
switch (intensity) {
case 0:
x = 0x0;
break;
case 1:
x = 0x4;
break;
case 2:
x = 0x8;
break;
default:
x = 0xc;
}
INITIALIZE_DATAGRAM(30, 0);
datagram[2] = x;
return DATAGRAM_30_LENGTH;
}
void parse_lamp_intensity(unsigned char* datagram, unsigned char* level) {
unsigned char x;
x = last_nibble(datagram[2]);
switch (x) {
case 0x0:
*level = 0;
break;
case 0x4:
*level = 1;
break;
case 0x8:
*level = 2;
break;
case 0xc:
*level = 3;
break;
}
}
unsigned char build_cancel_mob(unsigned char* datagram) {
// 36 00 01 no variation
INITIALIZE_DATAGRAM(36, 0);
datagram[2] = 1;
return DATAGRAM_36_LENGTH;
}
unsigned char build_lat_position(unsigned char* datagram, LATITUDE_HEMISPHERE hemisphere, unsigned char degrees, unsigned short int minutes_times_100) {
// use -ve degrees for southern hemisphere
// 50 Z2 XX YY YY
// XX degrees, (YYYY & 0x7fff)/100 minutes
// YYYY & 0x8000 south if set, north if clear
// Z = 0xa or 0x0 (Raystar 120 GPS, meaning unknown)
unsigned char xx, z;
unsigned short int yyyy;
xx = degrees;
yyyy = hemisphere == LATITUDE_HEMISPHERE_NORTH ? 0 : 0x8000;
yyyy |= minutes_times_100;
z = 0; // not impersonating a Raystar 120 so always 0
INITIALIZE_DATAGRAM(50, z);
datagram[2] = xx;
to_two_bytes(yyyy, datagram, 3);
return DATAGRAM_50_LENGTH;
}
void parse_lat_position(unsigned char* datagram, LATITUDE_HEMISPHERE* hemisphere, unsigned char* degrees, unsigned short int* minutes_times_100) {
unsigned char xx = datagram[2];
unsigned short int yyyy = from_two_bytes(datagram, 3);
*hemisphere = flag(yyyy, 0x8000) ? LATITUDE_HEMISPHERE_SOUTH : LATITUDE_HEMISPHERE_NORTH;
*degrees = xx;
*minutes_times_100 = yyyy & 0x7fff;
}
unsigned char build_lon_position(unsigned char* datagram, LONGITUDE_HEMISPHERE hemisphere, unsigned char degrees, unsigned short int minutes_times_100) {
// use -ve degrees for eastern hemisphere
// 51 Z2 XX YY YY
// XX degrees, (YYYY & 0x7fff)/100 minutes
// YYYY & 0x8000 east if set, west if clear
// Z = 0xa or 0x0 (Raystar 120 GPS, meaning unknown)
unsigned char xx = degrees;
unsigned short int yyyy = hemisphere == LONGITUDE_HEMISPHERE_WEST ? 0 : 0x8000;
yyyy |= minutes_times_100;
unsigned char z = 0; // not impersonaing a Raystar 120 so always 0)
INITIALIZE_DATAGRAM(51, z);
datagram[2] = xx;
to_two_bytes(yyyy, datagram, 3);
return DATAGRAM_51_LENGTH;
}
void parse_lon_position(unsigned char* datagram, LONGITUDE_HEMISPHERE* hemisphere, unsigned char* degrees, unsigned short int* minutes_times_100) {
unsigned char xx = datagram[2];
unsigned short int yyyy = from_two_bytes(datagram, 3);
*hemisphere = flag(yyyy, 0x8000) ? LONGITUDE_HEMISPHERE_EAST : LONGITUDE_HEMISPHERE_WEST;
*degrees = xx;
*minutes_times_100 = yyyy & 0x7fff;
}
unsigned char build_speed_over_ground(unsigned char* datagram, unsigned short int knots_times_10) {
// 52 01 XX XX
// XXXX/10 is speed over ground in kts
short int xxxx = knots_times_10;
INITIALIZE_DATAGRAM(52, 0);
to_two_bytes(xxxx, datagram, 2);
return DATAGRAM_52_LENGTH;
}
void parse_speed_over_ground(unsigned char* datagram, unsigned short int* knots_times_10) {
short int xxxx;
xxxx = from_two_bytes(datagram, 2);
*knots_times_10 = xxxx;
}
unsigned char build_course_over_ground(unsigned char* datagram, short int true_degrees) {
// 53 U0 VW
// true course = (U & 0x3) * 90 + (VW & 0x3F) * 2 + (U & 0xC) >> 2
unsigned char u, vw;
uvw_compass(&u, &vw, true_degrees);
INITIALIZE_DATAGRAM(53, u);
datagram[2] = vw;
return DATAGRAM_53_LENGTH;
}
void parse_course_over_ground(unsigned char* datagram, short int* true_degrees) {
short int u, vw;
u = first_nibble(datagram[1]);
vw = datagram[2];
*true_degrees = ((u & 0x3) * 90) + ((vw & 0x3f) * 2) + (u >> 3);
}
unsigned char build_gmt_time(unsigned char* datagram, unsigned char hours, unsigned char minutes, unsigned char seconds) {
// 54, T1, RS, HH
// HH = hours
// 6 highest bits of RST = minutes = (RS & 0xfc) >> 2
// 6 lowest bits of RST = seconds = ST & 0x3f
unsigned char t, rs, hh;
hh = hours;
rs = minutes << 2;
rs |= seconds >> 4;
t = seconds & 0xf;
INITIALIZE_DATAGRAM(54, t);
datagram[2] = rs;
datagram[3] = hh;
return DATAGRAM_54_LENGTH;
}
void parse_gmt_time(unsigned char* datagram, unsigned char* hours, unsigned char* minutes, unsigned char* seconds) {
unsigned char t, rs, hh;
t = first_nibble(datagram[1]);
rs = datagram[2];
hh = datagram[3];
*hours = hh;
*minutes = rs >> 2;
*seconds = ((rs & 0x3) << 4) | t;
}
unsigned char build_date(unsigned char* datagram, unsigned short int year, unsigned char month, unsigned char day) {
// 56 M1 DD YY
// YY year M month D day of month
unsigned char yy, m, dd;
yy = (unsigned char)year - 2000; // arbitrary epoch offset until real data available
m = month;
dd = day;
INITIALIZE_DATAGRAM(56, m);
datagram[2] = dd;
datagram[3] = yy;
return DATAGRAM_56_LENGTH;
}
void parse_date(unsigned char* datagram, unsigned short int* year, unsigned char* month, unsigned char* day) {
unsigned char m, dd;
m = first_nibble(datagram[1]);
dd = datagram[2];
*year = fix_twos_complement_char (datagram[3]) + 2000; // arbitrary epoch offset until real data available;
*month = m;
*day = dd;
}
unsigned char build_satellite_info(unsigned char* datagram, unsigned char satellite_count, unsigned char horizontal_dilution_of_position) {
// 58 z5 la xx yy lo qq rr
// lat/lon position
// la, lo degrees latitude, longitude
// minutes lat xxyy/1000
// minutes lon qqrr/1000
// z: south if z&1
// east if z&2
unsigned char s, dd;
s = satellite_count;
dd = horizontal_dilution_of_position;
INITIALIZE_DATAGRAM(57, s);
datagram[2] = dd;
return DATAGRAM_57_LENGTH;
}
void parse_satellite_info(unsigned char* datagram, unsigned char* satellite_count, unsigned char* horizontal_dilution_of_position) {
unsigned char s, dd;
s = first_nibble(datagram[1]);
dd = datagram[2];
*satellite_count = s;
*horizontal_dilution_of_position = dd;
}
unsigned char build_lat_lon_position(unsigned char* datagram, LATITUDE_HEMISPHERE hemisphere_latitude, unsigned char degrees_lat, unsigned short int minutes_lat_times_1000, LONGITUDE_HEMISPHERE hemisphere_longitude, unsigned char degrees_lon, unsigned short int minutes_lon_times_1000) {
unsigned char z, la, /*xx, yy,*/ lo/*, qq, rr*/;
z = 0;
if (hemisphere_latitude == LATITUDE_HEMISPHERE_SOUTH) {
z |= 0x1;
}
la = degrees_lat;
// xx = minutes_lat_times_1000 >> 8;
// yy = minutes_lat_times_1000 & 0xff;
if (hemisphere_longitude == LONGITUDE_HEMISPHERE_EAST) {
z |= 0x02;
}
lo = degrees_lon;
// qq = minutes_lon_times_1000 >> 8;
// rr = minutes_lon_times_1000 & 0xff;
INITIALIZE_DATAGRAM(58, z);
datagram[2] = la;
to_two_bytes(minutes_lat_times_1000, datagram, 3);
// datagram[3] = xx;
// datagram[4] = yy;
datagram[5] = lo;
to_two_bytes(minutes_lon_times_1000, datagram, 6);
// datagram[6] = rr;
// datagram[7] = qq;
return DATAGRAM_58_LENGTH;
}
void parse_lat_lon_position(unsigned char* datagram, LATITUDE_HEMISPHERE* hemisphere_latitude, unsigned char* degrees_lat, unsigned short int* minutes_lat_times_1000, LONGITUDE_HEMISPHERE* hemisphere_longitude, unsigned char* degrees_lon, unsigned short int* minutes_lon_times_1000) {
unsigned char z, la, lo;
unsigned short int xxyy, qqrr;
z = first_nibble(datagram[1]);
la = datagram[2];
xxyy = from_two_bytes(datagram, 3);
lo = datagram[5];
qqrr = from_two_bytes(datagram, 6);
*hemisphere_latitude = flag(z, 0x1) ? LATITUDE_HEMISPHERE_SOUTH : LATITUDE_HEMISPHERE_NORTH;
*degrees_lat = la;
*minutes_lat_times_1000 = xxyy;
*hemisphere_longitude = flag(z, 0x2) ? LONGITUDE_HEMISPHERE_EAST : LONGITUDE_HEMISPHERE_WEST;
*degrees_lon = lo;
*minutes_lon_times_1000 = qqrr;
}
unsigned char build_countdown_timer(unsigned char* datagram, unsigned char hours, unsigned char minutes, unsigned char seconds, TIMER_MODE mode) {
// 59 22 ss mm xh
// set countdown timer
// mm minutes, ss seconds, h hours
// msb mm count up start flag
// x: counter mode
// 0 = count up and start
// 4 = count down
// 8 = count down and start
unsigned char ss, mm, x, h;
h = hours;
mm = minutes;
ss = seconds;
switch (mode) {
case TIMER_MODE_COUNT_DOWN:
x = 4;
break;
case TIMER_MODE_COUNT_UP_AND_START:
x = 0;
mm |= 0x80;
break;
case TIMER_MODE_COUNT_DOWN_AND_START:
x = 8;
break;
default:
x = 0;
}
INITIALIZE_DATAGRAM(59, 2);
datagram[2] = ss;
datagram[3] = mm;
datagram[4] = (x << 4) | h;
return DATAGRAM_59_LENGTH;
}
void parse_countdown_timer(unsigned char* datagram, unsigned char* hours, unsigned char* minutes, unsigned char* seconds, TIMER_MODE* mode) {
unsigned char ss, mm, x, h;
ss = datagram[2];
mm = datagram[3];
x = first_nibble(datagram[4]);
h = last_nibble(datagram[4]);
*hours = h;
*minutes = mm & 0x7f;
*seconds = ss;
if (x == 0) {
if (mm & 0x80) {
*mode = TIMER_MODE_COUNT_UP_AND_START;
}
else {
*mode = TIMER_MODE_COUNT_UP;
}
}
else if (x == 4) {
*mode = TIMER_MODE_COUNT_DOWN;
}
else if (x == 8) {
*mode = TIMER_MODE_COUNT_DOWN_AND_START;
}
}
unsigned char build_wind_alarm(unsigned char* datagram, unsigned short int active_alarms) {
// 66 00 xy
// x: apparent wind alarm; y: true wind
// bits: & 0x8 angle low, 0x4 angle high, 0x2 speed low, 0x1 speed high
// xy = 0: end all wind alarms
short int x, y;
x = 0;
y = 0;
x |= alarm_is_active(active_alarms, ALARM_APPARENT_WIND_ANGLE_LOW) & 0x8;
x |= alarm_is_active(active_alarms, ALARM_APPARENT_WIND_ANGLE_HIGH) & 0x4;
x |= alarm_is_active(active_alarms, ALARM_APPARENT_WIND_SPEED_LOW) & 0x2;
x |= alarm_is_active(active_alarms, ALARM_APPARENT_WIND_SPEED_HIGH) & 0x1;
y |= alarm_is_active(active_alarms, ALARM_TRUE_WIND_ANGLE_LOW) & 0x8;
y |= alarm_is_active(active_alarms, ALARM_TRUE_WIND_ANGLE_HIGH) & 0x4;
y |= alarm_is_active(active_alarms, ALARM_TRUE_WIND_SPEED_LOW) & 0x2;
y |= alarm_is_active(active_alarms, ALARM_TRUE_WIND_SPEED_HIGH) & 0x1;
INITIALIZE_DATAGRAM(66, 0);
datagram[2] = (x << 4) | y;
return DATAGRAM_66_LENGTH;
}
void parse_wind_alarm(unsigned char* datagram, unsigned short int* active_alarms) {
short int x, y;
x = first_nibble(datagram[2]);
y = last_nibble(datagram[2]);
*active_alarms = 0;
*active_alarms |= flag(x, 0x8) ? ALARM_APPARENT_WIND_ANGLE_LOW : 0;
*active_alarms |= flag(x, 0x4) ? ALARM_APPARENT_WIND_ANGLE_HIGH : 0;
*active_alarms |= flag(x, 0x2) ? ALARM_APPARENT_WIND_SPEED_LOW : 0;
*active_alarms |= flag(x, 0x1) ? ALARM_APPARENT_WIND_SPEED_HIGH : 0;
*active_alarms |= flag(y, 0x8) ? ALARM_TRUE_WIND_ANGLE_LOW : 0;
*active_alarms |= flag(y, 0x4) ? ALARM_TRUE_WIND_ANGLE_HIGH : 0;
*active_alarms |= flag(y, 0x2) ? ALARM_TRUE_WIND_SPEED_LOW : 0;
*active_alarms |= flag(y, 0x1) ? ALARM_TRUE_WIND_SPEED_HIGH : 0;
}
unsigned char build_alarm_acknowledgement(unsigned char* datagram, unsigned short int acknowledged_alarm) {
// 68 x1 yy 00
// yy indicates which device acknowledged. Hard-coding to 01 here.
// x: 1-shallow water; 2-deep water; 3-anchor; 4-true wind speed high;
// 5-true wind speed low; 6-true wind angle high; 7-true wind angle low
// 8-apparent wind speed high; 9-apparent wind speed low;
// a-apparent wind angle high; b-apparent wind angle low
short int x;
switch (acknowledged_alarm) {
case ALARM_SHALLOW_WATER:
x = 1;
break;
case ALARM_DEEP_WATER:
x = 2;
break;
case ALARM_ANCHOR:
x = 3;
break;
case ALARM_TRUE_WIND_SPEED_HIGH:
x = 4;
break;
case ALARM_TRUE_WIND_SPEED_LOW:
x = 5;
break;
case ALARM_TRUE_WIND_ANGLE_HIGH:
x = 6;
break;
case ALARM_TRUE_WIND_ANGLE_LOW:
x = 7;
break;
case ALARM_APPARENT_WIND_SPEED_HIGH:
x = 8;
break;
case ALARM_APPARENT_WIND_SPEED_LOW:
x = 9;
break;
case ALARM_APPARENT_WIND_ANGLE_HIGH:
x = 0xa;
break;
case ALARM_APPARENT_WIND_ANGLE_LOW:
x = 0xb;
break;
default:
x = 0;
}
INITIALIZE_DATAGRAM(68, x);
datagram[2] = 1;
return DATAGRAM_68_LENGTH;
}
void parse_alarm_acknowledgement(unsigned char* datagram, unsigned short int* acknowledged_alarm) {
short int x;
int y;
x = first_nibble(datagram[1]);
y = datagram[2];
if (y == 0x15) {
*acknowledged_alarm = 0xffff;
}
else {
switch (x) {
case 1:
*acknowledged_alarm = ALARM_SHALLOW_WATER;;
break;
case 2:
*acknowledged_alarm = ALARM_DEEP_WATER;
break;
case 3:
*acknowledged_alarm = ALARM_ANCHOR;
break;
case 4:
*acknowledged_alarm = ALARM_TRUE_WIND_SPEED_HIGH;
break;
case 5:
*acknowledged_alarm = ALARM_TRUE_WIND_SPEED_LOW;;
break;
case 6:
*acknowledged_alarm = ALARM_TRUE_WIND_ANGLE_HIGH;
break;
case 7:
*acknowledged_alarm = ALARM_TRUE_WIND_ANGLE_LOW;;
break;
case 8:
*acknowledged_alarm = ALARM_APPARENT_WIND_SPEED_HIGH;
break;
case 9:
*acknowledged_alarm = ALARM_APPARENT_WIND_SPEED_LOW;
break;
case 10:
*acknowledged_alarm = ALARM_APPARENT_WIND_ANGLE_HIGH;
break;
case 11:
*acknowledged_alarm = ALARM_APPARENT_WIND_ANGLE_LOW;
break;
}
}
}
/*void parse_maxview_keystroke(unsigned char *datagram, short int *key_1, short int *key_2, short int *held_longer) {
// short int x, y;
// x = first_nibble(datagram[2]);
// y = last_nibble(datagram[2]);
// *key_1 = 0;
// *key_2 = 0;
// *held_longer = (x & 0x4) ? 1 : 0;
// if (x & 0x2 == 0) { // single keypress
// switch (y) {
// case 1:
// *key_1 =
}*/
unsigned char build_target_waypoint_name(unsigned char* datagram, char char1, char char2, char char3, char char4) {
// 82 05 xx !xx yy !yy zz !zz
// !<> means reverse bits so xx + !xx = 0xff
// last 4 chars of wpt name, upper case only; use 6 bits per char
// subtract 0x30 from character values before applying bit masks and shifts
// xx = six bits of char1 with high bits taken from lowest two of char2
// yy = low four bits of char2 with high nibble from low nibble of char3
// zz = high two bits of char3 and 6 high bits come from char4
unsigned char xx, xx_comp, yy, yy_comp, zz, zz_comp;
unsigned char c1, c2, c3, c4;
c1 = char1 - 0x30;
c2 = char2 - 0x30;
c3 = char3 - 0x30;
c4 = char4 - 0x30;
xx = (c1 & 0x3f) | ((c2 & 0x3) << 6);
xx_comp = complement_checksum(xx);
yy = (c2 >> 2) | ((c3 & 0xf) << 4);
yy_comp = complement_checksum(yy);
zz = ((c3 & 0x3c) >> 4) | (c4 << 2);
zz_comp = complement_checksum(zz);
INITIALIZE_DATAGRAM(82, 0);
datagram[2] = xx;
datagram[3] = xx_comp;
datagram[4] = yy;
datagram[5] = yy_comp;
datagram[6] = zz;
datagram[7] = zz_comp;
return DATAGRAM_82_LENGTH;
}
int parse_target_waypoint_name(unsigned char* datagram, char* char_1, char* char_2, char* char_3, char* char_4) {
unsigned char xx, xx_comp, yy, yy_comp, zz, zz_comp;
xx = datagram[2];
xx_comp = datagram[3];
yy = datagram[4];
yy_comp = datagram[5];
zz = datagram[6];
zz_comp = datagram[7];
if (((xx + xx_comp) != 0xff) || ((yy + yy_comp) != 0xff) || ((zz + zz_comp) != 0xff)) {
// transmission error detected
return -1;
}
*char_1 = 0x30 + (xx & 0x3f);
*char_2 = 0x30 + (((yy & 0xf) << 2) | ((xx & 0xc0) >> 6));
*char_3 = 0x30 + (((zz & 0x3) << 4) | ((yy & 0xf0) >> 4));
*char_4 = 0x30 + ((zz & 0xfc) >> 2);
return 0;
}
unsigned char build_course_computer_failure(unsigned char* datagram, COURSE_COMPUTER_FAILURE_TYPE failure_type) {
// 83 07 xx 00 00 00 00 00 80 00 00
// course compuer failure
// xx 0: sent after clearing failure and on power-up
// xx 1 failure, auto release error. Repeated every second
// xx 8 failure, drive stopped
unsigned char xx;
switch (failure_type) {
case COURSE_COMPUTER_FAILURE_TYPE_AUTO_RELEASE_ERROR:
xx = 1;
break;
case COURSE_COMPUTER_FAILURE_TYPE_DRIVE_STOPPED:
xx = 8;
break;
default:
xx = 0;
}
INITIALIZE_DATAGRAM(83, 0);
datagram[2] = xx;
return DATAGRAM_83_LENGTH;
}
void parse_course_computer_failure(unsigned char* datagram, COURSE_COMPUTER_FAILURE_TYPE* failure_type) {
short int xx;
xx = datagram[2];
switch (xx) {
case 0:
*failure_type = COURSE_COMPUTER_FAILURE_TYPE_NONE;
break;
case 1:
*failure_type = COURSE_COMPUTER_FAILURE_TYPE_AUTO_RELEASE_ERROR;
break;
case 8:
*failure_type = COURSE_COMPUTER_FAILURE_TYPE_DRIVE_STOPPED;
break;
}
}
unsigned char build_autopilot_status(unsigned char* datagram, short int compass_heading, TURN_DIRECTION turning_direction, short int target_heading, AUTOPILOT_MODE mode, short int rudder_position, unsigned short int alarms, unsigned char display_flags) {
// 84 u6 vw xy 0z 0m rr ss tt
// compass heading: (u & 0x3) * 90 + (vw & 0x3f) * 2 + (u & 0xc ? ((u & 0xc) == 0xc ? 2 : 1) : 0)
// turning direction: msb of u; 1 right, 0 left
// autopilot target course: high 2 bits of v * 90 + xy/2
// mode: z & 0x2 = 0: standby; & 0x2 = 1: auto; &0x4 = vane; &0x8 = track
// alarms: m & 4: off course; m & 8 wind shift
// rudder position in degrees; positive numbers steer right
// ss = display; 0x1 turn of heading display; 0x2: always on; 0x8: "no data"
// 0x10 "large xte"; 0x80: "auto rel"
// tt: 400G computer always 0x08; 150(G) computer always 0x05
unsigned char u, vw, xy, z, m, ss, tt;
short int rr, temp_direction;
uvw_heading_and_turning_direction(compass_heading, turning_direction, &u, &vw);
temp_direction = target_heading;
vw |= (temp_direction / 90) << 6;
xy = (temp_direction % 90) << 1;
z = mode;
rr = (unsigned char) rudder_position;
m = 0;
if (flag(alarms, ALARM_AUTOPILOT_OFF_COURSE)) {
m |= 0x04;
}
if (flag(alarms, ALARM_AUTOPILOT_WIND_SHIFT)) {
m |= 0x08;
}
ss = 0;
ss = display_flags;
tt = 0;
INITIALIZE_DATAGRAM(84, u);
datagram[2] = vw;
datagram[3] = xy;
datagram[4] = z;
datagram[5] = m;
datagram[6] = (unsigned char) rr;
datagram[7] = ss;
datagram[8] = tt;