From 34d63c155d46aa9fcc3e458731dcd2de91796c29 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Sat, 6 Feb 2021 03:25:42 +0300 Subject: [PATCH] lights: refactor my92xx mapping build flags --- code/espurna/config/defaults.h | 24 ++++++++++++++++++++++++ code/espurna/config/hardware.h | 22 ++++++++++++++-------- code/espurna/light.cpp | 11 ++--------- code/espurna/light_config.h | 29 ++++++++++++++++++++++++++--- code/test/build/light_my92xx.h | 6 +++++- 5 files changed, 71 insertions(+), 21 deletions(-) diff --git a/code/espurna/config/defaults.h b/code/espurna/config/defaults.h index 541f92cb..45e0ea50 100644 --- a/code/espurna/config/defaults.h +++ b/code/espurna/config/defaults.h @@ -1043,6 +1043,30 @@ #define LIGHT_CH5_INVERSE 0 #endif +// ----------------------------------------------------------------------------- +// my92xx +// ----------------------------------------------------------------------------- + +#ifndef MY92XX_CH1 +#define MY92XX_CH1 0 +#endif + +#ifndef MY92XX_CH2 +#define MY92XX_CH2 1 +#endif + +#ifndef MY92XX_CH3 +#define MY92XX_CH3 2 +#endif + +#ifndef MY92XX_CH4 +#define MY92XX_CH4 3 +#endif + +#ifndef MY92XX_CH5 +#define MY92XX_CH5 4 +#endif + // ----------------------------------------------------------------------------- // Tuya // ----------------------------------------------------------------------------- diff --git a/code/espurna/config/hardware.h b/code/espurna/config/hardware.h index fd379054..6b2bf621 100644 --- a/code/espurna/config/hardware.h +++ b/code/espurna/config/hardware.h @@ -832,12 +832,19 @@ // Light #define LIGHT_CHANNELS 5 + #define MY92XX_MODEL MY92XX_MODEL_MY9231 #define MY92XX_CHIPS 2 #define MY92XX_DI_PIN 12 #define MY92XX_DCKI_PIN 14 #define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT - #define MY92XX_MAPPING 4, 3, 5, 0, 1 + + #define MY92XX_CH1 4 + #define MY92XX_CH2 3 + #define MY92XX_CH3 5 + #define MY92XX_CH4 0 + #define MY92XX_CH5 1 + #define LIGHT_WHITE_FACTOR (0.1) // White LEDs are way more bright in the B1 #elif defined(ITEAD_SONOFF_LED) @@ -1256,7 +1263,6 @@ #define MY92XX_DI_PIN 13 #define MY92XX_DCKI_PIN 15 #define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT - #define MY92XX_MAPPING 0, 1, 2, 3 // ----------------------------------------------------------------------------- // Lyasi LED @@ -1276,7 +1282,6 @@ #define MY92XX_DI_PIN 4 #define MY92XX_DCKI_PIN 5 #define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT - #define MY92XX_MAPPING 0, 1, 2, 3 // ----------------------------------------------------------------------------- // LED Controller @@ -1916,7 +1921,6 @@ #define MY92XX_DI_PIN 13 #define MY92XX_DCKI_PIN 15 #define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT - #define MY92XX_MAPPING 0, 1, 2, 3 // ----------------------------------------------------------------------------- // XENON SM-PW701U @@ -3885,7 +3889,7 @@ #define MY92XX_DI_PIN 13 #define MY92XX_DCKI_PIN 15 #define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT - #define MY92XX_MAPPING 0, 1, 2, 3, 4 + #define LIGHT_WHITE_FACTOR (0.1) // White LEDs are way more bright in the B1 // https://www.amazon.com/gp/product/B07T7W7ZMW @@ -4111,14 +4115,17 @@ #define LIGHT_PROVIDER LIGHT_PROVIDER_MY92XX // Light - #define LIGHT_CHANNELS 5 + #define LIGHT_CHANNELS 2 + #define MY92XX_MODEL MY92XX_MODEL_MY9291 #define MY92XX_CHIPS 1 #define MY92XX_DI_PIN 4 #define MY92XX_DCKI_PIN 5 #define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT + // No RGB on this bulb. Warm white on channel 0, cool white on channel 3 - #define MY92XX_MAPPING 255, 255, 255, 3, 0 + #define MY92XX_CH1 3 + #define MY92XX_CH2 0 // ----------------------------------------------------------------------------- // Lombex Lux Nova 2 White and Color @@ -4140,7 +4147,6 @@ #define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT // RGB on channels 0/1/2, either cool or warm white on channel 3 // The bulb *should* have cool leds, but could also have warm leds as a common defect - #define MY92XX_MAPPING 0, 1, 2, 3 // ----------------------------------------------------------------------------- // Bestek Smart Plug with 2 USB ports diff --git a/code/espurna/light.cpp b/code/espurna/light.cpp index 12029ae3..bc2e1125 100644 --- a/code/espurna/light.cpp +++ b/code/espurna/light.cpp @@ -138,19 +138,12 @@ bool _light_state_changed = false; LightStateListener _light_state_listener = nullptr; #if LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX - #include -my92xx * _my92xx; -unsigned char _light_channel_map[] { - MY92XX_MAPPING -}; - +my92xx* _my92xx { nullptr }; #endif #if LIGHT_PROVIDER == LIGHT_PROVIDER_CUSTOM - std::unique_ptr _light_provider; - #endif // UI hint about channel distribution @@ -870,7 +863,7 @@ void _lightProviderHandleValue(unsigned char channel, float value) { #if LIGHT_PROVIDER == LIGHT_PROVIDER_DIMMER pwm_set_duty(pwm, channel); #elif LIGHT_PROVIDER == LIGHT_PROVIDER_MY92XX - _my92xx->setChannel(_light_channel_map[channel], pwm); + _my92xx->setChannel(_lightMy92xxChannel(channel), pwm); #endif } diff --git a/code/espurna/light_config.h b/code/espurna/light_config.h index 570711f7..5adee44e 100644 --- a/code/espurna/light_config.h +++ b/code/espurna/light_config.h @@ -8,11 +8,11 @@ LIGHT MODULE #include "espurna.h" -constexpr const unsigned char _lightEnablePin() { +constexpr unsigned char _lightEnablePin() { return LIGHT_ENABLE_PIN; } -constexpr const unsigned char _lightChannelPin(unsigned char index) { +constexpr unsigned char _lightChannelPin(unsigned char index) { return ( (index == 0) ? LIGHT_CH1_PIN : (index == 1) ? LIGHT_CH2_PIN : @@ -22,7 +22,7 @@ constexpr const unsigned char _lightChannelPin(unsigned char index) { ); } -constexpr const bool _lightInverse(unsigned char index) { +constexpr bool _lightInverse(unsigned char index) { return ( (index == 0) ? (1 == LIGHT_CH1_INVERSE) : (index == 1) ? (1 == LIGHT_CH2_INVERSE) : @@ -32,3 +32,26 @@ constexpr const bool _lightInverse(unsigned char index) { ); } +#ifdef MY92XX_MAPPING + +constexpr unsigned char _my92xx_mapping[LIGHT_CHANNELS] { + MY92XX_MAPPING +}; + +constexpr unsigned char _lightMy92xxChannel(unsigned char) + __attribute__((deprecated("MY92XX_CH# flags should be used instead of MY92XX_MAPPING"))); +constexpr unsigned char _lightMy92xxChannel(unsigned char channel) { + return _my92xx_mapping[channel]; +} + +#else + +constexpr unsigned char _lightMy92xxChannel(unsigned char channel) { + return (channel == 0) ? MY92XX_CH1 : + (channel == 1) ? MY92XX_CH2 : + (channel == 2) ? MY92XX_CH3 : + (channel == 3) ? MY92XX_CH4 : + (channel == 4) ? MY92XX_CH5 : 255u; +} + +#endif diff --git a/code/test/build/light_my92xx.h b/code/test/build/light_my92xx.h index c701ba26..2888a703 100644 --- a/code/test/build/light_my92xx.h +++ b/code/test/build/light_my92xx.h @@ -5,5 +5,9 @@ #define MY92XX_DI_PIN 1 #define MY92XX_DCKI_PIN 2 #define MY92XX_COMMAND MY92XX_COMMAND_DEFAULT -#define MY92XX_MAPPING 4,3,5,0,1 +#define MY92XX_CH1 4 +#define MY92XX_CH2 3 +#define MY92XX_CH3 5 +#define MY92XX_CH4 0 +#define MY92XX_CH5 1 #define IR_SUPPORT 1