From a80944d9a2be6ef5b8d22ee45b865b0f10d898ad Mon Sep 17 00:00:00 2001 From: Christopher Tremblay Date: Mon, 26 Oct 2020 00:09:44 -0700 Subject: [PATCH 1/2] Cleanup the EZO devices Fixed a few mistakes in the comments and organized the code to make it easier to read & extend. --- tasmota/my_user_config.h | 12 +-- tasmota/tasmota_configurations.h | 2 +- tasmota/xsns_78_ezo.ino | 4 +- tasmota/xsns_78_ezoco2.ino | 4 + tasmota/xsns_78_ezoec.ino | 10 ++- tasmota/xsns_78_ezohum.ino | 4 + tasmota/xsns_78_ezoorp.ino | 8 +- tasmota/xsns_78_ezoph.ino | 8 +- tasmota/xsns_78_ezortd.ino | 8 +- tasmota/xsns_78_xezo.ino | 125 ++++++++----------------------- 10 files changed, 75 insertions(+), 110 deletions(-) diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 34b479e0e..74fb012be 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -562,12 +562,12 @@ // #define USE_MCP9808 // [I2cDriver51] Enable MCP9808 temperature sensor (I2C addresses 0x18 - 0x1F) (+0k9 code) // #define USE_HP303B // [I2cDriver52] Enable HP303B temperature and pressure sensor (I2C address 0x76 or 0x77) (+6k2 code) // #define USE_MLX90640 // [I2cDriver53] Enable MLX90640 IR array temperature sensor (I2C address 0x33) (+20k code) -// #define USE_EZOPH // [I2cDriver55] Enable support for EZO's pH sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) -// #define USE_EZOORP // [I2cDriver55] Enable support for EZO's ORP sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) -// #define USE_EZORTD // [I2cDriver55] Enable support for EZO's RTD sensor (+0k2 code) - Shared EZO code required for any EZO device (+1k2 code) -// #define USE_EZOHUM // [I2cDriver55] Enable support for EZO's HUM sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) -// #define USE_EZOEC // [I2cDriver55] Enable support for EZO's EC sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) -// #define USE_EZOCO2 // [I2cDriver55] Enable support for EZO's CO2 sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) + #define USE_EZOPH // [I2cDriver55] Enable support for EZO's pH sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) + #define USE_EZOORP // [I2cDriver55] Enable support for EZO's ORP sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) + #define USE_EZORTD // [I2cDriver55] Enable support for EZO's RTD sensor (+0k2 code) - Shared EZO code required for any EZO device (+1k2 code) + #define USE_EZOHUM // [I2cDriver55] Enable support for EZO's HUM sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) + #define USE_EZOEC // [I2cDriver55] Enable support for EZO's EC sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) + #define USE_EZOCO2 // [I2cDriver55] Enable support for EZO's CO2 sensor (+0k2 code) - Shared EZO code required for any EZO device (+1k2 code) // #define USE_DISPLAY // Add I2C Display Support (+2k code) #define USE_DISPLAY_MODES1TO5 // Enable display mode 1 to 5 in addition to mode 0 diff --git a/tasmota/tasmota_configurations.h b/tasmota/tasmota_configurations.h index 95fdcbea0..ed425a645 100644 --- a/tasmota/tasmota_configurations.h +++ b/tasmota/tasmota_configurations.h @@ -132,7 +132,7 @@ //#define USE_EZORTD // [I2cDriver55] Enable support for EZO's RTD sensor (+0k2 code) - Shared EZO code required for any EZO device (+1k2 code) //#define USE_EZOHUM // [I2cDriver55] Enable support for EZO's HUM sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) //#define USE_EZOEC // [I2cDriver55] Enable support for EZO's EC sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) -//#define USE_EZOCO2 // [I2cDriver55] Enable support for EZO's CO2 sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) +//#define USE_EZOCO2 // [I2cDriver55] Enable support for EZO's CO2 sensor (+0k2 code) - Shared EZO code required for any EZO device (+1k2 code) #define USE_MHZ19 // Add support for MH-Z19 CO2 sensor (+2k code) #define USE_SENSEAIR // Add support for SenseAir K30, K70 and S8 CO2 sensor (+2k3 code) diff --git a/tasmota/xsns_78_ezo.ino b/tasmota/xsns_78_ezo.ino index 783d86538..36fcea423 100644 --- a/tasmota/xsns_78_ezo.ino +++ b/tasmota/xsns_78_ezo.ino @@ -80,6 +80,8 @@ struct EZOStruct { virtual void ProcessMeasurement(void); virtual void Show(bool json, const char *name); + static const char id[] PROGMEM; + protected: void ProcessMeasurement(char *const data, const uint32_t len, const uint32_t latency) { @@ -108,7 +110,7 @@ protected: uint32_t lastRead; }; - +const char EZOStruct::id[] PROGMEM = ""; #endif // USE_EZO #endif // USE_I2C diff --git a/tasmota/xsns_78_ezoco2.ino b/tasmota/xsns_78_ezoco2.ino index df23a05cf..f9f09e1c4 100644 --- a/tasmota/xsns_78_ezoco2.ino +++ b/tasmota/xsns_78_ezoco2.ino @@ -49,9 +49,13 @@ struct EZOCO2 : public EZOStruct { } } + static const char id[] PROGMEM; + private: uint16_t CO2; }; +const char EZOCO2::id[] PROGMEM = "CO2"; + #endif // USE_EZOCO2 #endif // USE_I2C diff --git a/tasmota/xsns_78_ezoec.ino b/tasmota/xsns_78_ezoec.ino index 8d5a22912..1566e2907 100644 --- a/tasmota/xsns_78_ezoec.ino +++ b/tasmota/xsns_78_ezoec.ino @@ -1,5 +1,5 @@ /* - xsns_78_ezoph.ino - EZO EC I2C EC sensor support for Tasmota + xsns_78_ezoec.ino - EZO EC I2C EC sensor support for Tasmota Copyright (C) 2020 Christopher Tremblay @@ -35,8 +35,8 @@ struct EZOEC : public EZOStruct { virtual void Show(bool json, const char *name) { - char str[6]; - dtostrfd(EC, 2, str); + char str[10]; + dtostrfd(EC, 3, str); if (json) { ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_EC "\":%s}" ), name, str); @@ -48,9 +48,13 @@ struct EZOEC : public EZOStruct { } } + static const char id[] PROGMEM; + private: float EC; }; +const char EZOEC::id[] PROGMEM = "EC"; + #endif // USE_EZOEC #endif // USE_I2C diff --git a/tasmota/xsns_78_ezohum.ino b/tasmota/xsns_78_ezohum.ino index 1eadb767d..62b6edc68 100644 --- a/tasmota/xsns_78_ezohum.ino +++ b/tasmota/xsns_78_ezohum.ino @@ -52,10 +52,14 @@ struct EZOHUM : public EZOStruct { } } + static const char id[] PROGMEM; + private: float humidity; float temperature; }; +const char EZOHUM::id[] PROGMEM = "HUM"; + #endif // USE_EZOHUM #endif // USE_I2C diff --git a/tasmota/xsns_78_ezoorp.ino b/tasmota/xsns_78_ezoorp.ino index 187245f35..d9faa999c 100644 --- a/tasmota/xsns_78_ezoorp.ino +++ b/tasmota/xsns_78_ezoorp.ino @@ -35,8 +35,8 @@ struct EZOORP : public EZOStruct { virtual void Show(bool json, const char *name) { - char str[6]; - dtostrfd(ORP, 0, str); + char str[8]; + dtostrfd(ORP, 2, str); if (json) { ResponseAppend_P(PSTR(",\"%s\":{\"" D_JSON_ORP "\":%s}" ), name, str); @@ -48,11 +48,13 @@ struct EZOORP : public EZOStruct { } } + static const char id[] PROGMEM; + private: float ORP; }; - +const char EZOORP::id[] PROGMEM = "ORP"; #endif // USE_EZOORP #endif // USE_I2C diff --git a/tasmota/xsns_78_ezoph.ino b/tasmota/xsns_78_ezoph.ino index 522f93eea..77939d97a 100644 --- a/tasmota/xsns_78_ezoph.ino +++ b/tasmota/xsns_78_ezoph.ino @@ -22,8 +22,8 @@ #define EZO_PH_READ_LATENCY 900 -struct EZOpH : public EZOStruct { - EZOpH(uint32_t addr) : EZOStruct(addr), pH(NAN) {} +struct EZOPH : public EZOStruct { + EZOPH(uint32_t addr) : EZOStruct(addr), pH(NAN) {} virtual void ProcessMeasurement(void) { @@ -48,9 +48,13 @@ struct EZOpH : public EZOStruct { } } + static const char id[] PROGMEM; + private: float pH; }; +const char EZOPH::id[] PROGMEM = "pH"; + #endif // USE_EZOPH #endif // USE_I2C diff --git a/tasmota/xsns_78_ezortd.ino b/tasmota/xsns_78_ezortd.ino index da5869439..1d564d095 100644 --- a/tasmota/xsns_78_ezortd.ino +++ b/tasmota/xsns_78_ezortd.ino @@ -34,7 +34,7 @@ struct EZORTD : public EZOStruct { } virtual void Show(bool json, const char *name) -{ + { char str[10]; dtostrfd(ConvertTemp(temperature), Settings.flag2.temperature_resolution, str); @@ -46,11 +46,15 @@ struct EZORTD : public EZOStruct { WSContentSend_PD(HTTP_SNS_TEMP, name, str, TempUnit()); #endif // USE_WEBSERVER } -} + } + + static const char id[] PROGMEM; private: float temperature; }; +const char EZORTD::id[] PROGMEM = "RTD"; + #endif // USE_EZORTD #endif // USE_I2C diff --git a/tasmota/xsns_78_xezo.ino b/tasmota/xsns_78_xezo.ino index beb8a2288..0415c335d 100644 --- a/tasmota/xsns_78_xezo.ino +++ b/tasmota/xsns_78_xezo.ino @@ -28,7 +28,6 @@ // List of known EZO devices and their default address - enum { EZO_DO = 0x61, // D.O. EZO_ORP = 0x62, // ORP @@ -49,83 +48,37 @@ enum { }; -const char EZO_EMPTY[] PROGMEM = ""; -//const char EZO_DO_NAME[] PROGMEM = "DO"; -#ifdef USE_EZOORP -const char EZO_ORP_NAME[] PROGMEM = "ORP"; -#endif -#ifdef USE_EZOPH -const char EZO_PH_NAME[] PROGMEM = "pH"; -#endif -#ifdef USE_EZOEC -const char EZO_EC_NAME[] PROGMEM = "EC"; -#endif -#ifdef USE_EZORTD -const char EZO_RTD_NAME[] PROGMEM = "RTD"; -#endif -//const char EZO_PMP_NAME[] PROGMEM = "PMP"; -//const char EZO_FLO_NAME[] PROGMEM = "FLO"; -const char EZO_CO2_NAME[] PROGMEM = "CO2"; -//const char EZO_PRS_NAME[] PROGMEM = "PRS"; -//const char EZO_O2_NAME[] PROGMEM = "O2"; -#ifdef USE_EZOHUM -const char EZO_HUM_NAME[] PROGMEM = "HUM"; -#endif -//const char EZO_RGB_NAME[] PROGMEM = "RGB"; +// Creates a complex preprocessor macro to fetch a specified class OR EZOStruct if it wasn't defined +template struct IsComplete : std::false_type {}; +template struct IsComplete< T, decltype(void(sizeof(T))) > : std::true_type {}; + +#define GET_EZO_CLASS(CLASS) std::conditional::value, CLASS, EZOStruct>::type + +// The order of the EZO devices must map with the enum declared above const char *const EZOSupport[EZO_ADDR_n] PROGMEM = { - EZO_EMPTY, - -#ifdef USE_EZOORP - EZO_ORP_NAME, -#else - EZO_EMPTY, -#endif - -#ifdef USE_EZOPH - EZO_PH_NAME, -#else - EZO_EMPTY, -#endif - -#ifdef USE_EZOEC - EZO_EC_NAME, -#else - EZO_EMPTY, -#endif - - EZO_EMPTY, - -#ifdef USE_EZORTD - EZO_RTD_NAME, -#else - EZO_EMPTY, -#endif - - EZO_EMPTY, - EZO_EMPTY, - -#ifdef USE_EZOCO2 - EZO_CO2_NAME, -#else - EZO_EMPTY, -#endif - - EZO_EMPTY, - EZO_EMPTY, - EZO_EMPTY, - EZO_EMPTY, - EZO_EMPTY, - -#ifdef USE_EZOHUM - EZO_HUM_NAME, -#else - EZO_EMPTY, -#endif - - EZO_EMPTY, + EZOStruct::id, // "DO" + GET_EZO_CLASS(EZOORP)::id, + GET_EZO_CLASS(EZOPH)::id, + GET_EZO_CLASS(EZOEC)::id, + EZOStruct::id, + GET_EZO_CLASS(EZORTD)::id, + EZOStruct::id, // "PMP" + EZOStruct::id, // "FLO" + GET_EZO_CLASS(EZOCO2)::id, + EZOStruct::id, // "PRS" + EZOStruct::id, + EZOStruct::id, // "O2" + EZOStruct::id, + EZOStruct::id, + GET_EZO_CLASS(EZOHUM)::id, + EZOStruct::id, // "RGB" }; +#define CREATE_EZO_CLASS(CLASS) \ + case EZO_ ## CLASS: \ + sensor[count] = new EZO ## CLASS(addr); \ + break; struct EZOManager { @@ -252,34 +205,22 @@ private: // We use switch intead of virtual function to save RAM switch (j + EZO_ADDR_0) { #ifdef USE_EZOORP - case EZO_ORP: - sensor[count] = new EZOORP(addr); - break; + CREATE_EZO_CLASS(ORP) #endif #ifdef USE_EZOPH - case EZO_PH: - sensor[count] = new EZOpH(addr); - break; + CREATE_EZO_CLASS(PH) #endif #ifdef USE_EZOEC - case EZO_EC: - sensor[count] = new EZOEC(addr); - break; + CREATE_EZO_CLASS(EC) #endif #ifdef USE_EZORTD - case EZO_RTD: - sensor[count] = new EZORTD(addr); - break; + CREATE_EZO_CLASS(RTD) #endif #ifdef USE_EZOCO2 - case EZO_CO2: - sensor[count] = new EZOCO2(addr); - break; + CREATE_EZO_CLASS(CO2) #endif #ifdef USE_EZOHUM - case EZO_HUM: - sensor[count] = new EZOHUM(addr); - break; + CREATE_EZO_CLASS(HUM) #endif } From faa45994d233f902c96fa66b3a9a4f14438e73b6 Mon Sep 17 00:00:00 2001 From: Christopher Tremblay Date: Mon, 26 Oct 2020 00:11:07 -0700 Subject: [PATCH 2/2] Reset deafult my_user_config Removed debug code --- tasmota/my_user_config.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 74fb012be..e1497d93a 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -562,12 +562,12 @@ // #define USE_MCP9808 // [I2cDriver51] Enable MCP9808 temperature sensor (I2C addresses 0x18 - 0x1F) (+0k9 code) // #define USE_HP303B // [I2cDriver52] Enable HP303B temperature and pressure sensor (I2C address 0x76 or 0x77) (+6k2 code) // #define USE_MLX90640 // [I2cDriver53] Enable MLX90640 IR array temperature sensor (I2C address 0x33) (+20k code) - #define USE_EZOPH // [I2cDriver55] Enable support for EZO's pH sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) - #define USE_EZOORP // [I2cDriver55] Enable support for EZO's ORP sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) - #define USE_EZORTD // [I2cDriver55] Enable support for EZO's RTD sensor (+0k2 code) - Shared EZO code required for any EZO device (+1k2 code) - #define USE_EZOHUM // [I2cDriver55] Enable support for EZO's HUM sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) - #define USE_EZOEC // [I2cDriver55] Enable support for EZO's EC sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) - #define USE_EZOCO2 // [I2cDriver55] Enable support for EZO's CO2 sensor (+0k2 code) - Shared EZO code required for any EZO device (+1k2 code) +// #define USE_EZOPH // [I2cDriver55] Enable support for EZO's pH sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) +// #define USE_EZOORP // [I2cDriver55] Enable support for EZO's ORP sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) +// #define USE_EZORTD // [I2cDriver55] Enable support for EZO's RTD sensor (+0k2 code) - Shared EZO code required for any EZO device (+1k2 code) +// #define USE_EZOHUM // [I2cDriver55] Enable support for EZO's HUM sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) +// #define USE_EZOEC // [I2cDriver55] Enable support for EZO's EC sensor (+0k3 code) - Shared EZO code required for any EZO device (+1k2 code) +// #define USE_EZOCO2 // [I2cDriver55] Enable support for EZO's CO2 sensor (+0k2 code) - Shared EZO code required for any EZO device (+1k2 code) // #define USE_DISPLAY // Add I2C Display Support (+2k code) #define USE_DISPLAY_MODES1TO5 // Enable display mode 1 to 5 in addition to mode 0