From 5e1cbcf94fc10f338b1d95258a0b7019df8d5e79 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Fri, 5 Apr 2019 15:27:06 +0200 Subject: [PATCH] Make MI Desk Lamp optional Make MI Desk Lamp optional --- sonoff/my_user_config.h | 2 +- sonoff/sonoff.ino | 59 +++++++++++++++++++----------------- sonoff/sonoff_template.h | 8 ++--- sonoff/support.ino | 10 ++++++ sonoff/xdrv_01_webserver.ino | 4 +-- 5 files changed, 48 insertions(+), 35 deletions(-) diff --git a/sonoff/my_user_config.h b/sonoff/my_user_config.h index 6e62e3031..407a25c13 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -369,7 +369,7 @@ #define TUYA_DIMMER_ID 0 // Default dimmer Id #define USE_ARMTRONIX_DIMMERS // Add support for Armtronix Dimmers (+1k4 code) #define USE_PS_16_DZ // Add support for PS-16-DZ Dimmer -//#define ROTARY_V1 // Add support for MI Desk Lamp +//#define ROTARY_V1 // Add support for MI Desk Lamp //#define USE_AZ7798 // Add support for AZ-Instrument 7798 CO2 datalogger (+1k6 code) //#define USE_PN532_HSU // Add support for PN532 using HSU (Serial) interface (+1k8 code, 140 bytes mem) // #define USE_PN532_CAUSE_EVENTS // Cause event execution for PN532_UID= and PN532_DATA=[if defined] (+ 30 bytes code) diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index c53510ada..d87aba99b 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -874,35 +874,40 @@ void MqttDataHandler(char* topic, uint8_t* data, unsigned int data_len) } else if (CMND_MODULE == command_code) { if ((payload >= 0) && (payload <= MAXMODULE)) { - if (0 == payload) { payload = 256; } - payload--; - Settings.last_module = Settings.module; - Settings.module = payload; - SetModuleType(); - if (Settings.last_module != payload) { - for (uint8_t i = 0; i < sizeof(Settings.my_gp); i++) { - Settings.my_gp.io[i] = GPIO_NONE; - } + bool present = false; + if (0 == payload) { + payload = 255; + present = true; + } else { + payload--; + present = ValidModule(payload); + } + if (present) { + Settings.last_module = Settings.module; + Settings.module = payload; + SetModuleType(); + if (Settings.last_module != payload) { + for (uint8_t i = 0; i < sizeof(Settings.my_gp); i++) { + Settings.my_gp.io[i] = GPIO_NONE; + } + } + restart_flag = 2; } - restart_flag = 2; } Response_P(S_JSON_COMMAND_NVALUE_SVALUE, command, ModuleNr(), ModuleName().c_str()); } else if (CMND_MODULES == command_code) { - for (uint8_t i = 0; i <= MAXMODULE; i++) { + uint8_t midx = USER_MODULE; + for (uint8_t i = 0; i <= sizeof(kModuleNiceList); i++) { + if (i > 0) { midx = pgm_read_byte(kModuleNiceList + i -1); } if (!jsflg) { Response_P(PSTR("{\"" D_CMND_MODULES "%d\":["), lines); } else { ResponseAppend_P(PSTR(",")); } jsflg = true; - uint8_t j = i; - if (0 == i) { j = USER_MODULE; } else { j--; } - -// ResponseAppend_P(PSTR("\"%d (%s)\""), i, AnyModuleName(j).c_str()); -// if ((strlen(mqtt_data) > (LOGSZ - TOPSZ)) || (i == MAXMODULE)) { - - if ((ResponseAppend_P(PSTR("\"%d (%s)\""), i, AnyModuleName(j).c_str()) > (LOGSZ - TOPSZ)) || (i == MAXMODULE)) { + uint8_t j = i ? midx +1 : 0; + if ((ResponseAppend_P(PSTR("\"%d (%s)\""), j, AnyModuleName(midx).c_str()) > (LOGSZ - TOPSZ)) || (i == sizeof(kModuleNiceList))) { ResponseAppend_P(PSTR("]}")); MqttPublishPrefixTopic_P(RESULT_OR_STAT, type); jsflg = false; @@ -957,10 +962,6 @@ void MqttDataHandler(char* topic, uint8_t* data, unsigned int data_len) ResponseAppend_P(PSTR(",")); } jsflg = true; - -// ResponseAppend_P(PSTR("\"%d (%s)\""), midx, GetTextIndexed(stemp1, sizeof(stemp1), midx, kSensorNames)); -// if ((strlen(mqtt_data) > (LOGSZ - TOPSZ)) || (i == sizeof(kGpioNiceList) -1)) { - if ((ResponseAppend_P(PSTR("\"%d (%s)\""), midx, GetTextIndexed(stemp1, sizeof(stemp1), midx, kSensorNames)) > (LOGSZ - TOPSZ)) || (i == sizeof(kGpioNiceList) -1)) { ResponseAppend_P(PSTR("]}")); MqttPublishPrefixTopic_P(RESULT_OR_STAT, type); @@ -2324,9 +2325,11 @@ void GpioInit(void) { uint8_t mpin; - if ((Settings.module >= MAXMODULE) && (Settings.module < USER_MODULE)) { - Settings.module = MODULE; - Settings.last_module = MODULE; + if (!ValidModule(Settings.module)) { + uint8_t module = MODULE; + if (!ValidModule(MODULE)) { module = SONOFF_BASIC; } + Settings.module = module; + Settings.last_module = module; } SetModuleType(); @@ -2509,7 +2512,7 @@ void GpioInit(void) SwitchInit(); #ifdef ROTARY_V1 RotaryInit(); -#endif +#endif #ifdef USE_WS2812 if (!light_type && (pin[GPIO_WS2812] < 99)) { // RGB led @@ -2702,9 +2705,9 @@ void loop(void) ButtonLoop(); SwitchLoop(); -#ifdef ROTARY_V1 +#ifdef ROTARY_V1 RotaryLoop(); -#endif +#endif if (TimeReached(state_50msecond)) { SetNextTimeInterval(state_50msecond, 50); diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index fbe5cd931..1461e0370 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -588,15 +588,15 @@ const uint8_t kGpioNiceList[] PROGMEM = { GPIO_ROT1B, // Rotary switch1 B Pin GPIO_ROT2A, // Rotary switch2 A Pin GPIO_ROT2B, // Rotary switch2 B Pin -#endif +#endif GPIO_ARIRFRCV, // AliLux RF Receive input #ifdef USE_HRE GPIO_HRE_CLOCK, GPIO_HRE_DATA -#endif +#endif }; -const uint8_t kModuleNiceList[MAXMODULE] PROGMEM = { +const uint8_t kModuleNiceList[] PROGMEM = { SONOFF_BASIC, // Sonoff Relay Devices SONOFF_RF, SONOFF_TH, @@ -660,7 +660,7 @@ const uint8_t kModuleNiceList[MAXMODULE] PROGMEM = { HUAFAN_SS, #ifdef ROTARY_V1 MI_DESK_LAMP, -#endif +#endif KMC_70011, AILIGHT, // Light Bulbs PHILIPS, diff --git a/sonoff/support.ino b/sonoff/support.ino index 1a222686c..d9fe3ed7a 100644 --- a/sonoff/support.ino +++ b/sonoff/support.ino @@ -846,6 +846,16 @@ uint8_t ModuleNr() return (USER_MODULE == Settings.module) ? 0 : Settings.module +1; } +bool ValidModule(uint8_t index) +{ + for (uint8_t i = 0; i < sizeof(kModuleNiceList); i++) { + if (index == pgm_read_byte(kModuleNiceList + i)) { + return true; + } + } + return false; +} + String AnyModuleName(uint8_t index) { if (USER_MODULE == index) { diff --git a/sonoff/xdrv_01_webserver.ino b/sonoff/xdrv_01_webserver.ino index 771a010b1..33391287f 100644 --- a/sonoff/xdrv_01_webserver.ino +++ b/sonoff/xdrv_01_webserver.ino @@ -1038,7 +1038,7 @@ void HandleTemplateConfiguration(void) if (WebServer->hasArg("m")) { WSContentBegin(200, CT_PLAIN); - for (uint8_t i = 0; i < MAXMODULE; i++) { // "}2'%d'>%s (%d)}3" - "}2'0'>Sonoff Basic (1)}3" + for (uint8_t i = 0; i < sizeof(kModuleNiceList); i++) { // "}2'%d'>%s (%d)}3" - "}2'0'>Sonoff Basic (1)}3" uint8_t midx = pgm_read_byte(kModuleNiceList + i); WSContentSend_P(HTTP_MODULE_TEMPLATE_REPLACE, midx, AnyModuleName(midx).c_str(), midx +1); } @@ -1159,7 +1159,7 @@ void HandleModuleConfiguration(void) if (WebServer->hasArg("m")) { WSContentBegin(200, CT_PLAIN); uint8_t vidx = 0; - for (uint8_t i = 0; i <= MAXMODULE; i++) { // "}2'%d'>%s (%d)}3" - "}2'255'>UserTemplate (0)}3" - "}2'0'>Sonoff Basic (1)}3" + for (uint8_t i = 0; i <= sizeof(kModuleNiceList); i++) { // "}2'%d'>%s (%d)}3" - "}2'255'>UserTemplate (0)}3" - "}2'0'>Sonoff Basic (1)}3" if (0 == i) { midx = USER_MODULE; vidx = 0;