Skip to content

Commit 373e395

Browse files
committed
Pass SF to sendBytes()
1 parent 05559a3 commit 373e395

File tree

3 files changed

+46
-39
lines changed

3 files changed

+46
-39
lines changed

docs/TheThingsNetwork.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ TheThingsNetwork ttn(Stream& modemStream, Stream& debugStream, fp_ttn_t fp, uint
1515
- `Stream& modemStream`: Stream for the LoRa modem (for The Things Node/Uno use `Serial1` and data rate `57600`).
1616
- `Stream& debugStream`: Stream to write debug logs to (for The Things Node/Uno use `Serial` and data rate `9600`).
1717
- `fp_ttn_fp fp`: The frequency plan: `TTN_FP_EU868` or `TTN_FP_US915` depending on the region you deploy in.
18-
- `uint8_t sf = 7`: Optional custom spreading factor. Can be `7` to `12` for `TTN_FP_EU868` and `7` to `12` for `TTN_FP_US915`. Defaults to `7`.
19-
- `uint8_t fsb = 2`: Optional custom front-side bus. Can be `1` to `8`. Defaults to `2`.
18+
- `uint8_t sf = 7`: Optional custom spreading factor. Can be `7` to `12` for `TTN_FP_EU868` and `7` to `10` for `TTN_FP_US915`. Defaults to `7`.
19+
- `uint8_t fsb = 2`: Optional custom frequency subband. Can be `1` to `8`. Defaults to `2` (for US915).
2020
2121
## Method: `reset`
2222
@@ -122,13 +122,14 @@ See the [ABP](https://github.com/TheThingsNetwork/arduino-device-lib/blob/master
122122
Send a message to the application using raw bytes.
123123
124124
```c
125-
ttn_response_t sendBytes(const byte* payload, size_t length, port_t port = 1, bool confirm = false);
125+
ttn_response_t sendBytes(const byte* payload, size_t length, port_t port = 1, bool confirm = false, uint8_t sf = 0);
126126
```
127127

128128
- `const byte* payload `: Bytes to send.
129129
- `size_t length`: The number of bytes. Use `sizeof(payload)` to get it.
130130
- `port_t port = 1`: The port to address. Defaults to `1`.
131131
- `bool confirm = false`: Whether to ask for confirmation. Defaults to `false`. If confirmation fails, the method will return error code `TTN_ERROR_UNEXPECTED_RESPONSE`.
132+
- `uint8_t sf = 0`: Override the spreading factor (SF). If the default value `0` is passed, the SF is not changed from the constructor or previous value.
132133

133134
Returns a success or error code and logs the related error message:
134135

src/TheThingsNetwork.cpp

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,8 @@ bool TheThingsNetwork::personalize(const char *devAddr, const char *nwkSKey, con
457457

458458
bool TheThingsNetwork::personalize()
459459
{
460-
configureChannels(sf, fsb);
460+
configureChannels(fsb);
461+
setSF(sf);
461462
sendJoinSet(MAC_JOIN_MODE_ABP);
462463
readLine(buffer, sizeof(buffer));
463464
if (pgmstrcmp(buffer, CMP_ACCEPTED) != 0)
@@ -498,7 +499,8 @@ bool TheThingsNetwork::provision(const char *appEui, const char *appKey)
498499

499500
bool TheThingsNetwork::join(int8_t retries, uint32_t retryDelay)
500501
{
501-
configureChannels(sf, fsb);
502+
configureChannels(fsb);
503+
setSF(sf);
502504
while (retries == -1 || retries-- >= 0)
503505
{
504506
if (!sendJoinSet(MAC_JOIN_MODE_OTAA))
@@ -529,8 +531,13 @@ bool TheThingsNetwork::join(const char *appEui, const char *appKey, int8_t retri
529531
return provision(appEui, appKey) && join(retries, retryDelay);
530532
}
531533

532-
ttn_response_t TheThingsNetwork::sendBytes(const uint8_t *payload, size_t length, port_t port, bool confirm)
534+
ttn_response_t TheThingsNetwork::sendBytes(const uint8_t *payload, size_t length, port_t port, bool confirm, uint8_t sf)
533535
{
536+
if (sf != 0)
537+
{
538+
setSF(sf);
539+
}
540+
534541
uint8_t mode = confirm ? MAC_TX_TYPE_CNF : MAC_TX_TYPE_UCNF;
535542
if (!sendPayload(mode, port, (uint8_t *)payload, length))
536543
{
@@ -599,7 +606,7 @@ void TheThingsNetwork::showStatus()
599606
debugPrintIndex(SHOW_RX_DELAY_2, buffer);
600607
}
601608

602-
void TheThingsNetwork::configureEU868(uint8_t sf)
609+
void TheThingsNetwork::configureEU868()
603610
{
604611
sendMacSet(MAC_RX2, "3 869525000");
605612
sendChSet(MAC_CHANNEL_DRRANGE, 1, "0 6");
@@ -620,16 +627,9 @@ void TheThingsNetwork::configureEU868(uint8_t sf)
620627
}
621628
}
622629
sendMacSet(MAC_PWRIDX, TTN_PWRIDX_EU868);
623-
if (sf >= 7 && sf <= 12)
624-
{
625-
char dr[2];
626-
dr[0] = '0' + (12 - sf);
627-
dr[1] = '\0';
628-
sendMacSet(MAC_DR, dr);
629-
}
630630
}
631631

632-
void TheThingsNetwork::configureUS915(uint8_t sf, uint8_t fsb)
632+
void TheThingsNetwork::configureUS915(uint8_t fsb)
633633
{
634634
uint8_t ch;
635635
uint8_t chLow = fsb > 0 ? (fsb - 1) * 8 : 0;
@@ -651,16 +651,9 @@ void TheThingsNetwork::configureUS915(uint8_t sf, uint8_t fsb)
651651
}
652652
}
653653
sendMacSet(MAC_PWRIDX, TTN_PWRIDX_US915);
654-
if (sf >= 7 && sf <= 10)
655-
{
656-
char dr[2];
657-
dr[0] = '0' + (10 - sf);
658-
dr[1] = '\0';
659-
sendMacSet(MAC_DR, dr);
660-
}
661654
}
662655

663-
void TheThingsNetwork::configureAS920_923(uint8_t sf)
656+
void TheThingsNetwork::configureAS920_923()
664657
{
665658
sendMacSet(MAC_ADR, "off"); // TODO: remove when ADR is implemented for this plan
666659
sendMacSet(MAC_RX2, "2 923200000");
@@ -687,27 +680,20 @@ void TheThingsNetwork::configureAS920_923(uint8_t sf)
687680
//sendChSet(MAC_CHANNEL_STATUS, 8, "on");
688681
// TODO: Add FSK channel
689682
sendMacSet(MAC_PWRIDX, TTN_PWRIDX_AS920_923);
690-
if (sf >= 7 && sf <= 12)
691-
{
692-
char dr[2];
693-
dr[0] = '0' + (12 - sf);
694-
dr[1] = '\0';
695-
sendMacSet(MAC_DR, dr);
696-
}
697683
}
698684

699-
void TheThingsNetwork::configureChannels(uint8_t sf, uint8_t fsb)
685+
void TheThingsNetwork::configureChannels(uint8_t fsb)
700686
{
701687
switch (fp)
702688
{
703689
case TTN_FP_EU868:
704-
configureEU868(sf);
690+
configureEU868();
705691
break;
706692
case TTN_FP_US915:
707-
configureUS915(sf, fsb);
693+
configureUS915(fsb);
708694
break;
709695
case TTN_FP_AS920_923:
710-
configureAS920_923(sf);
696+
configureAS920_923();
711697
break;
712698
default:
713699
debugPrintMessage(ERR_MESSAGE, ERR_INVALID_FP);
@@ -716,6 +702,25 @@ void TheThingsNetwork::configureChannels(uint8_t sf, uint8_t fsb)
716702
sendMacSet(MAC_RETX, TTN_RETX);
717703
}
718704

705+
bool TheThingsNetwork::setSF(uint8_t sf)
706+
{
707+
uint8_t dr;
708+
switch (fp)
709+
{
710+
case TTN_FP_EU868:
711+
case TTN_FP_AS920_923:
712+
dr = 12 - sf;
713+
break;
714+
case TTN_FP_US915:
715+
dr = 10 - sf;
716+
break;
717+
}
718+
char s[2];
719+
s[0] = '0' + dr;
720+
s[1] = '\0';
721+
return sendMacSet(MAC_DR, s);
722+
}
723+
719724
void TheThingsNetwork::sendCommand(uint8_t table, uint8_t index, bool appendSpace, bool print)
720725
{
721726
char command[100];

src/TheThingsNetwork.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ class TheThingsNetwork
5757
void debugPrintMessage(uint8_t type, uint8_t index, const char *value = NULL);
5858

5959
void autoBaud();
60-
void configureEU868(uint8_t sf);
61-
void configureUS915(uint8_t sf, uint8_t fsb);
62-
void configureAS920_923(uint8_t sf);
63-
void configureChannels(uint8_t sf, uint8_t fsb);
60+
void configureEU868();
61+
void configureUS915(uint8_t fsb);
62+
void configureAS920_923();
63+
void configureChannels(uint8_t fsb);
64+
bool setSF(uint8_t sf);
6465
bool waitForOk();
6566

6667
void sendCommand(uint8_t table, uint8_t index, bool appendSpace, bool print = true);
@@ -82,7 +83,7 @@ class TheThingsNetwork
8283
bool join(int8_t retries = -1, uint32_t retryDelay = 10000);
8384
bool personalize(const char *devAddr, const char *nwkSKey, const char *appSKey);
8485
bool personalize();
85-
ttn_response_t sendBytes(const uint8_t *payload, size_t length, port_t port = 1, bool confirm = false);
86+
ttn_response_t sendBytes(const uint8_t *payload, size_t length, port_t port = 1, bool confirm = false, uint8_t sf = 0);
8687
ttn_response_t poll(port_t port = 1, bool confirm = false);
8788
void sleep(uint32_t mseconds);
8889
void saveState();

0 commit comments

Comments
 (0)