Merge pull request #109 from thozza/development-leds

Add LEDs blinking into base MySensor class without any library dependency
This commit is contained in:
Henrik Ekblad
2015-05-06 22:29:07 +02:00
7 changed files with 209 additions and 180 deletions
+20
View File
@@ -45,6 +45,26 @@
#define MY_OTA_FLASH_JDECID 0x1F65 #define MY_OTA_FLASH_JDECID 0x1F65
/**********************************
* Information LEDs blinking
***********************************/
// This feature enables LEDs blinking on message receive, transmit
// or if some error occured. This was commonly used only in gateways,
// but now can be used in any sensor node. Also the LEDs can now be
// disabled in the gateway.
// #define WITH_LEDS_BLINKING
// default LEDs blinking period in milliseconds
#define DEFAULT_LED_BLINK_PERIOD 300
// The RX LED default pin
#define DEFAULT_RX_LED_PIN 8
// The TX LED default pin
#define DEFAULT_TX_LED_PIN 9
// The Error LED default pin
#define DEFAULT_ERR_LED_PIN 7
/********************************** /**********************************
* Message Signing Settings * Message Signing Settings
***********************************/ ***********************************/
+119
View File
@@ -52,6 +52,9 @@ static inline bool isValidDistance( const uint8_t distance ) {
MySensor::MySensor(MyTransport &_radio, MyHw &_hw MySensor::MySensor(MyTransport &_radio, MyHw &_hw
#ifdef MY_SIGNING_FEATURE #ifdef MY_SIGNING_FEATURE
, MySigning &_signer , MySigning &_signer
#endif
#ifdef WITH_LEDS_BLINKING
, uint8_t _rx, uint8_t _tx, uint8_t _er, unsigned long _blink_period
#endif #endif
) )
: :
@@ -59,6 +62,9 @@ MySensor::MySensor(MyTransport &_radio, MyHw &_hw
#ifdef MY_SIGNING_FEATURE #ifdef MY_SIGNING_FEATURE
signer(_signer), signer(_signer),
#endif #endif
#ifdef WITH_LEDS_BLINKING
pinRx(_rx), pinTx(_tx), pinEr(_er), ledBlinkPeriod(_blink_period),
#endif
#ifdef MY_OTA_FIRMWARE_FEATURE #ifdef MY_OTA_FIRMWARE_FEATURE
flash(MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID), flash(MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID),
#endif #endif
@@ -90,6 +96,64 @@ bool MySensor::isValidFirmware() {
#endif #endif
#ifdef WITH_LEDS_BLINKING
void MySensor::handleLedsBlinking() {
static unsigned long next_time = hw_millis() + ledBlinkPeriod;
// Just return if it is not the time...
// http://playground.arduino.cc/Code/TimingRollover
if ((long)(hw_millis() - next_time) < 0)
return;
else
next_time = hw_millis() + ledBlinkPeriod;
// do the actual blinking
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;
if(countErr && countErr != 255) {
// switch led on
digitalWrite(pinEr, LOW);
}
else if(!countErr) {
// switching off
digitalWrite(pinEr, HIGH);
}
if(countErr != 255)
--countErr;
}
void MySensor::rxBlink(uint8_t cnt) {
if(countRx == 255) { countRx = cnt; }
}
void MySensor::txBlink(uint8_t cnt) {
if(countTx == 255) { countTx = cnt; }
}
void MySensor::errBlink(uint8_t cnt) {
if(countErr == 255) { countErr = cnt; }
}
#endif
void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId) { void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId) {
hw_init(); hw_init();
@@ -111,6 +175,23 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b
hw_readConfigBlock((void*)doSign, (void*)EEPROM_SIGNING_REQUIREMENT_TABLE_ADDRESS, sizeof(doSign)); hw_readConfigBlock((void*)doSign, (void*)EEPROM_SIGNING_REQUIREMENT_TABLE_ADDRESS, sizeof(doSign));
#endif #endif
#ifdef WITH_LEDS_BLINKING
// Setup led pins
pinMode(pinRx, OUTPUT);
pinMode(pinTx, OUTPUT);
pinMode(pinEr, OUTPUT);
// Set initial state of leds
digitalWrite(pinRx, HIGH);
digitalWrite(pinTx, HIGH);
digitalWrite(pinEr, HIGH);
// initialize counters
countRx = 0;
countTx = 0;
countErr = 0;
#endif
if (isGateway) { if (isGateway) {
// Set configuration for gateway // Set configuration for gateway
nc.parentNodeId = GATEWAY_ADDRESS; nc.parentNodeId = GATEWAY_ADDRESS;
@@ -246,12 +327,18 @@ boolean MySensor::sendRoute(MyMessage &message) {
// If we still don't have any parent id, re-request and skip this message. // If we still don't have any parent id, re-request and skip this message.
if (nc.parentNodeId == AUTO) { if (nc.parentNodeId == AUTO) {
findParentNode(); findParentNode();
#ifdef WITH_LEDS_BLINKING
errBlink(1);
#endif
return false; return false;
} }
// If we still don't have any node id, re-request and skip this message. // If we still don't have any node id, re-request and skip this message.
if (nc.nodeId == AUTO) { if (nc.nodeId == AUTO) {
requestNodeId(); requestNodeId();
#ifdef WITH_LEDS_BLINKING
errBlink(1);
#endif
return false; return false;
} }
@@ -288,10 +375,16 @@ boolean MySensor::sendRoute(MyMessage &message) {
} }
if (hw_millis() - enter > MY_VERIFICATION_TIMEOUT_MS) { if (hw_millis() - enter > MY_VERIFICATION_TIMEOUT_MS) {
debug(PSTR("nonce tmo\n")); debug(PSTR("nonce tmo\n"));
#ifdef WITH_LEDS_BLINKING
errBlink(1);
#endif
return false; return false;
} }
if (!signOk) { if (!signOk) {
debug(PSTR("sign fail\n")); debug(PSTR("sign fail\n"));
#ifdef WITH_LEDS_BLINKING
errBlink(1);
#endif
return false; return false;
} }
// After this point, only the 'last' member of the message structure is allowed to be altered if the message has been signed, // 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) {
if (!ok) { if (!ok) {
// Failure when sending to parent node. The parent node might be down and we // Failure when sending to parent node. The parent node might be down and we
// need to find another route to gateway. // need to find another route to gateway.
#ifdef WITH_LEDS_BLINKING
errBlink(1);
#endif
failedTransmissions++; failedTransmissions++;
if (autoFindParent && failedTransmissions > SEARCH_FAILURES) { if (autoFindParent && failedTransmissions > SEARCH_FAILURES) {
findParentNode(); findParentNode();
@@ -364,6 +460,9 @@ boolean MySensor::sendWrite(uint8_t to, MyMessage &message) {
mSetVersion(message, PROTOCOL_VERSION); mSetVersion(message, PROTOCOL_VERSION);
uint8_t length = mGetSigned(message) ? MAX_MESSAGE_LENGTH : mGetLength(message); uint8_t length = mGetSigned(message) ? MAX_MESSAGE_LENGTH : mGetLength(message);
message.last = nc.nodeId; message.last = nc.nodeId;
#ifdef WITH_LEDS_BLINKING
txBlink(1);
#endif
bool ok = radio.send(to, &message, min(MAX_MESSAGE_LENGTH, HEADER_SIZE + length)); bool ok = radio.send(to, &message, min(MAX_MESSAGE_LENGTH, HEADER_SIZE + length));
debug(PSTR("send: %d-%d-%d-%d s=%d,c=%d,t=%d,pt=%d,l=%d,sg=%d,st=%s:%s\n"), 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)) {
boolean MySensor::process() { boolean MySensor::process() {
hw_watchdogReset(); hw_watchdogReset();
#ifdef WITH_LEDS_BLINKING
handleLedsBlinking();
#endif
uint8_t to = 0; uint8_t to = 0;
if (!radio.available(&to)) if (!radio.available(&to))
{ {
@@ -418,6 +522,9 @@ boolean MySensor::process() {
debug(PSTR("fw upd fail\n")); debug(PSTR("fw upd fail\n"));
// Give up. We have requested MY_OTA_RETRY times without any packet in return. // Give up. We have requested MY_OTA_RETRY times without any packet in return.
fwUpdateOngoing = false; fwUpdateOngoing = false;
#ifdef WITH_LEDS_BLINKING
errBlink(1);
#endif
return false; return false;
} }
fwRetry--; fwRetry--;
@@ -439,6 +546,9 @@ boolean MySensor::process() {
#endif #endif
uint8_t len = radio.receive((uint8_t *)&msg); uint8_t len = radio.receive((uint8_t *)&msg);
#ifdef WITH_LEDS_BLINKING
rxBlink(1);
#endif
#ifdef MY_SIGNING_FEATURE #ifdef MY_SIGNING_FEATURE
// Before processing message, reject unsigned messages if signing is required and check signature (if it is signed and addressed to us) // 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() {
if (!mGetSigned(msg)) { if (!mGetSigned(msg)) {
// Got unsigned message that should have been signed // Got unsigned message that should have been signed
debug(PSTR("no sign\n")); debug(PSTR("no sign\n"));
#ifdef WITH_LEDS_BLINKING
errBlink(1);
#endif
return false; return false;
} }
else if (!signer.verifyMsg(msg)) { else if (!signer.verifyMsg(msg)) {
debug(PSTR("verify fail\n")); debug(PSTR("verify fail\n"));
#ifdef WITH_LEDS_BLINKING
errBlink(1);
#endif
return false; // This signed message has been tampered with! return false; // This signed message has been tampered with!
} }
} }
@@ -468,6 +584,9 @@ boolean MySensor::process() {
if(!(mGetVersion(msg) == PROTOCOL_VERSION)) { if(!(mGetVersion(msg) == PROTOCOL_VERSION)) {
debug(PSTR("ver mismatch\n")); debug(PSTR("ver mismatch\n"));
#ifdef WITH_LEDS_BLINKING
errBlink(1);
#endif
return false; return false;
} }
+29
View File
@@ -145,6 +145,12 @@ class MySensor
MySensor(MyTransport &radio =*new MyTransportNRF24(), MyHw &hw=*new MyHwDriver() MySensor(MyTransport &radio =*new MyTransportNRF24(), MyHw &hw=*new MyHwDriver()
#ifdef MY_SIGNING_FEATURE #ifdef MY_SIGNING_FEATURE
, MySigning &signer=*new MySigningNone() , MySigning &signer=*new MySigningNone()
#endif
#ifdef WITH_LEDS_BLINKING
, uint8_t _rx=DEFAULT_RX_LED_PIN,
uint8_t _tx=DEFAULT_TX_LED_PIN,
uint8_t _er=DEFAULT_ERR_LED_PIN,
unsigned long _blink_period=DEFAULT_LED_BLINK_PERIOD
#endif #endif
); );
@@ -299,6 +305,15 @@ class MySensor
*/ */
int8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms=0); int8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms=0);
#ifdef WITH_LEDS_BLINKING
/**
* Blink with LEDs
* @param cnt how many blink cycles to keep the LED on. Default cycle is 300ms
*/
void rxBlink(uint8_t cnt);
void txBlink(uint8_t cnt);
void errBlink(uint8_t cnt);
#endif
protected: protected:
NodeConfig nc; // Essential settings for node to work NodeConfig nc; // Essential settings for node to work
@@ -324,6 +339,20 @@ class MySensor
MySigning& signer; MySigning& signer;
#endif #endif
#ifdef WITH_LEDS_BLINKING
uint8_t pinRx; // Rx led pin
uint8_t pinTx; // Tx led pin
uint8_t pinEr; // Err led pin
// these variables don't need to be volatile, since we are not using interrupts
uint8_t countRx;
uint8_t countTx;
uint8_t countErr;
unsigned long ledBlinkPeriod;
void handleLedsBlinking(); // do the actual blinking
#endif
MyTransport& radio; MyTransport& radio;
MyHw& hw; MyHw& hw;
@@ -46,6 +46,7 @@
* E.g. If you want to use the defualt values in this sketch enter: 192.168.178.66:5003 * E.g. If you want to use the defualt values in this sketch enter: 192.168.178.66:5003
* *
* LED purposes: * 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 * - 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
@@ -68,7 +69,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"
@@ -104,7 +104,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
#define IP_PORT 5003 // The port you want to open #define IP_PORT 5003 // The port you want to open
@@ -136,11 +141,8 @@ void output(const char *fmt, ... ) {
void setup() void setup()
{ {
Ethernet.begin(mac, myIp); 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 setupGateway(INCLUSION_MODE_PIN, INCLUSION_MODE_TIME, output);
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);
@@ -7,9 +7,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
@@ -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 #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);
} }
@@ -76,11 +45,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));
} }
@@ -106,7 +75,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();
@@ -123,10 +92,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
} }
} }
} }
@@ -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 #else
#error This example is only for use on Arduino. #error This example is only for use on Arduino.
#endif // ARDUINO #endif // ARDUINO
@@ -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"
@@ -59,7 +59,7 @@
#define RADIO_TX_LED_PIN 5 // the PCB, on board LED #define RADIO_TX_LED_PIN 5 // the PCB, on board LED
// NRFRF24L01 radio driver (set low transmit power by default) // NRFRF24L01 radio driver (set low transmit power by default)
MyTransportNRF24 transport(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW); MyTransportNRF24 transport(RF24_CE_PIN, RF24_CS_PIN, RF24_PA_LEVEL_GW);
//MyTransportRFM69 transport; //MyTransportRFM69 transport;
// Message signing driver (signer needed if MY_SIGNING_FEATURE is turned on in MyConfig.h) // 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);
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);