@@ -52,13 +52,19 @@ static inline bool isValidDistance( const uint8_t distance ) {
52
52
MySensor::MySensor (MyTransport &_radio, MyHw &_hw
53
53
#ifdef MY_SIGNING_FEATURE
54
54
, MySigning &_signer
55
+ #endif
56
+ #ifdef WITH_LEDS_BLINKING
57
+ , uint8_t _rx, uint8_t _tx, uint8_t _er, unsigned long _blink_period
55
58
#endif
56
59
)
57
60
:
58
61
radio(_radio),
59
62
#ifdef MY_SIGNING_FEATURE
60
63
signer (_signer),
61
64
#endif
65
+ #ifdef WITH_LEDS_BLINKING
66
+ pinRx (_rx), pinTx(_tx), pinEr(_er), ledBlinkPeriod(_blink_period),
67
+ #endif
62
68
#ifdef MY_OTA_FIRMWARE_FEATURE
63
69
flash (MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID),
64
70
#endif
@@ -90,6 +96,64 @@ bool MySensor::isValidFirmware() {
90
96
91
97
#endif
92
98
99
+ #ifdef WITH_LEDS_BLINKING
100
+ void MySensor::handleLedsBlinking () {
101
+ static unsigned long next_time = hw_millis () + ledBlinkPeriod;
102
+
103
+ // Just return if it is not the time...
104
+ // http://playground.arduino.cc/Code/TimingRollover
105
+ if ((long )(hw_millis () - next_time) < 0 )
106
+ return ;
107
+ else
108
+ next_time = hw_millis () + ledBlinkPeriod;
109
+
110
+ // do the actual blinking
111
+ if (countRx && countRx != 255 ) {
112
+ // switch led on
113
+ digitalWrite (pinRx, LOW);
114
+ }
115
+ else if (!countRx) {
116
+ // switching off
117
+ digitalWrite (pinRx, HIGH);
118
+ }
119
+ if (countRx != 255 )
120
+ --countRx;
121
+
122
+ if (countTx && countTx != 255 ) {
123
+ // switch led on
124
+ digitalWrite (pinTx, LOW);
125
+ }
126
+ else if (!countTx) {
127
+ // switching off
128
+ digitalWrite (pinTx, HIGH);
129
+ }
130
+ if (countTx != 255 )
131
+ --countTx;
132
+
133
+ if (countErr && countErr != 255 ) {
134
+ // switch led on
135
+ digitalWrite (pinEr, LOW);
136
+ }
137
+ else if (!countErr) {
138
+ // switching off
139
+ digitalWrite (pinEr, HIGH);
140
+ }
141
+ if (countErr != 255 )
142
+ --countErr;
143
+ }
144
+
145
+ void MySensor::rxBlink (uint8_t cnt) {
146
+ if (countRx == 255 ) { countRx = cnt; }
147
+ }
148
+
149
+ void MySensor::txBlink (uint8_t cnt) {
150
+ if (countTx == 255 ) { countTx = cnt; }
151
+ }
152
+
153
+ void MySensor::errBlink (uint8_t cnt) {
154
+ if (countErr == 255 ) { countErr = cnt; }
155
+ }
156
+ #endif
93
157
94
158
void MySensor::begin (void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId) {
95
159
hw_init ();
@@ -111,6 +175,23 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
111
175
hw_readConfigBlock ((void *)doSign, (void *)EEPROM_SIGNING_REQUIREMENT_TABLE_ADDRESS, sizeof (doSign));
112
176
#endif
113
177
178
+ #ifdef WITH_LEDS_BLINKING
179
+ // Setup led pins
180
+ pinMode (pinRx, OUTPUT);
181
+ pinMode (pinTx, OUTPUT);
182
+ pinMode (pinEr, OUTPUT);
183
+
184
+ // Set initial state of leds
185
+ digitalWrite (pinRx, HIGH);
186
+ digitalWrite (pinTx, HIGH);
187
+ digitalWrite (pinEr, HIGH);
188
+
189
+ // initialize counters
190
+ countRx = 0 ;
191
+ countTx = 0 ;
192
+ countErr = 0 ;
193
+ #endif
194
+
114
195
if (isGateway) {
115
196
// Set configuration for gateway
116
197
nc.parentNodeId = GATEWAY_ADDRESS;
@@ -246,12 +327,18 @@ boolean MySensor::sendRoute(MyMessage &message) {
246
327
// If we still don't have any parent id, re-request and skip this message.
247
328
if (nc.parentNodeId == AUTO) {
248
329
findParentNode ();
330
+ #ifdef WITH_LEDS_BLINKING
331
+ errBlink (1 );
332
+ #endif
249
333
return false ;
250
334
}
251
335
252
336
// If we still don't have any node id, re-request and skip this message.
253
337
if (nc.nodeId == AUTO) {
254
338
requestNodeId ();
339
+ #ifdef WITH_LEDS_BLINKING
340
+ errBlink (1 );
341
+ #endif
255
342
return false ;
256
343
}
257
344
@@ -288,10 +375,16 @@ boolean MySensor::sendRoute(MyMessage &message) {
288
375
}
289
376
if (hw_millis () - enter > MY_VERIFICATION_TIMEOUT_MS) {
290
377
debug (PSTR (" nonce tmo\n " ));
378
+ #ifdef WITH_LEDS_BLINKING
379
+ errBlink (1 );
380
+ #endif
291
381
return false ;
292
382
}
293
383
if (!signOk) {
294
384
debug (PSTR (" sign fail\n " ));
385
+ #ifdef WITH_LEDS_BLINKING
386
+ errBlink (1 );
387
+ #endif
295
388
return false ;
296
389
}
297
390
// After this point, only the 'last' member of the message structure is allowed to be altered if the message has been signed,
@@ -350,6 +443,9 @@ boolean MySensor::sendRoute(MyMessage &message) {
350
443
if (!ok) {
351
444
// Failure when sending to parent node. The parent node might be down and we
352
445
// need to find another route to gateway.
446
+ #ifdef WITH_LEDS_BLINKING
447
+ errBlink (1 );
448
+ #endif
353
449
failedTransmissions++;
354
450
if (autoFindParent && failedTransmissions > SEARCH_FAILURES) {
355
451
findParentNode ();
@@ -364,6 +460,9 @@ boolean MySensor::sendWrite(uint8_t to, MyMessage &message) {
364
460
mSetVersion (message, PROTOCOL_VERSION);
365
461
uint8_t length = mGetSigned (message) ? MAX_MESSAGE_LENGTH : mGetLength (message);
366
462
message.last = nc.nodeId ;
463
+ #ifdef WITH_LEDS_BLINKING
464
+ txBlink (1 );
465
+ #endif
367
466
bool ok = radio.send (to, &message, min (MAX_MESSAGE_LENGTH, HEADER_SIZE + length));
368
467
369
468
debug (PSTR (" send: %d-%d-%d-%d s=%d,c=%d,t=%d,pt=%d,l=%d,sg=%d,st=%s:%s\n " ),
@@ -408,6 +507,11 @@ void MySensor::requestTime(void (* _timeCallback)(unsigned long)) {
408
507
409
508
boolean MySensor::process () {
410
509
hw_watchdogReset ();
510
+
511
+ #ifdef WITH_LEDS_BLINKING
512
+ handleLedsBlinking ();
513
+ #endif
514
+
411
515
uint8_t to = 0 ;
412
516
if (!radio.available (&to))
413
517
{
@@ -418,6 +522,9 @@ boolean MySensor::process() {
418
522
debug (PSTR (" fw upd fail\n " ));
419
523
// Give up. We have requested MY_OTA_RETRY times without any packet in return.
420
524
fwUpdateOngoing = false ;
525
+ #ifdef WITH_LEDS_BLINKING
526
+ errBlink (1 );
527
+ #endif
421
528
return false ;
422
529
}
423
530
fwRetry--;
@@ -439,6 +546,9 @@ boolean MySensor::process() {
439
546
#endif
440
547
441
548
uint8_t len = radio.receive ((uint8_t *)&msg);
549
+ #ifdef WITH_LEDS_BLINKING
550
+ rxBlink (1 );
551
+ #endif
442
552
443
553
#ifdef MY_SIGNING_FEATURE
444
554
// Before processing message, reject unsigned messages if signing is required and check signature (if it is signed and addressed to us)
@@ -451,10 +561,16 @@ boolean MySensor::process() {
451
561
if (!mGetSigned (msg)) {
452
562
// Got unsigned message that should have been signed
453
563
debug (PSTR (" no sign\n " ));
564
+ #ifdef WITH_LEDS_BLINKING
565
+ errBlink (1 );
566
+ #endif
454
567
return false ;
455
568
}
456
569
else if (!signer.verifyMsg (msg)) {
457
570
debug (PSTR (" verify fail\n " ));
571
+ #ifdef WITH_LEDS_BLINKING
572
+ errBlink (1 );
573
+ #endif
458
574
return false ; // This signed message has been tampered with!
459
575
}
460
576
}
@@ -468,6 +584,9 @@ boolean MySensor::process() {
468
584
469
585
if (!(mGetVersion (msg) == PROTOCOL_VERSION)) {
470
586
debug (PSTR (" ver mismatch\n " ));
587
+ #ifdef WITH_LEDS_BLINKING
588
+ errBlink (1 );
589
+ #endif
471
590
return false ;
472
591
}
473
592
0 commit comments