From 2aac0683bee2ed93d8757867989017db19cd3616 Mon Sep 17 00:00:00 2001 From: "dh.harald" Date: Fri, 11 Jan 2019 00:50:40 +0000 Subject: [PATCH] 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