Skip to content

Commit ab09e24

Browse files
committed
Use shared buffer and introduced saveState()
1 parent dab86ba commit ab09e24

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

src/TheThingsNetwork.cpp

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,6 @@ TheThingsNetwork::TheThingsNetwork(Stream &modemStream, Stream &debugStream, ttn
291291

292292
void TheThingsNetwork::debugPrintIndex(uint8_t index, const char *value)
293293
{
294-
char buffer[100];
295294
strcpy_P(buffer, (char *)pgm_read_word(&(show_table[index])));
296295
debugPrint(buffer);
297296
if (value)
@@ -302,7 +301,6 @@ void TheThingsNetwork::debugPrintIndex(uint8_t index, const char *value)
302301

303302
void TheThingsNetwork::debugPrintMessage(uint8_t type, uint8_t index, const char *output)
304303
{
305-
char buffer[100];
306304
switch (type)
307305
{
308306
case ERR_MESSAGE:
@@ -368,7 +366,6 @@ size_t TheThingsNetwork::readResponse(uint8_t prefixTable, uint8_t indexTable, u
368366

369367
void TheThingsNetwork::reset(bool adr)
370368
{
371-
char buffer[100];
372369
size_t length = readResponse(SYS_TABLE, SYS_RESET, buffer, sizeof(buffer));
373370
debugPrintLn(buffer);
374371

@@ -388,6 +385,15 @@ void TheThingsNetwork::reset(bool adr)
388385
}
389386
}
390387

388+
void TheThingsNetwork::saveState()
389+
{
390+
debugPrint(SENDING);
391+
sendCommand(MAC_TABLE, MAC_PREFIX, true);
392+
sendCommand(MAC_TABLE, MAC_SAVE, false);
393+
modemStream->write(SEND_MSG);
394+
debugPrintLn();
395+
}
396+
391397
void TheThingsNetwork::onMessage(void (*cb)(const uint8_t *payload, size_t size, port_t port))
392398
{
393399
messageCallback = cb;
@@ -411,8 +417,6 @@ bool TheThingsNetwork::personalize()
411417
{
412418
configureChannels(sf, fsb);
413419
sendJoinSet(MAC_JOIN_MODE_ABP);
414-
415-
char buffer[100];
416420
readLine(buffer, sizeof(buffer));
417421
if (pgmstrcmp(buffer, CMP_ACCEPTED) != 0)
418422
{
@@ -433,26 +437,17 @@ bool TheThingsNetwork::provision(const char *appEui, const char *appKey)
433437
debugPrintMessage(ERR_MESSAGE, ERR_KEY_LENGTH);
434438
return false;
435439
}
436-
437-
char buffer[17];
438440
readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_HWEUI, buffer, sizeof(buffer));
439441
sendMacSet(MAC_SET_DEVEUI, buffer);
440-
441442
sendMacSet(MAC_SET_APPEUI, appEui);
442443
sendMacSet(MAC_SET_APPKEY, appKey);
443-
debugPrint(SENDING);
444-
sendCommand(MAC_TABLE, MAC_PREFIX, true);
445-
sendCommand(MAC_TABLE, MAC_SAVE, false);
446-
modemStream->write(SEND_MSG);
447-
debugPrintLn();
444+
saveState();
448445
return true;
449446
}
450447

451448
bool TheThingsNetwork::join(int8_t retries, uint32_t retryDelay)
452449
{
453450
configureChannels(sf, fsb);
454-
455-
char buffer[100];
456451
while (--retries)
457452
{
458453
if (!sendJoinSet(MAC_JOIN_MODE_OTAA))
@@ -540,7 +535,6 @@ int8_t TheThingsNetwork::poll(port_t port, bool confirm)
540535

541536
void TheThingsNetwork::showStatus()
542537
{
543-
char buffer[100];
544538
readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_HWEUI, buffer, sizeof(buffer));
545539
debugPrintIndex(SHOW_EUI, buffer);
546540
readResponse(SYS_TABLE, SYS_TABLE, SYS_GET_VDD, buffer, sizeof(buffer));
@@ -687,9 +681,8 @@ void TheThingsNetwork::configureChannels(uint8_t sf, uint8_t fsb)
687681
sendMacSet(MAC_SET_RETX, TTN_RETX);
688682
}
689683

690-
void TheThingsNetwork::sendCommand(uint8_t table, uint8_t index, bool with_space, bool print)
684+
void TheThingsNetwork::sendCommand(uint8_t table, uint8_t index, bool appendSpace, bool print)
691685
{
692-
char buffer[500];
693686
switch (table)
694687
{
695688
case MAC_TABLE:
@@ -717,7 +710,7 @@ void TheThingsNetwork::sendCommand(uint8_t table, uint8_t index, bool with_space
717710
return;
718711
}
719712
modemStream->write(buffer);
720-
if (with_space)
713+
if (appendSpace)
721714
{
722715
modemStream->write(" ");
723716
}
@@ -743,7 +736,6 @@ bool TheThingsNetwork::sendMacSet(uint8_t index, const char *setting)
743736

744737
bool TheThingsNetwork::waitForOk()
745738
{
746-
char buffer[100];
747739
readLine(buffer, sizeof(buffer));
748740
if (pgmstrcmp(buffer, CMP_OK) != 0)
749741
{
@@ -857,7 +849,6 @@ void TheThingsNetwork::sleep(unsigned long msec)
857849
sendCommand(SYS_TABLE, SYS_PREFIX, true);
858850
sendCommand(SYS_TABLE, SYS_SLEEP, true);
859851

860-
char buffer[11];
861852
sprintf(buffer, "%ld", msec);
862853
modemStream->write(buffer);
863854
modemStream->write(SEND_MSG);

src/TheThingsNetwork.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,12 @@ enum ttn_fp_t
3636
class TheThingsNetwork
3737
{
3838
private:
39-
port_t port;
4039
Stream *modemStream;
4140
Stream *debugStream;
42-
bool model_EU;
4341
ttn_fp_t fp;
4442
uint8_t sf;
4543
uint8_t fsb;
44+
char buffer[500];
4645
void (*messageCallback)(const uint8_t *payload, size_t size, port_t port);
4746

4847
void clearReadBuffer();
@@ -58,14 +57,14 @@ class TheThingsNetwork
5857
void configureUS915(uint8_t sf, uint8_t fsb);
5958
void configureChannels(uint8_t sf, uint8_t fsb);
6059
bool waitForOk();
61-
62-
void sendCommand(uint8_t table, uint8_t index, bool with_space, bool print = true);
60+
61+
void sendCommand(uint8_t table, uint8_t index, bool appendSpace, bool print = true);
6362
bool sendMacSet(uint8_t index, const char *setting);
6463
bool sendChSet(uint8_t index, uint8_t channel, const char *setting);
6564
bool sendJoinSet(uint8_t type);
6665
bool sendPayload(uint8_t mode, uint8_t port, uint8_t *payload, size_t len);
6766
void sendGetValue(uint8_t table, uint8_t prefix, uint8_t index);
68-
67+
6968
public:
7069
TheThingsNetwork(Stream &modemStream, Stream &debugStream, ttn_fp_t fp, uint8_t sf = TTN_DEFAULT_SF, uint8_t fsb = TTN_DEFAULT_FSB);
7170
void showStatus();
@@ -78,6 +77,7 @@ class TheThingsNetwork
7877
ttn_response_t sendBytes(const uint8_t *payload, size_t length, port_t port = 1, bool confirm = false);
7978
void sleep(unsigned long mseconds);
8079
int8_t poll(port_t port = 1, bool confirm = false);
80+
void saveState();
8181
};
8282

8383
#endif

0 commit comments

Comments
 (0)