-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlw_sf30d.cpp
1429 lines (1304 loc) · 51.8 KB
/
lw_sf30d.cpp
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
// www.lightware.co.za June 2020
// Serial and I2C interface for the SF30/D
//----------------------------------------
#include <inttypes.h>
//#include <Arduino.h>
#include <HardwareSerial.h>
#include <Wire.h>
#include "lw_sf30d.h"
LW_SF30::LW_SF30(HardwareSerial &serial1){
Serial1 = &serial1;
}
LW_SF30::LW_SF30(HardwareSerial &serial1, HardwareSerial &print){//}, TwoWire &wire){
Serial1 = &serial1;
Serial = &print;
}
void LW_SF30::test(){
Serial->print("Print Test ");
Serial->println("Print Test ");
Serial->println("Print Test ");
}
LW_SF30::LW_SF30(uint8_t Wire_i2c, HardwareSerial &print){
Serial = &print;
Wire.setClock(100000);
// Wire.beginTransmission(I2CAddress);
// Wire.write(120); // Set to Binary mode
// Wire.write(170); // Set to Binary mode
// Wire.write(170); // Set to Binary mode
// Wire.endTransmission();
}
//----------------------------------------
//------- Serial Interface ---------------
//----------------------------------------
// The serial communication works on the basis that the SF30/D will only
// reply with data when asked, except in the case where streaming was
// enabled where it will then continuosely send data.
// Serial communication Packet for Write and Read
// Serial communication header
// Byte:0 - Start Byte (always 0xAA)
// Byte:1 - Flags Low Byte
// Byte:2 - Flags High Byte
// Flags Bits 6-15 is number of Data bytes (1 to 1023)
// Flags Bits 1 to 5 reserved
// Flags Bit 0 indicated Write(1) or Read(0)
// Serial Communication Body (n = number of data bytes)
// Byte:3 - Packet ID
// Byte:4 up to Byte:4+n - Data:0 to Data:n
// Serial Communication Checksum
// Byte:n+5 - Checksum CRC Low Byte
// Byte:n+6 - Checksum CRC High Byte
// Packet ID descriptions - Typical operational IDs (Refer to documentation for full list)
// 0 - Product Name (Read Flag bit will request data from SF30/D, than the SF30/D reply)
// 1 - Hardware Version (Read Flag bit will request data from SF30/D, than the SF30/D reply)
// 2 - Frimware Version (Read Flag bit will request data from SF30/D, than the SF30/D reply)
// 3 - Serial Number (Read Flag bit will request data from SF30/D, than the SF30/D reply)
// 10 - Token (Read Flag bit will request data from SF30/D, than the SF30/D reply)
// 12 - Save Parameters (Write flag bit must be set)
// 14 - Reset (Write flag bit must be set)
// 27 - Distance output configuration (Read Flag bit will request current setting from SF30/D, then the SF30/D reply)
// (Write flag bit with data will change current setting on SF30/D, then the SF30/D reply)
// 30 - Current Data Stream Type (Read Flag bit will request current setting from SF30/D, then the SF30/D reply)
// (Write flag bit with data will change current setting on SF30/D, then the SF30/D reply)
// 79 - Serial BaudRate (Read Flag bit will request current setting from SF30/D, then the SF30/D reply)
// (Write flag bit with data will change current setting on SF30/D, then the SF30/D reply)
// 44 - Distance Data (Read Flag bit will request current data from SF30/D, then the SF30/D reply)
// 80 - I2C address (Read Flag bit will request current setting from SF30/D, then the SF30/D reply)
// (Write flag bit with data will change current setting on SF30/D, then the SF30/D reply)
// This function creates the CRC for the Serial data
uint16_t LW_SF30::createCRC(uint8_t* Data, uint16_t Size)
{
uint16_t crc = 0;
for (uint32_t i = 0; i < Size; ++i)
{
uint16_t code = crc >> 8;
code ^= Data[i];
code ^= code >> 4;
crc = crc << 8;
crc ^= code;
code = code << 5;
crc ^= code;
code = code << 7;
crc ^= code;
}
return crc;
}
// This will request the product Hardware Name
void LW_SF30::readRequestHardwareName(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 0;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will request the product Hardware Version Number
void LW_SF30::readRequestHardwareVersion(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 1;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will request the product Firmware Version number
void LW_SF30::readRequestFirmwareVersion(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 2;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will request the product Serial Number
void LW_SF30::readRequestSerialNumber(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 3;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will request the next usable Token
void LW_SF30::readRequestToken(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 10;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will request the current Distance Output Configuration settings
void LW_SF30::readRequestDistOutConfig(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 29;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will request the current Data Stream Type setting
void LW_SF30::readRequestDataStreamType(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 30;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will request the current Serial interface Baudrate
void LW_SF30::readRequestSerialBaudrate(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 91;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will request the current Distance Output as per the Distance output Configuration settings
void LW_SF30::readRequestDistance(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 44;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will request the current I2C Address
void LW_SF30::readRequestI2CAddress(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 92;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will request the APD Temperature
void LW_SF30::readRequestAPDTemp(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 55;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will request the Sampling Rate
void LW_SF30::readRequestSamplingRate(void){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 76;
serialOutputHeader.Length = 1;
serialOutputHeader.Write_Read = 0;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 4);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[4],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],6);
}
// This will send a save command.
// A new token must be requested and recieved and then passed to this
// function to send with the command
void LW_SF30::writeSaveParameters(uint16_t Token){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 12;
serialOutputHeader.Length = 3;
serialOutputHeader.Write_Read = 1;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// move the data to the output buffer
memcpy(&serialOutputBuffer[4],&Token,2);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 6);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[6],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],8);
}
// This will send a Reset command.
// A new token must be requested and recieved and then passed to this
// function to send with the command
void LW_SF30::writeResetCommand(uint16_t Token){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 14;
serialOutputHeader.Length = 3;
serialOutputHeader.Write_Read = 1;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// move the data to the output buffer
memcpy(&serialOutputBuffer[4],&Token,2);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 6);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[6],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],8);
}
// This will change the Distance Output configuration
// The integer value of the selected bits must be passed to the function, alternative pass a 0 value
// and then the configuration will be set in the function below
void LW_SF30::writeDistOutConfig(uint32_t Value){
uint16_t CRC = 0;
//When Value is set to 0, the selected display outputs is set below,
//otherwise when Value is not 0, it will dictate which outputs.
//The following bits must be set to output value:
//bit0 - First Return Raw
//bit1 - First Return Filtered
//bit2 - First Return Strength
//bit3 - Last Return Raw
//bit4 - Last Return Filtered
//bit5 - Last Return Strength
//bit6 - Background Noise
//bit7 - Temperature
// if no setting was passed to the function, we use the following:
if (Value == 0){
// comment ones not the be displayed
Value |= 0x01; //bit0 - First Return Raw
//Value |= 0x02; //bit1 - First Return Filtered
Value |= 0x04; //bit2 - First Return Strength
Value |= 0x08; //bit3 - Last Return Raw
//Value |= 0x10; //bit4 - Last Return Filtered
Value |= 0x20; //bit5 - Last Return Strength
Value |= 0x40; //bit6 - Background Noise
Value |= 0x80; //bit7 - Temperature
}
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 29;
serialOutputHeader.Length = 5;
serialOutputHeader.Write_Read = 1;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// move the data to the output buffer
memcpy(&serialOutputBuffer[4],&Value,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 8);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[8],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],10);
DistanceOutConfig = Value;
}
// This will change the Data Stream output Type
// The integer value of the selected Typt must be passed to the function
// The available stream types are:
// 0 - Disable Streaming
// 10 - Signal Probability Data (reply with ID 200 and 201)
// 5 - Distance Data in cm(reply with ID 44)(output as set with Distance Output Configuration ID 29)
void LW_SF30::writeDataStreamType(uint32_t Type){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 30;
serialOutputHeader.Length = 5;
serialOutputHeader.Write_Read = 1;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// move the data to the output buffer
memcpy(&serialOutputBuffer[4],&Type,4);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 8);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[8],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],10);
}
// This will change the Serial Baudrate
void LW_SF30::writeSerialBaudrate(uint8_t Rate){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 91;
serialOutputHeader.Length = 2;
serialOutputHeader.Write_Read = 1;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// move the data to the output buffer
memcpy(&serialOutputBuffer[4],&Rate,1);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 5);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[5],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],7);
}
// This will change the I2C interface Address
void LW_SF30::writeI2CAddress(uint8_t Address){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 92;
serialOutputHeader.Length = 2;
serialOutputHeader.Write_Read = 1;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// move the data to the output buffer
memcpy(&serialOutputBuffer[4],&Address,1);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 5);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[5],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],7);
}
// This will change the Sampling Rate
void LW_SF30::writeSamplingRate(uint8_t Rate){
uint16_t CRC = 0;
// Setup the Header Data
serialOutputHeader.Start = 0xAA;
serialOutputHeader.ID = 76;
serialOutputHeader.Length = 2;
serialOutputHeader.Write_Read = 1;
// move the header to the output buffer
memcpy(&serialOutputBuffer[0],&serialOutputHeader,4);
// move the data to the output buffer
memcpy(&serialOutputBuffer[4],&Rate,1);
// calculate the CRC
CRC = createCRC(&serialOutputBuffer[0], 5);
// move the CRC to the output buffer
memcpy(&serialOutputBuffer[5],&CRC,2);
// write the output buffer to the serial port
Serial1->write(&serialOutputBuffer[0],7);
}
// Read data sent back from the SF30/D
// This function will check if new input data is available and will then
// process it.
// Data received will then be unpacked and saved in global variables.
// The function will return 1 if a new data packet has been processed.
// Pass 1 to the function if you want to report the received data on the
// serial terminal
uint8_t LW_SF30::ProcessSerialInput(int16_t report){
uint8_t temp_byte = 0;
uint16_t calc_crc = 0;
uint16_t recv_crc = 0;
uint8_t new_data = 0;
uint32_t tempSel = DistanceOutConfig;
uint32_t data_cnt = 0;
uint8_t raw_stream_cnt = 0;
int16_t i = 0;
//Need to check if there is any new data available in the receive buffer
while (Serial1->available()) {
if (serialInputCount == 0){
temp_byte = Serial1->read();
if (temp_byte == 0xAA){
serialInputBuffer[serialInputCount] = temp_byte;
serialInputCount++;
}
}
else {
serialInputBuffer[serialInputCount] = Serial1->read();
//Serial->println(serialInputBuffer[serialInputCount],HEX);
serialInputCount++;
}
}
//The minimum length received for a valid data packet will be 6 bytes
//Lets start checking the data as soon as we received 6 bytes
if (serialInputCount >= 6){
// move the first 4 bytes to the serial input Header
memcpy(&serialInputHeader,&serialInputBuffer[0],4);
if ((serialInputHeader.Length + 5) <= serialInputCount){
// We received all the data required for this packet
// calculate the CRC of the received packet
calc_crc = createCRC(&serialInputBuffer[0], (serialInputHeader.Length + 3));
// get the received CRC
memcpy(&recv_crc,&serialInputBuffer[serialInputHeader.Length + 3],2);
// compare the received and calculated CRC values
if (calc_crc == recv_crc){
// all checks out and we can use the data
if (report == 1){
Serial->print("Data packet received with ID: ");
Serial->println(serialInputHeader.ID,DEC);
}
// lets assume for now there is no other data in the input buffer
// and then clear the counter
serialInputCount = 0;
//we have now valid data to process
switch (serialInputHeader.ID){
case 0:// Product Name (16 bytes string)
//move the data from the buffer to the variable
memcpy(&HardwareModel[0],&serialInputBuffer[4],16);
// lets print the received data if required
if (report == 1){
Serial->print("Hardware Model: ");
Serial->write(HardwareModel,16);
Serial->println(" ");
}
new_data = 1;
break;
case 1:// Hardware Version (4 bytes)
//move the data from the buffer to the variable
memcpy(&HardwareVersion,&serialInputBuffer[4],4);
// lets print the received data if required
if (report == 1){
Serial->print("Hardware Version: ");
Serial->print(HardwareVersion,DEC);
Serial->println(" ");
}
new_data = 1;
break;
case 2: // Frimware Version (4 bytes)
//move the data from the buffer to the variable
FirmwareVersionPatch = serialInputBuffer[4];
FirmwareVersionMinor = serialInputBuffer[5];
FirmwareVersionMajor = serialInputBuffer[6];
FirmwareVersionReserved = serialInputBuffer[7];
// lets print the received data if required
if (report == 1){
Serial->print("Firmware Version: ");
Serial->print(FirmwareVersionMajor,DEC);
Serial->print(".");
Serial->print(FirmwareVersionMinor,DEC);
Serial->print(".");
Serial->println(FirmwareVersionPatch,DEC);
}
new_data = 1;
break;
case 3: // Serial Number (16 bytes string)
//move the data from the buffer to the variable
memcpy(&SerialNumber[0],&serialInputBuffer[4],16);
// lets print the received data if required
if (report == 1){
Serial->print("Serial Number: ");
Serial->write(SerialNumber,16);
Serial->println(" ");
}
new_data = 1;
break;
case 10: // Token (2 bytes)
//move the data from the buffer to the variable
memcpy(&Token,&serialInputBuffer[4],2);
// lets print the received data if required
if (report == 1){
Serial->print("Token: ");
Serial->println(Token,DEC);
}
new_data = 1;
break;
case 12: // Save Parameters (none)
// lets print the received data if required
if (report == 1){
Serial->println("Save Parameter Return Recieved");
}
break;
case 14: // Reset (none)
// lets print the received data if required
if (report == 1){
Serial->println("Save Parameter Return Recieved");
}
break;
case 29: // Distance output configuration (4 bytes)
//move the data from the buffer to the variable
memcpy(&DistanceOutConfig,&serialInputBuffer[4],4);
// lets print the received data if required
if (report == 1){
Serial->print("Distance output Config: ");
Serial->println(DistanceOutConfig,DEC);
}
new_data = 1;
break;
case 30: // Current Data Stream Type (4 bytes)
//move the data from the buffer to the variable
memcpy(&DataStreamType,&serialInputBuffer[4],4);
// lets print the received data if required
if (report == 1){
Serial->print("Data Stream Type: ");
Serial->println(DataStreamType,DEC);
}
new_data = 1;
break;
// case 40: // Raw Streaming Data (varies number of bytes)
// data_cnt = 4;
// //move the data from the buffer to the variable
// raw_stream_cnt = serialInputBuffer[data_cnt++];
// memcpy(&RawDataStream[0],&serialInputBuffer[data_cnt],raw_stream_cnt*2);
// // lets print the received data if required
// if (report == 1){
// for (i = 0; i < raw_stream_cnt; i++){
// Serial->println(RawDataStream[i],DEC);
// }
// }
// new_data = 1;
// break;
case 44: // Distance Data (varies number of bytes)
//we need to run through the Distance Output Configuration bits to know what
//data is being sent to us
data_cnt = 4;
for (i = 0; i < 16; i++){
//Serial->print(i);
//Serial->println(" ");
if ((tempSel & 1) == 1){
switch (i){
case 0:
firstRaw_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
if (report == 1){
Serial->print(firstRaw_cm);
Serial->print(" ");
}
break;
case 1:
firstFiltered_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
if (report == 1){
Serial->print(firstFiltered_cm);
Serial->print(" ");
}
break;
case 2:
firstStrength_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
if (report == 1){
Serial->print(firstStrength_cm);
Serial->print(" ");
}
break;
case 3:
lastRaw_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
if (report == 1){
Serial->print(lastRaw_cm);
Serial->print(" ");
}
break;
case 4:
lastFiltered_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
if (report == 1){
Serial->print(lastFiltered_cm);
Serial->print(" ");
}
break;
case 5:
lastStrength_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
if (report == 1){
Serial->print(lastStrength_cm);
Serial->print(" ");
}
break;
case 6:
backgroundNoise = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8);
if (report == 1){
Serial->print(backgroundNoise);
Serial->print(" ");
}
break;
case 7:
APDTemperature = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8);
if (report == 1){
Serial->print(APDTemperature);
Serial->print(" ");
}
break;
default:
break;
}
}
tempSel >>= 1;
}
if (report == 1){
Serial->println(" ");
}
new_data = 1;
break;
case 55: // APD Temperature (4 byte)
//move the data from the buffer to the variable
memcpy(&APDTemperature,&serialInputBuffer[4],4);
// lets print the received data if required
if (report == 1){
Serial->print("APD Temperature: ");
Serial->println(APDTemperature,DEC);
}
new_data = 1;
break;
case 91: // Serial BaudRate (1 byte)
//move the data from the buffer to the variable
serialBaudrate = serialInputBuffer[4];
// lets print the received data if required
if (report == 1){
Serial->print("Serial Baudrate: ");
Serial->println(serialBaudTable[serialBaudrate],DEC);
}
new_data = 1;
break;
case 92: // I2C address (1 byte)
//move the data from the buffer to the variable
I2CAddress = serialInputBuffer[4];
// lets print the received data if required
if (report == 1){
Serial->print("I2C Address: ");
Serial->println(I2CAddress,HEX);
}
new_data = 1;
break;
case 76: // Sampling Rate (1 byte)
//move the data from the buffer to the variable
memcpy(&SamplingRate,&serialInputBuffer[4],1);
// lets print the received data if required
if (report == 1){
Serial->print("Sampling Rate: ");
Serial->println(SamplingRate,DEC);
}
new_data = 1;
break;
case 200: // Signal probability Data (?????)(200 and 201)
data_cnt = 4;
//move the data from the buffer to the variable
StatBucketCount = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8)
| ((uint32_t)serialInputBuffer[data_cnt++] << 16) | ((uint32_t)serialInputBuffer[data_cnt++] << 24);
memcpy(&StatBucketsFront[0],&serialInputBuffer[data_cnt],StatBucketCount*2);
data_cnt += StatBucketCount*2;
memcpy(&StatBucketsRear[0],&serialInputBuffer[data_cnt],StatBucketCount*2);
data_cnt += StatBucketCount*2;
StatShotCount = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8)
| ((uint32_t)serialInputBuffer[data_cnt++] << 16) | ((uint32_t)serialInputBuffer[data_cnt++] << 24);
// lets print the received data if required
if (report == 1){
Serial->print("Statistic Bucket Count: ");
Serial->println(StatBucketCount,DEC);
Serial->print("Statistic Shot Count: ");
Serial->println(StatShotCount,DEC);
for (i = 0; i < StatBucketCount; i++){
Serial->print(i,DEC);
Serial->print(" ");
Serial->print(StatBucketsFront[i],DEC);
Serial->print(" ");
Serial->println(StatBucketsRear[i],DEC);
}
}
new_data = 1;
break;
// case 202: // Distance Data in mm(varies number of bytes)
// //we need to run through the Distance Output Configuration bits to know what
// //data is being sent to us
// data_cnt = 4;
// for (i = 0; i < 16; i++){
// //Serial->print(i);
// //Serial->println(" ");
// if ((tempSel & 1) == 1){
// switch (i){
// case 0:
// firstRaw_mm = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8)
// | ((uint32_t)serialInputBuffer[data_cnt++] << 16) | ((uint32_t)serialInputBuffer[data_cnt++] << 24);
// if (report == 1){
// Serial->print(firstRaw_mm);
// Serial->print(" ");
// }
// break;
// case 1:
// firstFiltered_mm = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8)
// | ((uint32_t)serialInputBuffer[data_cnt++] << 16) | ((uint32_t)serialInputBuffer[data_cnt++] << 24);
// if (report == 1){
// Serial->print(firstFiltered_mm);
// Serial->print(" ");
// }
// break;
// case 2:
// firstStrength_mm = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8)
// | ((uint32_t)serialInputBuffer[data_cnt++] << 16) | ((uint32_t)serialInputBuffer[data_cnt++] << 24);
// if (report == 1){
// Serial->print(firstStrength_mm);
// Serial->print(" ");
// }
// break;
// case 3:
// lastRaw_mm = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8)
// | ((uint32_t)serialInputBuffer[data_cnt++] << 16) | ((uint32_t)serialInputBuffer[data_cnt++] << 24);
// if (report == 1){
// Serial->print(lastRaw_mm);
// Serial->print(" ");
// }
// break;
// case 4:
// lastFiltered_mm = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8)
// | ((uint32_t)serialInputBuffer[data_cnt++] << 16) | ((uint32_t)serialInputBuffer[data_cnt++] << 24);
// if (report == 1){
// Serial->print(lastFiltered_mm);
// Serial->print(" ");
// }
// break;
// case 5:
// lastStrength_mm = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8)
// | ((uint32_t)serialInputBuffer[data_cnt++] << 16) | ((uint32_t)serialInputBuffer[data_cnt++] << 24);
// if (report == 1){
// Serial->print(lastStrength_mm);
// Serial->print(" ");
// }
// break;
// case 6:
// backgroundNoise = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8)
// | ((uint32_t)serialInputBuffer[data_cnt++] << 16) | ((uint32_t)serialInputBuffer[data_cnt++] << 24);
// if (report == 1){
// Serial->print(backgroundNoise);
// Serial->print(" ");
// }
// break;
// case 7:
// APDTemperature = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8)
// | ((uint32_t)serialInputBuffer[data_cnt++] << 16) | ((uint32_t)serialInputBuffer[data_cnt++] << 24);
// if (report == 1){
// Serial->print(APDTemperature);
// Serial->print(" ");
// }
// break;
// default:
// break;
// }
// }
// tempSel >>= 1;
// }
// if (report == 1){
// Serial->println(" ");
// }
// new_data = 1;
// break;
// case 204: // Distance Data in cm (varies number of bytes)
// //we need to run through the Distance Output Configuration bits to know what
// //data is being sent to us
// data_cnt = 4;
// for (i = 0; i < 16; i++){
// //Serial->print(i);
// //Serial->println(" ");
// if ((tempSel & 1) == 1){
// switch (i){
// case 0:
// firstRaw_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
// if (report == 1){
// Serial->print(firstRaw_cm);
// Serial->print(" ");
// }
// break;
// case 1:
// firstFiltered_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
// if (report == 1){
// Serial->print(firstFiltered_cm);
// Serial->print(" ");
// }
// break;
// case 2:
// firstStrength_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
// if (report == 1){
// Serial->print(firstStrength_cm);
// Serial->print(" ");
// }
// break;
// case 3:
// lastRaw_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
// if (report == 1){
// Serial->print(lastRaw_cm);
// Serial->print(" ");
// }
// break;
// case 4:
// lastFiltered_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
// if (report == 1){
// Serial->print(lastFiltered_cm);
// Serial->print(" ");
// }
// break;
// case 5:
// lastStrength_cm = ((uint16_t)serialInputBuffer[data_cnt++] << 0) | ((uint16_t)serialInputBuffer[data_cnt++] << 8);
// if (report == 1){
// Serial->print(lastStrength_cm);
// Serial->print(" ");
// }
// break;
// case 6:
// backgroundNoise = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8);
// if (report == 1){
// Serial->print(backgroundNoise);
// Serial->print(" ");
// }
// break;
// case 7:
// APDTemperature = ((uint32_t)serialInputBuffer[data_cnt++] << 0) | ((uint32_t)serialInputBuffer[data_cnt++] << 8);
// if (report == 1){
// Serial->print(APDTemperature);
// Serial->print(" ");
// }
// break;
// default:
// break;
// }
// }
// tempSel >>= 1;
// }
// if (report == 1){
// Serial->println(" ");
// }
// new_data = 1;
// break;
default:
Serial->println("Unknown packet ID received");
break;
}
}
else {
//we failed the crc so the data is corrupted. Lets clear the buffer.
serialInputCount = 0;
}
}
}
return new_data;