Skip to content

Commit 29b8680

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

File tree

2 files changed

+20
-89
lines changed

2 files changed

+20
-89
lines changed

libraries/MySensors/examples/EthernetGateway/EthernetGateway.ino

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
* E.g. If you want to use the defualt values in this sketch enter: 192.168.178.66:5003
4747
*
4848
* LED purposes:
49+
* - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h
4950
* - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
5051
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
5152
* - ERR (red) - fast blink on error during transmission error or recieve crc error
@@ -68,7 +69,6 @@
6869
#include <MyParserSerial.h>
6970
#include <MySensor.h>
7071
#include <stdarg.h>
71-
#include <MsTimer2.h>
7272
#include <PinChangeInt.h>
7373
#include "GatewayUtil.h"
7474

@@ -104,7 +104,12 @@ MyTransportNRF24 transport(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);
104104
MyHwATMega328 hw;
105105

106106
// Construct MySensors library (signer needed if MY_SIGNING_FEATURE is turned on in MyConfig.h)
107+
// To use LEDs blinking, uncomment WITH_LEDS_BLINKING in MyConfig.h
108+
#ifdef WITH_LEDS_BLINKING
109+
MySensor gw(transport, hw /*, signer*/, RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN);
110+
#else
107111
MySensor gw(transport, hw /*, signer*/);
112+
#endif
108113

109114

