From 2aac0683bee2ed93d8757867989017db19cd3616 Mon Sep 17 00:00:00 2001 From: "dh.harald" Date: Fri, 11 Jan 2019 00:50:40 +0000 Subject: [PATCH 1/6] Add support for Mi Desk Lamp --- sonoff/_changelog.ino | 6 +- sonoff/i18n.h | 1 + sonoff/my_user_config.h | 1 + sonoff/settings.h | 2 +- sonoff/settings.ino | 5 ++ sonoff/sonoff.h | 1 + sonoff/sonoff.ino | 2 + sonoff/sonoff_template.h | 23 +++++++ sonoff/sonoff_version.h | 2 +- sonoff/support_rotary.ino | 134 ++++++++++++++++++++++++++++++++++++++ 10 files changed, 174 insertions(+), 3 deletions(-) create mode 100644 sonoff/support_rotary.ino diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 42b97ddcb..90a05184c 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,4 +1,8 @@ -/* 6.4.1.8 20190107 +/* 6.4.1.9 20190111 + * Add support for rotary switch + * Add support for Mi Desk Lamp + * + * 6.4.1.8 20190107 * Change sonoff_template.h layout regarding optional module flags like ADC0 * Add command SetOption62 1 to force no Button/Switch pullup on dedicated modules. Currently only supported on Shelly2 (#4841) * Fix Display exception 28 when JSON value is NULL received diff --git a/sonoff/i18n.h b/sonoff/i18n.h index a90028e8d..f12f562da 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -214,6 +214,7 @@ #define D_CMND_COUNTERDEBOUNCE "CounterDebounce" #define D_CMND_BUTTONDEBOUNCE "ButtonDebounce" #define D_CMND_SWITCHDEBOUNCE "SwitchDebounce" +#define D_CMND_ROTARYDEBOUNCE "RotaryDebounce" #define D_CMND_SLEEP "Sleep" #define D_CMND_UPLOAD "Upload" #define D_CMND_UPGRADE "Upgrade" diff --git a/sonoff/my_user_config.h b/sonoff/my_user_config.h index 706276c22..a364fcf22 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -177,6 +177,7 @@ #define KEY_HOLD_TIME 40 // [SetOption32] Number of 0.1 seconds to hold Button or external Pushbutton before sending HOLD message #define SWITCH_DEBOUNCE_TIME 50 // [SwitchDebounce] Number of mSeconds switch press debounce time #define SWITCH_MODE TOGGLE // [SwitchMode] TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, PUSHBUTTONHOLD, PUSHBUTTONHOLD_INV, PUSHBUTTON_TOGGLE (the wall switch state) +#define ROTARY_DEBOUNCE_TIME 50 // [RotaryDebounce] Number of mSeconds rotary debounce time #define WS2812_LEDS 30 // [Pixels] Number of WS2812 LEDs to start with (max is 512) #define TEMP_CONVERSION 0 // [SetOption8] Return temperature in (0 = Celsius or 1 = Fahrenheit) diff --git a/sonoff/settings.h b/sonoff/settings.h index 714dee800..e472ff292 100644 --- a/sonoff/settings.h +++ b/sonoff/settings.h @@ -330,7 +330,7 @@ struct SYSCFG { uint32_t energy_kWhtotal_time; // 7B4 unsigned long weight_item; // 7B8 Weight of one item in gram * 10 - byte free_7BC[2]; // 7BC + uint16_t rotary_debounce; // 7BC uint16_t weight_max; // 7BE Total max weight in kilogram unsigned long weight_reference; // 7C0 Reference weight in gram diff --git a/sonoff/settings.ino b/sonoff/settings.ino index 65f42e979..db0c7cbef 100644 --- a/sonoff/settings.ino +++ b/sonoff/settings.ino @@ -789,6 +789,7 @@ void SettingsDefaultSet2(void) SettingsDefaultSet_5_13_1c(); // Time STD/DST settings Settings.button_debounce = KEY_DEBOUNCE_TIME; + Settings.rotary_debounce = ROTARY_DEBOUNCE_TIME; Settings.switch_debounce = SWITCH_DEBOUNCE_TIME; for (byte j = 0; j < 5; j++) { @@ -1023,6 +1024,10 @@ void SettingsDelta(void) Settings.param[P_MDNS_DELAYED_START] = 0; } + if (Settings.version < 0x06040109) { + Settings.rotary_debounce = ROTARY_DEBOUNCE_TIME; + } + Settings.version = VERSION; SettingsSave(1); } diff --git a/sonoff/sonoff.h b/sonoff/sonoff.h index 59b7a96f2..9cf6e46f2 100644 --- a/sonoff/sonoff.h +++ b/sonoff/sonoff.h @@ -260,5 +260,6 @@ const uint8_t kIFan02Speed[MAX_FAN_SPEED][3] = {{6,6,6}, {7,6,6}, {7,7,6}, {7,6, \*********************************************************************************************/ extern uint8_t light_device; // Light device number +extern uint8_t light_power; // Light power #endif // _SONOFF_H_ diff --git a/sonoff/sonoff.ino b/sonoff/sonoff.ino index 16239e046..d4ccbc904 100755 --- a/sonoff/sonoff.ino +++ b/sonoff/sonoff.ino @@ -2362,6 +2362,7 @@ void GpioInit(void) ButtonInit(); SwitchInit(); + RotaryInit(); #ifdef USE_WS2812 if (!light_type && (pin[GPIO_WS2812] < 99)) { // RGB led @@ -2548,6 +2549,7 @@ void loop(void) ButtonLoop(); SwitchLoop(); + RotaryLoop(); if (TimeReached(state_50msecond)) { SetNextTimeInterval(state_50msecond, 50); diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index 66a83cd1e..4c2ca84a6 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -146,6 +146,8 @@ enum UserSelectablePins { GPIO_MAX31855CS, // MAX31855 Serial interface GPIO_MAX31855CLK, // MAX31855 Serial interface GPIO_MAX31855DO, // MAX31855 Serial interface + GPIO_ROT_A, // Rotary switch A Pin + GPIO_ROT_B, // Rotary switch B Pin GPIO_SENSOR_END }; // Programmer selectable GPIO functionality offset by user selectable GPIOs @@ -278,6 +280,7 @@ enum SupportedModules { DIGOO, KA10, ZX2820, + MI_DESK_LAMP, MAXMODULE }; /********************************************************************************************/ @@ -503,6 +506,8 @@ const uint8_t kGpioNiceList[] PROGMEM = { GPIO_MAX31855CLK, // MAX31855 Serial interface GPIO_MAX31855DO, // MAX31855 Serial interface #endif + GPIO_ROT_A, // Rotary switch A pin + GPIO_ROT_B, // Rotary switch B pin }; const uint8_t kModuleNiceList[MAXMODULE] PROGMEM = { @@ -565,6 +570,7 @@ const uint8_t kModuleNiceList[MAXMODULE] PROGMEM = { ARILUX_LC11, ZENGGE_ZF_WF017, HUAFAN_SS, + MI_DESK_LAMP, KMC_70011, AILIGHT, // Light Bulbs PHILIPS, @@ -1754,6 +1760,23 @@ const mytmplt kModules[MAXMODULE] PROGMEM = { GPIO_LED1_INV, // GPIO13 Green Led - Link and Power status GPIO_REL1, // GPIO14 Relay 0, 0, 0 + }, + { "Mi Desk Lamp", // Mi LED Desk Lamp + // https://www.mi.com/global/smartlamp/ + 0, 0, + GPIO_KEY1, // GPIO02 Button + 0, + GPIO_PWM1, // GPIO04 Cold White + GPIO_PWM2, // GPIO05 Warm White + // GPIO06 (SD_CLK Flash) + // GPIO07 (SD_DATA0 Flash QIO/DIO/DOUT) + // GPIO08 (SD_DATA1 Flash QIO/DIO/DOUT) + 0, + 0, + // GPIO11 (SD_CMD Flash) + GPIO_ROT_A, // GPIO12 Rotary switch A pin + GPIO_ROT_B, // GPIO13 Rotary switch B pin + 0, 0, 0, 0 } }; diff --git a/sonoff/sonoff_version.h b/sonoff/sonoff_version.h index f371936aa..534276418 100644 --- a/sonoff/sonoff_version.h +++ b/sonoff/sonoff_version.h @@ -20,7 +20,7 @@ #ifndef _SONOFF_VERSION_H_ #define _SONOFF_VERSION_H_ -#define VERSION 0x06040108 +#define VERSION 0x06040109 #define D_PROGRAMNAME "Sonoff-Tasmota" #define D_AUTHOR "Theo Arends" diff --git a/sonoff/support_rotary.ino b/sonoff/support_rotary.ino new file mode 100644 index 000000000..842bc7ca5 --- /dev/null +++ b/sonoff/support_rotary.ino @@ -0,0 +1,134 @@ +/* + support_rotary.ino - rotary switch support for Sonoff-Tasmota + + Copyright (C) 2019 Theo Arends + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#define ROTARY_V1 +#ifdef ROTARY_V1 +/*********************************************************************************************\ + * Rotary support +\*********************************************************************************************/ + +unsigned long rotary_debounce = 0; // Rotary debounce timer +uint8_t rotaries_found = 0; +uint8_t rotary_state = 0; +uint8_t rotary_position = 128; +uint8_t rotary_last_position = 128; +uint8_t interrupts_in_use = 0; + +/********************************************************************************************/ + +void update_position() { + uint8_t s; + + /* + * https://github.com/PaulStoffregen/Encoder/blob/master/Encoder.h + */ + + s = rotary_state & 3; + if (digitalRead(pin[GPIO_ROT_A])) s |= 4; + if (digitalRead(pin[GPIO_ROT_B])) s |= 8; + switch (s) { + case 0: case 5: case 10: case 15: + break; + case 1: case 7: case 8: case 14: + rotary_position++; break; + case 2: case 4: case 11: case 13: + rotary_position--; break; + case 3: case 12: + rotary_position = rotary_position + 2; break; + default: + rotary_position = rotary_position - 2; break; + } + rotary_state = (s >> 2); +} + +void update_rotary() { + if (MI_DESK_LAMP == Settings.module){ + if (light_power) { + update_position(); + } + } +} + +void RotaryInit(void) +{ + rotaries_found = 0; + if (pin[GPIO_ROT_A] < 99 && pin[GPIO_ROT_B] < 99) { + rotaries_found++; + pinMode(pin[GPIO_ROT_A], INPUT_PULLUP); + pinMode(pin[GPIO_ROT_B], INPUT_PULLUP); + + // GPIO6-GPIO11 are typically used to interface with the flash memory IC on + // most esp8266 modules, so we should avoid adding interrupts to these pins. + + if (pin[GPIO_ROT_A] < 6 || pin[GPIO_ROT_A] > 11) { + attachInterrupt(digitalPinToInterrupt(pin[GPIO_ROT_A]), update_rotary, CHANGE); + interrupts_in_use++; + } + if (pin[GPIO_ROT_B] < 6 || pin[GPIO_ROT_B] > 11) { + attachInterrupt(digitalPinToInterrupt(pin[GPIO_ROT_B]), update_rotary, CHANGE); + interrupts_in_use++; + } + } +} + +/*********************************************************************************************\ + * Rotary handler +\*********************************************************************************************/ + +void RotaryHandler(void) +{ + if (interrupts_in_use < 2) { + noInterrupts(); + update_rotary(); + } else { + noInterrupts(); + } + interrupts(); + + if (rotary_last_position != rotary_position) { + if (MI_DESK_LAMP == Settings.module) { // Mi Desk lamp + int8_t d = Settings.light_dimmer; + d = d + (rotary_position - rotary_last_position); + if (d < 1) { + d = 1; + } + if (d > 100) { + d = 100; + } + snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_CMND_DIMMER " %d"), rotary_position - rotary_last_position); + AddLog(LOG_LEVEL_DEBUG); + + LightSetDimmer(d); + Settings.light_dimmer = d; + } + rotary_last_position = rotary_position; + } +} + +void RotaryLoop(void) +{ + if (rotaries_found) { + if (TimeReached(rotary_debounce)) { + SetNextTimeInterval(rotary_debounce, Settings.rotary_debounce); + RotaryHandler(); + } + } +} + +#endif // ROTARY_V1 From 8e43bb38101728af57a759620b7018dfb9b8aea4 Mon Sep 17 00:00:00 2001 From: andrethomas2 <43345003+andrethomas2@users.noreply.github.com> Date: Fri, 11 Jan 2019 13:08:09 +0200 Subject: [PATCH 2/6] Add void's to functions that have no parameters --- sonoff/support_rotary.ino | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sonoff/support_rotary.ino b/sonoff/support_rotary.ino index 842bc7ca5..2e1cf8f3d 100644 --- a/sonoff/support_rotary.ino +++ b/sonoff/support_rotary.ino @@ -32,7 +32,7 @@ uint8_t interrupts_in_use = 0; /********************************************************************************************/ -void update_position() { +void update_position(void) { uint8_t s; /* @@ -57,7 +57,7 @@ void update_position() { rotary_state = (s >> 2); } -void update_rotary() { +void update_rotary(void) { if (MI_DESK_LAMP == Settings.module){ if (light_power) { update_position(); From 4952c4d72fa8a24b51d6456e5a2844d545d8d160 Mon Sep 17 00:00:00 2001 From: andrethomas2 <43345003+andrethomas2@users.noreply.github.com> Date: Fri, 11 Jan 2019 13:12:08 +0200 Subject: [PATCH 3/6] Update support_rotary.ino --- sonoff/support_rotary.ino | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sonoff/support_rotary.ino b/sonoff/support_rotary.ino index 2e1cf8f3d..6a186bae4 100644 --- a/sonoff/support_rotary.ino +++ b/sonoff/support_rotary.ino @@ -68,7 +68,7 @@ void update_rotary(void) { void RotaryInit(void) { rotaries_found = 0; - if (pin[GPIO_ROT_A] < 99 && pin[GPIO_ROT_B] < 99) { + if ((pin[GPIO_ROT_A] < 99) && (pin[GPIO_ROT_B] < 99)) { rotaries_found++; pinMode(pin[GPIO_ROT_A], INPUT_PULLUP); pinMode(pin[GPIO_ROT_B], INPUT_PULLUP); @@ -76,11 +76,11 @@ void RotaryInit(void) // GPIO6-GPIO11 are typically used to interface with the flash memory IC on // most esp8266 modules, so we should avoid adding interrupts to these pins. - if (pin[GPIO_ROT_A] < 6 || pin[GPIO_ROT_A] > 11) { + if ((pin[GPIO_ROT_A] < 6) || (pin[GPIO_ROT_A] > 11)) { attachInterrupt(digitalPinToInterrupt(pin[GPIO_ROT_A]), update_rotary, CHANGE); interrupts_in_use++; } - if (pin[GPIO_ROT_B] < 6 || pin[GPIO_ROT_B] > 11) { + if ((pin[GPIO_ROT_B] < 6) || (pin[GPIO_ROT_B] > 11)) { attachInterrupt(digitalPinToInterrupt(pin[GPIO_ROT_B]), update_rotary, CHANGE); interrupts_in_use++; } From b5ad2c8c06adc08545ef22e2c9b307d086a7ea3f Mon Sep 17 00:00:00 2001 From: Peter Nyilas Date: Fri, 11 Jan 2019 13:59:51 +0000 Subject: [PATCH 4/6] Remove rotary_debounce from settings --- sonoff/_changelog.ino | 10 ++++------ sonoff/i18n.h | 1 - sonoff/my_user_config.h | 1 - sonoff/settings.h | 2 +- sonoff/settings.ino | 5 ----- sonoff/sonoff_version.h | 2 +- sonoff/support_rotary.ino | 2 +- 7 files changed, 7 insertions(+), 16 deletions(-) diff --git a/sonoff/_changelog.ino b/sonoff/_changelog.ino index 90a05184c..1d133a47d 100644 --- a/sonoff/_changelog.ino +++ b/sonoff/_changelog.ino @@ -1,8 +1,4 @@ -/* 6.4.1.9 20190111 - * Add support for rotary switch - * Add support for Mi Desk Lamp - * - * 6.4.1.8 20190107 +/* 6.4.1.8 20190107 * Change sonoff_template.h layout regarding optional module flags like ADC0 * Add command SetOption62 1 to force no Button/Switch pullup on dedicated modules. Currently only supported on Shelly2 (#4841) * Fix Display exception 28 when JSON value is NULL received @@ -15,7 +11,9 @@ * Add support for Luminea ZX2820 Smart Socket with Energy monitoring (#4921) * Add define MDNS_ENABLE to control initial mDNS state (#4923) * Add split interlock part 1 (#4910) - * + * Add support for rotary switch + * Add support for Mi Desk Lamp + * * 6.4.1.7 20190106 * Fix HLW8012, HJL01 and BL0937 based energy sensors low Power (below 10W) measurement regression from 6.4.1.6 * Add Power status functionality to LED2 when configured leaving LED1 for Link status indication diff --git a/sonoff/i18n.h b/sonoff/i18n.h index f12f562da..a90028e8d 100644 --- a/sonoff/i18n.h +++ b/sonoff/i18n.h @@ -214,7 +214,6 @@ #define D_CMND_COUNTERDEBOUNCE "CounterDebounce" #define D_CMND_BUTTONDEBOUNCE "ButtonDebounce" #define D_CMND_SWITCHDEBOUNCE "SwitchDebounce" -#define D_CMND_ROTARYDEBOUNCE "RotaryDebounce" #define D_CMND_SLEEP "Sleep" #define D_CMND_UPLOAD "Upload" #define D_CMND_UPGRADE "Upgrade" diff --git a/sonoff/my_user_config.h b/sonoff/my_user_config.h index a364fcf22..706276c22 100644 --- a/sonoff/my_user_config.h +++ b/sonoff/my_user_config.h @@ -177,7 +177,6 @@ #define KEY_HOLD_TIME 40 // [SetOption32] Number of 0.1 seconds to hold Button or external Pushbutton before sending HOLD message #define SWITCH_DEBOUNCE_TIME 50 // [SwitchDebounce] Number of mSeconds switch press debounce time #define SWITCH_MODE TOGGLE // [SwitchMode] TOGGLE, FOLLOW, FOLLOW_INV, PUSHBUTTON, PUSHBUTTON_INV, PUSHBUTTONHOLD, PUSHBUTTONHOLD_INV, PUSHBUTTON_TOGGLE (the wall switch state) -#define ROTARY_DEBOUNCE_TIME 50 // [RotaryDebounce] Number of mSeconds rotary debounce time #define WS2812_LEDS 30 // [Pixels] Number of WS2812 LEDs to start with (max is 512) #define TEMP_CONVERSION 0 // [SetOption8] Return temperature in (0 = Celsius or 1 = Fahrenheit) diff --git a/sonoff/settings.h b/sonoff/settings.h index e472ff292..714dee800 100644 --- a/sonoff/settings.h +++ b/sonoff/settings.h @@ -330,7 +330,7 @@ struct SYSCFG { uint32_t energy_kWhtotal_time; // 7B4 unsigned long weight_item; // 7B8 Weight of one item in gram * 10 - uint16_t rotary_debounce; // 7BC + byte free_7BC[2]; // 7BC uint16_t weight_max; // 7BE Total max weight in kilogram unsigned long weight_reference; // 7C0 Reference weight in gram diff --git a/sonoff/settings.ino b/sonoff/settings.ino index db0c7cbef..65f42e979 100644 --- a/sonoff/settings.ino +++ b/sonoff/settings.ino @@ -789,7 +789,6 @@ void SettingsDefaultSet2(void) SettingsDefaultSet_5_13_1c(); // Time STD/DST settings Settings.button_debounce = KEY_DEBOUNCE_TIME; - Settings.rotary_debounce = ROTARY_DEBOUNCE_TIME; Settings.switch_debounce = SWITCH_DEBOUNCE_TIME; for (byte j = 0; j < 5; j++) { @@ -1024,10 +1023,6 @@ void SettingsDelta(void) Settings.param[P_MDNS_DELAYED_START] = 0; } - if (Settings.version < 0x06040109) { - Settings.rotary_debounce = ROTARY_DEBOUNCE_TIME; - } - Settings.version = VERSION; SettingsSave(1); } diff --git a/sonoff/sonoff_version.h b/sonoff/sonoff_version.h index 534276418..f371936aa 100644 --- a/sonoff/sonoff_version.h +++ b/sonoff/sonoff_version.h @@ -20,7 +20,7 @@ #ifndef _SONOFF_VERSION_H_ #define _SONOFF_VERSION_H_ -#define VERSION 0x06040109 +#define VERSION 0x06040108 #define D_PROGRAMNAME "Sonoff-Tasmota" #define D_AUTHOR "Theo Arends" diff --git a/sonoff/support_rotary.ino b/sonoff/support_rotary.ino index 6a186bae4..9c7600b09 100644 --- a/sonoff/support_rotary.ino +++ b/sonoff/support_rotary.ino @@ -125,7 +125,7 @@ void RotaryLoop(void) { if (rotaries_found) { if (TimeReached(rotary_debounce)) { - SetNextTimeInterval(rotary_debounce, Settings.rotary_debounce); + SetNextTimeInterval(rotary_debounce, Settings.button_debounce); // Using button_debounce setting for this as well RotaryHandler(); } } From 9f030ae431e3789d0c8ee3907f02939c864906aa Mon Sep 17 00:00:00 2001 From: "dh.harald" Date: Tue, 15 Jan 2019 00:45:19 +0000 Subject: [PATCH 5/6] Change color temp with rotary when Button1 pressed --- sonoff/sonoff.h | 1 + sonoff/support_button.ino | 24 +++++++++++--------- sonoff/support_rotary.ino | 47 ++++++++++++++++++++++++++------------- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/sonoff/sonoff.h b/sonoff/sonoff.h index 9cf6e46f2..c520d3981 100644 --- a/sonoff/sonoff.h +++ b/sonoff/sonoff.h @@ -261,5 +261,6 @@ const uint8_t kIFan02Speed[MAX_FAN_SPEED][3] = {{6,6,6}, {7,6,6}, {7,7,6}, {7,6, extern uint8_t light_device; // Light device number extern uint8_t light_power; // Light power +extern uint8_t rotary_changed; // Rotary switch changed #endif // _SONOFF_H_ diff --git a/sonoff/support_button.ino b/sonoff/support_button.ino index ed5cecce6..dd4b298c8 100644 --- a/sonoff/support_button.ino +++ b/sonoff/support_button.ino @@ -203,19 +203,23 @@ void ButtonHandler(void) multipress[button_index] = 1; } } + if ((MI_DESK_LAMP == Settings.module) && (button_index == 0) && (rotary_changed) && (light_power)) { + rotary_changed = 0; // Color temp changed, no need to turn of the light + } else { if (single_press && SendKey(0, button_index + multipress[button_index], POWER_TOGGLE)) { // Execute Toggle command via MQTT if ButtonTopic is set // Success } else { - if (multipress[button_index] < 3) { // Single or Double press - if (WifiState() > WIFI_RESTART) { // WPSconfig, Smartconfig or Wifimanager active - restart_flag = 1; - } else { - ExecuteCommandPower(button_index + multipress[button_index], POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally - } - } else { // 3 - 7 press - if (!Settings.flag.button_restrict) { - snprintf_P(scmnd, sizeof(scmnd), kCommands[multipress[button_index] -3]); - ExecuteCommand(scmnd, SRC_BUTTON); + if (multipress[button_index] < 3) { // Single or Double press + if (WifiState() > WIFI_RESTART) { // WPSconfig, Smartconfig or Wifimanager active + restart_flag = 1; + } else { + ExecuteCommandPower(button_index + multipress[button_index], POWER_TOGGLE, SRC_BUTTON); // Execute Toggle command internally + } + } else { // 3 - 7 press + if (!Settings.flag.button_restrict) { + snprintf_P(scmnd, sizeof(scmnd), kCommands[multipress[button_index] -3]); + ExecuteCommand(scmnd, SRC_BUTTON); + } } } } diff --git a/sonoff/support_rotary.ino b/sonoff/support_rotary.ino index 9c7600b09..d11450858 100644 --- a/sonoff/support_rotary.ino +++ b/sonoff/support_rotary.ino @@ -29,6 +29,7 @@ uint8_t rotary_state = 0; uint8_t rotary_position = 128; uint8_t rotary_last_position = 128; uint8_t interrupts_in_use = 0; +uint8_t rotary_changed = 0; /********************************************************************************************/ @@ -99,26 +100,42 @@ void RotaryHandler(void) } else { noInterrupts(); } - interrupts(); - if (rotary_last_position != rotary_position) { if (MI_DESK_LAMP == Settings.module) { // Mi Desk lamp - int8_t d = Settings.light_dimmer; - d = d + (rotary_position - rotary_last_position); - if (d < 1) { - d = 1; - } - if (d > 100) { - d = 100; - } - snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_CMND_DIMMER " %d"), rotary_position - rotary_last_position); - AddLog(LOG_LEVEL_DEBUG); + if (holdbutton[0]) { + rotary_changed = 1; + // button1 is pressed: set color temperature + int16_t t = LightGetColorTemp(); + t = t + (rotary_position - rotary_last_position); + if (t < 153) { + t = 153; + } + if (t > 500) { + t = 500; + } + snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_CMND_COLORTEMPERATURE " %d"), rotary_position - rotary_last_position); + AddLog(LOG_LEVEL_DEBUG); + LightSetColorTemp((uint16_t)t); + } else { + int8_t d = Settings.light_dimmer; + d = d + (rotary_position - rotary_last_position); + if (d < 1) { + d = 1; + } + if (d > 100) { + d = 100; + } + snprintf_P(log_data, sizeof(log_data), PSTR(D_LOG_APPLICATION D_CMND_DIMMER " %d"), rotary_position - rotary_last_position); + AddLog(LOG_LEVEL_DEBUG); - LightSetDimmer(d); - Settings.light_dimmer = d; + LightSetDimmer((uint8_t)d); + Settings.light_dimmer = d; + } } - rotary_last_position = rotary_position; + rotary_last_position = 128; + rotary_position = 128; } + interrupts(); } void RotaryLoop(void) From fbf8370b4acc76815d4cef6772ef3e37db3718ad Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 15 Jan 2019 15:35:13 +0100 Subject: [PATCH 6/6] Update sonoff_template.h Move rotary GPIO to non-user config for now. --- sonoff/sonoff_template.h | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sonoff/sonoff_template.h b/sonoff/sonoff_template.h index 4c2ca84a6..5de0caf74 100644 --- a/sonoff/sonoff_template.h +++ b/sonoff/sonoff_template.h @@ -146,8 +146,6 @@ enum UserSelectablePins { GPIO_MAX31855CS, // MAX31855 Serial interface GPIO_MAX31855CLK, // MAX31855 Serial interface GPIO_MAX31855DO, // MAX31855 Serial interface - GPIO_ROT_A, // Rotary switch A Pin - GPIO_ROT_B, // Rotary switch B Pin GPIO_SENSOR_END }; // Programmer selectable GPIO functionality offset by user selectable GPIOs @@ -165,6 +163,8 @@ enum ProgramSelectablePins { GPIO_DI, // my92x1 PWM input GPIO_DCKI, // my92x1 CLK input GPIO_ARIRFRCV, // AliLux RF Receive input + GPIO_ROT_A, // Rotary switch A Pin + GPIO_ROT_B, // Rotary switch B Pin GPIO_USER, // User configurable GPIO_MAX }; @@ -506,8 +506,6 @@ const uint8_t kGpioNiceList[] PROGMEM = { GPIO_MAX31855CLK, // MAX31855 Serial interface GPIO_MAX31855DO, // MAX31855 Serial interface #endif - GPIO_ROT_A, // Rotary switch A pin - GPIO_ROT_B, // Rotary switch B pin }; const uint8_t kModuleNiceList[MAXMODULE] PROGMEM = {