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