diff --git a/code/espurna/button.cpp b/code/espurna/button.cpp index 8806cde5..87fdc986 100644 --- a/code/espurna/button.cpp +++ b/code/espurna/button.cpp @@ -20,6 +20,7 @@ Copyright (C) 2019-2021 by Maxim Prokhorov ; -struct Button -{ +struct Button { Button() = delete; - Button(ButtonActions &&actions, ButtonEventDelays &&delays); - Button(BasePinPtr &&pin, const debounce_event::types::Config &config, - ButtonActions &&actions, ButtonEventDelays &&delays); + Button(ButtonActions&& actions, ButtonEventDelays&& delays); + Button(BasePinPtr&& pin, const debounce_event::types::Config& config, + ButtonActions&& actions, ButtonEventDelays&& delays); bool state(); ButtonEvent loop(); @@ -85,974 +82,892 @@ struct Button ButtonEventDelays event_delays; }; -namespace espurna -{ - namespace button - { - namespace settings - { - namespace keys - { - namespace - { - PROGMEM_STRING(Gpio, "btnGpio"); - PROGMEM_STRING(GpioType, "btnGpioType"); - PROGMEM_STRING(Provider, "btnProv"); - PROGMEM_STRING(Mode, "btnMode"); - PROGMEM_STRING(DefaultValue, "btnDefVal"); - PROGMEM_STRING(PinMode, "btnPinMode"); - - PROGMEM_STRING(Release, "btnRlse"); - PROGMEM_STRING(Press, "btnPress"); - PROGMEM_STRING(Click, "btnClick"); - PROGMEM_STRING(DoubleClick, "btnDclk"); - PROGMEM_STRING(TripleClick, "btnTclk"); - PROGMEM_STRING(LongClick, "btnLclk"); - PROGMEM_STRING(LongLongClick, "btnLLclk"); - - PROGMEM_STRING(DebounceDelay, "btnDebDel"); - PROGMEM_STRING(LongClickDelay, "btnLclkDel"); - PROGMEM_STRING(LongLongClickDelay, "btnLLclkDel"); - PROGMEM_STRING(RepeatDelay, "btnRepDel"); - - PROGMEM_STRING(Relay, "btnRelay"); - - PROGMEM_STRING(MqttSendAll, "btnMqttSendAll"); - PROGMEM_STRING(MqttRetain, "btnMqttRetain"); - - [[gnu::unused]] PROGMEM_STRING(AnalogLevel, "btnLevel"); - - [[gnu::unused]] PROGMEM_STRING(TerminalCommand, "btnTermCmd"); - - } // namespace - } // namespace keys - - namespace options - { - namespace - { - - using espurna::settings::options::Enumeration; - - PROGMEM_STRING(Switch, "switch"); - PROGMEM_STRING(Pushbutton, "pushbutton"); - - static constexpr std::array, 2> DebounceEventMode PROGMEM{ - {{debounce_event::types::Mode::Switch, Switch}, - {debounce_event::types::Mode::Pushbutton, Pushbutton}}}; - - PROGMEM_STRING(Low, "low"); - PROGMEM_STRING(High, "high"); - PROGMEM_STRING(Initial, "initial"); - - 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}}}; - - PROGMEM_STRING(Input, "default"); - PROGMEM_STRING(InputPullup, "pull-up"); - PROGMEM_STRING(InputPulldown, "pull-down"); - - 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}}}; - - PROGMEM_STRING(None, "none"); - PROGMEM_STRING(Dummy, "dummy"); - PROGMEM_STRING(Gpio, "gpio"); - PROGMEM_STRING(Analog, "analog"); - PROGMEM_STRING(Lightfox, "lightfox"); - - static constexpr std::array, 5> ButtonProviderOptions PROGMEM{ - {{ButtonProvider::None, None}, - {ButtonProvider::Dummy, Dummy}, - {ButtonProvider::Gpio, Gpio}, - {ButtonProvider::Analog, Analog}, - {ButtonProvider::Lightfox, Lightfox}}}; - - [[gnu::unused]] PROGMEM_STRING(Toggle, "relay-toggle"); - [[gnu::unused]] PROGMEM_STRING(On, "relay-on"); - [[gnu::unused]] PROGMEM_STRING(Off, "relay-off"); - - PROGMEM_STRING(AccessPoint, "wifi-ap"); - PROGMEM_STRING(Reset, "reset"); - PROGMEM_STRING(FactoryReset, "factory"); - - [[gnu::unused]] PROGMEM_STRING(BrightnessIncrease, "bri-inc"); - [[gnu::unused]] PROGMEM_STRING(BrightnessDecrease, "bri-dec"); - - [[gnu::unused]] PROGMEM_STRING(DisplayOn, "display-on"); - - PROGMEM_STRING(Custom, "custom"); - - [[gnu::unused]] PROGMEM_STRING(FanLow, "fan-low"); - [[gnu::unused]] PROGMEM_STRING(FanMedium, "fan-medium"); - [[gnu::unused]] PROGMEM_STRING(FanHigh, "fan-high"); +namespace espurna { +namespace button { +namespace settings { +namespace keys { +namespace { + +PROGMEM_STRING(Gpio, "btnGpio"); +PROGMEM_STRING(GpioType, "btnGpioType"); +PROGMEM_STRING(Provider, "btnProv"); +PROGMEM_STRING(Mode, "btnMode"); +PROGMEM_STRING(DefaultValue, "btnDefVal"); +PROGMEM_STRING(PinMode, "btnPinMode"); + +PROGMEM_STRING(Release, "btnRlse"); +PROGMEM_STRING(Press, "btnPress"); +PROGMEM_STRING(Click, "btnClick"); +PROGMEM_STRING(DoubleClick, "btnDclk"); +PROGMEM_STRING(TripleClick, "btnTclk"); +PROGMEM_STRING(LongClick, "btnLclk"); +PROGMEM_STRING(LongLongClick, "btnLLclk"); + +PROGMEM_STRING(DebounceDelay, "btnDebDel"); +PROGMEM_STRING(LongClickDelay, "btnLclkDel"); +PROGMEM_STRING(LongLongClickDelay, "btnLLclkDel"); +PROGMEM_STRING(RepeatDelay, "btnRepDel"); + +PROGMEM_STRING(Relay, "btnRelay"); + +PROGMEM_STRING(MqttSendAll, "btnMqttSendAll"); +PROGMEM_STRING(MqttRetain, "btnMqttRetain"); + +[[gnu::unused]] PROGMEM_STRING(AnalogLevel, "btnLevel"); + +[[gnu::unused]] PROGMEM_STRING(TerminalCommand, "btnTermCmd"); + +} // namespace +} // namespace keys + +namespace options { +namespace { + +using espurna::settings::options::Enumeration; + +PROGMEM_STRING(Switch, "switch"); +PROGMEM_STRING(Pushbutton, "pushbutton"); + +static constexpr std::array, 2> DebounceEventMode PROGMEM { + {{debounce_event::types::Mode::Switch, Switch}, + {debounce_event::types::Mode::Pushbutton, Pushbutton}} +}; + +PROGMEM_STRING(Low, "low"); +PROGMEM_STRING(High, "high"); +PROGMEM_STRING(Initial, "initial"); + +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}} +}; + +PROGMEM_STRING(Input, "default"); +PROGMEM_STRING(InputPullup, "pull-up"); +PROGMEM_STRING(InputPulldown, "pull-down"); + +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}} +}; + +PROGMEM_STRING(None, "none"); +PROGMEM_STRING(Dummy, "dummy"); +PROGMEM_STRING(Gpio, "gpio"); +PROGMEM_STRING(Analog, "analog"); +PROGMEM_STRING(Lightfox, "lightfox"); + +static constexpr std::array, 5> ButtonProviderOptions PROGMEM { + {{ButtonProvider::None, None}, + {ButtonProvider::Dummy, Dummy}, + {ButtonProvider::Gpio, Gpio}, + {ButtonProvider::Analog, Analog}, + {ButtonProvider::Lightfox, Lightfox}} +}; + +[[gnu::unused]] PROGMEM_STRING(Toggle, "relay-toggle"); +[[gnu::unused]] PROGMEM_STRING(On, "relay-on"); +[[gnu::unused]] PROGMEM_STRING(Off, "relay-off"); - PROGMEM_STRING(TerminalCommand, "term-cmd"); +PROGMEM_STRING(AccessPoint, "wifi-ap"); +PROGMEM_STRING(Reset, "reset"); +PROGMEM_STRING(FactoryReset, "factory"); - static constexpr Enumeration ButtonActionOptions[] PROGMEM - { - {ButtonAction::None, None}, +[[gnu::unused]] PROGMEM_STRING(BrightnessIncrease, "bri-inc"); +[[gnu::unused]] PROGMEM_STRING(BrightnessDecrease, "bri-dec"); + +[[gnu::unused]] PROGMEM_STRING(DisplayOn, "display-on"); + +PROGMEM_STRING(Custom, "custom"); + +[[gnu::unused]] PROGMEM_STRING(FanLow, "fan-low"); +[[gnu::unused]] PROGMEM_STRING(FanMedium, "fan-medium"); +[[gnu::unused]] PROGMEM_STRING(FanHigh, "fan-high"); + +PROGMEM_STRING(TerminalCommand, "term-cmd"); + +static constexpr Enumeration ButtonActionOptions[] PROGMEM { + {ButtonAction::None, None}, #if RELAY_SUPPORT - {ButtonAction::Toggle, Toggle}, - {ButtonAction::On, On}, - {ButtonAction::Off, Off}, + {ButtonAction::Toggle, Toggle}, + {ButtonAction::On, On}, + {ButtonAction::Off, Off}, #endif - {ButtonAction::AccessPoint, AccessPoint}, - {ButtonAction::Reset, Reset}, - {ButtonAction::FactoryReset, FactoryReset}, + {ButtonAction::AccessPoint, AccessPoint}, + {ButtonAction::Reset, Reset}, + {ButtonAction::FactoryReset, FactoryReset}, #if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE - {ButtonAction::BrightnessIncrease, BrightnessIncrease}, - {ButtonAction::BrightnessDecrease, BrightnessDecrease}, + {ButtonAction::BrightnessIncrease, BrightnessIncrease}, + {ButtonAction::BrightnessDecrease, BrightnessDecrease}, #endif #if THERMOSTAT_DISPLAY_SUPPORT - {ButtonAction::DisplayOn, DisplayOn}, + {ButtonAction::DisplayOn, DisplayOn}, #endif - {ButtonAction::Custom, Custom}, + {ButtonAction::Custom, Custom}, #if FAN_SUPPORT - {ButtonAction::FanLow, FanLow}, - {ButtonAction::FanMedium, FanMedium}, - {ButtonAction::FanHigh, FanHigh}, + {ButtonAction::FanLow, FanLow}, + {ButtonAction::FanMedium, FanMedium}, + {ButtonAction::FanHigh, FanHigh}, #endif #if TERMINAL_SUPPORT - {ButtonAction::TerminalCommand, TerminalCommand}, + {ButtonAction::TerminalCommand, TerminalCommand}, #endif - }; +}; - } // namespace - } // namespace query - } // namespace settings - } // namespace button +} // namespace +} // namespace query +} // namespace settings +} // namespace button - namespace settings - { - namespace internal - { - namespace - { - - using espurna::button::settings::options::ButtonActionOptions; - using espurna::button::settings::options::ButtonProviderOptions; - using espurna::button::settings::options::DebounceEventMode; - using espurna::button::settings::options::DebounceEventPinMode; - using espurna::button::settings::options::DebounceEventPinValue; - - } // namespace - - template <> - debounce_event::types::Mode convert(const String &value) - { - return convert(DebounceEventMode, value, debounce_event::types::Mode::Pushbutton); - } +namespace settings { +namespace internal { +namespace { - String serialize(debounce_event::types::Mode value) - { - return serialize(DebounceEventMode, value); - } +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; - template <> - debounce_event::types::PinValue convert(const String &value) - { - return convert(DebounceEventPinValue, value, debounce_event::types::PinValue::Low); - } +} // namespace - String serialize(debounce_event::types::PinValue value) - { - return serialize(DebounceEventPinValue, value); - } +template<> +debounce_event::types::Mode convert(const String& value) { + return convert(DebounceEventMode, value, debounce_event::types::Mode::Pushbutton); +} - template <> - debounce_event::types::PinMode convert(const String &value) - { - return convert(DebounceEventPinMode, value, debounce_event::types::PinMode::Input); - } +String serialize(debounce_event::types::Mode value) { + return serialize(DebounceEventMode, value); +} - String serialize(debounce_event::types::PinMode mode) - { - return serialize(DebounceEventPinMode, mode); - } +template<> +debounce_event::types::PinValue convert(const String& value) { + return convert(DebounceEventPinValue, value, debounce_event::types::PinValue::Low); +} - template <> - ButtonProvider convert(const String &value) - { - return convert(ButtonProviderOptions, value, ButtonProvider::None); - } +String serialize(debounce_event::types::PinValue value) { + return serialize(DebounceEventPinValue, value); +} - String serialize(ButtonProvider value) - { - return serialize(ButtonProviderOptions, value); - } +template<> +debounce_event::types::PinMode convert(const String& value) { + return convert(DebounceEventPinMode, value, debounce_event::types::PinMode::Input); +} - template <> - ButtonAction convert(const String &value) - { - return convert(ButtonActionOptions, value, ButtonAction::None); - } +String serialize(debounce_event::types::PinMode mode) { + return serialize(DebounceEventPinMode, mode); +} - String serialize(::ButtonAction value) - { - return serialize(ButtonActionOptions, value); - } +template <> +ButtonProvider convert(const String& value) { + return convert(ButtonProviderOptions, value, ButtonProvider::None); +} - } // namespace internal - } // namespace settings +String serialize(ButtonProvider value) { + return serialize(ButtonProviderOptions, value); +} - // ----------------------------------------------------------------------------- +template<> +ButtonAction convert(const String& value) { + return convert(ButtonActionOptions, value, ButtonAction::None); +} - namespace button - { - namespace internal - { - namespace - { - - static std::vector