EthernetGateway: Use internal LEDs feature for blinking
Signed-off-by: Tomas Hozza <thozza@gmail.com>
This commit is contained in:
@@ -46,6 +46,7 @@
|
||||
* E.g. If you want to use the defualt values in this sketch enter: 192.168.178.66:5003
|
||||
*
|
||||
* LED purposes:
|
||||
* - To use the feature, uncomment WITH_LEDS_BLINKING in MyConfig.h
|
||||
* - RX (green) - blink fast on radio message recieved. In inclusion mode will blink fast only on presentation recieved
|
||||
* - TX (yellow) - blink fast on radio message transmitted. In inclusion mode will blink slowly
|
||||
* - ERR (red) - fast blink on error during transmission error or recieve crc error
|
||||
@@ -68,7 +69,6 @@
|
||||
#include <MyParserSerial.h>
|
||||
#include <MySensor.h>
|
||||
#include <stdarg.h>
|
||||
#include <MsTimer2.h>
|
||||
#include <PinChangeInt.h>
|
||||
#include "GatewayUtil.h"
|
||||
|
||||
@@ -104,7 +104,12 @@ MyTransportNRF24 transport(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);
|
||||
MyHwATMega328 hw;
|
||||
|
||||
// Construct MySensors library (signer needed if MY_SIGNING_FEATURE is turned on in MyConfig.h)
|
||||
// To use LEDs blinking, uncomment WITH_LEDS_BLINKING in MyConfig.h
|
||||
#ifdef WITH_LEDS_BLINKING
|
||||
MySensor gw(transport, hw /*, signer*/, RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN);
|
||||
#else
|
||||
MySensor gw(transport, hw /*, signer*/);
|
||||
#endif
|
||||
|
||||
|
||||
#define IP_PORT 5003 // The port you want to open
|
||||
@@ -136,11 +141,8 @@ void output(const char *fmt, ... ) {
|
||||
void setup()
|
||||
{
|
||||
Ethernet.begin(mac, myIp);
|
||||
setupGateway(RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN, INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
|
||||
|
||||
// Add led timer interrupt
|
||||
MsTimer2::set(300, ledTimersInterrupt);
|
||||
MsTimer2::start();
|
||||
setupGateway(INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
|
||||
|
||||
// Add interrupt for inclusion button to pin
|
||||
PCintPort::attachInterrupt(pinInclusion, startInclusionInterrupt, RISING);
|
||||
|
||||
@@ -7,9 +7,6 @@
|
||||
#ifdef ARDUINO
|
||||
|
||||
|
||||
uint8_t pinRx = 8; // Rx led pin
|
||||
uint8_t pinTx = 9; // Tx led pin
|
||||
uint8_t pinEr = 7; // Err led pin
|
||||
uint8_t inclusionTime = 1; // Number of minutes inclusion mode is enabled
|
||||
uint8_t pinInclusion = 3; // Input pin that should trigger inclusion mode
|
||||
|
||||
@@ -17,56 +14,28 @@ uint8_t pinInclusion = 3; // Input pin that should trigger inclusion mode
|
||||
#define MAX_SEND_LENGTH 120 // Max buffersize needed for messages destined for controller
|
||||
|
||||
volatile boolean buttonTriggeredInclusion;
|
||||
volatile uint8_t countRx;
|
||||
volatile uint8_t countTx;
|
||||
volatile uint8_t countErr;
|
||||
boolean inclusionMode; // Keeps track on inclusion mode
|
||||
void (*serial)(const char *fmt, ... );
|
||||
|
||||
MyParserSerial parser;
|
||||
|
||||
void setInclusionMode(boolean newMode);
|
||||
void txBlink(uint8_t cnt);
|
||||
void rxBlink(uint8_t cnt);
|
||||
void errBlink(uint8_t cnt);
|
||||
|
||||
char convBuf[MAX_PAYLOAD*2+1];
|
||||
char serialBuffer[MAX_SEND_LENGTH]; // Buffer for building string when sending data to vera
|
||||
unsigned long inclusionStartTime;
|
||||
|
||||
void setupGateway(uint8_t _rx, uint8_t _tx, uint8_t _er, uint8_t _inc, uint8_t _incTime, void (* _serial)(const char *, ... )) {
|
||||
void setupGateway(uint8_t _inc, uint8_t _incTime, void (* _serial)(const char *, ... )) {
|
||||
inclusionMode = 0;
|
||||
buttonTriggeredInclusion = false;
|
||||
serial = _serial;
|
||||
|
||||
pinRx = _rx;
|
||||
pinTx = _tx;
|
||||
pinEr = _er;
|
||||
pinInclusion = _inc;
|
||||
inclusionTime = _incTime;
|
||||
|
||||
countRx = 0;
|
||||
countTx = 0;
|
||||
countErr = 0;
|
||||
|
||||
|
||||
// Setup led pins
|
||||
pinMode(pinRx, OUTPUT);
|
||||
pinMode(pinTx, OUTPUT);
|
||||
pinMode(pinEr, OUTPUT);
|
||||
digitalWrite(pinRx, LOW);
|
||||
digitalWrite(pinTx, LOW);
|
||||
digitalWrite(pinEr, LOW);
|
||||
|
||||
// Setup digital in that triggers inclusion mode
|
||||
pinMode(pinInclusion, INPUT);
|
||||
digitalWrite(pinInclusion, HIGH);
|
||||
|
||||
// Set initial state of leds
|
||||
digitalWrite(pinRx, HIGH);
|
||||
digitalWrite(pinTx, HIGH);
|
||||
digitalWrite(pinEr, HIGH);
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -76,11 +45,11 @@ void startInclusionInterrupt() {
|
||||
}
|
||||
|
||||
void incomingMessage(const MyMessage &message) {
|
||||
if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
|
||||
rxBlink(3);
|
||||
} else {
|
||||
rxBlink(1);
|
||||
}
|
||||
// if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
|
||||
// gw.rxBlink(3);
|
||||
// } else {
|
||||
// gw.rxBlink(1);
|
||||
// }
|
||||
// Pass along the message from sensors to serial line
|
||||
serial(PSTR("%d;%d;%d;%d;%d;%s\n"),message.sender, message.sensor, mGetCommand(message), mGetAck(message), message.type, message.getString(convBuf));
|
||||
}
|
||||
@@ -106,7 +75,7 @@ void checkInclusionFinished() {
|
||||
}
|
||||
}
|
||||
|
||||
void parseAndSend(MySensor gw, char *commandBuffer) {
|
||||
void parseAndSend(MySensor &gw, char *commandBuffer) {
|
||||
boolean ok;
|
||||
MyMessage &msg = gw.getLastMessage();
|
||||
|
||||
@@ -123,10 +92,14 @@ void parseAndSend(MySensor gw, char *commandBuffer) {
|
||||
setInclusionMode(atoi(msg.data) == 1);
|
||||
}
|
||||
} else {
|
||||
txBlink(1);
|
||||
#ifdef WITH_LEDS_BLINKING
|
||||
gw.txBlink(1);
|
||||
#endif
|
||||
ok = gw.sendRoute(msg);
|
||||
if (!ok) {
|
||||
errBlink(1);
|
||||
#ifdef WITH_LEDS_BLINKING
|
||||
gw.errBlink(1);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,50 +118,6 @@ void setInclusionMode(boolean newMode) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void ledTimersInterrupt() {
|
||||
if(countRx && countRx != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinRx, LOW);
|
||||
} else if(!countRx) {
|
||||
// switching off
|
||||
digitalWrite(pinRx, HIGH);
|
||||
}
|
||||
if(countRx != 255) { countRx--; }
|
||||
|
||||
if(countTx && countTx != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinTx, LOW);
|
||||
} else if(!countTx) {
|
||||
// switching off
|
||||
digitalWrite(pinTx, HIGH);
|
||||
}
|
||||
if(countTx != 255) { countTx--; }
|
||||
else if(inclusionMode) { countTx = 8; }
|
||||
|
||||
if(countErr && countErr != 255) {
|
||||
// switch led on
|
||||
digitalWrite(pinEr, LOW);
|
||||
} else if(!countErr) {
|
||||
// switching off
|
||||
digitalWrite(pinEr, HIGH);
|
||||
}
|
||||
if(countErr != 255) { countErr--; }
|
||||
}
|
||||
|
||||
void rxBlink(uint8_t cnt) {
|
||||
if(countRx == 255) { countRx = cnt; }
|
||||
}
|
||||
void txBlink(uint8_t cnt) {
|
||||
if(countTx == 255 && !inclusionMode) { countTx = cnt; }
|
||||
}
|
||||
void errBlink(uint8_t cnt) {
|
||||
if(countErr == 255) { countErr = cnt; }
|
||||
}
|
||||
|
||||
|
||||
|
||||
#else
|
||||
#error This example is only for use on Arduino.
|
||||
#endif // ARDUINO
|
||||
|
||||
Reference in New Issue
Block a user