@@ -287,7 +287,7 @@ uint8_t receivedPort(const char *s)
287
287
return port;
288
288
}
289
289
290
- TheThingsNetwork::TheThingsNetwork (Stream &modemStream, Stream &debugStream, ttn_fp_t fp, uint8_t sf, uint8_t fsb)
290
+ TheThingsNetwork::TheThingsNetwork (SerialType &modemStream, Stream &debugStream, ttn_fp_t fp, uint8_t sf, uint8_t fsb)
291
291
{
292
292
this ->debugStream = &debugStream;
293
293
this ->modemStream = &modemStream;
@@ -387,6 +387,12 @@ size_t TheThingsNetwork::readResponse(uint8_t prefixTable, uint8_t indexTable, u
387
387
return readLine (buffer, size);
388
388
}
389
389
390
+ size_t TheThingsNetwork::checkModuleAvailable ()
391
+ {
392
+ // Send sys get ver check we have an answer
393
+ return readResponse (SYS_TABLE, SYS_TABLE, SYS_GET_VER, buffer, sizeof (buffer));
394
+ }
395
+
390
396
void TheThingsNetwork::autoBaud ()
391
397
{
392
398
// Courtesy of @jpmeijers
@@ -396,19 +402,51 @@ void TheThingsNetwork::autoBaud()
396
402
while (attempts-- && length == 0 )
397
403
{
398
404
delay (100 );
405
+ // Send break + Autobaud
399
406
modemStream->write ((byte)0x00 );
400
407
modemStream->write (0x55 );
401
408
modemStream->write (SEND_MSG);
402
- sendCommand (SYS_TABLE, 0 , true , false );
403
- sendCommand (SYS_TABLE, SYS_GET, true , false );
404
- sendCommand (SYS_TABLE, SYS_GET_VER, false , false );
405
- modemStream-> write (SEND_MSG);
406
- length = modemStream-> readBytesUntil ( ' \n ' , buffer, sizeof (buffer)) ;
409
+ // check we can talk
410
+ length = checkModuleAvailable ( );
411
+
412
+ // We succeeded talking to the module ?
413
+ baudDetermined = (length > 0 ) ;
407
414
}
408
415
delay (100 );
409
416
clearReadBuffer ();
410
417
modemStream->setTimeout (10000 );
411
- baudDetermined = true ;
418
+ }
419
+
420
+ bool TheThingsNetwork::isSleeping ()
421
+ {
422
+ return !baudDetermined;
423
+ }
424
+
425
+ void TheThingsNetwork::wake ()
426
+ {
427
+ // If sleeping
428
+ if (isSleeping ())
429
+ {
430
+ // Send a 0 at lower speed to be sure always received
431
+ // as a character a 57600 baud rate
432
+ modemStream->flush ();
433
+ #ifdef HARDWARE_UART
434
+ modemStream->begin (2400 );
435
+ #endif
436
+ modemStream->write ((uint8_t ) 0x00 );
437
+ modemStream->flush ();
438
+ delay (20 );
439
+ // set baudrate back to normal and send autobaud
440
+ #ifdef HARDWARE_UART
441
+ modemStream->begin (57600 );
442
+ #endif
443
+ modemStream->write ((uint8_t )0x55 );
444
+ modemStream->flush ();
445
+ modemStream->write (SEND_MSG);
446
+ if (checkModuleAvailable () > 0 ) {
447
+ baudDetermined = true ;
448
+ }
449
+ }
412
450
}
413
451
414
452
void TheThingsNetwork::reset (bool adr)
@@ -612,18 +650,16 @@ void TheThingsNetwork::showStatus()
612
650
debugPrintIndex (SHOW_RX_DELAY_2, buffer);
613
651
}
614
652
615
- void TheThingsNetwork::configureEU868 ()
653
+ // Puting this common function saves 238 bytes of flash
654
+ void TheThingsNetwork::configureChannelsFreq (uint32_t freq, uint8_t first, uint8_t last, uint8_t first_dr)
616
655
{
617
- sendMacSet (MAC_RX2, " 3 869525000" );
618
- sendChSet (MAC_CHANNEL_DRRANGE, 1 , " 0 6" );
619
-
620
- char buf[10 ];
621
- uint32_t freq = 867100000 ;
622
656
uint8_t ch;
623
- for (ch = 0 ; ch < 8 ; ch++)
657
+ char buf[10 ];
658
+
659
+ for (ch = first; ch <= last; ch++)
624
660
{
625
661
sendChSet (MAC_CHANNEL_DCYCLE, ch, " 799" );
626
- if (ch > 2 )
662
+ if (ch > first_dr )
627
663
{
628
664
sprintf (buf, " %lu" , freq);
629
665
sendChSet (MAC_CHANNEL_FREQ, ch, buf);
@@ -632,6 +668,13 @@ void TheThingsNetwork::configureEU868()
632
668
freq = freq + 200000 ;
633
669
}
634
670
}
671
+ }
672
+
673
+ void TheThingsNetwork::configureEU868 ()
674
+ {
675
+ sendMacSet (MAC_RX2, " 3 869525000" );
676
+ sendChSet (MAC_CHANNEL_DRRANGE, 1 , " 0 6" );
677
+ configureChannelsFreq (867100000 , 0 , 8 , 2 );
635
678
sendMacSet (MAC_PWRIDX, TTN_PWRIDX_EU868);
636
679
}
637
680
@@ -665,24 +708,9 @@ void TheThingsNetwork::configureAS920_923()
665
708
* CH0 = 923.2MHz
666
709
* CH1 = 923.4MHz
667
710
*/
668
- sendMacSet (MAC_ADR, " off" ); // TODO: remove when ADR is implemented for this plan
669
711
sendMacSet (MAC_RX2, " 2 923200000" );
670
712
671
- char buf[10 ];
672
- uint32_t freq = 922000000 ;
673
- uint8_t ch;
674
- for (ch = 0 ; ch < 8 ; ch++)
675
- {
676
- sendChSet (MAC_CHANNEL_DCYCLE, ch, " 799" );
677
- if (ch > 1 )
678
- {
679
- sprintf (buf, " %lu" , freq);
680
- sendChSet (MAC_CHANNEL_FREQ, ch, buf);
681
- sendChSet (MAC_CHANNEL_DRRANGE, ch, " 0 5" );
682
- sendChSet (MAC_CHANNEL_STATUS, ch, " on" );
683
- freq = freq + 200000 ;
684
- }
685
- }
713
+ configureChannelsFreq (922000000 , 0 , 8 , 1 );
686
714
// TODO: SF7BW250/DR6 channel, not properly supported by RN2903AS yet
687
715
// sendChSet(MAC_CHANNEL_DCYCLE, 8, "799");
688
716
// sendChSet(MAC_CHANNEL_FREQ, 8, "922100000");
@@ -701,21 +729,7 @@ void TheThingsNetwork::configureAS923_925()
701
729
sendMacSet (MAC_ADR, " off" ); // TODO: remove when ADR is implemented for this plan
702
730
sendMacSet (MAC_RX2, " 2 923200000" );
703
731
704
- char buf[10 ];
705
- uint32_t freq = 923600000 ;
706
- uint8_t ch;
707
- for (ch = 0 ; ch < 8 ; ch++)
708
- {
709
- sendChSet (MAC_CHANNEL_DCYCLE, ch, " 799" );
710
- if (ch > 1 )
711
- {
712
- sprintf (buf, " %lu" , freq);
713
- sendChSet (MAC_CHANNEL_FREQ, ch, buf);
714
- sendChSet (MAC_CHANNEL_DRRANGE, ch, " 0 5" );
715
- sendChSet (MAC_CHANNEL_STATUS, ch, " on" );
716
- freq = freq + 200000 ;
717
- }
718
- }
732
+ configureChannelsFreq (923600000 , 0 , 8 , 1 );
719
733
// TODO: SF7BW250/DR6 channel, not properly supported by RN2903AS yet
720
734
// sendChSet(MAC_CHANNEL_DCYCLE, 8, "799");
721
735
// sendChSet(MAC_CHANNEL_FREQ, 8, "924500000");
@@ -734,18 +748,7 @@ void TheThingsNetwork::configureKR920_923()
734
748
sendChSet (MAC_CHANNEL_STATUS, 0 , " off" );
735
749
sendChSet (MAC_CHANNEL_STATUS, 1 , " off" );
736
750
737
- char buf[10 ];
738
- uint32_t freq = 922100000 ;
739
- uint8_t ch;
740
- for (ch = 2 ; ch < 9 ; ch++)
741
- {
742
- sendChSet (MAC_CHANNEL_DCYCLE, ch, " 799" );
743
- sprintf (buf, " %lu" , freq);
744
- sendChSet (MAC_CHANNEL_FREQ, ch, buf);
745
- sendChSet (MAC_CHANNEL_DRRANGE, ch, " 0 5" );
746
- sendChSet (MAC_CHANNEL_STATUS, ch, " on" );
747
- freq = freq + 200000 ;
748
- }
751
+ configureChannelsFreq (922100000 , 2 , 9 , 0 );
749
752
sendMacSet (MAC_PWRIDX, TTN_PWRIDX_KR920_923);
750
753
}
751
754
@@ -865,24 +868,13 @@ bool TheThingsNetwork::sendChSet(uint8_t index, uint8_t channel, const char *val
865
868
{
866
869
clearReadBuffer ();
867
870
char ch[5 ];
868
- if (channel > 9 )
869
- {
870
- ch[0 ] = ((channel - (channel % 10 )) / 10 ) + 48 ;
871
- ch[1 ] = (channel % 10 ) + 48 ;
872
- ch[2 ] = ' \0 ' ;
873
- }
874
- else
875
- {
876
- ch[0 ] = channel + 48 ;
877
- ch[1 ] = ' \0 ' ;
878
- }
871
+ sprintf (ch, " %d " , channel);
879
872
debugPrint (F (SENDING));
880
873
sendCommand (MAC_TABLE, MAC_PREFIX, true );
881
874
sendCommand (MAC_TABLE, MAC_SET, true );
882
875
sendCommand (MAC_GET_SET_TABLE, MAC_CH, true );
883
876
sendCommand (MAC_CH_TABLE, index, true );
884
877
modemStream->write (ch);
885
- modemStream->write (" " );
886
878
modemStream->write (value);
887
879
modemStream->write (SEND_MSG);
888
880
debugPrint (channel);
@@ -910,44 +902,16 @@ bool TheThingsNetwork::sendPayload(uint8_t mode, uint8_t port, uint8_t *payload,
910
902
sendCommand (MAC_TABLE, MAC_PREFIX, true );
911
903
sendCommand (MAC_TABLE, MAC_TX, true );
912
904
sendCommand (MAC_TX_TABLE, mode, true );
913
- char sport[4 ];
914
- if (port > 99 )
915
- {
916
- sport[0 ] = ((port - (port % 100 )) / 100 ) + 48 ;
917
- sport[1 ] = (((port % 100 ) - (port % 10 )) / 10 ) + 48 ;
918
- sport[2 ] = (port % 10 ) + 48 ;
919
- sport[3 ] = ' \0 ' ;
920
- }
921
- else if (port > 9 )
922
- {
923
- sport[0 ] = ((port - (port % 10 )) / 10 ) + 48 ;
924
- sport[1 ] = (port % 10 ) + 48 ;
925
- sport[2 ] = ' \0 ' ;
926
- }
927
- else
928
- {
929
- sport[0 ] = port + 48 ;
930
- sport[1 ] = ' \0 ' ;
931
- }
932
- modemStream->write (sport);
933
- modemStream->print (" " );
934
- debugPrint (sport);
935
- debugPrint (F (" " ));
905
+ char buf[5 ];
906
+ sprintf (buf, " %d " , port);
907
+ modemStream->write (buf);
908
+ debugPrint (buf);
936
909
uint8_t i = 0 ;
937
910
for (i = 0 ; i < length; i++)
938
911
{
939
- if (payload[i] < 16 )
940
- {
941
- modemStream->print (" 0" );
942
- modemStream->print (payload[i], HEX);
943
- debugPrint (F (" 0" ));
944
- debugPrint (payload[i], HEX);
945
- }
946
- else
947
- {
948
- modemStream->print (payload[i], HEX);
949
- debugPrint (payload[i], HEX);
950
- }
912
+ sprintf (buf, " %02X" , payload[i] );
913
+ modemStream->print (buf);
914
+ debugPrint (buf);
951
915
}
952
916
modemStream->write (SEND_MSG);
953
917
debugPrintLn ();
@@ -969,11 +933,9 @@ void TheThingsNetwork::sleep(uint32_t mseconds)
969
933
modemStream->write (buffer);
970
934
modemStream->write (SEND_MSG);
971
935
debugPrintLn (buffer);
972
- }
973
936
974
- void TheThingsNetwork::wake ()
975
- {
976
- autoBaud ();
937
+ // to be determined back on wake up
938
+ baudDetermined = false ;
977
939
}
978
940
979
941
void TheThingsNetwork::linkCheck (uint16_t seconds)
0 commit comments