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
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2019-06-16 15:43:23 +01:00
|
|
|
#ifdef USE_LIGHT
|
2019-08-17 15:19:58 +01:00
|
|
|
//#define ROTARY_V1
|
|
|
|
#ifdef ROTARY_V1
|
2019-01-11 00:50:40 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Rotary support
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-08-17 15:19:58 +01:00
|
|
|
struct ROTARY {
|
|
|
|
unsigned long debounce = 0; // Rotary debounce timer
|
|
|
|
uint8_t present = 0;
|
|
|
|
uint8_t state = 0;
|
|
|
|
uint8_t position = 128;
|
|
|
|
uint8_t last_position = 128;
|
|
|
|
uint8_t interrupts_in_use_count = 0;
|
|
|
|
uint8_t changed = 0;
|
|
|
|
} Rotary;
|
2019-04-05 07:41:26 +01:00
|
|
|
|
2019-01-11 00:50:40 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2019-01-15 15:11:42 +00:00
|
|
|
void update_position(void)
|
|
|
|
{
|
2019-01-11 00:50:40 +00:00
|
|
|
uint8_t s;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* https://github.com/PaulStoffregen/Encoder/blob/master/Encoder.h
|
|
|
|
*/
|
|
|
|
|
2019-08-17 15:19:58 +01:00
|
|
|
s = Rotary.state & 3;
|
2019-02-11 18:21:49 +00:00
|
|
|
if (digitalRead(pin[GPIO_ROT1A])) s |= 4;
|
|
|
|
if (digitalRead(pin[GPIO_ROT1B])) s |= 8;
|
2019-01-11 00:50:40 +00:00
|
|
|
switch (s) {
|
|
|
|
case 0: case 5: case 10: case 15:
|
|
|
|
break;
|
|
|
|
case 1: case 7: case 8: case 14:
|
2019-08-17 15:19:58 +01:00
|
|
|
Rotary.position++; break;
|
2019-01-11 00:50:40 +00:00
|
|
|
case 2: case 4: case 11: case 13:
|
2019-08-17 15:19:58 +01:00
|
|
|
Rotary.position--; break;
|
2019-01-11 00:50:40 +00:00
|
|
|
case 3: case 12:
|
2019-08-17 15:19:58 +01:00
|
|
|
Rotary.position = Rotary.position + 2; break;
|
2019-01-11 00:50:40 +00:00
|
|
|
default:
|
2019-08-17 15:19:58 +01:00
|
|
|
Rotary.position = Rotary.position - 2; break;
|
2019-01-11 00:50:40 +00:00
|
|
|
}
|
2019-08-17 15:19:58 +01:00
|
|
|
Rotary.state = (s >> 2);
|
2019-01-11 00:50:40 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 09:39:37 +01:00
|
|
|
#ifndef ARDUINO_ESP8266_RELEASE_2_3_0 // Fix core 2.5.x ISR not in IRAM Exception
|
2019-05-22 12:51:33 +01:00
|
|
|
void update_rotary(void) ICACHE_RAM_ATTR;
|
2019-05-22 09:39:37 +01:00
|
|
|
#endif // ARDUINO_ESP8266_RELEASE_2_3_0
|
|
|
|
|
2019-01-15 15:11:42 +00:00
|
|
|
void update_rotary(void)
|
|
|
|
{
|
2019-02-11 18:21:49 +00:00
|
|
|
if (MI_DESK_LAMP == my_module_type){
|
2019-08-17 12:17:30 +01:00
|
|
|
if (LightPower()) {
|
2019-01-11 00:50:40 +00:00
|
|
|
update_position();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-17 15:19:58 +01:00
|
|
|
bool RotaryButtonPressed(void)
|
|
|
|
{
|
|
|
|
if ((MI_DESK_LAMP == my_module_type) && (Rotary.changed) && LightPower()) {
|
|
|
|
Rotary.changed = 0; // Color temp changed, no need to turn of the light
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-11 00:50:40 +00:00
|
|
|
void RotaryInit(void)
|
|
|
|
{
|
2019-08-17 15:19:58 +01:00
|
|
|
Rotary.present = 0;
|
2019-02-11 18:21:49 +00:00
|
|
|
if ((pin[GPIO_ROT1A] < 99) && (pin[GPIO_ROT1B] < 99)) {
|
2019-08-17 15:19:58 +01:00
|
|
|
Rotary.present++;
|
2019-02-11 18:21:49 +00:00
|
|
|
pinMode(pin[GPIO_ROT1A], INPUT_PULLUP);
|
|
|
|
pinMode(pin[GPIO_ROT1B], INPUT_PULLUP);
|
2019-01-11 00:50:40 +00:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2019-02-11 18:21:49 +00:00
|
|
|
if ((pin[GPIO_ROT1A] < 6) || (pin[GPIO_ROT1A] > 11)) {
|
|
|
|
attachInterrupt(digitalPinToInterrupt(pin[GPIO_ROT1A]), update_rotary, CHANGE);
|
2019-08-17 15:19:58 +01:00
|
|
|
Rotary.interrupts_in_use_count++;
|
2019-01-11 00:50:40 +00:00
|
|
|
}
|
2019-02-11 18:21:49 +00:00
|
|
|
if ((pin[GPIO_ROT1B] < 6) || (pin[GPIO_ROT1B] > 11)) {
|
|
|
|
attachInterrupt(digitalPinToInterrupt(pin[GPIO_ROT1B]), update_rotary, CHANGE);
|
2019-08-17 15:19:58 +01:00
|
|
|
Rotary.interrupts_in_use_count++;
|
2019-01-11 00:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Rotary handler
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
void RotaryHandler(void)
|
|
|
|
{
|
2019-08-17 15:19:58 +01:00
|
|
|
if (Rotary.interrupts_in_use_count < 2) {
|
2019-01-11 00:50:40 +00:00
|
|
|
noInterrupts();
|
|
|
|
update_rotary();
|
|
|
|
} else {
|
|
|
|
noInterrupts();
|
|
|
|
}
|
2019-08-17 15:19:58 +01:00
|
|
|
if (Rotary.last_position != Rotary.position) {
|
2019-02-11 18:21:49 +00:00
|
|
|
if (MI_DESK_LAMP == my_module_type) { // Mi Desk lamp
|
2019-08-17 15:19:58 +01:00
|
|
|
if (Button.hold_timer[0]) {
|
|
|
|
Rotary.changed = 1;
|
2019-01-15 00:45:19 +00:00
|
|
|
// button1 is pressed: set color temperature
|
|
|
|
int16_t t = LightGetColorTemp();
|
2019-08-17 15:19:58 +01:00
|
|
|
t = t + (Rotary.position - Rotary.last_position);
|
2019-01-15 00:45:19 +00:00
|
|
|
if (t < 153) {
|
|
|
|
t = 153;
|
|
|
|
}
|
|
|
|
if (t > 500) {
|
|
|
|
t = 500;
|
|
|
|
}
|
2019-08-17 15:19:58 +01:00
|
|
|
DEBUG_CORE_LOG(PSTR("ROT: " D_CMND_COLORTEMPERATURE " %d"), Rotary.position - Rotary.last_position);
|
2019-01-15 00:45:19 +00:00
|
|
|
LightSetColorTemp((uint16_t)t);
|
|
|
|
} else {
|
|
|
|
int8_t d = Settings.light_dimmer;
|
2019-08-17 15:19:58 +01:00
|
|
|
d = d + (Rotary.position - Rotary.last_position);
|
2019-01-15 00:45:19 +00:00
|
|
|
if (d < 1) {
|
|
|
|
d = 1;
|
|
|
|
}
|
|
|
|
if (d > 100) {
|
|
|
|
d = 100;
|
|
|
|
}
|
2019-08-17 15:19:58 +01:00
|
|
|
DEBUG_CORE_LOG(PSTR("ROT: " D_CMND_DIMMER " %d"), Rotary.position - Rotary.last_position);
|
2019-01-15 00:45:19 +00:00
|
|
|
|
|
|
|
LightSetDimmer((uint8_t)d);
|
|
|
|
Settings.light_dimmer = d;
|
2019-01-11 00:50:40 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-17 15:19:58 +01:00
|
|
|
Rotary.last_position = 128;
|
|
|
|
Rotary.position = 128;
|
2019-01-11 00:50:40 +00:00
|
|
|
}
|
2019-01-15 00:45:19 +00:00
|
|
|
interrupts();
|
2019-01-11 00:50:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RotaryLoop(void)
|
|
|
|
{
|
2019-08-17 15:19:58 +01:00
|
|
|
if (Rotary.present) {
|
|
|
|
if (TimeReached(Rotary.debounce)) {
|
|
|
|
SetNextTimeInterval(Rotary.debounce, Settings.button_debounce); // Using button_debounce setting for this as well
|
2019-01-11 00:50:40 +00:00
|
|
|
RotaryHandler();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ROTARY_V1
|
2019-06-16 15:43:23 +01:00
|
|
|
#endif // USE_LIGHT
|