Skip to content

Commit d0bbe13

Browse files
committed
Merge pull request #30 from stelgenhof/fix_throws
Added try-catch blocks to catch thrown exceptions gracefully
2 parents 67f6826 + f45b5d9 commit d0bbe13

File tree

2 files changed

+36
-22
lines changed

2 files changed

+36
-22
lines changed

MyGateway.cpp

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
Created by Henrik Ekblad <[email protected]>
66
12/10/14 - Ported to Raspberry Pi by OUJABER Mohamed <[email protected]>
7-
7+
88
This program is free software; you can redistribute it and/or
99
modify it under the terms of the GNU General Public License
1010
version 2 as published by the Free Software Foundation.
@@ -89,7 +89,12 @@ void MyGateway::begin(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_e da
8989
#endif
9090

9191
// Start up the radio library
92-
setupRadio(paLevel, channel, dataRate);
92+
try {
93+
setupRadio(paLevel, channel, dataRate);
94+
} catch (const char* msg) {
95+
printf("Unable to start up the radio library. (Error: %s)\n", msg);
96+
exit(EXIT_FAILURE);
97+
}
9398
RF24::openReadingPipe(WRITE_PIPE, BASE_RADIO_ID);
9499
RF24::openReadingPipe(CURRENT_NODE_PIPE, BASE_RADIO_ID);
95100
RF24::startListening();
@@ -237,20 +242,25 @@ void MyGateway::setInclusionMode(boolean newMode) {
237242
}
238243

239244
void MyGateway::processRadioMessage() {
240-
if (process()) {
241-
// A new message was received from one of the sensors
242-
MyMessage message = getLastMessage();
243-
if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
244-
rxBlink(3);
245-
} else {
246-
rxBlink(1);
247-
}
248-
// Pass along the message from sensors to serial line
249-
serial(message);
250-
}
245+
try {
246+
if (process()) {
247+
// A new message was received from one of the sensors
248+
MyMessage message = getLastMessage();
249+
if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
250+
rxBlink(3);
251+
} else {
252+
rxBlink(1);
253+
}
254+
// Pass along the message from sensors to serial line
255+
serial(message);
256+
}
257+
} catch (const char* msg) {
258+
printf("Unable to process radio messages. (Error: %s)\n", msg);
259+
exit(EXIT_FAILURE);
260+
}
251261

252-
checkButtonTriggeredInclusion();
253-
checkInclusionFinished();
262+
checkButtonTriggeredInclusion();
263+
checkInclusionFinished();
254264
}
255265

256266
void MyGateway::serial(const char *fmt, ... ) {
@@ -319,4 +329,3 @@ void MyGateway::errBlink(uint8_t cnt) {
319329
if(countErr == 255) { countErr = cnt; }
320330
#endif
321331
}
322-

MySensor.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
Created by Henrik Ekblad <[email protected]>
66
12/10/14 - Ported to Raspberry Pi by OUJABER Mohamed <[email protected]>
7-
7+
88
This program is free software; you can redistribute it and/or
99
modify it under the terms of the GNU General Public License
1010
version 2 as published by the Free Software Foundation.
@@ -101,7 +101,7 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
101101
debug(PSTR("%s started, id %d\n"), repeaterMode?"repeater":"sensor", nc.nodeId);
102102

103103
// If we got an id, set this node to use it
104-
if (nc.nodeId != AUTO) {
104+
if (nc.nodeId != AUTO) {
105105
setupNode();
106106
// Wait configuration reply.
107107
wait(2000);
@@ -130,7 +130,7 @@ void MySensor::setupRadio(rf24_pa_dbm_e paLevel, uint8_t channel, rf24_datarate_
130130

131131
// All nodes listen to broadcast pipe (for FIND_PARENT_RESPONSE messages)
132132
RF24::openReadingPipe(BROADCAST_PIPE, TO_ADDR(BROADCAST_ADDRESS));
133-
133+
134134
RF24::printDetails();
135135
}
136136

@@ -538,7 +538,12 @@ void MySensor::wait(unsigned long ms) {
538538
// reset watchdog
539539
wdt_reset();
540540
#endif
541-
process();
541+
try {
542+
process();
543+
} catch (const char* msg) {
544+
printf("Unable to process radio messages. (Error: %s)\n", msg);
545+
exit(EXIT_FAILURE);
546+
}
542547
}
543548
}
544549

@@ -702,7 +707,7 @@ char* MySensor::ltoa(long value, char* result, int base) {
702707
char * MySensor::dtostrf(float f, int width, int decimals, char *result)
703708
{
704709
char widths[3];
705-
char decimalss[3];
710+
char decimalss[3];
706711
char format[100];
707712
itoa(width,widths,10);
708713
itoa(decimals,decimalss,10);
@@ -711,7 +716,7 @@ char * MySensor::dtostrf(float f, int width, int decimals, char *result)
711716
strcat(format,".");
712717
strcat(format,decimalss);
713718
strcat(format,"f");
714-
719+
715720
sprintf(result,format,f);
716721
return result;
717722
}

0 commit comments

Comments
 (0)