-
Notifications
You must be signed in to change notification settings - Fork 0
/
xbeeAPI.cpp
1961 lines (1678 loc) · 56.4 KB
/
xbeeAPI.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
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <pt.h> // libraries
#include <SPI.h>
#include "xbeeAPI.h"
/*******DEBUG DEFINITIONS, DEFINE IN SKETCH TO DEBUG SPEICIFIC FUNCTIONS*******/
// #define debug_all - turns on debugging for all functions in library
// #define debug_Xbee
// #define debug_begin
// #define debug_setSPI_clockFreq
// #define debug_setSPI_bitOrder
// #define debug_setSPI_mode
// #define debug_setDestinationAddress
// #define debug_setCommandInterruptPin
// #define debug_setCutdownInterruptPin
// #define debug_setLocalCommand_DIO
// #define debug_setLocalCutdown_DIO
// #define debug_setDestinationCommand_DIO
// #define debug_setDestinationCutdown_DIO
// #define debug_sendFrame
// #define debug_sendPayload
// #define debug_printStats
// #define debug_sendAvailable
// #define debug_messageWaiting
// #define debug_protothreadLoop
// #define debug_setMaxPayloadSize
// #define debug_resetStatus
// #define debug_clearBuffers
// #define debug_sendCommand
// #define debug_setMode
#define debug_receivePacket_sense
// #define debug_parsePacket_sense
// #define debug_commandMode_sense
// #define debug_commandModeTO_sense
// #define debug_fastMode_sense
// #define debug_accModeLevel0_sense
// #define debug_accModeLevel1_sense
// #define debug_receiverMode_sense
// #define debug_garbageCollect_sense
// #define debug_command_ISR
// #define debug_cutdown_ISR
// #define debug_checkSum
#define debug_recSPI
// #define debug_parsePacket
// #define debug_exitCommandMode
// #define debug_setDestinationCommand_State
// #define debug_setDestinationCutdown_State
#ifdef debug_all
#define debug_Xbee
#define debug_begin
#define debug_setSPI_clockFreq
#define debug_setSPI_bitOrder
#define debug_setSPI_mode
#define debug_setDestinationAddress
#define debug_setCommandInterruptPin
#define debug_setCutdownInterruptPin
#define debug_setLocalCommand_DIO
#define debug_setLocalCutdown_DIO
#define debug_setDestinationCommand_DIO
#define debug_setDestinationCutdown_DIO
#define debug_sendFrame
#define debug_sendPayload
#define debug_printStats
#define debug_sendAvailable
#define debug_messageWaiting
#define debug_protothreadLoop
#define debug_setMaxPayloadSize
#define debug_resetStatus
#define debug_clearBuffers
#define debug_sendCommand
#define debug_setMode
#define debug_receivePacket_sense
#define debug_parsePacket_sense
#define debug_commandMode_sense
#define debug_commandModeTO_sense
#define debug_fastMode_sense
#define debug_accModeLevel0_sense
#define debug_accModeLevel1_sense
#define debug_receiverMode_sense
#define debug_garbageCollect_sense
#define debug_command_ISR
#define debug_cutdown_ISR
#define debug_checkSum
#define debug_recSPI
#define debug_parsePacket
#define debug_exitCommandMode
#define debug_setDestinationCommand_State
#define debug_setDestinationCutdown_State
#endif
/***************************************************************************
************************* PUBLIC FUNCTIONS ********************************
***************************************************************************/
/***************************************************************************
CONSTRUCTOR
***************************************************************************/
Xbee::Xbee(uint8_t slave_select, uint8_t attention_pin)
{
#ifdef debug_Xbee
Serial.println(F("constructor: called"));
#endif
_XBEE_SS = slave_select;
_XBEE_ATTN = attention_pin;
_XBEE_CMD = NULL;
_XBEE_CUTDWN = NULL;
pinMode(_XBEE_SS,OUTPUT);
pinMode(_XBEE_ATTN, INPUT_PULLUP);
digitalWrite(_XBEE_SS, HIGH);
PT_INIT(&receivePacket_PT);
PT_INIT(&parsePacket_PT);
PT_INIT(&commandMode_PT);
PT_INIT(&commandModeTO_PT);
PT_INIT(&fastMode_PT);
PT_INIT(&accModeLevel0_PT);
PT_INIT(&accModeLevel1_PT);
PT_INIT(&receiverMode_PT);
PT_INIT(&garbageCollect_PT);
#ifdef debug_Xbee
Serial.println(F("constructor: leaving"));
#endif
}
/***************************************************************************
BEGIN
***************************************************************************/
uint16_t Xbee::begin(void)
{
#ifdef debug_begin
Serial.println(F("begin : called"));
#endif
activity_counter = millis();
_MODE = 1;
_SPI_DATAMODE = SPI_MODE0;
_SPI_CLK_FREQ = 500000;
_SPI_BITORDER = MSBFIRST;
commandMode = false;
commandMode_TO_threshold = 5000;
data_received_length = 0;
command_receive_length = -1;
_maxPackets = 0;
lastPacket_TIME = 0;
errorStatus = 0x00;
uint8_t send_header_init[17] = {0x7E, 0x00, 0x00, 0x10, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFE, 0x00, 0x40};
for(int i = 0; i < 17; i++)
{
_send_header[i] = send_header_init[i];
}
#ifdef debug_begin
Serial.print(F("begin : send_header = "));
for(int i = 0; i < 17; i++)
{
if(send_header_init[i] <= 0x0F)
{
Serial.print(F("0"));
}
Serial.print(send_header_init[i]);
Serial.print(F(" "));
}
Serial.println("");
#endif
uint8_t tester[8] = {0x7E, 0x00, 0x04, 0x08, 0x01, 0x4E, 0x50, 0x58};
uint8_t tester_reply[15] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
long begin_to = 0;
bool attn_state = true;
int begin_index = -1;
SPI.begin();
SPI.beginTransaction(SPISettings(_SPI_CLK_FREQ,_SPI_BITORDER,_SPI_DATAMODE));
digitalWrite(_XBEE_SS,LOW);
SPI.transfer(tester,8);
digitalWrite(_XBEE_SS,HIGH);
SPI.endTransaction();
begin_to = millis();
while(attn_state && (millis()-begin_to)<=5000)
{
attn_state = digitalRead(_XBEE_ATTN);
}
#ifdef debug_begin
Serial.print(F("begin : receive while loop exited, attn_state = "));
Serial.println(attn_state);
#endif
if(!attn_state)
{
SPI.beginTransaction(SPISettings(_SPI_CLK_FREQ,_SPI_BITORDER,_SPI_DATAMODE));
digitalWrite(_XBEE_SS,LOW);
SPI.transfer(tester_reply,15);
digitalWrite(_XBEE_SS,HIGH);
SPI.endTransaction();
for(int i = 0; i < 15; i++)
{
if(tester_reply[i] == uint8_t(0x7E))
{
#ifdef debug_begin
Serial.println(F("begin : start byte found on reply!"));
#endif
begin_index = i;
break;
}
}
if(begin_index < 0 || begin_index > 4)
{
#ifdef debug_begin
Serial.println(F("begin : begin_index kickout, return false"));
#endif
return false;
}
uint8_t reply_status = tester_reply[begin_index+7];
uint8_t np_msb = tester_reply[begin_index+8];
uint8_t np_lsb = tester_reply[begin_index+9];
#ifdef debug_begin
Serial.print(F("begin : reply_status = "));
Serial.println(reply_status);
Serial.print(F("begin : np_msb = "));
Serial.println(np_msb);
Serial.print(F("begin : np_lsb = "));
Serial.println(np_lsb);
#endif
if(!reply_status)
{
#ifdef debug_begin
Serial.println(F("begin : success, returning _NP"));
#endif
_NP = (np_msb*256) + np_lsb;
max_payload_size = _NP;
return _NP;
}
else
{
#ifdef debug_begin
Serial.println(F("begin : reply status != 0, return false"));
#endif
return false;
}
}
#ifdef debug_begin
Serial.println(F("begin : attn_state cause return false"));
#endif
return false;
}
/***************************************************************************
setSPI_clockFreq
***************************************************************************/
void Xbee::setSPI_clockFreq(uint32_t freq)
{
#ifdef debug_setSPI_clockFreq
Serial.println(F("debug_setSPI_clockFreq : called"));
#endif
activity_counter = millis();
_SPI_CLK_FREQ = freq;
#ifdef debug_setSPI_clockFreq
Serial.println(F("debug_setSPI_clockFreq : exited"));
#endif
}
/***************************************************************************
setSPI_bitOrder
***************************************************************************/
void Xbee::setSPI_bitOrder(BitOrder bitorder)
{
#ifdef debug_setSPI_bitOrder
Serial.println(F("setSPI_bitOrder : called"));
#endif
activity_counter = millis();
_SPI_BITORDER = bitorder;
#ifdef debug_setSPI_bitOrder
Serial.println(F("setSPI_bitOrder : exit"));
#endif
}
/***************************************************************************
setSPI_mode
***************************************************************************/
void Xbee::setSPI_mode(uint8_t spi_mode)
{
#ifdef debug_setSPI_mode
Serial.println(F("setSPI_mode : called"));
#endif
activity_counter = millis();
_SPI_DATAMODE = spi_mode;
#ifdef debug_setSPI_mode
Serial.println(F("setSPI_mode : exit"));
#endif
}
/***************************************************************************
setDestinationAddress
***************************************************************************/
void Xbee::setDestinationAddress(uint64_t addr)
{
#ifdef debug_setDestinationAddress
Serial.println(F("setDestinationAddress : called"));
#endif
activity_counter = millis();
uint32_t upperAddy = (uint32_t)(addr >> 32);
uint32_t lowerAddy = (uint32_t)(addr);
for(int i = 3; i >= 0; i--)
{
uint32_t bitMask = 0b11111111 << ((3 - i) * 8);
_ADDR[i+4] = (uint8_t)((lowerAddy & bitMask) >> ((3 - i) * 8));
_ADDR[i] = (uint8_t)((upperAddy & bitMask) >> ((3 - i) * 8));
}
for(int i = 0; i < 8; i++)
{
_send_header[i+5] = _ADDR[i];
}
#ifdef debug_setDestinationAddress
Serial.println(F("setDestinationAddress : exit"));
#endif
}
/***************************************************************************
setCommandInterruptPin not functional
***************************************************************************/
void Xbee::setCommandInterruptPin(uint8_t pin)
{
#ifdef debug_setCommandInterruptPin
Serial.println(F("setCommandInterruptPin : called"));
#endif
activity_counter = millis();
_XBEE_CMD = pin;
pinMode(_XBEE_CMD,INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(_XBEE_CMD),Xbee::command_ISR,RISING); // brokend for some reason
#ifdef debug_setCommandInterruptPin
Serial.println(F("setCommandInterruptPin : exit"));
#endif
}
/***************************************************************************
setCutdownInterruptPin not functional
***************************************************************************/
void Xbee::setCutdownInterruptPin(uint8_t pin)
{
#ifdef debug_setCutdownInterruptPin
Serial.println(F("setCutdownInterruptPin : called"));
#endif
activity_counter = millis();
_XBEE_CUTDWN = pin;
pinMode(_XBEE_CUTDWN,INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(_XBEE_CMD),Xbee::cutdown_ISR,RISING); // broken for some reason
#ifdef debug_setCutdownInterruptPin
Serial.println(F("setCutdownInterruptPin : exit"));
#endif
}
/***************************************************************************
setLocalCommand_DIO
***************************************************************************/
void Xbee::setLocalCommand_DIO(uint8_t dio_pin)
{
#ifdef debug_setLocalCommand_DIO
Serial.println(F("setLocalCommand_DIO : called"));
#endif
activity_counter = millis();
_LOCAL_COMMAND = dio_pin;
#ifdef debug_setLocalCommand_DIO
Serial.println(F("setLocalCommand_DIO : exit"));
#endif
}
/***************************************************************************
setLocalCutdown_DIO
***************************************************************************/
void Xbee::setLocalCutdown_DIO(uint8_t dio_pin)
{
#ifdef debug_setLocalCutdown_DIO
Serial.println(F("setLocalCutdown_DIO : called"));
#endif
activity_counter = millis();
_LOCAL_CUTDOWN = dio_pin;
#ifdef debug_setLocalCutdown_DIO
Serial.println(F("setLocalCutdown_DIO : exit"));
#endif
}
/***************************************************************************
setDestinationCommand_DIO
***************************************************************************/
void Xbee::setDestinationCommand_DIO(uint8_t dio_pin)
{
#ifdef debug_setDestinationCommand_DIO
Serial.println(F("setDestinationCommand_DIO : called"));
#endif
activity_counter = millis();
_DEST_COMMAND[0] = 'D';
_DEST_COMMAND[1] = dio_pin;
#ifdef debug_setDestinationCommand_DIO
Serial.println(F("setDestinationCommand_DIO : exit"));
#endif
}
/***************************************************************************
setDestinationCutdown_DIO
***************************************************************************/
void Xbee::setDestinationCutdown_DIO(uint8_t dio_pin)
{
#ifdef debug_setDestinationCutdown_DIO
Serial.println(F("setDestinationCutdown_DIO : called"));
#endif
activity_counter = millis();
_DEST_CUTDOWN[0] = 'D';
_DEST_CUTDOWN[1] = dio_pin;
#ifdef debug_setDestinationCutdown_DIO
Serial.println(F("setDestinationCutdown_DIO : exit"));
#endif
}
/***************************************************************************
sendFrame
***************************************************************************/
void Xbee::sendFrame(uint8_t sendBuffer[], uint16_t length_of_buffer)
{
#ifdef debug_sendFrame
Serial.println(F("sendFrame : called"));
for(int i = 0; i < length_of_buffer; i++)
{
if(sendBuffer[i] <= 0x0F)
{
Serial.print(F("0"));
}
Serial.print(sendBuffer[i],HEX);
Serial.print(F(" "));
}
Serial.println("");
#endif
activity_counter = millis();
_sending = true;
if(length_of_buffer <= (_NP + 18))
{
#ifdef debug_sendFrame
Serial.println(F("sendFrame : length_of_buffer acceptable, sending frame"));
#endif
SPI.beginTransaction(SPISettings(_SPI_CLK_FREQ,_SPI_BITORDER,_SPI_DATAMODE));
digitalWrite(_XBEE_SS,LOW);
SPI.transfer(sendBuffer,length_of_buffer);
digitalWrite(_XBEE_SS,HIGH);
SPI.endTransaction();
}
if(_MODE == 0)
{
#ifdef debug_sendFrame
Serial.println(F("sendFrame : mode = 0 so _sending = false before exit"));
#endif
_sending = false;
}
#ifdef debug_sendFrame
Serial.println(F("sendFrame : exit"));
#endif
}
/***************************************************************************
sendPayload
***************************************************************************/
void Xbee::sendPayload(uint8_t sendBuffer[], uint16_t length_of_buffer)
{
#ifdef debug_sendPayload
Serial.println(F("sendPayload : called"));
#endif
activity_counter = millis();
_sendPayload = true;
if(length_of_buffer <= max_payload_size)
{
#ifdef debug_sendPayload
Serial.println(F("sendPayload : length of buff < max_payload_size, sending as-is"));
#endif
uint16_t frame_length = 14 + length_of_buffer;
_send_header[2] = (uint8_t)frame_length;
_send_header[1] = (uint8_t)(frame_length >> 8);
for(int i = 0; i< 17; i++)
{
_send_buffer[i] = _send_header[i];
}
for(int i = 0; i < length_of_buffer; i++)
{
_send_buffer[i+17] = sendBuffer[i];
}
#ifdef debug_sendPayload
Serial.print(F("sendPayload : sendBuffer with header = "));
for(int i = 0; i < frame_length+3; i++)
{
if(_send_buffer[i] <= 0x0F)
{
Serial.print(F("0"));
}
Serial.print(_send_buffer[i],HEX);
Serial.print(F(" "));
}
Serial.println(F(""));
#endif
uint8_t intermed_buff[length_of_buffer+14];
for(int i = 0; i < length_of_buffer+14; i++)
{
intermed_buff[i] = _send_buffer[i+3];
}
_send_buffer[length_of_buffer + 17] = Xbee::checkSum(intermed_buff,(length_of_buffer+14));
#ifdef debug_sendPayload
Serial.print(F("sendPayload : intermed_buff = "));
for(int i = 0; i < length_of_buffer+14; i++)
{
if(intermed_buff[i] <= 0x0F)
{
Serial.print(F("0"));
}
Serial.print(intermed_buff[i],HEX);
Serial.print(F(" "));
}
Serial.println(F(""));
Serial.print(F("sendPayload : chksum = "));
Serial.println(_send_buffer[length_of_buffer+17],HEX);
#endif
Xbee::sendFrame(_send_buffer,length_of_buffer+18);
}
_sendPayload = false;
#ifdef debug_sendPayload
Serial.println(F("sendPayload : exit"));
#endif
}
/***************************************************************************
printStats
***************************************************************************/
void Xbee::printStats(void)
{
activity_counter = millis();
Serial.print(F("xbee ss pin = "));
Serial.println(_XBEE_SS);
Serial.print(F("xbee attn pin = "));
Serial.println(_XBEE_ATTN);
Serial.print(F("xbee cmdpin = "));
Serial.println(_XBEE_CMD);
Serial.print(F("xbee cutdown = "));
Serial.println(_XBEE_CUTDWN);
Serial.print(F("xbee _SPI_CLK_FREQ = "));
Serial.println(_SPI_CLK_FREQ);
Serial.print(F("xbee _NP = "));
Serial.println(_NP);
Serial.print(F("xbee max_payload_size = "));
Serial.println(max_payload_size);
Serial.print(F("Mode = "));
Serial.println(_MODE);
Serial.print(F("xbee _LOCAL_COMMAND = "));
Serial.println(_LOCAL_COMMAND);
Serial.print(F("xbee _LOCAL_CUTDOWN = "));
Serial.println(_LOCAL_CUTDOWN);
Serial.print(F("xbee _DEST_COMMAND = "));
Serial.print((char)_DEST_COMMAND[0]);
Serial.println(_DEST_COMMAND[1]);
Serial.print(F("xbee _DEST_CUTDOWN = "));
Serial.print((char)_DEST_CUTDOWN[0]);
Serial.println(_DEST_CUTDOWN[1]);
Serial.print(F("xbee _ADDR = "));
for(int i = 0; i<8; i++)
{
if(_ADDR[i] <= 0x0F)
{
Serial.print(F("0"));
}
Serial.print(_ADDR[i],HEX);
Serial.print(' ');
}
return;
}
/***************************************************************************
sendAvailable
***************************************************************************/
bool Xbee::sendAvailable(void)
{
#ifdef debug_sendAvailable
Serial.println(F("sendAvailable : called and returning"));
#endif
return !(_sendPayload || _sending);
}
/***************************************************************************
messageWaiting
***************************************************************************/
uint16_t Xbee::messageWaiting(void)
{
#ifdef debug_messageWaiting
Serial.println(F("messageWaiting : called and returning"));
#endif
activity_counter = millis();
return data_received_length;
}
/***************************************************************************
protothreadLoop
***************************************************************************/
void Xbee::protothreadLoop(void)
{
#ifdef debug_protothreadLoop
Serial.println(F("protothreadLoop : called"));
#endif
Xbee::receivePacket_sense(&receivePacket_PT);
Xbee::parsePacket_sense(&parsePacket_PT);
Xbee::commandMode_sense(&commandMode_PT);
Xbee::commandModeTO_sense(&commandModeTO_PT);
Xbee::fastMode_sense(&fastMode_PT);
Xbee::accModeLevel0_sense(&accModeLevel0_PT);
Xbee::accModeLevel1_sense(&accModeLevel1_PT);
Xbee::receiverMode_sense(&receiverMode_PT);
Xbee::garbageCollect_sense(&garbageCollect_PT);
#ifdef debug_protothreadLoop
Serial.println(F("protothreadLoop : exit"));
#endif
}
/***************************************************************************
setMaxPayloadSize NOT TESTED
***************************************************************************/
void Xbee::setMaxPayloadSize(int size_bytes)
{
#ifdef debug_setMaxPayloadSize
Serial.println(F("setMaxPayloadSize : called"));
#endif
activity_counter = millis();
max_payload_size= (uint16_t) size_bytes;
//_PAYLOAD_STRING.reserve(size_bytes); // not needed
#ifdef debug_setMaxPayloadSize
Serial.println(F("setMaxPayloadSize : exit"));
#endif
}
/***************************************************************************
resetStatus NOT TESTED
***************************************************************************/
void Xbee::resetStatus(void)
{
#ifdef debug_resetStatus
Serial.println(F("resetStatus : called"));
#endif
activity_counter = millis();
_sendPayload = false;
_packetReceived = false;
_packetParsed = false;
_send_confirmed = false;
_sending = false;
#ifdef debug_resetStatus
Serial.println(F("resetStatus : exit"));
#endif
}
/***************************************************************************
clearBuffers NOT TESTED
***************************************************************************/
void Xbee::clearBuffers(void)
{
#ifdef debug_clearBuffers
Serial.println(F("clearBuffers : called"));
#endif
activity_counter = millis();
_data_received_length = 0;
data_received_length = 0;
command_receive_length = -1;
for(int i = 0; i < 274; i++)
{
if(i<20)
{
command_receive_buffer[i] = 0x00;
data_received[i] = 0x00;
_receive_buffer[i] = 0x00;
_send_buffer[i] = 0x00;
}
else if(i >=20 && i < 256)
{
data_received[i] = 0x00;
_receive_buffer[i] = 0x00;
_send_buffer[i] = 0x00;
}
else
{
_receive_buffer[i] = 0x00;
_send_buffer[i] = 0x00;
}
}
#ifdef debug_clearBuffers
Serial.println(F("clearBuffers : exit"));
#endif
}
/***************************************************************************
sendCommand NOT TESTED
***************************************************************************/
bool Xbee::sendCommand(uint8_t command_type, uint8_t command_val)
{
uint8_t mode = _MODE;
Xbee::setMode(1);
//uint8_t sendMode = _send_header[4];
//_MODE = 1;
//_send_header[4] = 0x01;
bool functionState = false;
bool command_state = false;
bool command_response = false;
#ifdef debug_sendCommand
Serial.println(F("sendCommand : entering resetStatus from sendCommand"));
#endif
Xbee::resetStatus();
#ifdef debug_sendCommand
Serial.println(F("sendCommand : returning to sendCommand from resetStatus"));
#endif
_data_received_length = 0;
_packetReceived = false;
uint8_t frame[20];
for(int i = 0; i < 17; i++)
{
frame[i] = _send_header[i];
}
frame[17] = command_type;
frame[18] = command_val;
frame[1] = 0x00;
frame[2] = 0x10;
#ifdef debug_sendCommand
Serial.print(F("sendCommand : frame with header = "));
for(int i = 0; i < 20; i++)
{
if(frame[i] <= 0x0F)
{
Serial.print(F("0"));
}
Serial.print(frame[i],HEX);
Serial.print(F(" "));
}
Serial.println(F(""));
#endif
uint8_t subframe[16];
for(int i = 0; i < 16; i++)
{
subframe[i] = frame[i+3];
}
frame[19] = Xbee::checkSum(subframe,16);
#ifdef debug_sendCommand
Serial.print(F("sendCommand : subframe = "));
for(int i = 0; i < 16; i++)
{
if(subframe[i] <= 0x0F)
{
Serial.print(F("0"));
}
Serial.print(subframe[i],HEX);
Serial.print(F(" "));
}
Serial.println(F(""));
Serial.print(F("sendCommand : subframe checksum = "));
Serial.println(frame[19],HEX);
#endif
long command_timer = millis();
while((!command_state) && (millis() - command_timer <= 5000))
{
#ifdef debug_sendCommand
Serial.println(F("sendCommand : setting commandState to true")); //DEBUG
#endif
command_state = Xbee::setDestinationCommand_State(true);
}
_packetReceived = false;
_data_received_length = 0;
#ifdef debug_sendCommand
Serial.println(F("sendCommand : exiting setCommandState Loop")); //DEBUG
#endif
if(command_state)
{
#ifdef debug_sendCommand
Serial.println(F("sendCommand : commandState is true!"));
delay(5000); // DEBUG
#endif
sendFrame(frame,20);
command_timer = millis();
while(_data_received_length == 0 && millis()-command_timer < 1000)
{
Xbee::receivePacket_sense(&receivePacket_PT);
Xbee::parsePacket_sense(&parsePacket_PT);
}
if(_data_received_length)
{
#ifdef debug_sendCommand
Serial.println(F("sendCommand : while loop done, send confirmed!")); // DEBUG
#endif
command_response = _send_confirmed;
}
#ifdef debug_sendCommand
else
{
Serial.println(F("sendCommand : while loop done, _data_received_length = 0"));
} // DEBUG
#endif
}
#ifdef debug_sendCommand
else
{
Serial.println(F("sendCommand : failed to enter commandState"));
} // DEBUG
#endif
command_timer = millis();
_packetReceived = false;
_data_received_length = 0;
command_state = false;
while((!command_state) && (millis() - command_timer <= 5000))
{
#ifdef debug_sendCommand
Serial.println(F("sendCommand : setting commandState to false")); // DEBUG
#endif
command_state = Xbee::setDestinationCommand_State(false);
}
#ifdef debug_sendCommand
if(command_state)
{
Serial.println(F("sendCommand : confirmed commandState to false!"));
delay(5000);
}
else
{
Serial.println(F("sendCommand : failed to set commandState to false"));
} // DEBUG
#endif
Xbee::setMode(mode);
//_MODE = mode;
//_send_header[4] = sendMode;
#ifdef debug_sendCommand
Serial.println(F("sendCommand : exit")); // DEBUG
#endif
return functionState;
}
/***************************************************************************
setMode NOT TESTED
***************************************************************************/
void Xbee::setMode(uint8_t mode)
{
#ifdef debug_setMode
Serial.println(F("setMode : called"));
#endif
_MODE = mode;
switch (_MODE)
{
case 0:
_send_header[4] = 0x00;
break;
case 1:
_send_header[4] = 0x01;
break;
case 2:
_send_header[4] = 0x01;
break;
}
#ifdef debug_setMode
Serial.print(F("setMode : _send_header[4] = 0x0"));
Serial.println(_send_header[4],HEX);
Serial.println(F("setMode : exit"));
#endif
}
/***************************************************************************
************************ PRIVATE FUNCTIONS ********************************
***************************************************************************/
/***************************************************************************
receivePacket_sense PROTOTHREAD
***************************************************************************/
int Xbee::receivePacket_sense(struct pt *pt)
{
#ifdef debug_receivePacket_sense
//Serial.println(F("receivePacket_sense : called"));
#endif
PT_BEGIN(pt);
while(1)
{
PT_WAIT_UNTIL(pt, (_MODE > 0 && digitalRead(_XBEE_ATTN) == LOW));
_packetReceived = Xbee::recSPI();
#ifdef debug_receivePacket_sense
Serial.print(F(" "));
//Serial.print(F("_packetReceived = "));
//Serial.println(_packetReceived);
#endif
}
PT_END(pt);
#ifdef debug_receivePacket_sense
//Serial.println(F("receivePacket_sense : exit"));
#endif
}
/***************************************************************************
parsePacket_sense PROTOTHREAD
***************************************************************************/
int Xbee::parsePacket_sense(struct pt *pt)
{