110115
#define IP_PORT 5003 // The port you want to open
@@ -136,11 +141,8 @@ void output(const char *fmt, ... ) {
136141
void setup()
137142
{
138143
Ethernet.begin(mac, myIp);
139-
setupGateway(RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN, INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
140144

141-
// Add led timer interrupt
142-
MsTimer2::set(300, ledTimersInterrupt);
143-
MsTimer2::start();
145+
setupGateway(INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
144146

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

libraries/MySensors/examples/EthernetGateway/GatewayUtil.h

Lines changed: 13 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,35 @@
77
#ifdef ARDUINO
88

99

10-
uint8_t pinRx = 8; // Rx led pin
11-
uint8_t pinTx = 9; // Tx led pin
12-
uint8_t pinEr = 7; // Err led pin
1310
uint8_t inclusionTime = 1; // Number of minutes inclusion mode is enabled
1411
uint8_t pinInclusion = 3; // Input pin that should trigger inclusion mode
1512

1613
#define MAX_RECEIVE_LENGTH 100 // Max buffersize needed for messages coming from controller
1714
#define MAX_SEND_LENGTH 120 // Max buffersize needed for messages destined for controller
1815

1916
volatile boolean buttonTriggeredInclusion;
20-
volatile uint8_t countRx;
21-
volatile uint8_t countTx;
22-
volatile uint8_t countErr;
2317
boolean inclusionMode; // Keeps track on inclusion mode
2418
void (*serial)(const char *fmt, ... );
2519

2620
MyParserSerial parser;
2721

2822
void setInclusionMode(boolean newMode);
29-
void txBlink(uint8_t cnt);
30-
void rxBlink(uint8_t cnt);
31-
void errBlink(uint8_t cnt);
3223

3324
char convBuf[MAX_PAYLOAD*2+1];
3425
char serialBuffer[MAX_SEND_LENGTH]; // Buffer for building string when sending data to vera
3526
unsigned long inclusionStartTime;
3627

37-
void setupGateway(uint8_t _rx, uint8_t _tx, uint8_t _er, uint8_t _inc, uint8_t _incTime, void (* _serial)(const char *, ... )) {
28+
void setupGateway(uint8_t _inc, uint8_t _incTime, void (* _serial)(const char *, ... )) {
3829
inclusionMode = 0;
3930
buttonTriggeredInclusion = false;
4031
serial = _serial;
4132

42-
pinRx = _rx;
43-
pinTx = _tx;
44-
pinEr = _er;
4533
pinInclusion = _inc;
4634
inclusionTime = _incTime;
4735

48-
countRx = 0;
49-
countTx = 0;
50-
countErr = 0;
51-
52-
53-
// Setup led pins
54-
pinMode(pinRx, OUTPUT);
55-
pinMode(pinTx, OUTPUT);
56-
pinMode(pinEr, OUTPUT);
57-
digitalWrite(pinRx, LOW);
58-
digitalWrite(pinTx, LOW);
59-
digitalWrite(pinEr, LOW);
60-
6136
// Setup digital in that triggers inclusion mode
6237
pinMode(pinInclusion, INPUT);
6338
digitalWrite(pinInclusion, HIGH);
64-
65-
// Set initial state of leds
66-
digitalWrite(pinRx, HIGH);
67-
digitalWrite(pinTx, HIGH);
68-
digitalWrite(pinEr, HIGH);
69-
7039
}
7140

7241

@@ -76,11 +45,11 @@ void startInclusionInterrupt() {
7645
}
7746

7847
void incomingMessage(const MyMessage &message) {
79-
if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
80-
rxBlink(3);
81-
} else {
82-
rxBlink(1);
83-
}
48+
// if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
49+
// gw.rxBlink(3);
50+
// } else {
51+
// gw.rxBlink(1);
52+
// }
8453
// Pass along the message from sensors to serial line
8554
serial(PSTR("%d;%d;%d;%d;%d;%s\n"),message.sender, message.sensor, mGetCommand(message), mGetAck(message), message.type, message.getString(convBuf));
8655
}
@@ -106,7 +75,7 @@ void checkInclusionFinished() {
10675
}
10776
}
10877

109-
void parseAndSend(MySensor gw, char *commandBuffer) {
78+
void parseAndSend(MySensor &gw, char *commandBuffer) {
11079
boolean ok;
11180
MyMessage &msg = gw.getLastMessage();
11281

@@ -123,10 +92,14 @@ void parseAndSend(MySensor gw, char *commandBuffer) {
12392
setInclusionMode(atoi(msg.data) == 1);
12493
}
12594
} else {
126-
txBlink(1);
95+
#ifdef WITH_LEDS_BLINKING
96+
gw.txBlink(1);
97+
#endif
12798
ok = gw.sendRoute(msg);
12899
if (!ok) {
129-
errBlink(1);
100+
#ifdef WITH_LEDS_BLINKING
101+
gw.errBlink(1);
102+
#endif
130103
}
131104
}
132105
}
@@ -145,50 +118,6 @@ void setInclusionMode(boolean newMode) {
145118
}
146119

147120

148-
149-
150-
void ledTimersInterrupt() {
151-
if(countRx && countRx != 255) {
152-
// switch led on
153-
digitalWrite(pinRx, LOW);
154-
} else if(!countRx) {
155-
// switching off
156-
digitalWrite(pinRx, HIGH);
157-
}
158-
if(countRx != 255) { countRx--; }
159-
160-
if(countTx && countTx != 255) {
161-
// switch led on
162-
digitalWrite(pinTx, LOW);
163-
} else if(!countTx) {
164-
// switching off
165-
digitalWrite(pinTx, HIGH);
166-
}
167-
if(countTx != 255) { countTx--; }
168-
else if(inclusionMode) { countTx = 8; }
169-
170-
if(countErr && countErr != 255) {
171-
// switch led on
172-
digitalWrite(pinEr, LOW);
173-
} else if(!countErr) {
174-
// switching off
175-
digitalWrite(pinEr, HIGH);
176-
}
177-
if(countErr != 255) { countErr--; }
178-
}
179-
180-
void rxBlink(uint8_t cnt) {
181-
if(countRx == 255) { countRx = cnt; }
182-
}
183-
void txBlink(uint8_t cnt) {
184-
if(countTx == 255 && !inclusionMode) { countTx = cnt; }
185-
}
186-
void errBlink(uint8_t cnt) {
187-
if(countErr == 255) { countErr = cnt; }
188-
}
189-
190-
191-
192121
#else
193122
#error This example is only for use on Arduino.
194123
#endif // ARDUINO

0 commit comments

Comments
 (0)