From 07e100e5a43c2cfc874aa58454c62cfd56a02031 Mon Sep 17 00:00:00 2001 From: pa-pa <> Date: Mon, 8 May 2017 07:24:40 +0200 Subject: [PATCH] move OTA code into library shorter device code - saves also some bytes in flash and ram --- BatterySensor.h | 6 +- Channel.h | 2 + Device.h | 104 +++++++----------- Dimmer.h | 3 +- Message.h | 13 +++ MultiChannelDevice.h | 35 +++--- examples/HM-ES-TX-WM/HM-ES-TX-WM.ino | 73 +++++------- .../HM-LC-Dim1PWM-CV/HM-LC-Dim1PWM-CV.ino | 67 ++++------- examples/HM-LC-SWX-SM/HM-LC-SWX-SM.ino | 85 +++++--------- examples/HM-RC-4/HM-RC-4.ino | 69 ++++-------- examples/HM-SEC-MDIR/HM-SEC-MDIR.ino | 59 +++------- examples/HM-WDS10-TH-O/HM-WDS10-TH-O.ino | 56 +++------- .../HM-WDS100-C6-O-2/HM-WDS100-C6-O-2.ino | 65 ++++------- examples/stm32/HM-LC-SWX-SM/HM-LC-SWX-SM.ino | 52 +++------ 14 files changed, 240 insertions(+), 449 deletions(-) diff --git a/BatterySensor.h b/BatterySensor.h index c473b003..09c0ea91 100644 --- a/BatterySensor.h +++ b/BatterySensor.h @@ -74,15 +74,15 @@ class BatterySensor : public Alarm { m_LastValue = voltage(); } - uint8_t current () { + uint8_t current () const{ return m_LastValue; } - bool critical () { + bool critical () const { return m_LastValue < CRITICALVALUE; } - bool low () { + bool low () const { return m_LastValue < LOWVALUE; } diff --git a/Channel.h b/Channel.h index c896a5fd..72e8e0e0 100644 --- a/Channel.h +++ b/Channel.h @@ -35,6 +35,8 @@ class Channel { DeviceType& device () { return *dev; } + const DeviceType& device () const { return *dev; } + uint8_t number () const { return num; } uint16_t address () const { return addr; } diff --git a/Device.h b/Device.h index 3a9bed01..d89224bd 100644 --- a/Device.h +++ b/Device.h @@ -14,6 +14,10 @@ #include "Radio.h" #include "Led.h" +#define OTA_MODEL_START 0x7ff0 // start address of 2 byte model id in bootloader +#define OTA_SERIAL_START 0x7ff2 // start address of 10 byte serial number in bootloader +#define OTA_HMID_START 0x7ffc // start address of 3 byte device id in bootloader + namespace as { class DeviceType { @@ -53,17 +57,9 @@ class Device { typedef typename HalType::RadioType RadioType; private: - HMID devid; - HMID master; - char serial[11]; - - uint8_t firmversion; - uint8_t model[2]; - uint8_t subtype; - uint8_t devinfo[3]; - HalType* hal; - uint8_t msgcount; + List0& list0; + uint8_t msgcount; HMID lastdev; uint8_t lastmsg; @@ -74,13 +70,14 @@ class Device { public: - Device (uint16_t addr) : firmversion(0), subtype(0), hal(0), msgcount(0), lastmsg(0), kstore(addr) { + Device (uint16_t addr,List0& l) : hal(0), list0(l), msgcount(0), lastmsg(0), kstore(addr) { // TODO init seed } virtual ~Device () {} LedType& led () { return hal->led; } BatteryType& battery () { return hal->battery; } + const BatteryType& battery () const { return hal->battery; } RadioType& radio () { return hal->radio; } KeyStore& keystore () { return this->kstore; } Activity& activity () { return hal->activity; } @@ -101,64 +98,44 @@ class Device { hal = &h; } - void setFirmwareVersion (uint8_t v) { - firmversion = v; - } - - void setModel (uint8_t m1, uint8_t m2) { - model[0] = m1; - model[1] = m2; - } - - const uint8_t* getModel () const { - return model; - } - - void setModel (uint16_t address) { - HalType::pgm_read(model,address,sizeof(model)); - } - - void setSubType (uint8_t st) { - subtype = st; - } - - void setInfo (uint8_t i1, uint8_t i2, uint8_t i3) { - devinfo[0] = i1; - devinfo[1] = i2; - devinfo[2] = i3; - } - - void setMasterID (const HMID& id) { - master = id; - } - - const HMID& getMasterID () const { - return master; - } - - void setDeviceID (const HMID& id) { - devid=id; + void getDeviceID (HMID& id) { +#ifdef USE_OTA_BOOTLOADER + HalType::pgm_read((uint8_t*)&id,OTA_HMID_START,sizeof(id)); +#else + id = DEVICE_ID; +#endif } - void setDeviceID (uint16_t address) { - HalType::pgm_read((uint8_t*)&devid,address,sizeof(devid)); + void getDeviceSerial (uint8_t* serial) { +#ifdef USE_OTA_BOOTLOADER + HalType::pgm_read((uint8_t*)serial,OTA_SERIAL_START,10); +#else + memcpy(serial,DEVICE_SERIAL,10); +#endif } - const HMID& getDeviceID () const { - return devid; + bool isDeviceSerial (const uint8_t* serial) { + uint8_t tmp[10]; + getDeviceSerial(tmp); + return memcmp(tmp,serial,10)==0; } - void setSerial (const char* ser) { - memcpy(serial,ser,10); - serial[10] = 0; + void getDeviceModel (uint8_t* model) { +#ifdef USE_OTA_BOOTLOADER + HalType::pgm_read(model,OTA_MODEL_START,2); +#else + uint8_t dm[2] = {DEVICE_MODEL}; + memcpy(model,dm,sizeof(dm)); +#endif } - void setSerial (uint16_t address) { - HalType::pgm_read((uint8_t*)serial,address,10); + void getDeviceInfo (uint8_t* info) { + uint8_t di[3] = {DEVICE_INFO}; + memcpy(info,di,sizeof(di)); } - const char* getSerial () const { - return serial; + HMID getMasterID () { + return list0.masterid(); } template @@ -222,7 +199,7 @@ class Device { bool send(Message& msg,const HMID& to) { msg.to(to); - msg.from(devid); + getDeviceID(msg.from()); msg.setRpten(); // has to be set always bool result = false; uint8_t maxsend = 6; @@ -293,14 +270,17 @@ class Device { void sendDeviceInfo (const HMID& to,uint8_t count) { DeviceInfoMsg& pm = msg.deviceInfo(); pm.init(to,count); - pm.fill(firmversion,model,serial,subtype,devinfo); + pm.fill(DEVICE_FIRMWARE,DEVICE_TYPE); + getDeviceModel(pm.model()); + getDeviceSerial(pm.serial()); + getDeviceInfo(pm.info()); send(msg,to); } void sendSerialInfo (const HMID& to,uint8_t count) { SerialInfoMsg& pm = msg.serialInfo(); pm.init(to,count); - pm.fill(serial); + getDeviceSerial(pm.serial()); send(msg,to); } diff --git a/Dimmer.h b/Dimmer.h index 41e48265..96c799ad 100644 --- a/Dimmer.h +++ b/Dimmer.h @@ -911,7 +911,8 @@ class DimmerDevice : public MultiChannelDevice { typedef Device DeviceType; - MultiChannelDevice (uint16_t addr) : Device(addr), list0(addr + DeviceType::keystore().size()), numChannels(ChannelCount), cfgChannel(0xff) { + MultiChannelDevice (uint16_t addr) : Device(addr,list0), list0(addr + DeviceType::keystore().size()), numChannels(ChannelCount), cfgChannel(0xff) { addr = list0.address() + list0.size(); for( uint8_t i=0; i { void dumpSize () { ChannelType ch = channel(channels()); - uint16_t addr = ch.address() + ch.size(); - DPRINT("Address Space: ");DDEC(DeviceType::keystore().address());DPRINT(" - ");DDECLN(addr); + DPRINT("Address Space: ");DDEC(DeviceType::keystore().address());DPRINT(" - ");DDECLN((uint16_t)(ch.address() + ch.size())); } uint16_t checksum () { @@ -91,23 +90,16 @@ class MultiChannelDevice : public Device { return number != 0 && number <= channels(); } - void init (HalType& hal,const HMID& id,const char* serial) { - DeviceType::keystore().init(); - // read master id from flash - DeviceType::setHal(hal); - DeviceType::setMasterID(list0.masterid()); - DeviceType::setDeviceID(id); - DeviceType::setSerial(serial); - } - - void init (HalType& hal,uint16_t idaddr, uint16_t serialaddr) { + void init (HalType& hal) { + dumpSize(); + // first initialize EEProm if needed + if( storage.setup(checksum()) == true ) { + firstinit(); + storage.store(); + } DeviceType::keystore().init(); DeviceType::setHal(hal); - // read master id from flash - DeviceType::setMasterID(list0.masterid()); - // read id & serial from bootloader - DeviceType::setDeviceID(idaddr); - DeviceType::setSerial(serialaddr); + hal.init(); } void firstinit () { @@ -168,7 +160,9 @@ class MultiChannelDevice : public Device { } void process(Message& msg) { - if( msg.to() == DeviceType::getDeviceID() || (msg.to() == HMID::boardcast && DeviceType::isBoardcastMsg(msg))) { + HMID devid; + DeviceType::getDeviceID(devid); + if( msg.to() == devid || (msg.to() == HMID::boardcast && DeviceType::isBoardcastMsg(msg))) { DPRINT(F("-> ")); msg.dump(); // ignore repeated messages @@ -184,7 +178,7 @@ class MultiChannelDevice : public Device { if( mtype == AS_MESSAGE_CONFIG ) { DeviceType::activity().stayAwake(millis2ticks(500)); // PAIR_SERIAL - if( msubc == AS_CONFIG_PAIR_SERIAL && memcmp(msg.data(),DeviceType::getSerial(),10)==0 ) { + if( msubc == AS_CONFIG_PAIR_SERIAL && DeviceType::isDeviceSerial(msg.data())==true ) { DeviceType::led().set(LedStates::pairing); DeviceType::activity().stayAwake( seconds2ticks(20) ); // 20 seconds DeviceType::sendDeviceInfo(DeviceType::getMasterID(),msg.length()); @@ -269,7 +263,6 @@ class MultiChannelDevice : public Device { // CONFIG_END else if( msubc == AS_CONFIG_END ) { if( cfgList.address() == list0.address() ) { - DeviceType::setMasterID(list0.masterid()); DeviceType::led().set(LedStates::nothing); } else { diff --git a/examples/HM-ES-TX-WM/HM-ES-TX-WM.ino b/examples/HM-ES-TX-WM/HM-ES-TX-WM.ino index 18eccec8..65367323 100644 --- a/examples/HM-ES-TX-WM/HM-ES-TX-WM.ino +++ b/examples/HM-ES-TX-WM/HM-ES-TX-WM.ino @@ -3,12 +3,16 @@ // 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //- ----------------------------------------------------------------------------------------------------------------------- -/* - * Setup defines to configure the library. - */ -// #define USE_AES -// #define HM_DEF_KEY 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10 -// #define HM_DEF_KEY_INDEX 0 +// define this to read the device id, serial and device type from bootloader section +// #define USE_OTA_BOOTLOADER + +// define all device properties +#define DEVICE_ID HMID(0x90,0x12,0x34) +#define DEVICE_SERIAL "papa555555" +#define DEVICE_MODEL 0x00,0xde +#define DEVICE_FIRMWARE 0x11 +#define DEVICE_TYPE DeviceType::PowerMeter +#define DEVICE_INFO 0x03,0x01,0x00 #include #include @@ -17,19 +21,6 @@ #include -// define this to read the device id, serial and device type from bootloader section -// #define USE_OTA_BOOTLOADER - -#ifdef USE_OTA_BOOTLOADER - #define OTA_MODEL_START 0x7ff0 // start address of 2 byte model id in bootloader - #define OTA_SERIAL_START 0x7ff2 // start address of 10 byte serial number in bootloader - #define OTA_HMID_START 0x7ffc // start address of 3 byte device id in bootloader -#else - // device ID - #define DEVICE_ID HMID(0x90,0x12,0x34) - // serial number - #define DEVICE_SERIAL "papa555555" -#endif // we use a Pro Mini // Arduino pin for the LED @@ -53,9 +44,11 @@ using namespace as; /** * Configure the used hardware */ -typedef AvrSPI<10,11,12,13> RadioSPI; -typedef AskSin,BatterySensor<22,19>,Radio > Hal; -Hal hal; +typedef AvrSPI<10,11,12,13> SPIType; +typedef Radio RadioType; +typedef StatusLed<4> LedType; +typedef BatterySensor<22,19> BatteryType; +typedef AskSin HalType; class MeterList0Data : public List0Data { uint8_t LocalResetDisbale : 1; // 0x18 - 24 @@ -97,9 +90,11 @@ class MeterList0 : public ChannelList { public: MeterList0(uint16_t a) : ChannelList(a) {} + operator List0& () const { return *(List0*)this; } + // from List0 - HMID masterid () { return HMID(getByte(1),getByte(2),getByte(3)); } - void masterid (const HMID& mid) { setByte(1,mid.id0()); setByte(2,mid.id1()); setByte(3,mid.id2()); }; + HMID masterid () { return ((List0*)this)->masterid(); } + void masterid (const HMID& mid) { ((List0*)this)->masterid(mid); } bool localResetDisable () const { return isBitSet(sizeof(List0Data) + 0,0x01); } bool localResetDisable (bool value) const { return setBit(sizeof(List0Data) + 0,0x01,value); } @@ -248,7 +243,7 @@ public: } }; -class MeterChannel : public Channel, public Alarm { +class MeterChannel : public Channel, public Alarm { const uint32_t maxVal = 838860700; uint64_t counterSum; @@ -268,14 +263,14 @@ public: } uint8_t flags () const { - return hal.battery.low() ? 0x80 : 0x00; + return device().battery().low() ? 0x80 : 0x00; } void next () { // only count rotations/flashes and calculate real value when sending, to prevent inaccuracy counter++; - hal.led.ledOn(millis2ticks(300)); + device().led().ledOn(millis2ticks(300)); #ifndef NDEBUG DHEXLN(counter); @@ -333,7 +328,9 @@ public: } }; -typedef MultiChannelDevice MeterType; +typedef MultiChannelDevice MeterType; + +HalType hal; MeterType sdev(0x20); template @@ -369,7 +366,7 @@ public: sysclock.add(*this); } - virtual void trigger (AlarmClock& clock) { + virtual void trigger (__attribute__ ((unused)) AlarmClock& clock) { attach(); } }; @@ -388,26 +385,10 @@ ConfigButton cfgBtn(sdev); void setup () { DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER); - - if( storage.setup(sdev.checksum()) == true ) { - sdev.firstinit(); - } + sdev.init(hal); buttonISR(cfgBtn,CONFIG_BUTTON_PIN); -#ifdef USE_OTA_BOOTLOADER - sdev.init(hal,OTA_HMID_START,OTA_SERIAL_START); - sdev.setModel(OTA_MODEL_START); -#else - sdev.init(hal,DEVICE_ID,DEVICE_SERIAL); - sdev.setModel(0x00,0xde); -#endif - sdev.setFirmwareVersion(0x11); - sdev.setSubType(DeviceType::PowerMeter); - sdev.setInfo(0x03,0x01,0x00); - - hal.init(); - // set low voltage to 2.2V // measure battery every 1h hal.battery.init(seconds2ticks(60UL*60),sysclock); diff --git a/examples/HM-LC-Dim1PWM-CV/HM-LC-Dim1PWM-CV.ino b/examples/HM-LC-Dim1PWM-CV/HM-LC-Dim1PWM-CV.ino index 25789af0..ae926305 100644 --- a/examples/HM-LC-Dim1PWM-CV/HM-LC-Dim1PWM-CV.ino +++ b/examples/HM-LC-Dim1PWM-CV/HM-LC-Dim1PWM-CV.ino @@ -3,34 +3,24 @@ // 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //- ----------------------------------------------------------------------------------------------------------------------- -/* - * Setup defines to configure the library. - */ -// #define USE_AES -// #define HM_DEF_KEY 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10 -// #define HM_DEF_KEY_INDEX 0 +// define this to read the device id, serial and device type from bootloader section +// #define USE_OTA_BOOTLOADER + +// define all device properties +#define DEVICE_ID HMID(0x11,0x12,0x22) +#define DEVICE_SERIAL "papa111222" +#define DEVICE_MODEL 0x00,0x67 +#define DEVICE_FIRMWARE 0x25 +#define DEVICE_TYPE DeviceType::Dimmer +#define DEVICE_INFO 0x41,0x01,0x00 #include -// #include // when we include SPI.h - we can use LibSPI class #include #include #include #include -// define this to read the device id, serial and device type from bootloader section -// #define USE_OTA_BOOTLOADER - -#ifdef USE_OTA_BOOTLOADER - #define OTA_MODEL_START 0x7ff0 // start address of 2 byte model id in bootloader - #define OTA_SERIAL_START 0x7ff2 // start address of 10 byte serial number in bootloader - #define OTA_HMID_START 0x7ffc // start address of 3 byte device id in bootloader -#else - // device ID - #define DEVICE_ID HMID(0x11,0x12,0x22) - // serial number - #define DEVICE_SERIAL "papa111222" -#endif // we use a Pro Mini // Arduino pin for the LED @@ -53,40 +43,21 @@ using namespace as; /** * Configure the used hardware */ -typedef AvrSPI<10,11,12,13> RadioSPI; -typedef AskSin,NoBattery,Radio > Hal; -Hal hal; - -// setup the device with channel type and number of channels -typedef DimmerDevice,3> DimmerType; +typedef AvrSPI<10,11,12,13> SPIType; +typedef Radio RadioType; +typedef StatusLed LedType; +typedef AskSin HalType; +typedef DimmerChannel ChannelType; +typedef DimmerDevice DimmerType; + +HalType hal; DimmerType sdev(0x20); - ConfigToggleButton cfgBtn(sdev); void setup () { DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER); - sdev.dumpSize(); - - // first initialize EEProm if needed - if( storage.setup(sdev.checksum()) == true ) { - sdev.firstinit(); - } - + sdev.init(hal,DIMMER_PIN); buttonISR(cfgBtn,CONFIG_BUTTON_PIN); - -#ifdef USE_OTA_BOOTLOADER - sdev.init(hal,OTA_HMID_START,OTA_SERIAL_START); - sdev.setModel(OTA_MODEL_START); -#else - sdev.init(hal,DEVICE_ID,DEVICE_SERIAL); - sdev.setModel(0x00,0x67); -#endif - sdev.setFirmwareVersion(0x25); - sdev.setSubType(DeviceType::Dimmer); - sdev.setInfo(0x41,0x01,0x00); - - hal.init(); - sdev.initPwm(DIMMER_PIN); // TODO - random delay } diff --git a/examples/HM-LC-SWX-SM/HM-LC-SWX-SM.ino b/examples/HM-LC-SWX-SM/HM-LC-SWX-SM.ino index f1b50d85..7b6336c0 100644 --- a/examples/HM-LC-SWX-SM/HM-LC-SWX-SM.ino +++ b/examples/HM-LC-SWX-SM/HM-LC-SWX-SM.ino @@ -3,15 +3,22 @@ // 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //- ----------------------------------------------------------------------------------------------------------------------- -/* - * Setup defines to configure the library. - */ -// #define USE_AES -// #define HM_DEF_KEY 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10 -// #define HM_DEF_KEY_INDEX 0 +// define this to read the device id, serial and device type from bootloader section +// #define USE_OTA_BOOTLOADER + +// number of relays - possible values 1,2,4 +// will map to HM-LC-SW1-SM, HM-LC-SW2-SM, HM-LC-SW4-SM +#define RELAY_COUNT 4 + +// define all device properties +#define DEVICE_ID HMID(0x12,0x34,0x56) +#define DEVICE_SERIAL "papa000000" +#define DEVICE_MODEL 0x00,(REALY_COUNT==2 ? 0x0a : (REALY_COUNT==4 ? 0x03 : 0x02)) +#define DEVICE_FIRMWARE 0x16 +#define DEVICE_TYPE DeviceType::Switch +#define DEVICE_INFO 0x41,0x01,0x00 #include -// #include // when we include SPI.h - we can use LibSPI class #include #include #include @@ -19,31 +26,6 @@ #include #include -// number of relays - possible values 1,2,4 -// will map to HM-LC-SW1-SM, HM-LC-SW2-SM, HM-LC-SW4-SM -#define RELAY_COUNT 4 - -// define this to read the device id, serial and device type from bootloader section -// #define USE_OTA_BOOTLOADER - -#ifdef USE_OTA_BOOTLOADER - #define OTA_MODEL_START 0x7ff0 // start address of 2 byte model id in bootloader - #define OTA_SERIAL_START 0x7ff2 // start address of 10 byte serial number in bootloader - #define OTA_HMID_START 0x7ffc // start address of 3 byte device id in bootloader -#else - // define model is matching the number of relays - #if RELAY_COUNT == 2 - #define SW_MODEL 0x0a - #elif RELAY_COUNT == 4 - #define SW_MODEL 0x03 - #else - #define SW_MODEL 0x02 - #endif - // device ID - #define DEVICE_ID HMID(0x12,0x34,0x56) - // serial number - #define DEVICE_SERIAL "papa000000" -#endif // we use a Pro Mini // Arduino pin for the LED @@ -71,9 +53,14 @@ using namespace as; * Configure the used hardware */ typedef AvrSPI<10,11,12,13> RadioSPI; -//typedef LibSPI<10> RadioSPI; typedef AskSin,NoBattery,Radio > Hal; + +// setup the device with channel type and number of channels +typedef MultiChannelDevice,RELAY_COUNT> SwitchType; + Hal hal; +SwitchType sdev(0x20); +ConfigToggleButton cfgBtn(sdev); // map number of channel to pin // this will be called by the SwitchChannel class @@ -86,12 +73,6 @@ uint8_t SwitchPin (uint8_t number) { return RELAY1_PIN; } -// setup the device with channel type and number of channels -typedef MultiChannelDevice,RELAY_COUNT> SwitchType; -SwitchType sdev(0x20); - -ConfigToggleButton cfgBtn(sdev); - // if A0 and A1 connected // we use LOW for ON and HIGH for OFF bool checkLowActive () { @@ -104,11 +85,7 @@ bool checkLowActive () { void setup () { DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER); - - // first initialize EEProm if needed - if( storage.setup(sdev.checksum()) == true ) { - sdev.firstinit(); - } + sdev.init(hal); bool low = checkLowActive(); for( uint8_t i=1; i<=sdev.channels(); ++i ) { @@ -117,31 +94,19 @@ void setup () { buttonISR(cfgBtn,CONFIG_BUTTON_PIN); -#ifdef USE_OTA_BOOTLOADER - sdev.init(hal,OTA_HMID_START,OTA_SERIAL_START); - sdev.setModel(OTA_MODEL_START); - if( sdev.getModel()[1] == 0x02 ) { + uint8_t model[2]; + sdev.getDeviceModel(model); + if( model[1] == 0x02 ) { sdev.channels(1); DPRINTLN(F("HM-LC-SW1-SM")); } - else if( sdev.getModel()[1] == 0x0a ) { + else if( model[1] == 0x0a ) { sdev.channels(2); DPRINTLN(F("HM-LC-SW2-SM")); } else { DPRINTLN(F("HM-LC-SW4-SM")); } -#else - sdev.init(hal,DEVICE_ID,DEVICE_SERIAL); - sdev.setModel(0x00,SW_MODEL); -#endif - sdev.setFirmwareVersion(0x16); - sdev.setSubType(DeviceType::Switch); - sdev.setInfo(0x41,0x01,0x00); - - hal.init(); - - // TODO - random delay } void loop() { diff --git a/examples/HM-RC-4/HM-RC-4.ino b/examples/HM-RC-4/HM-RC-4.ino index 942d9959..68837aa8 100644 --- a/examples/HM-RC-4/HM-RC-4.ino +++ b/examples/HM-RC-4/HM-RC-4.ino @@ -3,12 +3,16 @@ // 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //- ----------------------------------------------------------------------------------------------------------------------- -/* - * Setup defines to configure the library. - */ -// #define USE_AES -// #define HM_DEF_KEY 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10 -// #define HM_DEF_KEY_INDEX 0 +// define this to read the device id, serial and device type from bootloader section +// #define USE_OTA_BOOTLOADER + +// define all device properties +#define DEVICE_ID HMID(0x78,0x90,0x12) +#define DEVICE_SERIAL "papa333333" +#define DEVICE_MODEL 0x00,0x08 +#define DEVICE_FIRMWARE 0x11 +#define DEVICE_TYPE DeviceType::Remote +#define DEVICE_INFO 0x04,0x00,0x00 #include #include // after including SPI Library - we can use LibSPI class @@ -19,19 +23,6 @@ #include #include -// define this to read the device id, serial and device type from bootloader section -// #define USE_OTA_BOOTLOADER - -#ifdef USE_OTA_BOOTLOADER - #define OTA_MODEL_START 0x7ff0 // start address of 2 byte model id in bootloader - #define OTA_SERIAL_START 0x7ff2 // start address of 10 byte serial number in bootloader - #define OTA_HMID_START 0x7ffc // start address of 3 byte device id in bootloader -#else - // device ID - #define DEVICE_ID HMID(0x78,0x90,0x12) - // serial number - #define DEVICE_SERIAL "papa333333" -#endif // we use a Pro Mini // Arduino pin for the LED @@ -58,15 +49,17 @@ using namespace as; /** * Configure the used hardware */ -// typedef AvrSPI<10,11,12,13> RadioSPI; -typedef LibSPI<10> RadioSPI; -typedef AskSin,BatterySensor<22,19>,Radio > HalBase; -class Hal : public HalBase { +typedef LibSPI<10> SPIType; +typedef Radio RadioType; +typedef DualStatusLed<5,4> LedType; +typedef BatterySensor<22,19> BatteryType; +typedef AskSin HalType; +class Hal : public HalType { // extra clock to count button press events AlarmClock btncounter; public: void init () { - HalBase::init(); + HalType::init(); // get new battery value after 50 key press battery.init(50,btncounter); } @@ -76,23 +69,20 @@ public: } bool runready () { - return HalBase::runready() || btncounter.runready(); + return HalType::runready() || btncounter.runready(); } -} hal; +}; +typedef RemoteChannel ChannelType; +typedef MultiChannelDevice RemoteType; -typedef MultiChannelDevice,4> RemoteType; +Hal hal; RemoteType sdev(0x20); - ConfigButton cfgBtn(sdev); void setup () { DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER); - sdev.dumpSize(); - - if( storage.setup(sdev.checksum()) == true ) { - sdev.firstinit(); - } + sdev.init(hal); remoteISR(sdev,1,BTN1_PIN); remoteISR(sdev,2,BTN2_PIN); @@ -100,19 +90,6 @@ void setup () { remoteISR(sdev,4,BTN4_PIN); buttonISR(cfgBtn,CONFIG_BUTTON_PIN); - -#ifdef USE_OTA_BOOTLOADER - sdev.init(hal,OTA_HMID_START,OTA_SERIAL_START); - sdev.setModel(OTA_MODEL_START); -#else - sdev.init(hal,DEVICE_ID,DEVICE_SERIAL); - sdev.setModel(0x00,0x08); -#endif - sdev.setFirmwareVersion(0x11); - sdev.setSubType(DeviceType::Remote); - sdev.setInfo(0x04,0x00,0x00); - - hal.init(); } void loop() { diff --git a/examples/HM-SEC-MDIR/HM-SEC-MDIR.ino b/examples/HM-SEC-MDIR/HM-SEC-MDIR.ino index 71125b9e..af61496c 100644 --- a/examples/HM-SEC-MDIR/HM-SEC-MDIR.ino +++ b/examples/HM-SEC-MDIR/HM-SEC-MDIR.ino @@ -3,12 +3,16 @@ // 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //- ----------------------------------------------------------------------------------------------------------------------- -/* - * Setup defines to configure the library. - */ -// #define USE_AES -// #define HM_DEF_KEY 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10 -// #define HM_DEF_KEY_INDEX 0 +// define this to read the device id, serial and device type from bootloader section +// #define USE_OTA_BOOTLOADER + +// define all device properties +#define DEVICE_ID HMID(0x56,0x78,0x90) +#define DEVICE_SERIAL "papa222222" +#define DEVICE_MODEL 0x00,0x4a +#define DEVICE_FIRMWARE 0x16 +#define DEVICE_TYPE DeviceType::MotionDetector +#define DEVICE_INFO 0x01,0x01,0x00 #include #include @@ -21,19 +25,6 @@ #include #include -// define this to read the device id, serial and device type from bootloader section -// #define USE_OTA_BOOTLOADER - -#ifdef USE_OTA_BOOTLOADER - #define OTA_MODEL_START 0x7ff0 // start address of 2 byte model id in bootloader - #define OTA_SERIAL_START 0x7ff2 // start address of 10 byte serial number in bootloader - #define OTA_HMID_START 0x7ffc // start address of 3 byte device id in bootloader -#else - // device ID - #define DEVICE_ID HMID(0x56,0x78,0x90) - // serial number - #define DEVICE_SERIAL "papa222222" -#endif // we use a Pro Mini // Arduino pin for the LED @@ -55,8 +46,11 @@ using namespace as; /** * Configure the used hardware */ -typedef AvrSPI<10,11,12,13> RadioSPI; -typedef AskSin,BatterySensor<22,19>,Radio > BaseHal; +typedef AvrSPI<10,11,12,13> SPIType; +typedef Radio RadioType; +typedef StatusLed<4> LedType; +typedef BatterySensor<22,19> BatteryType; +typedef AskSin BaseHal; class Hal : public BaseHal { public: void init () { @@ -156,7 +150,7 @@ class MotionChannel : public Channel cfgBtn(sdev); void setup () { DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER); - - if( storage.setup(sdev.checksum()) == true ) { - sdev.firstinit(); - } + sdev.init(hal); light.begin(); // If gain = false (0), device is set to low gain (1X) @@ -301,20 +292,6 @@ void setup () { light.setPowerUp(); buttonISR(cfgBtn,CONFIG_BUTTON_PIN); - -#ifdef USE_OTA_BOOTLOADER - sdev.init(hal,OTA_HMID_START,OTA_SERIAL_START); - sdev.setModel(OTA_MODEL_START); -#else - sdev.init(hal,DEVICE_ID,DEVICE_SERIAL); - sdev.setModel(0x00,0x4a); -#endif - sdev.setFirmwareVersion(0x16); - // TODO check sub type and infos - sdev.setSubType(DeviceType::MotionDetector); - sdev.setInfo(0x01,0x01,0x00); - - hal.init(); } void loop() { diff --git a/examples/HM-WDS10-TH-O/HM-WDS10-TH-O.ino b/examples/HM-WDS10-TH-O/HM-WDS10-TH-O.ino index 75567ae2..f48a94a0 100644 --- a/examples/HM-WDS10-TH-O/HM-WDS10-TH-O.ino +++ b/examples/HM-WDS10-TH-O/HM-WDS10-TH-O.ino @@ -3,12 +3,16 @@ // 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //- ----------------------------------------------------------------------------------------------------------------------- -/* - * Setup defines to configure the library. - */ -// #define USE_AES -// #define HM_DEF_KEY 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10 -// #define HM_DEF_KEY_INDEX 0 +// define this to read the device id, serial and device type from bootloader section +// #define USE_OTA_BOOTLOADER + +// define all device properties +#define DEVICE_ID HMID(0x34,0x56,0x78) +#define DEVICE_SERIAL "papa111111" +#define DEVICE_MODEL 0x00,0x3d +#define DEVICE_FIRMWARE 0x10 +#define DEVICE_TYPE DeviceType::THSensor +#define DEVICE_INFO 0x03,0x01,0x00 #include #include @@ -17,19 +21,6 @@ #include -// define this to read the device id, serial and device type from bootloader section -// #define USE_OTA_BOOTLOADER - -#ifdef USE_OTA_BOOTLOADER - #define OTA_MODEL_START 0x7ff0 // start address of 2 byte model id in bootloader - #define OTA_SERIAL_START 0x7ff2 // start address of 10 byte serial number in bootloader - #define OTA_HMID_START 0x7ffc // start address of 3 byte device id in bootloader -#else - // device ID - #define DEVICE_ID HMID(0x34,0x56,0x78) - // serial number - #define DEVICE_SERIAL "papa111111" -#endif // we use a Pro Mini // Arduino pin for the LED @@ -50,13 +41,15 @@ using namespace as; /** * Configure the used hardware */ -typedef AvrSPI<10,11,12,13> RadioSPI; -typedef AskSin,BatterySensor<22,19>,Radio > BaseHal; +typedef AvrSPI<10,11,12,13> SPIType; +typedef Radio RadioType; +typedef StatusLed<4> LedType; +typedef BatterySensor<22,19> BatteryType; +typedef AskSin BaseHal; class Hal : public BaseHal { public: void init () { BaseHal::init(); - // set low voltage to 2.2V // measure battery every 1h battery.init(seconds2ticks(60UL*60),sysclock); } @@ -136,25 +129,8 @@ ConfigButton cfgBtn(sdev); void setup () { DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER); - - if( storage.setup(sdev.checksum()) == true ) { - sdev.firstinit(); - } - + sdev.init(hal); buttonISR(cfgBtn,CONFIG_BUTTON_PIN); - -#ifdef USE_OTA_BOOTLOADER - sdev.init(hal,OTA_HMID_START,OTA_SERIAL_START); - sdev.setModel(OTA_MODEL_START); -#else - sdev.init(hal,DEVICE_ID,DEVICE_SERIAL); - sdev.setModel(0x00,0x3d); -#endif - sdev.setFirmwareVersion(0x10); - sdev.setSubType(DeviceType::THSensor); - sdev.setInfo(0x03,0x01,0x00); - - hal.init(); } void loop() { diff --git a/examples/HM-WDS100-C6-O-2/HM-WDS100-C6-O-2.ino b/examples/HM-WDS100-C6-O-2/HM-WDS100-C6-O-2.ino index a8702052..5ac1d65a 100644 --- a/examples/HM-WDS100-C6-O-2/HM-WDS100-C6-O-2.ino +++ b/examples/HM-WDS100-C6-O-2/HM-WDS100-C6-O-2.ino @@ -3,12 +3,16 @@ // 2016-10-31 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //- ----------------------------------------------------------------------------------------------------------------------- -/* - * Setup defines to configure the library. - */ -// #define USE_AES -// #define HM_DEF_KEY 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10 -// #define HM_DEF_KEY_INDEX 0 +// define this to read the device id, serial and device type from bootloader section +// #define USE_OTA_BOOTLOADER + +// define all device properties +#define DEVICE_ID HMID(0x09,0x21,0x43) +#define DEVICE_SERIAL "papa666666" +#define DEVICE_MODEL 0x00,0xae +#define DEVICE_FIRMWARE 0x11 +#define DEVICE_TYPE DeviceType::THSensor +#define DEVICE_INFO 0x03,0x01,0x00 #include #include @@ -16,21 +20,7 @@ #include #include -#include - -// define this to read the device id, serial and device type from bootloader section -// #define USE_OTA_BOOTLOADER -#ifdef USE_OTA_BOOTLOADER - #define OTA_MODEL_START 0x7ff0 // start address of 2 byte model id in bootloader - #define OTA_SERIAL_START 0x7ff2 // start address of 10 byte serial number in bootloader - #define OTA_HMID_START 0x7ffc // start address of 3 byte device id in bootloader -#else - // device ID - #define DEVICE_ID HMID(0x09,0x21,0x43) - // serial number - #define DEVICE_SERIAL "papa666666" -#endif // we use a Pro Mini // Arduino pin for the LED @@ -49,13 +39,15 @@ using namespace as; /** * Configure the used hardware */ -typedef AvrSPI<10,11,12,13> RadioSPI; -typedef AskSin,BatterySensor<22,19>,Radio > BaseHal; +typedef AvrSPI<10,11,12,13> SPIType; +typedef Radio RadioType; +typedef StatusLed<4> LedType; +typedef BatterySensor<22,19> BatteryType; +typedef AskSin BaseHal; class Hal : public BaseHal { public: void init () { BaseHal::init(); - // set low voltage to 2.2V // measure battery every 1h battery.init(seconds2ticks(60UL*60),sysclock); } @@ -92,9 +84,11 @@ class Wds100List0 : public ChannelList { public: Wds100List0(uint16_t a) : ChannelList(a) {} + operator List0& () const { return *(List0*)this; } + // from List0 - HMID masterid () { return HMID(getByte(1),getByte(2),getByte(3)); } - void masterid (const HMID& mid) { setByte(1,mid.id0()); setByte(2,mid.id1()); setByte(3,mid.id2()); }; + HMID masterid () { return ((List0*)this)->masterid(); } + void masterid (const HMID& mid) { ((List0*)this)->masterid(mid); } bool liveModeRx () const { return getByte(sizeof(List0Data) + 0); } bool liveModeRx (bool value) const { return setByte(sizeof(List0Data) + 0,value); } @@ -219,27 +213,8 @@ ConfigButton cfgBtn(sdev); void setup () { DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER); - - if( storage.setup(sdev.checksum()) == true ) { - sdev.firstinit(); - } - + sdev.init(hal); buttonISR(cfgBtn,CONFIG_BUTTON_PIN); - -#ifdef USE_OTA_BOOTLOADER - sdev.init(hal,OTA_HMID_START,OTA_SERIAL_START); - sdev.setModel(OTA_MODEL_START); -#else - sdev.init(hal,DEVICE_ID,DEVICE_SERIAL); - sdev.setModel(0x00,0xae); -#endif - // TODO check with version to use - sdev.setFirmwareVersion(0x11); - sdev.setSubType(DeviceType::THSensor); - sdev.setInfo(0x03,0x01,0x00); - - hal.init(); - // add channel 1 to timer to send event sysclock.add(sdev.channel(1)); } diff --git a/examples/stm32/HM-LC-SWX-SM/HM-LC-SWX-SM.ino b/examples/stm32/HM-LC-SWX-SM/HM-LC-SWX-SM.ino index b9546c08..1916f5a7 100644 --- a/examples/stm32/HM-LC-SWX-SM/HM-LC-SWX-SM.ino +++ b/examples/stm32/HM-LC-SWX-SM/HM-LC-SWX-SM.ino @@ -3,12 +3,21 @@ // 2017-03-20 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/ //- ----------------------------------------------------------------------------------------------------------------------- -/* - * Setup defines to configure the library. - */ -// #define USE_AES -// #define HM_DEF_KEY 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x10 -// #define HM_DEF_KEY_INDEX 0 +// define this to read the device id, serial and device type from bootloader section +// #define USE_OTA_BOOTLOADER + +// number of relays - possible values 1,2,4 +// will map to HM-LC-SW1-SM, HM-LC-SW2-SM, HM-LC-SW4-SM +#define RELAY_COUNT 4 + +// define all device properties +#define DEVICE_ID HMID(0x12,0x34,0x56) +#define DEVICE_SERIAL "papa000000" +//#define DEVICE_MODEL 0x00,(REALY_COUNT==2 ? 0x0a : (REALY_COUNT==4 ? 0x03 : 0x02)) +#define DEVICE_MODEL 0x00,0x03 +#define DEVICE_FIRMWARE 0x16 +#define DEVICE_TYPE DeviceType::Switch +#define DEVICE_INFO 0x41,0x01,0x00 #include // when we include SPI.h - we can use LibSPI class #include // the EEPROM library contains Flash Access Methods @@ -17,22 +26,6 @@ #include #include -// number of relays - possible values 1,2,4 -// will map to HM-LC-SW1-SM, HM-LC-SW2-SM, HM-LC-SW4-SM -#define RELAY_COUNT 4 - -// define model is matching the number of relays -#if RELAY_COUNT == 2 - #define SW_MODEL 0x0a -#elif RELAY_COUNT == 4 - #define SW_MODEL 0x03 -#else - #define SW_MODEL 0x02 -#endif -// device ID -#define DEVICE_ID HMID(0x12,0x34,0x56) -// serial number -#define DEVICE_SERIAL "papa000000" // use builtin led #define LED_PIN LED_BUILTIN @@ -79,12 +72,8 @@ ConfigToggleButton cfgBtn(sdev); void setup () { DINIT(57600,ASKSIN_PLUS_PLUS_IDENTIFIER); + sdev.init(hal); - // first initialize EEProm if needed - if( storage.setup(sdev.checksum()) == true ) { - sdev.firstinit(); - storage.store(); - } // this will also trigger powerUpAction handling for( uint8_t i=1; i<=sdev.channels(); ++i ) { sdev.channel(i).lowactive(false); @@ -92,15 +81,6 @@ void setup () { buttonISR(cfgBtn,CONFIG_BUTTON_PIN); - sdev.init(hal,DEVICE_ID,DEVICE_SERIAL); - sdev.setModel(0x00,SW_MODEL); - - sdev.setFirmwareVersion(0x16); - sdev.setSubType(DeviceType::Switch); - sdev.setInfo(0x41,0x01,0x00); - - hal.init(); - // TODO - random delay }