From 828a64815b77e1f0390888c59652041eea0b88bb Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Tue, 30 Jun 2020 17:48:38 +0200 Subject: [PATCH] Add rotary encoder color control Add rotary encoder color control when button pressed and postpone flash writes while turning rotary. --- tasmota/support_rotary.ino | 10 +++++++++- tasmota/xdrv_04_light.ino | 27 ++++++++++++++++++++++----- 2 files changed, 31 insertions(+), 6 deletions(-) diff --git a/tasmota/support_rotary.ino b/tasmota/support_rotary.ino index ec804173e..58f9925b3 100644 --- a/tasmota/support_rotary.ino +++ b/tasmota/support_rotary.ino @@ -88,10 +88,18 @@ void RotaryHandler(void) { Rotary.busy = true; int rotary_position = Rotary.position - Rotary.last_position; + if (Settings.save_data) { + if (save_data_counter < 2) { + save_data_counter = 2; // Postpone flash writes while rotary is turned + } + } + if (Button.hold_timer[0]) { // Button1 is pressed: set color temperature // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("ROT: " D_CMND_COLORTEMPERATURE " %d"), rotary_position); Rotary.changed = 1; - LightColorTempOffset(rotary_position * 4); + if (!LightColorTempOffset(rotary_position * 4)) { // Ct from 153 - 500 + LightColorOffset(rotary_position * 4); // Hue from 0 - 359 + } } else { // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("ROT: " D_CMND_DIMMER " %d"), rotary_position); LightDimmerOffset(rotary_position); diff --git a/tasmota/xdrv_04_light.ino b/tasmota/xdrv_04_light.ino index 9755d3904..0562cc900 100644 --- a/tasmota/xdrv_04_light.ino +++ b/tasmota/xdrv_04_light.ino @@ -1478,14 +1478,31 @@ void LightSetBri(uint8_t device, uint8_t bri) { } } -void LightColorTempOffset(int32_t offset) { +void LightColorOffset(int32_t offset) { + uint16_t hue; + uint8_t sat; + light_state.getHSB(&hue, &sat, nullptr); // Allow user control over Saturation + hue += offset; + if (hue < 0) { hue = 0; } + if (hue > 359) { hue = 359; } + if (!Light.pwm_multi_channels) { + light_state.setHS(hue, sat); + } else { + light_state.setHS(hue, 255); + light_state.setBri(255); // If multi-channel, force bri to max, it will be later dimmed to correct value + } + light_controller.calcLevels(Light.new_color); +} + +bool LightColorTempOffset(int32_t offset) { int32_t ct = LightGetColorTemp(); - if (0 == ct) { return; } // CT not supported + if (0 == ct) { return false; } // CT not supported ct += offset; if (ct < CT_MIN) { ct = CT_MIN; } else if (ct > CT_MAX) { ct = CT_MAX; } LightSetColorTemp(ct); + return true; } void LightSetColorTemp(uint16_t ct) @@ -1769,9 +1786,9 @@ void LightCycleColor(int8_t direction) // AddLog_P2(LOG_LEVEL_DEBUG, PSTR("LGT: random %d, wheel %d, hue %d"), Light.random, Light.wheel, hue); if (!Light.pwm_multi_channels) { - uint8_t sat; - light_state.getHSB(nullptr, &sat, nullptr); // Allow user control over Saturation - light_state.setHS(hue, sat); + uint8_t sat; + light_state.getHSB(nullptr, &sat, nullptr); // Allow user control over Saturation + light_state.setHS(hue, sat); } else { light_state.setHS(hue, 255); light_state.setBri(255); // If multi-channel, force bri to max, it will be later dimmed to correct value