Add support for Mi Desk Lamp

This commit is contained in:
dh.harald 2019-01-11 00:50:40 +00:00
parent fc1198c1f9
commit 2aac0683be
10 changed files with 174 additions and 3 deletions

View File

@ -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

View File

@ -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"

View File

@ -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)

View File

@ -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

View File

@ -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);
}

View File

@ -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_

View File

@ -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);

View File

@ -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
}
};

View File

@ -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"

134
sonoff/support_rotary.ino Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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