2019-01-11 00:50:40 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
support_rotary.ino - rotary switch support for Tasmota
|
2019-01-11 00:50:40 +00:00
|
|
|
|
2019-12-31 13:23:34 +00:00
|
|
|
Copyright (C) 2020 Theo Arends
|
2019-01-11 00:50:40 +00:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2019-08-17 15:19:58 +01:00
|
|
|
#ifdef ROTARY_V1
|
2019-01-11 00:50:40 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Rotary support
|
2020-07-02 17:13:14 +01:00
|
|
|
*
|
|
|
|
* Supports full range in 10 steps of the Rotary Encoder:
|
|
|
|
* - Light Dimmer
|
|
|
|
* - Light Color for RGB lights when Button1 pressed
|
|
|
|
* - Light Color Temperature for CW lights when Button1 pressed
|
|
|
|
*
|
|
|
|
* _______ _______
|
|
|
|
* GPIO_ROT1A ______| |_______| |______ GPIO_ROT1A
|
|
|
|
* negative <-- _______ _______ __ --> positive
|
|
|
|
* GPIO_ROT1B __| |_______| |_______| GPIO_ROT1B
|
|
|
|
*
|
2019-01-11 00:50:40 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-07-02 17:13:14 +01:00
|
|
|
#ifndef ROTARY_MAX_STEPS
|
2020-07-13 14:10:23 +01:00
|
|
|
#define ROTARY_MAX_STEPS 10 // Rotary step boundary
|
2020-07-02 17:13:14 +01:00
|
|
|
#endif
|
2020-10-25 13:17:40 +00:00
|
|
|
#ifndef ROTARY_START_DIM
|
|
|
|
#define ROTARY_START_DIM 1 // Minimal dimmer value after power on with SetOption113 1
|
|
|
|
#endif
|
2020-10-22 13:48:12 +01:00
|
|
|
#ifndef ROTARY_TIMEOUT
|
|
|
|
#define ROTARY_TIMEOUT 2 // 2 * RotaryHandler() call which is usually 2 * 0.05 seconds
|
|
|
|
#endif
|
|
|
|
#ifndef ROTARY_DEBOUNCE
|
|
|
|
#define ROTARY_DEBOUNCE 10 // Debounce time in milliseconds
|
|
|
|
#endif
|
2020-07-02 17:13:14 +01:00
|
|
|
|
2020-10-20 14:13:12 +01:00
|
|
|
// (0) = Mi Desk lamp (1) = Normal rotary
|
|
|
|
// ---------------------------- ----------------------
|
|
|
|
const uint8_t rotary_dimmer_increment[2] = { 100 / (ROTARY_MAX_STEPS * 3), 100 / ROTARY_MAX_STEPS }; // Dimmer 1..100 = 100
|
|
|
|
const uint8_t rotary_ct_increment[2] = { 350 / (ROTARY_MAX_STEPS * 3), 350 / ROTARY_MAX_STEPS }; // Ct 153..500 = 347
|
|
|
|
const uint8_t rotary_color_increment[2] = { 360 / (ROTARY_MAX_STEPS * 3), 360 / ROTARY_MAX_STEPS }; // Hue 0..359 = 360
|
2020-10-22 13:48:12 +01:00
|
|
|
const uint8_t rotary_offset = 128;
|
|
|
|
const int8_t rotary_state_pos[16] = { 0, 1, -1, 2, -1, 0, -2, 1, 1, -2, 0, -1, 2, -1, 1, 0 };
|
2020-07-01 09:22:20 +01:00
|
|
|
|
2019-08-17 15:19:58 +01:00
|
|
|
struct ROTARY {
|
2020-10-22 13:48:12 +01:00
|
|
|
uint8_t model;
|
|
|
|
bool present;
|
2020-07-13 14:10:23 +01:00
|
|
|
} Rotary;
|
|
|
|
|
2020-07-14 14:01:41 +01:00
|
|
|
struct tEncoder {
|
2020-10-13 14:14:34 +01:00
|
|
|
volatile uint32_t debounce = 0;
|
2020-10-20 14:13:12 +01:00
|
|
|
volatile uint8_t state = 0;
|
2020-10-22 13:48:12 +01:00
|
|
|
volatile uint8_t position;
|
|
|
|
volatile int8_t direction = 0; // Control consistent direction
|
|
|
|
volatile int8_t pina;
|
|
|
|
volatile int8_t pinb;
|
2020-07-13 14:10:23 +01:00
|
|
|
uint8_t timeout = 0; // Disallow direction change within 0.5 second
|
2020-10-20 14:13:12 +01:00
|
|
|
int8_t abs_position[2] = { 0 };
|
2020-07-02 17:13:14 +01:00
|
|
|
bool changed = false;
|
2020-07-14 14:01:41 +01:00
|
|
|
};
|
|
|
|
tEncoder Encoder[MAX_ROTARIES];
|
2019-04-05 07:41:26 +01:00
|
|
|
|
2019-01-11 00:50:40 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2020-07-12 17:53:57 +01:00
|
|
|
bool RotaryButtonPressed(uint32_t button_index) {
|
2020-07-03 16:17:57 +01:00
|
|
|
if (!Rotary.present) { return false; }
|
|
|
|
|
2020-07-13 14:10:23 +01:00
|
|
|
for (uint32_t index = 0; index < MAX_ROTARIES; index++) {
|
2020-10-19 17:08:40 +01:00
|
|
|
if (-1 == Encoder[index].pinb) { continue; }
|
2020-07-13 14:10:23 +01:00
|
|
|
if (index != button_index) { continue; }
|
|
|
|
|
2020-10-28 18:03:39 +00:00
|
|
|
bool powered_on = (TasmotaGlobal.power);
|
2020-07-02 17:13:14 +01:00
|
|
|
#ifdef USE_LIGHT
|
2020-07-13 14:10:23 +01:00
|
|
|
if (!Settings.flag4.rotary_uses_rules) { // SetOption98 - Use rules instead of light control
|
|
|
|
powered_on = LightPower();
|
|
|
|
}
|
2020-07-02 17:13:14 +01:00
|
|
|
#endif // USE_LIGHT
|
2020-07-13 14:10:23 +01:00
|
|
|
if (Encoder[index].changed && powered_on) {
|
|
|
|
Encoder[index].changed = false; // Color (temp) changed, no need to turn of the light
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2019-08-17 15:19:58 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-20 14:13:12 +01:00
|
|
|
void ICACHE_RAM_ATTR RotaryIsrArgMiDesk(void *arg) {
|
2020-07-14 14:01:41 +01:00
|
|
|
tEncoder* encoder = static_cast<tEncoder*>(arg);
|
|
|
|
|
2020-10-19 17:08:40 +01:00
|
|
|
// https://github.com/PaulStoffregen/Encoder/blob/master/Encoder.h
|
2020-10-22 13:48:12 +01:00
|
|
|
uint32_t state = encoder->state & 3;
|
2020-10-19 17:08:40 +01:00
|
|
|
if (digitalRead(encoder->pina)) { state |= 4; }
|
|
|
|
if (digitalRead(encoder->pinb)) { state |= 8; }
|
2020-10-22 13:48:12 +01:00
|
|
|
encoder->position += rotary_state_pos[state];
|
2020-10-19 17:08:40 +01:00
|
|
|
encoder->state = (state >> 2);
|
2020-10-20 14:13:12 +01:00
|
|
|
}
|
2020-10-19 17:08:40 +01:00
|
|
|
|
2020-10-20 14:13:12 +01:00
|
|
|
void ICACHE_RAM_ATTR RotaryIsrArg(void *arg) {
|
|
|
|
tEncoder* encoder = static_cast<tEncoder*>(arg);
|
|
|
|
|
2020-10-19 17:08:40 +01:00
|
|
|
// Theo Arends
|
2020-07-14 14:01:41 +01:00
|
|
|
uint32_t time = millis();
|
|
|
|
if ((encoder->debounce < time) || (encoder->debounce > time + ROTARY_DEBOUNCE)) {
|
2020-10-19 17:08:40 +01:00
|
|
|
int direction = (digitalRead(encoder->pinb)) ? -1 : 1;
|
2020-07-14 14:01:41 +01:00
|
|
|
if ((0 == encoder->direction) || (direction == encoder->direction)) {
|
|
|
|
encoder->position += direction;
|
|
|
|
encoder->direction = direction;
|
|
|
|
}
|
|
|
|
encoder->debounce = time + ROTARY_DEBOUNCE; // Experimental debounce
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 15:58:36 +01:00
|
|
|
void RotaryInit(void) {
|
2020-07-13 14:10:23 +01:00
|
|
|
Rotary.present = false;
|
2020-10-20 14:13:12 +01:00
|
|
|
Rotary.model = 1;
|
|
|
|
#ifdef ESP8266
|
|
|
|
if (MI_DESK_LAMP == my_module_type) {
|
|
|
|
Rotary.model = 0;
|
|
|
|
}
|
|
|
|
#endif // ESP8266
|
2020-07-13 14:10:23 +01:00
|
|
|
for (uint32_t index = 0; index < MAX_ROTARIES; index++) {
|
2020-10-20 14:13:12 +01:00
|
|
|
Encoder[index].pinb = -1;
|
2020-10-19 17:08:40 +01:00
|
|
|
if (PinUsed(GPIO_ROT1A, index) && PinUsed(GPIO_ROT1B, index)) {
|
2020-10-22 13:48:12 +01:00
|
|
|
Encoder[index].position = rotary_offset;
|
2020-10-19 17:08:40 +01:00
|
|
|
Encoder[index].pina = Pin(GPIO_ROT1A, index);
|
2020-10-20 14:13:12 +01:00
|
|
|
Encoder[index].pinb = Pin(GPIO_ROT1B, index);
|
2020-10-22 13:48:12 +01:00
|
|
|
pinMode(Encoder[index].pina, INPUT_PULLUP);
|
2020-10-20 14:13:12 +01:00
|
|
|
pinMode(Encoder[index].pinb, INPUT_PULLUP);
|
|
|
|
if (0 == Rotary.model) {
|
|
|
|
attachInterruptArg(Encoder[index].pina, RotaryIsrArgMiDesk, &Encoder[index], CHANGE);
|
|
|
|
attachInterruptArg(Encoder[index].pinb, RotaryIsrArgMiDesk, &Encoder[index], CHANGE);
|
|
|
|
} else {
|
|
|
|
attachInterruptArg(Encoder[index].pina, RotaryIsrArg, &Encoder[index], FALLING);
|
|
|
|
}
|
2020-07-13 14:10:23 +01:00
|
|
|
}
|
2020-10-19 17:08:40 +01:00
|
|
|
Rotary.present |= (Encoder[index].pinb > -1);
|
2019-01-11 00:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Rotary handler
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-06-30 15:58:36 +01:00
|
|
|
void RotaryHandler(void) {
|
2020-07-03 16:17:57 +01:00
|
|
|
if (!Rotary.present) { return; }
|
|
|
|
|
2020-07-13 14:10:23 +01:00
|
|
|
for (uint32_t index = 0; index < MAX_ROTARIES; index++) {
|
2020-10-19 17:08:40 +01:00
|
|
|
if (-1 == Encoder[index].pinb) { continue; }
|
2020-07-13 14:10:23 +01:00
|
|
|
|
|
|
|
if (Encoder[index].timeout) {
|
|
|
|
Encoder[index].timeout--;
|
|
|
|
if (!Encoder[index].timeout) {
|
2020-07-12 17:53:57 +01:00
|
|
|
#ifdef USE_LIGHT
|
2020-07-13 14:10:23 +01:00
|
|
|
if (!Settings.flag4.rotary_uses_rules) { // SetOption98 - Use rules instead of light control
|
|
|
|
ResponseLightState(0);
|
2020-07-20 16:24:51 +01:00
|
|
|
MqttPublishPrefixTopicRulesProcess_P(RESULT_OR_STAT, PSTR(D_CMND_STATE));
|
2020-07-13 14:10:23 +01:00
|
|
|
}
|
2020-07-12 17:53:57 +01:00
|
|
|
#endif // USE_LIGHT
|
2020-07-13 14:10:23 +01:00
|
|
|
Encoder[index].direction = 0;
|
|
|
|
}
|
2020-07-02 17:13:14 +01:00
|
|
|
}
|
2020-10-22 13:48:12 +01:00
|
|
|
if (rotary_offset == Encoder[index].position) { continue; }
|
2020-07-01 09:27:04 +01:00
|
|
|
|
2020-07-13 14:10:23 +01:00
|
|
|
Encoder[index].timeout = ROTARY_TIMEOUT; // Prevent fast direction changes within 0.5 second
|
2020-07-02 17:13:14 +01:00
|
|
|
|
2020-10-22 13:48:12 +01:00
|
|
|
noInterrupts();
|
|
|
|
int rotary_position = Encoder[index].position - rotary_offset;
|
|
|
|
Encoder[index].position = rotary_offset;
|
|
|
|
interrupts();
|
2020-06-30 15:58:36 +01:00
|
|
|
|
2020-10-29 13:41:12 +00:00
|
|
|
if (Settings.save_data && (TasmotaGlobal.save_data_counter < 2)) {
|
|
|
|
TasmotaGlobal.save_data_counter = 3; // Postpone flash writes while rotary is turned
|
2020-07-13 14:10:23 +01:00
|
|
|
}
|
2019-01-11 00:50:40 +00:00
|
|
|
|
2020-07-13 14:10:23 +01:00
|
|
|
bool button_pressed = (Button.hold_timer[index]); // Button is pressed: set color temperature
|
|
|
|
if (button_pressed) { Encoder[index].changed = true; }
|
|
|
|
// AddLog_P2(LOG_LEVEL_DEBUG, PSTR("ROT: Button1 %d, Position %d"), button_pressed, rotary_position);
|
2020-07-02 17:13:14 +01:00
|
|
|
|
|
|
|
#ifdef USE_LIGHT
|
2020-07-13 14:10:23 +01:00
|
|
|
if (!Settings.flag4.rotary_uses_rules) { // SetOption98 - Use rules instead of light control
|
2020-10-19 17:08:40 +01:00
|
|
|
bool second_rotary = (Encoder[1].pinb > -1);
|
2020-07-13 14:10:23 +01:00
|
|
|
if (0 == index) { // Rotary1
|
|
|
|
if (button_pressed) {
|
|
|
|
if (second_rotary) { // Color RGB
|
2020-10-20 14:13:12 +01:00
|
|
|
LightColorOffset(rotary_position * rotary_color_increment[Rotary.model]);
|
2020-07-13 14:10:23 +01:00
|
|
|
} else { // Color Temperature or Color RGB
|
2020-10-20 14:13:12 +01:00
|
|
|
if (!LightColorTempOffset(rotary_position * rotary_ct_increment[Rotary.model])) {
|
|
|
|
LightColorOffset(rotary_position * rotary_color_increment[Rotary.model]);
|
2020-07-13 14:10:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // Dimmer RGBCW or RGB only if second rotary
|
2020-10-25 11:59:24 +00:00
|
|
|
uint32_t dimmer_index = second_rotary ? 1 : 0;
|
2020-10-28 18:03:39 +00:00
|
|
|
if (!Settings.flag4.rotary_poweron_dimlow || TasmotaGlobal.power) { // SetOption113 - On rotary dial after power off set dimmer low
|
2020-10-25 11:59:24 +00:00
|
|
|
LightDimmerOffset(dimmer_index, rotary_position * rotary_dimmer_increment[Rotary.model]);
|
|
|
|
} else {
|
|
|
|
if (rotary_position > 0) { // Only power on if rotary increase
|
2020-10-25 13:17:40 +00:00
|
|
|
LightDimmerOffset(dimmer_index, -LightGetDimmer(dimmer_index) + ROTARY_START_DIM);
|
2020-10-25 11:59:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-13 14:10:23 +01:00
|
|
|
}
|
|
|
|
} else { // Rotary2
|
|
|
|
if (button_pressed) { // Color Temperature
|
2020-10-20 14:13:12 +01:00
|
|
|
LightColorTempOffset(rotary_position * rotary_ct_increment[Rotary.model]);
|
2020-07-13 14:10:23 +01:00
|
|
|
} else { // Dimmer CW
|
2020-10-20 14:13:12 +01:00
|
|
|
LightDimmerOffset(2, rotary_position * rotary_dimmer_increment[Rotary.model]);
|
2020-07-13 14:10:23 +01:00
|
|
|
}
|
2020-07-02 17:13:14 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#endif // USE_LIGHT
|
2020-07-14 14:01:41 +01:00
|
|
|
Encoder[index].abs_position[button_pressed] += rotary_position;
|
|
|
|
if (Encoder[index].abs_position[button_pressed] < 0) {
|
|
|
|
Encoder[index].abs_position[button_pressed] = 0;
|
|
|
|
}
|
|
|
|
if (Encoder[index].abs_position[button_pressed] > ROTARY_MAX_STEPS) {
|
|
|
|
Encoder[index].abs_position[button_pressed] = ROTARY_MAX_STEPS;
|
2020-07-13 14:10:23 +01:00
|
|
|
}
|
2020-07-14 14:01:41 +01:00
|
|
|
Response_P(PSTR("{\"Rotary%d\":{\"Pos1\":%d,\"Pos2\":%d}}"), index +1, Encoder[index].abs_position[0], Encoder[index].abs_position[1]);
|
2020-07-13 14:10:23 +01:00
|
|
|
XdrvRulesProcess();
|
2020-07-02 17:13:14 +01:00
|
|
|
#ifdef USE_LIGHT
|
2020-07-13 14:10:23 +01:00
|
|
|
}
|
2020-07-02 17:13:14 +01:00
|
|
|
#endif // USE_LIGHT
|
2020-07-13 14:10:23 +01:00
|
|
|
}
|
2019-01-11 00:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ROTARY_V1
|