Skip to content

Commit 0d6da23

Browse files
committed
SerialGateway: Use internal LEDs feature for blinking
Signed-off-by: Tomas Hozza <[email protected]>
1 parent 24464a6 commit 0d6da23

File tree

2 files changed

+21
-91
lines changed

2 files changed

+21
-91
lines changed

libraries/MySensors/examples/SerialGateway/GatewayUtil.h

Lines changed: 13 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -27,66 +27,35 @@
2727
#ifdef ARDUINO
2828

2929

30-
uint8_t pinRx = 8; // Rx led pin
31-
uint8_t pinTx = 9; // Tx led pin
32-
uint8_t pinEr = 7; // Err led pin
3330
uint8_t inclusionTime = 1; // Number of minutes inclusion mode is enabled
3431
uint8_t pinInclusion = 3; // Input pin that should trigger inclusion mode
3532

3633
#define MAX_RECEIVE_LENGTH 100 // Max buffersize needed for messages coming from controller
3734
#define MAX_SEND_LENGTH 120 // Max buffersize needed for messages destined for controller
3835

3936
volatile boolean buttonTriggeredInclusion;
40-
volatile uint8_t countRx;
41-
volatile uint8_t countTx;
42-
volatile uint8_t countErr;
4337
boolean inclusionMode; // Keeps track on inclusion mode
4438
void (*serial)(const char *fmt, ... );
4539

4640
MyParserSerial parser;
4741

4842
void setInclusionMode(boolean newMode);
49-
void txBlink(uint8_t cnt);
50-
void rxBlink(uint8_t cnt);
51-
void errBlink(uint8_t cnt);
5243

5344
char convBuf[MAX_PAYLOAD*2+1];
5445
char serialBuffer[MAX_SEND_LENGTH]; // Buffer for building string when sending data to vera
5546
unsigned long inclusionStartTime;
5647

57-
void setupGateway(uint8_t _rx, uint8_t _tx, uint8_t _er, uint8_t _inc, uint8_t _incTime, void (* _serial)(const char *, ... )) {
48+
void setupGateway(uint8_t _inc, uint8_t _incTime, void (* _serial)(const char *, ... )) {
5849
inclusionMode = 0;
5950
buttonTriggeredInclusion = false;
6051
serial = _serial;
6152

62-
pinRx = _rx;
63-
pinTx = _tx;
64-
pinEr = _er;
6553
pinInclusion = _inc;
6654
inclusionTime = _incTime;
6755

68-
countRx = 0;
69-
countTx = 0;
70-
countErr = 0;
71-
72-
73-
// Setup led pins
74-
pinMode(pinRx, OUTPUT);
75-
pinMode(pinTx, OUTPUT);
76-
pinMode(pinEr, OUTPUT);
77-
digitalWrite(pinRx, LOW);
78-
digitalWrite(pinTx, LOW);
79-
digitalWrite(pinEr, LOW);
80-
8156
// Setup digital in that triggers inclusion mode
8257
pinMode(pinInclusion, INPUT);
8358
digitalWrite(pinInclusion, HIGH);
84-
85-
// Set initial state of leds
86-
digitalWrite(pinRx, HIGH);
87-
digitalWrite(pinTx, HIGH);
88-
digitalWrite(pinEr, HIGH);
89-
9059
}
9160

9261

@@ -96,11 +65,11 @@ void startInclusionInterrupt() {
9665
}
9766

9867
void incomingMessage(const MyMessage &message) {
99-
if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
100-
rxBlink(3);
101-
} else {
102-
rxBlink(1);
103-
}
68+
// if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
69+
// gw.rxBlink(3);
70+
// } else {
71+
// gw.rxBlink(1);
72+
// }
10473
// Pass along the message from sensors to serial line
10574
serial(PSTR("%d;%d;%d;%d;%d;%s\n"),message.sender, message.sensor, mGetCommand(message), mGetAck(message), message.type, message.getString(convBuf));
10675
}
@@ -126,7 +95,7 @@ void checkInclusionFinished() {
12695
}
12796
}
12897

