diff --git a/libraries/MySensors/MyConfig.h b/libraries/MySensors/MyConfig.h index 437ef3e..c2148ab 100644 --- a/libraries/MySensors/MyConfig.h +++ b/libraries/MySensors/MyConfig.h @@ -45,6 +45,26 @@ #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 ***********************************/ diff --git a/libraries/MySensors/MySensor.cpp b/libraries/MySensors/MySensor.cpp index 837ffd6..f059b9e 100644 --- a/libraries/MySensors/MySensor.cpp +++ b/libraries/MySensors/MySensor.cpp @@ -52,6 +52,9 @@ static inline bool isValidDistance( const uint8_t distance ) { MySensor::MySensor(MyTransport &_radio, MyHw &_hw #ifdef MY_SIGNING_FEATURE , MySigning &_signer +#endif +#ifdef WITH_LEDS_BLINKING + , uint8_t _rx, uint8_t _tx, uint8_t _er, unsigned long _blink_period #endif ) : @@ -59,6 +62,9 @@ MySensor::MySensor(MyTransport &_radio, MyHw &_hw #ifdef MY_SIGNING_FEATURE signer(_signer), #endif +#ifdef WITH_LEDS_BLINKING + pinRx(_rx), pinTx(_tx), pinEr(_er), ledBlinkPeriod(_blink_period), +#endif #ifdef MY_OTA_FIRMWARE_FEATURE flash(MY_OTA_FLASH_SS, MY_OTA_FLASH_JDECID), #endif @@ -90,6 +96,64 @@ bool MySensor::isValidFirmware() { #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) { 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)); #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) { // Set configuration for gateway nc.parentNodeId = GATEWAY_ADDRESS; @@ -408,6 +489,11 @@ void MySensor::requestTime(void (* _timeCallback)(unsigned long)) { boolean MySensor::process() { hw_watchdogReset(); + +#ifdef WITH_LEDS_BLINKING + handleLedsBlinking(); +#endif + uint8_t to = 0; if (!radio.available(&to)) { diff --git a/libraries/MySensors/MySensor.h b/libraries/MySensors/MySensor.h index 0081b90..9917b66 100644 --- a/libraries/MySensors/MySensor.h +++ b/libraries/MySensors/MySensor.h @@ -145,6 +145,12 @@ class MySensor MySensor(MyTransport &radio =*new MyTransportNRF24(), MyHw &hw=*new MyHwDriver() #ifdef MY_SIGNING_FEATURE , 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 ); @@ -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); +#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: NodeConfig nc; // Essential settings for node to work @@ -324,6 +339,20 @@ class MySensor MySigning& signer; #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; MyHw& hw;