SerialGateway: Use internal LEDs feature for blinking

Signed-off-by: Tomas Hozza <thozza@gmail.com>
This commit is contained in:
Tomas Hozza
2015-05-06 21:45:00 +02:00
parent 24464a6255
commit 0d6da230c5
2 changed files with 21 additions and 91 deletions
@@ -27,9 +27,6 @@
#ifdef ARDUINO #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 inclusionTime = 1; // Number of minutes inclusion mode is enabled
uint8_t pinInclusion = 3; // Input pin that should trigger inclusion mode uint8_t pinInclusion = 3; // Input pin that should trigger inclusion mode
@@ -37,56 +34,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 #define MAX_SEND_LENGTH 120 // Max buffersize needed for messages destined for controller
volatile boolean buttonTriggeredInclusion; volatile boolean buttonTriggeredInclusion;
volatile uint8_t countRx;
volatile uint8_t countTx;
volatile uint8_t countErr;
boolean inclusionMode; // Keeps track on inclusion mode boolean inclusionMode; // Keeps track on inclusion mode
void (*serial)(const char *fmt, ... ); void (*serial)(const char *fmt, ... );
MyParserSerial parser; MyParserSerial parser;
void setInclusionMode(boolean newMode); 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 convBuf[MAX_PAYLOAD*2+1];
char serialBuffer[MAX_SEND_LENGTH]; // Buffer for building string when sending data to vera char serialBuffer[MAX_SEND_LENGTH]; // Buffer for building string when sending data to vera
unsigned long inclusionStartTime; 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; inclusionMode = 0;
buttonTriggeredInclusion = false; buttonTriggeredInclusion = false;
serial = _serial; serial = _serial;
pinRx = _rx;
pinTx = _tx;
pinEr = _er;
pinInclusion = _inc; pinInclusion = _inc;
inclusionTime = _incTime; 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 // Setup digital in that triggers inclusion mode
pinMode(pinInclusion, INPUT); pinMode(pinInclusion, INPUT);
digitalWrite(pinInclusion, HIGH); digitalWrite(pinInclusion, HIGH);
// Set initial state of leds
digitalWrite(pinRx, HIGH);
digitalWrite(pinTx, HIGH);
digitalWrite(pinEr, HIGH);
} }
@@ -96,11 +65,11 @@ void startInclusionInterrupt() {
} }
void incomingMessage(const MyMessage &message) { void incomingMessage(const MyMessage &message) {
if (mGetCommand(message) == C_PRESENTATION && inclusionMode) { // if (mGetCommand(message) == C_PRESENTATION && inclusionMode) {
rxBlink(3); // gw.rxBlink(3);
} else { // } else {
rxBlink(1); // gw.rxBlink(1);
} // }
// Pass along the message from sensors to serial line // 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)); serial(PSTR("%d;%d;%d;%d;%d;%s\n"),message.sender, message.sensor, mGetCommand(message), mGetAck(message), message.type, message.getString(convBuf));
} }
@@ -126,7 +95,7 @@ void checkInclusionFinished() {
} }
} }
void parseAndSend(MySensor gw, char *commandBuffer) { void parseAndSend(MySensor &gw, char *commandBuffer) {
boolean ok; boolean ok;
MyMessage &msg = gw.getLastMessage(); MyMessage &msg = gw.getLastMessage();
@@ -143,10 +112,14 @@ void parseAndSend(MySensor gw, char *commandBuffer) {
setInclusionMode(atoi(msg.data) == 1); setInclusionMode(atoi(msg.data) == 1);
} }
} else { } else {
txBlink(1); #ifdef WITH_LEDS_BLINKING
gw.txBlink(1);
#endif
ok = gw.sendRoute(msg); ok = gw.sendRoute(msg);
if (!ok) { if (!ok) {
errBlink(1); #ifdef WITH_LEDS_BLINKING
gw.errBlink(1);
#endif
} }
} }
} }
@@ -165,50 +138,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 #else
#error This example is only for use on Arduino. #error This example is only for use on Arduino.
#endif // ARDUINO #endif // ARDUINO
@@ -29,6 +29,7 @@
* - RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series * - RX/TX/ERR leds need to be connected between +5V (anode) and digital pin 6/5/4 with resistor 270-330R in a series
* *
* LEDs (OPTIONAL): * LEDs (OPTIONAL):
* - 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 * - 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 * - 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 * - ERR (red) - fast blink on error during transmission error or recieve crc error
@@ -48,7 +49,6 @@
#include <MyParserSerial.h> #include <MyParserSerial.h>
#include <MySensor.h> #include <MySensor.h>
#include <stdarg.h> #include <stdarg.h>
#include <MsTimer2.h>
#include <PinChangeInt.h> #include <PinChangeInt.h>
#include "GatewayUtil.h" #include "GatewayUtil.h"
@@ -71,7 +71,12 @@ MyTransportNRF24 transport(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);
MyHwATMega328 hw; MyHwATMega328 hw;
// Construct MySensors library (signer needed if MY_SIGNING_FEATURE is turned on in MyConfig.h) // 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*/); MySensor gw(transport, hw /*, signer*/);
#endif
char inputString[MAX_RECEIVE_LENGTH] = ""; // A string to hold incoming commands from serial/ethernet interface char inputString[MAX_RECEIVE_LENGTH] = ""; // A string to hold incoming commands from serial/ethernet interface
int inputPos = 0; int inputPos = 0;
@@ -92,11 +97,7 @@ void setup()
{ {
gw.begin(incomingMessage, 0, true, 0); gw.begin(incomingMessage, 0, true, 0);
setupGateway(RADIO_RX_LED_PIN, RADIO_TX_LED_PIN, RADIO_ERROR_LED_PIN, INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output); setupGateway(INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
// Add led timer interrupt
MsTimer2::set(300, ledTimersInterrupt);
MsTimer2::start();
// Add interrupt for inclusion button to pin // Add interrupt for inclusion button to pin
PCintPort::attachInterrupt(pinInclusion, startInclusionInterrupt, RISING); PCintPort::attachInterrupt(pinInclusion, startInclusionInterrupt, RISING);