From e213b58db2d96d5bc33ef9e0e8eb534d461cb1e4 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 29 Dec 2021 23:54:09 +0300 Subject: [PATCH] settings: continue refactoring internal constants Settling on naming 'options' for enumerations (...possibly, everything else in the future, would that make sense to store for 'setting' object) Update terminal commands that were reporting status to also report a full list of 'indexed' settings for the specific entity Also updates the WebUI outputs which are (hopefuly) are already handled as-is through the .js processing pipeline and the .html properties receiving certain special string values More namespacing and save ~2KiB of RAM by reducing the amount of loaded keys strings However, ROM side of things may suffer b/c of template specializations for the generic conversion functions when there are many different types involved. --- code/espurna/button.cpp | 494 ++++++++++++++++--------- code/espurna/debug.cpp | 88 +++-- code/espurna/gpio.cpp | 51 ++- code/espurna/gpio.h | 8 + code/espurna/ifan.cpp | 17 +- code/espurna/led.cpp | 240 ++++++------ code/espurna/light.cpp | 3 +- code/espurna/migrate.cpp | 102 ++--- code/espurna/mqtt.cpp | 86 +++-- code/espurna/relay.cpp | 633 ++++++++++++++++++++------------ code/espurna/rpnrules.cpp | 142 +++---- code/espurna/scheduler.cpp | 251 +++++++------ code/espurna/sensor.cpp | 290 ++++++++------- code/espurna/settings.cpp | 212 +++++------ code/espurna/settings.h | 43 ++- code/espurna/settings_helpers.h | 572 ++++++++++++++++++----------- code/espurna/system.cpp | 99 ++++- code/espurna/system.h | 9 + code/espurna/utils.cpp | 70 +++- code/espurna/utils.h | 4 + code/espurna/wifi.cpp | 528 ++++++++++++++++++++------ code/espurna/ws.cpp | 68 ++-- code/espurna/ws_utils.h | 62 +++- code/html/custom.js | 4 +- 24 files changed, 2615 insertions(+), 1461 deletions(-) diff --git a/code/espurna/button.cpp b/code/espurna/button.cpp index 2123a576..ef994844 100644 --- a/code/espurna/button.cpp +++ b/code/espurna/button.cpp @@ -30,143 +30,190 @@ Copyright (C) 2019-2021 by Maxim Prokhorov , 2> DebounceEventModeOptions PROGMEM { +static constexpr std::array, 2> DebounceEventMode PROGMEM { {{debounce_event::types::Mode::Switch, Switch}, {debounce_event::types::Mode::Pushbutton, Pushbutton}} }; -} // namespace - -template<> -debounce_event::types::Mode convert(const String& value) { - return convert(DebounceEventModeOptions, value, debounce_event::types::Mode::Pushbutton); -} - -String serialize(debounce_event::types::Mode value) { - return serialize(DebounceEventModeOptions, value); -} - -namespace { - alignas(4) static constexpr char Low[] PROGMEM = "low"; alignas(4) static constexpr char High[] PROGMEM = "high"; alignas(4) static constexpr char Initial[] PROGMEM = "initial"; -constexpr static const std::array, 3> DebounceEventPinValueOptions PROGMEM { +static constexpr std::array, 3> DebounceEventPinValue PROGMEM { {{debounce_event::types::PinValue::Low, Low}, {debounce_event::types::PinValue::High, High}, {debounce_event::types::PinValue::Initial, Initial}} }; -} // namespace - -template<> -debounce_event::types::PinValue convert(const String& value) { - return convert(DebounceEventPinValueOptions, value, debounce_event::types::PinValue::Low); -} - -String serialize(debounce_event::types::PinValue value) { - return serialize(DebounceEventPinValueOptions, value); -} - -namespace { - alignas(4) static constexpr char Input[] PROGMEM = "default"; alignas(4) static constexpr char InputPullup[] PROGMEM = "pull-up"; alignas(4) static constexpr char InputPulldown[] PROGMEM = "pull-down"; -constexpr static const std::array, 3> DebounceEventPinModeOptions PROGMEM { +static constexpr std::array, 3> DebounceEventPinMode PROGMEM { {{debounce_event::types::PinMode::Input, Input}, {debounce_event::types::PinMode::InputPullup, InputPullup}, {debounce_event::types::PinMode::InputPulldown, InputPulldown}} }; +alignas(4) static constexpr char None[] PROGMEM = "none"; +alignas(4) static constexpr char Gpio[] PROGMEM = "gpio"; +alignas(4) static constexpr char Analog[] PROGMEM = "analog"; + +static constexpr std::array, 3> ButtonProviderOptions PROGMEM { + {{ButtonProvider::None, None}, + {ButtonProvider::Gpio, Gpio}, + {ButtonProvider::Analog, Analog}} +}; + +[[gnu::unused]] alignas(4) static constexpr char Toggle[] PROGMEM = "relay-toggle"; +[[gnu::unused]] alignas(4) static constexpr char On[] PROGMEM = "relay-on"; +[[gnu::unused]] alignas(4) static constexpr char Off[] PROGMEM = "relay-off"; + +alignas(4) static constexpr char AccessPoint[] PROGMEM = "wifi-ap"; +alignas(4) static constexpr char Reset[] PROGMEM = "reset"; +alignas(4) static constexpr char FactoryReset[] PROGMEM = "factory"; + +[[gnu::unused]] alignas(4) static constexpr char BrightnessIncrease[] PROGMEM = "bri-inc"; +[[gnu::unused]] alignas(4) static constexpr char BrightnessDecrease[] PROGMEM = "bri-dec"; + +[[gnu::unused]] alignas(4) static constexpr char DisplayOn[] PROGMEM = "display-on"; + +alignas(4) static constexpr char Custom[] PROGMEM = "custom"; + +[[gnu::unused]] alignas(4) static constexpr char FanLow[] PROGMEM = "fan-low"; +[[gnu::unused]] alignas(4) static constexpr char FanMedium[] PROGMEM = "fan-medium"; +[[gnu::unused]] alignas(4) static constexpr char FanHigh[] PROGMEM = "fan-high"; + +static constexpr Enumeration ButtonActionOptions[] PROGMEM { + {ButtonAction::None, None}, +#if RELAY_SUPPORT + {ButtonAction::Toggle, Toggle}, + {ButtonAction::On, On}, + {ButtonAction::Off, Off}, +#endif + {ButtonAction::AccessPoint, AccessPoint}, + {ButtonAction::Reset, Reset}, + {ButtonAction::FactoryReset, FactoryReset}, +#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE + {ButtonAction::BrightnessIncrease, BrightnessIncrease}, + {ButtonAction::BrightnessDecrease, BrightnessDecrease}, +#endif +#if THERMOSTAT_DISPLAY_SUPPORT + {ButtonAction::DisplayOn, DisplayOn}, +#endif + {ButtonAction::Custom, Custom}, +#if FAN_SUPPORT + {ButtonAction::FanLow, FanLow}, + {ButtonAction::FanMedium, FanMedium}, + {ButtonAction::FanHigh, FanHigh}, +#endif +}; + +} // namespace +} // namespace query +} // namespace settings +} // namespace button +} // namespace espurna + +namespace settings { +namespace internal { +namespace { + +using espurna::button::settings::options::DebounceEventMode; +using espurna::button::settings::options::DebounceEventPinValue; +using espurna::button::settings::options::DebounceEventPinMode; +using espurna::button::settings::options::ButtonProviderOptions; +using espurna::button::settings::options::ButtonActionOptions; + } // namespace +template<> +debounce_event::types::Mode convert(const String& value) { + return convert(DebounceEventMode, value, debounce_event::types::Mode::Pushbutton); +} + +String serialize(debounce_event::types::Mode value) { + return serialize(DebounceEventMode, value); +} + +template<> +debounce_event::types::PinValue convert(const String& value) { + return convert(DebounceEventPinValue, value, debounce_event::types::PinValue::Low); +} + +String serialize(debounce_event::types::PinValue value) { + return serialize(DebounceEventPinValue, value); +} + template<> debounce_event::types::PinMode convert(const String& value) { - return convert(DebounceEventPinModeOptions, value, debounce_event::types::PinMode::Input); + return convert(DebounceEventPinMode, value, debounce_event::types::PinMode::Input); } String serialize(debounce_event::types::PinMode mode) { - return serialize(DebounceEventPinModeOptions, mode); + return serialize(DebounceEventPinMode, mode); } -namespace { - -} // namespace - template <> ButtonProvider convert(const String& value) { - alignas(4) static constexpr char None[] PROGMEM = "none"; - alignas(4) static constexpr char Gpio[] PROGMEM = "gpio"; - alignas(4) static constexpr char Analog[] PROGMEM = "analog"; + return convert(ButtonProviderOptions, value, ButtonProvider::None); +} - constexpr static const std::array, 3> options PROGMEM { - {{ButtonProvider::None, None}, - {ButtonProvider::Gpio, Gpio}, - {ButtonProvider::Analog, Analog}} - }; - - return convert(options, value, ButtonProvider::None); +String serialize(ButtonProvider value) { + return serialize(ButtonProviderOptions, value); } template<> ButtonAction convert(const String& value) { - alignas(4) static constexpr char None[] PROGMEM = "none"; + return convert(ButtonActionOptions, value, ButtonAction::None); +} - [[gnu::unused]] alignas(4) static constexpr char Toggle[] PROGMEM = "relay-toggle"; - [[gnu::unused]] alignas(4) static constexpr char On[] PROGMEM = "relay-on"; - [[gnu::unused]] alignas(4) static constexpr char Off[] PROGMEM = "relay-off"; - - alignas(4) static constexpr char AccessPoint[] PROGMEM = "wifi-ap"; - alignas(4) static constexpr char Reset[] PROGMEM = "reset"; - alignas(4) static constexpr char FactoryReset[] PROGMEM = "factory"; - - [[gnu::unused]] alignas(4) static constexpr char BrightnessIncrease[] PROGMEM = "bri-inc"; - [[gnu::unused]] alignas(4) static constexpr char BrightnessDecrease[] PROGMEM = "bri-dec"; - - [[gnu::unused]] alignas(4) static constexpr char DisplayOn[] PROGMEM = "display-on"; - - alignas(4) static constexpr char Custom[] PROGMEM = "custom"; - - [[gnu::unused]] alignas(4) static constexpr char FanLow[] PROGMEM = "fan-low"; - [[gnu::unused]] alignas(4) static constexpr char FanMedium[] PROGMEM = "fan-medium"; - [[gnu::unused]] alignas(4) static constexpr char FanHigh[] PROGMEM = "fan-high"; - - constexpr static const EnumOption options[] PROGMEM { - {ButtonAction::None, None}, -#if RELAY_SUPPORT - {ButtonAction::Toggle, Toggle}, - {ButtonAction::On, On}, - {ButtonAction::Off, Off}, -#endif - {ButtonAction::AccessPoint, AccessPoint}, - {ButtonAction::Reset, Reset}, - {ButtonAction::FactoryReset, FactoryReset}, -#if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE - {ButtonAction::BrightnessIncrease, BrightnessIncrease}, - {ButtonAction::BrightnessDecrease, BrightnessDecrease}, -#endif -#if THERMOSTAT_DISPLAY_SUPPORT - {ButtonAction::DisplayOn, DisplayOn}, -#endif - {ButtonAction::Custom, Custom}, -#if FAN_SUPPORT - {ButtonAction::FanLow, FanLow}, - {ButtonAction::FanMedium, FanMedium}, - {ButtonAction::FanHigh, FanHigh}, -#endif - }; - - return convert(options, value, ButtonAction::None); +String serialize(::ButtonAction value) { + return serialize(ButtonActionOptions, value); } } // namespace internal @@ -174,9 +221,18 @@ ButtonAction convert(const String& value) { // ----------------------------------------------------------------------------- +namespace espurna { namespace button { +namespace internal { namespace { + +static std::vector