129-
void parseAndSend(MySensor gw, char *commandBuffer) {
98+
void parseAndSend(MySensor &gw, char *commandBuffer) {
13099
boolean ok;
131100
MyMessage &msg = gw.getLastMessage();
132101

@@ -143,10 +112,14 @@ void parseAndSend(MySensor gw, char *commandBuffer) {
143112
setInclusionMode(atoi(msg.data) == 1);
144113
}
145114
} else {
146-
txBlink(1);
115+
#ifdef WITH_LEDS_BLINKING
116+
gw.txBlink(1);
117+
#endif
147118
ok = gw.sendRoute(msg);
148119
if (!ok) {
149-
errBlink(1);
120+
#ifdef WITH_LEDS_BLINKING
121+
gw.errBlink(1);
122+
#endif
150123
}
151124
}
152125
}
@@ -165,50 +138,6 @@ void setInclusionMode(boolean newMode) {
165138
}
166139

167140

168-
169-
170-
void ledTimersInterrupt() {
171-
if(countRx && countRx != 255) {
172-
// switch led on
173-
digitalWrite(pinRx, LOW);
174-
} else if(!countRx) {
175-
// switching off
176-
digitalWrite(pinRx, HIGH);
177-
}
178-
if(countRx != 255) { countRx--; }
179-
180-
if(countTx && countTx != 255) {
181-
// switch led on
182-
digitalWrite(pinTx, LOW);
183-
} else if(!countTx) {
184-
// switching off
185-
digitalWrite(pinTx, HIGH);
186-
}
187-
if(countTx != 255) { countTx--; }
188-
else if(inclusionMode) { countTx = 8; }
189-
190-
if(countErr && countErr != 255) {
191-
// switch led on
192-
digitalWrite(pinEr, LOW);
193-
} else if(!countErr) {
194-
// switching off
195-
digitalWrite(pinEr, HIGH);
196-
}
197-
if(countErr != 255) { countErr--; }
198-
}
199-
200-
void rxBlink(uint8_t cnt) {
201-
if(countRx == 255) { countRx = cnt; }
202-
}
203-
void txBlink(uint8_t cnt) {
204-
if(countTx == 255 && !inclusionMode) { countTx = cnt; }
205-
}
206-
void errBlink(uint8_t cnt) {
207-
if(countErr == 255) { countErr = cnt; }
208-
}
209-
210-
211-
212141
#else
213142
#error This example is only for use on Arduino.
214143
#endif // ARDUINO

libraries/MySensors/examples/SerialGateway/SerialGateway.ino

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
* - RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series
3030
*
3131
* LEDs (OPTIONAL):
32+
* - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h
3233
* - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
3334
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
3435
* - ERR (red) - fast blink on error during transmission error or recieve crc error
@@ -48,7 +49,6 @@
4849
#include <MyParserSerial.h>
4950
#include <MySensor.h>
5051
#include <stdarg.h>
51-
#include <MsTimer2.h>
5252
#include <PinChangeInt.h>
5353
#include "GatewayUtil.h"
5454

@@ -59,7 +59,7 @@
5959
#define RADIO_TX_LED_PIN 5 // the PCB, on board LED
6060

6161
// NRFRF24L01 radio driver (set low transmit power by default)
62-
MyTransportNRF24 transport(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);
62+
MyTransportNRF24 transport(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);
6363
//MyTransportRFM69 transport;
6464

6565
// Message signing driver (signer needed if MY_SIGNING_FEATURE is turned on in MyConfig.h)
@@ -71,7 +71,12 @@ MyTransportNRF24 transport(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);
7171
MyHwATMega328 hw;
7272

7373
// Construct MySensors library (signer needed if MY_SIGNING_FEATURE is turned on in MyConfig.h)
74+
// To use LEDs blinking, uncomment WITH_LEDS_BLINKING in MyConfig.h
75+
#ifdef WITH_LEDS_BLINKING
76+
MySensor gw(transport, hw /*, signer*/, RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN);
77+
#else
7478
MySensor gw(transport, hw /*, signer*/);
79+
#endif
7580

7681
char inputString[MAX_RECEIVE_LENGTH] = ""; // A string to hold incoming commands from serial/ethernet interface
7782
int inputPos = 0;
@@ -92,11 +97,7 @@ void setup()
9297
{
9398
gw.begin(incomingMessage, 0, true, 0);
9499

95-
setupGateway(RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN, INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
96-
97-
// Add led timer interrupt
98-
MsTimer2::set(300, ledTimersInterrupt);
99-
MsTimer2::start();
100+
setupGateway(INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
100101

101102
// Add interrupt for inclusion button to pin
102103
PCintPort::attachInterrupt(pinInclusion, startInclusionInterrupt, RISING);

0 commit comments

Comments
 (0)