From 73024ea4c98ed45308c3708ffc0479029cf524b2 Mon Sep 17 00:00:00 2001 From: Henrik Ekblad Date: Mon, 16 Mar 2015 10:43:09 +0100 Subject: [PATCH] Weird compile error for Patrick --- libraries/MySensors/MyConfig.h | 10 ++++-- libraries/MySensors/MyHw.h | 11 +++--- libraries/MySensors/MyHwATMega328.cpp | 44 +++++++++++++---------- libraries/MySensors/MyHwATMega328.h | 25 +++++++++---- libraries/MySensors/MySensor.cpp | 52 +++++++++++++-------------- libraries/MySensors/MySensor.h | 5 ++- 6 files changed, 85 insertions(+), 62 deletions(-) diff --git a/libraries/MySensors/MyConfig.h b/libraries/MySensors/MyConfig.h index 3e09981..0b4d806 100644 --- a/libraries/MySensors/MyConfig.h +++ b/libraries/MySensors/MyConfig.h @@ -1,13 +1,17 @@ #ifndef MyConfig_h #define MyConfig_h #include -/********************************** -* Enable/Disable debug logging -***********************************/ + +// Enable debug flag for debug prints. This will add a lot to the size of the final sketch but good +// to see what is actually is happening when developing #define DEBUG +// Serial output baud rate #define BAUD_RATE 115200 +// Set the hardware driver to use (initialized by MySensor-class) +#include "MyHwATMega328.h" +typedef MyHwATMega328 MyHwDriver; /********************************** * Message Signing Settings diff --git a/libraries/MySensors/MyHw.h b/libraries/MySensors/MyHw.h index a7ce870..95cf2a4 100644 --- a/libraries/MySensors/MyHw.h +++ b/libraries/MySensors/MyHw.h @@ -15,17 +15,18 @@ class MyHw public: // MyHw constructor MyHw(); - virtual void init() = 0; +/* // The following is defined as macros to save space + virtual void init() = 0; virtual void watchdogReset() = 0; virtual void reboot() = 0; virtual unsigned long millisec() = 0; + virtual uint8_t readConfig(uint8_t pos) = 0; + virtual void writeConfig(uint8_t pos, uint8_t value) = 0; + virtual void readConfigBlock(void* buf, void * pos, size_t length) = 0; + virtual void writeConfigBlock(void* pos, void* buf, uint16_t length) = 0; */ virtual void sleep(unsigned long ms) = 0; virtual bool sleep(uint8_t interrupt, uint8_t mode, unsigned long ms) = 0; virtual uint8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms) = 0; - virtual uint8_t readConfigByte(uint8_t pos) = 0; - virtual void writeConfigByte(uint8_t pos, uint8_t value) = 0; - virtual void readConfigBlock(void* buf, void * pos, size_t length) = 0; - virtual void writeConfigBlock(void* pos, void* buf, uint16_t length) = 0; #ifdef DEBUG virtual void debugPrint(bool isGW, const char *fmt, ... ) = 0; #endif diff --git a/libraries/MySensors/MyHwATMega328.cpp b/libraries/MySensors/MyHwATMega328.cpp index 7b03cd7..4f9d9a8 100644 --- a/libraries/MySensors/MyHwATMega328.cpp +++ b/libraries/MySensors/MyHwATMega328.cpp @@ -16,6 +16,27 @@ MyHwATMega328::MyHwATMega328() : MyHw() } +/* + +// The following was redifined as macros to save space + +inline uint8_t MyHwATMega328::readConfig(uint8_t pos) { + return eeprom_read_byte((uint8_t*)pos); +} + +inline void MyHwATMega328::writeConfig(uint8_t pos, uint8_t value) { + eeprom_update_byte((uint8_t*)pos, value); +} + +inline void MyHwATMega328::readConfigBlock(void* buf, void * pos, size_t length) { + eeprom_read_block(buf, (void*)pos, length); +} + +inline void MyHwATMega328::writeConfigBlock(void* pos, void* buf, size_t length) { + eeprom_write_block((void*)pos, (void*)buf, length); +} + + inline void MyHwATMega328::init() { Serial.begin(BAUD_RATE); } @@ -25,13 +46,13 @@ inline void MyHwATMega328::watchdogReset() { } inline void MyHwATMega328::reboot() { - wdt_enable(WDTO_15MS); - for (;;); + wdt_enable(WDTO_15MS); for (;;); } -inline unsigned long MyHwATMega328::millisec() { - return millis(); +inline unsigned long MyHwATMega328::millis() { + return ::millis(); } +*/ void powerDown(period_t period) { @@ -128,21 +149,6 @@ inline uint8_t MyHwATMega328::sleep(uint8_t interrupt1, uint8_t mode1, uint8_t i return retVal; } -inline uint8_t MyHwATMega328::readConfigByte(uint8_t pos) { - return eeprom_read_byte((uint8_t*)pos); -} - -inline void MyHwATMega328::writeConfigByte(uint8_t pos, uint8_t value) { - eeprom_update_byte((uint8_t*)pos, value); -} - -inline void MyHwATMega328::readConfigBlock(void* buf, void * pos, size_t length) { - eeprom_read_block(buf, (void*)pos, length); -} - -inline void MyHwATMega328::writeConfigBlock(void* pos, void* buf, size_t length) { - eeprom_write_block((void*)pos, (void*)buf, length); -} #ifdef DEBUG diff --git a/libraries/MySensors/MyHwATMega328.h b/libraries/MySensors/MyHwATMega328.h index bd197c7..618a789 100644 --- a/libraries/MySensors/MyHwATMega328.h +++ b/libraries/MySensors/MyHwATMega328.h @@ -17,6 +17,18 @@ #include #endif +// Define these as macros to save valuable space +#define hw_init() Serial.begin(BAUD_RATE) +#define hw_watchdogReset() wdt_reset() +#define hw_reboot() wdt_enable(WDTO_15MS); for (;;) +#define hw_millis() millis() +#define hw_readConfig(__pos) eeprom_read_byte((uint8_t*)__pos) +#define hw_writeConfig(__pos, __value) eeprom_update_byte((uint8_t*)__pos, __value) +#define hw_readConfigBlock(__buf, __pos, __length) eeprom_read_block(buf, (void*)__pos, __length) +#define hw_writeConfigBlock(__pos, __buf, __length) eeprom_write_block((void*)__pos, (void*)__buf, __length) + + + enum period_t { SLEEP_15Ms, @@ -37,17 +49,18 @@ class MyHwATMega328 : public MyHw public: MyHwATMega328(); - void init(); +/* void init(); void watchdogReset(); void reboot(); - unsigned long millisec(); + unsigned long millis(); + uint8_t readConfig(uint8_t pos); + void writeConfig(uint8_t pos, uint8_t value); + void readConfigBlock(void* buf, void * pos, size_t length); + void writeConfigBlock(void* pos, void* buf, size_t length); */ + void sleep(unsigned long ms); bool sleep(uint8_t interrupt, uint8_t mode, unsigned long ms); uint8_t sleep(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2, unsigned long ms); - uint8_t readConfigByte(uint8_t pos); - void writeConfigByte(uint8_t pos, uint8_t value); - void readConfigBlock(void* buf, void * pos, size_t length); - void writeConfigBlock(void* pos, void* buf, size_t length); #ifdef DEBUG void debugPrint(bool isGW, const char *fmt, ... ); #endif diff --git a/libraries/MySensors/MySensor.cpp b/libraries/MySensors/MySensor.cpp index 32645a3..c058e2e 100755 --- a/libraries/MySensors/MySensor.cpp +++ b/libraries/MySensors/MySensor.cpp @@ -48,7 +48,7 @@ MySensor::MySensor(MyTransport &_radio, MySigning &_signer, MyHw &_hw) void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, boolean _repeaterMode, uint8_t _parentNodeId) { - hw.init(); + hw_init(); repeaterMode = _repeaterMode; msgCallback = _msgCallback; failedTransmissions = 0; @@ -60,11 +60,11 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b radio.init(); // Read settings from eeprom - hw.readConfigBlock((void*)&nc, (void*)EEPROM_NODE_ID_ADDRESS, sizeof(NodeConfig)); + hw_readConfigBlock((void*)&nc, (void*)EEPROM_NODE_ID_ADDRESS, sizeof(NodeConfig)); // Read latest received controller configuration from EEPROM - hw.readConfigBlock((void*)&cc, (void*)EEPROM_CONTROLLER_CONFIG_ADDRESS, sizeof(ControllerConfig)); + hw_readConfigBlock((void*)&cc, (void*)EEPROM_CONTROLLER_CONFIG_ADDRESS, sizeof(ControllerConfig)); // Read out the signing requirements from EEPROM - hw.readConfigBlock((void*)doSign, (void*)EEPROM_SIGNING_REQUIREMENT_TABLE_ADDRESS, sizeof(doSign)); + hw_readConfigBlock((void*)doSign, (void*)EEPROM_SIGNING_REQUIREMENT_TABLE_ADDRESS, sizeof(doSign)); if (isGateway) { nc.distance = 0; @@ -79,7 +79,7 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b if (!autoFindParent) { nc.parentNodeId = _parentNodeId; // Save static parent id in eeprom (used by bootloader) - hw.writeConfigByte(EEPROM_PARENT_NODE_ID_ADDRESS, _parentNodeId); + hw_writeConfig(EEPROM_PARENT_NODE_ID_ADDRESS, _parentNodeId); // We don't actually know the distance to gw here. Let's pretend it is 1. // If the current node is also repeater, be aware of this. nc.distance = 1; @@ -94,7 +94,7 @@ void MySensor::begin(void (*_msgCallback)(const MyMessage &), uint8_t _nodeId, b nc.nodeId = _nodeId; // Save static id in eeprom if (_nodeId > 0) - hw.writeConfigByte(EEPROM_NODE_ID_ADDRESS, _nodeId); + hw_writeConfig(EEPROM_NODE_ID_ADDRESS, _nodeId); } else if (isValidParent(nc.parentNodeId)) { // Try to fetch node-id from gateway requestNodeId(); @@ -206,7 +206,7 @@ boolean MySensor::sendRoute(MyMessage &message) { ok = sendWrite(nc.parentNodeId, message); } else { // Relay the message - uint8_t route = hw.readConfigByte(EEPROM_ROUTES_ADDRESS+dest); + uint8_t route = hw_readConfig(EEPROM_ROUTES_ADDRESS+dest); if (route > GATEWAY_ADDRESS && route < BROADCAST_ADDRESS) { // This message should be forwarded to a child node. If we send message // to this nodes pipe then all children will receive it because the are @@ -244,7 +244,7 @@ boolean MySensor::sendRoute(MyMessage &message) { // This message should be routed back towards sensor net gateway ok = sendWrite(nc.parentNodeId, message); // Add this child to our "routing table" if it not already exist - hw.writeConfigByte(EEPROM_ROUTES_ADDRESS+sender, last); + hw_writeConfig(EEPROM_ROUTES_ADDRESS+sender, last); } } @@ -308,7 +308,7 @@ void MySensor::requestTime(void (* _timeCallback)(unsigned long)) { } boolean MySensor::process() { - hw.watchdogReset(); + hw_watchdogReset(); uint8_t to = 0; if (!radio.available(&to)) return false; @@ -357,7 +357,7 @@ boolean MySensor::process() { if (repeaterMode && last != nc.parentNodeId) { // Message is from one of the child nodes. Add it to routing table. - hw.writeConfigByte(EEPROM_ROUTES_ADDRESS+sender, last); + hw_writeConfig(EEPROM_ROUTES_ADDRESS+sender, last); } // Check if sender requests an ack back. @@ -385,8 +385,8 @@ boolean MySensor::process() { // Found a neighbor closer to GW than previously found nc.distance = distance; nc.parentNodeId = msg.sender; - hw.writeConfigByte(EEPROM_PARENT_NODE_ID_ADDRESS, nc.parentNodeId); - hw.writeConfigByte(EEPROM_DISTANCE_ADDRESS, nc.distance); + hw_writeConfig(EEPROM_PARENT_NODE_ID_ADDRESS, nc.parentNodeId); + hw_writeConfig(EEPROM_DISTANCE_ADDRESS, nc.distance); debug(PSTR("parent=%d, d=%d\n"), nc.parentNodeId, nc.distance); } } @@ -406,7 +406,7 @@ boolean MySensor::process() { CLEAR_SIGN(msg.sender); } // Save updated table - hw.writeConfigBlock((void*)EEPROM_SIGNING_REQUIREMENT_TABLE_ADDRESS, (void*)doSign, sizeof(doSign)); + hw_writeConfigBlock((void*)EEPROM_SIGNING_REQUIREMENT_TABLE_ADDRESS, (void*)doSign, sizeof(doSign)); // Inform sender about our preference if we are a gateway, but only require signing if the sender required signing // We do not currently want a gateway to require signing from all nodes in a network just because it wants one node @@ -425,7 +425,7 @@ boolean MySensor::process() { if (type == I_REBOOT) { // Requires MySensors or other bootloader with watchdogs enabled - hw.reboot(); + hw_reboot(); } else if (type == I_ID_RESPONSE) { if (nc.nodeId == AUTO) { nc.nodeId = msg.getByte(); @@ -436,7 +436,7 @@ boolean MySensor::process() { } setupNode(); // Write id to EEPROM - hw.writeConfigByte(EEPROM_NODE_ID_ADDRESS, nc.nodeId); + hw_writeConfig(EEPROM_NODE_ID_ADDRESS, nc.nodeId); debug(PSTR("id=%d\n"), nc.nodeId); } } else if (type == I_CONFIG) { @@ -444,18 +444,18 @@ boolean MySensor::process() { // and store it in eeprom if changed isMetric = msg.getString()[0] == 'M' ; cc.isMetric = isMetric; - hw.writeConfigByte(EEPROM_CONTROLLER_CONFIG_ADDRESS, isMetric); + hw_writeConfig(EEPROM_CONTROLLER_CONFIG_ADDRESS, isMetric); } else if (type == I_CHILDREN) { if (repeaterMode && msg.getString()[0] == 'C') { // Clears child relay data for this node debug(PSTR("clear\n")); uint8_t i = 255; do { - hw.writeConfigByte(EEPROM_ROUTES_ADDRESS+i, 0xff); + hw_writeConfig(EEPROM_ROUTES_ADDRESS+i, 0xff); } while (i--); // Clear parent node id & distance to gw - hw.writeConfigByte(EEPROM_PARENT_NODE_ID_ADDRESS, 0xFF); - hw.writeConfigByte(EEPROM_DISTANCE_ADDRESS, 0xFF); + hw_writeConfig(EEPROM_PARENT_NODE_ID_ADDRESS, 0xFF); + hw_writeConfig(EEPROM_DISTANCE_ADDRESS, 0xFF); // Find parent node findParentNode(); sendRoute(build(msg, nc.nodeId, GATEWAY_ADDRESS, NODE_SENSOR_ID, C_INTERNAL, I_CHILDREN,false).set("")); @@ -480,7 +480,7 @@ boolean MySensor::process() { // Relaying nodes should always answer ping messages // Wait a random delay of 0-2 seconds to minimize collision // between ping ack messages from other relaying nodes - wait(hw.millisec() & 0x3ff); + wait(hw_millis() & 0x3ff); sendWrite(sender, build(msg, nc.nodeId, sender, NODE_SENSOR_ID, C_INTERNAL, I_FIND_PARENT_RESPONSE, false).set(nc.distance)); } } else if (to == nc.nodeId) { @@ -496,15 +496,15 @@ MyMessage& MySensor::getLastMessage() { } void MySensor::saveState(uint8_t pos, uint8_t value) { - hw.writeConfigByte(EEPROM_LOCAL_CONFIG_ADDRESS+pos, value); + hw_writeConfig(EEPROM_LOCAL_CONFIG_ADDRESS+pos, value); } uint8_t MySensor::loadState(uint8_t pos) { - return hw.readConfigByte(EEPROM_LOCAL_CONFIG_ADDRESS+pos); + return hw_readConfig(EEPROM_LOCAL_CONFIG_ADDRESS+pos); } void MySensor::wait(unsigned long ms) { - unsigned long enter = hw.millisec(); - while (hw.millisec() - enter < ms) { + unsigned long enter = hw_millis(); + while (hw_millis() - enter < ms) { process(); } } @@ -530,9 +530,9 @@ bool MySensor::sign(MyMessage &message) { } else { // We have to wait for the nonce to arrive before we can sign our original message // Other messages could come in-between. We trust process() takes care of them - unsigned long enter = hw.millisec(); + unsigned long enter = hw_millis(); msgSign = message; // Copy the message to sign since message buffer might be touched in process() - while (hw.millisec() - enter < 5000) { + while (hw_millis() - enter < 5000) { if (process()) { if (mGetCommand(getLastMessage()) == C_INTERNAL && getLastMessage().type == I_GET_NONCE_RESPONSE) { // Proceed with signing if nonce has been received diff --git a/libraries/MySensors/MySensor.h b/libraries/MySensors/MySensor.h index a58d93a..9a2ec57 100644 --- a/libraries/MySensors/MySensor.h +++ b/libraries/MySensors/MySensor.h @@ -13,14 +13,13 @@ #define MySensor_h #include "Version.h" // Auto generated by bot +#include "MyConfig.h" #include "MyHw.h" -#include "MyHwATMega328.h" #include "MyTransport.h" #include "MyTransportNRF24.h" #include "MyParser.h" #include "MySigning.h" #include "MySigningNone.h" -#include "MyConfig.h" #include "MyMessage.h" #include #include @@ -74,7 +73,7 @@ class MySensor * Creates a new instance of Sensor class. * */ - MySensor(MyTransport &radio =*new MyTransportNRF24(), MySigning &signer=*new MySigningNone(), MyHw &hw=*new MyHwATMega328()); + MySensor(MyTransport &radio =*new MyTransportNRF24(), MySigning &signer=*new MySigningNone(), MyHw &hw=*new MyHwDriver()); /** * Begin operation of the MySensors library