mirror of
https://github.com/xoseperez/espurna.git
synced 2026-03-12 11:17:06 +01:00
button: refactor inline getSetting into functions
This commit is contained in:
@@ -7,14 +7,11 @@ Copyright (C) 2019-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com
|
||||
|
||||
*/
|
||||
|
||||
#include "button.h"
|
||||
#include "espurna.h"
|
||||
|
||||
#if BUTTON_SUPPORT
|
||||
|
||||
#include <bitset>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
#include "button.h"
|
||||
#include "compat.h"
|
||||
#include "fan.h"
|
||||
#include "gpio.h"
|
||||
@@ -24,12 +21,12 @@ Copyright (C) 2019-2021 by Maxim Prokhorov <prokhorov dot max at outlook dot com
|
||||
#include "system.h"
|
||||
#include "ws.h"
|
||||
|
||||
#include "libs/BasePin.h"
|
||||
#include "libs/DebounceEvent.h"
|
||||
#include "gpio_pin.h"
|
||||
#include "mcp23s08_pin.h"
|
||||
|
||||
#include "button_config.h"
|
||||
#include <bitset>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -168,6 +165,452 @@ ButtonAction convert(const String& value) {
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
namespace button {
|
||||
namespace {
|
||||
namespace build {
|
||||
|
||||
constexpr size_t pin(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_PIN :
|
||||
(index == 1) ? BUTTON2_PIN :
|
||||
(index == 2) ? BUTTON3_PIN :
|
||||
(index == 3) ? BUTTON4_PIN :
|
||||
(index == 4) ? BUTTON5_PIN :
|
||||
(index == 5) ? BUTTON6_PIN :
|
||||
(index == 6) ? BUTTON7_PIN :
|
||||
(index == 7) ? BUTTON8_PIN : GPIO_NONE
|
||||
);
|
||||
}
|
||||
|
||||
constexpr GpioType pinType(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_PIN_TYPE :
|
||||
(index == 1) ? BUTTON2_PIN_TYPE :
|
||||
(index == 2) ? BUTTON3_PIN_TYPE :
|
||||
(index == 3) ? BUTTON4_PIN_TYPE :
|
||||
(index == 4) ? BUTTON5_PIN_TYPE :
|
||||
(index == 5) ? BUTTON6_PIN_TYPE :
|
||||
(index == 6) ? BUTTON7_PIN_TYPE :
|
||||
(index == 7) ? BUTTON8_PIN_TYPE : GPIO_TYPE_NONE
|
||||
);
|
||||
}
|
||||
|
||||
namespace internal {
|
||||
namespace ButtonMask {
|
||||
|
||||
constexpr int Pushbutton { 1 << 0 };
|
||||
constexpr int Switch { 1 << 1 };
|
||||
constexpr int DefaultLow { 1 << 2 };
|
||||
constexpr int DefaultHigh { 1 << 3 };
|
||||
constexpr int DefaultBoot { 1 << 4 };
|
||||
constexpr int SetPullup { 1 << 5 };
|
||||
constexpr int SetPulldown { 1 << 6 };
|
||||
|
||||
} // namespace ButtonMask
|
||||
|
||||
constexpr int configBitmask(size_t index) {
|
||||
return (
|
||||
(index == 0) ? (BUTTON1_CONFIG) :
|
||||
(index == 1) ? (BUTTON2_CONFIG) :
|
||||
(index == 2) ? (BUTTON3_CONFIG) :
|
||||
(index == 3) ? (BUTTON4_CONFIG) :
|
||||
(index == 4) ? (BUTTON5_CONFIG) :
|
||||
(index == 5) ? (BUTTON6_CONFIG) :
|
||||
(index == 6) ? (BUTTON7_CONFIG) :
|
||||
(index == 7) ? (BUTTON8_CONFIG) : (BUTTON_PUSHBUTTON | BUTTON_SET_PULLUP | BUTTON_DEFAULT_HIGH)
|
||||
);
|
||||
}
|
||||
|
||||
constexpr debounce_event::types::Config decode(int bitmask) {
|
||||
return {
|
||||
((bitmask & ButtonMask::Pushbutton)
|
||||
? debounce_event::types::Mode::Pushbutton
|
||||
: debounce_event::types::Mode::Switch),
|
||||
((bitmask & ButtonMask::DefaultLow) ? debounce_event::types::PinValue::Low
|
||||
: (bitmask & ButtonMask::DefaultHigh) ? debounce_event::types::PinValue::High
|
||||
: (bitmask & ButtonMask::DefaultBoot) ? debounce_event::types::PinValue::Initial
|
||||
: debounce_event::types::PinValue::Low),
|
||||
((bitmask & ButtonMask::SetPullup) ? debounce_event::types::PinMode::InputPullup
|
||||
: (bitmask & ButtonMask::SetPulldown) ? debounce_event::types::PinMode::InputPulldown
|
||||
: debounce_event::types::PinMode::Input)
|
||||
};
|
||||
}
|
||||
|
||||
constexpr debounce_event::types::Mode mode(size_t index) {
|
||||
return decode(configBitmask(index)).mode;
|
||||
}
|
||||
|
||||
constexpr debounce_event::types::PinValue defaultValue(size_t index) {
|
||||
return decode(configBitmask(index)).default_value;
|
||||
}
|
||||
|
||||
constexpr debounce_event::types::PinMode pinMode(size_t index) {
|
||||
return decode(configBitmask(index)).pin_mode;
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
constexpr debounce_event::types::Mode mode(size_t index) {
|
||||
return internal::mode(index);
|
||||
}
|
||||
|
||||
constexpr debounce_event::types::PinValue defaultValue(size_t index) {
|
||||
return internal::defaultValue(index);
|
||||
}
|
||||
|
||||
constexpr debounce_event::types::PinMode pinMode(size_t index) {
|
||||
return internal::pinMode(index);
|
||||
}
|
||||
|
||||
constexpr ButtonAction release(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_RELEASE :
|
||||
(index == 1) ? BUTTON2_RELEASE :
|
||||
(index == 2) ? BUTTON3_RELEASE :
|
||||
(index == 3) ? BUTTON4_RELEASE :
|
||||
(index == 4) ? BUTTON5_RELEASE :
|
||||
(index == 5) ? BUTTON6_RELEASE :
|
||||
(index == 6) ? BUTTON7_RELEASE :
|
||||
(index == 7) ? BUTTON8_RELEASE : BUTTON_ACTION_NONE
|
||||
);
|
||||
}
|
||||
|
||||
constexpr ButtonAction press(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_PRESS :
|
||||
(index == 1) ? BUTTON2_PRESS :
|
||||
(index == 2) ? BUTTON3_PRESS :
|
||||
(index == 3) ? BUTTON4_PRESS :
|
||||
(index == 4) ? BUTTON5_PRESS :
|
||||
(index == 5) ? BUTTON6_PRESS :
|
||||
(index == 6) ? BUTTON7_PRESS :
|
||||
(index == 7) ? BUTTON8_PRESS : BUTTON_ACTION_NONE
|
||||
);
|
||||
}
|
||||
|
||||
constexpr ButtonAction click(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_CLICK :
|
||||
(index == 1) ? BUTTON2_CLICK :
|
||||
(index == 2) ? BUTTON3_CLICK :
|
||||
(index == 3) ? BUTTON4_CLICK :
|
||||
(index == 4) ? BUTTON5_CLICK :
|
||||
(index == 5) ? BUTTON6_CLICK :
|
||||
(index == 6) ? BUTTON7_CLICK :
|
||||
(index == 7) ? BUTTON8_CLICK : BUTTON_ACTION_NONE
|
||||
);
|
||||
}
|
||||
|
||||
constexpr ButtonAction doubleClick(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_DBLCLICK :
|
||||
(index == 1) ? BUTTON2_DBLCLICK :
|
||||
(index == 2) ? BUTTON3_DBLCLICK :
|
||||
(index == 3) ? BUTTON4_DBLCLICK :
|
||||
(index == 4) ? BUTTON5_DBLCLICK :
|
||||
(index == 5) ? BUTTON6_DBLCLICK :
|
||||
(index == 6) ? BUTTON7_DBLCLICK :
|
||||
(index == 7) ? BUTTON8_DBLCLICK : BUTTON_ACTION_NONE
|
||||
);
|
||||
}
|
||||
|
||||
constexpr ButtonAction tripleClick(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_TRIPLECLICK :
|
||||
(index == 1) ? BUTTON2_TRIPLECLICK :
|
||||
(index == 2) ? BUTTON3_TRIPLECLICK :
|
||||
(index == 3) ? BUTTON4_TRIPLECLICK :
|
||||
(index == 4) ? BUTTON5_TRIPLECLICK :
|
||||
(index == 5) ? BUTTON6_TRIPLECLICK :
|
||||
(index == 6) ? BUTTON7_TRIPLECLICK :
|
||||
(index == 7) ? BUTTON8_TRIPLECLICK : BUTTON_ACTION_NONE
|
||||
);
|
||||
}
|
||||
|
||||
constexpr ButtonAction longClick(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_LNGCLICK :
|
||||
(index == 1) ? BUTTON2_LNGCLICK :
|
||||
(index == 2) ? BUTTON3_LNGCLICK :
|
||||
(index == 3) ? BUTTON4_LNGCLICK :
|
||||
(index == 4) ? BUTTON5_LNGCLICK :
|
||||
(index == 5) ? BUTTON6_LNGCLICK :
|
||||
(index == 6) ? BUTTON7_LNGCLICK :
|
||||
(index == 7) ? BUTTON8_LNGCLICK : BUTTON_ACTION_NONE
|
||||
);
|
||||
}
|
||||
|
||||
constexpr ButtonAction longLongClick(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_LNGLNGCLICK :
|
||||
(index == 1) ? BUTTON2_LNGLNGCLICK :
|
||||
(index == 2) ? BUTTON3_LNGLNGCLICK :
|
||||
(index == 3) ? BUTTON4_LNGLNGCLICK :
|
||||
(index == 4) ? BUTTON5_LNGLNGCLICK :
|
||||
(index == 5) ? BUTTON6_LNGLNGCLICK :
|
||||
(index == 6) ? BUTTON7_LNGLNGCLICK :
|
||||
(index == 7) ? BUTTON8_LNGLNGCLICK : BUTTON_ACTION_NONE
|
||||
);
|
||||
}
|
||||
|
||||
constexpr size_t relay(size_t index) {
|
||||
return (
|
||||
(index == 0) ? (BUTTON1_RELAY - 1) :
|
||||
(index == 1) ? (BUTTON2_RELAY - 1) :
|
||||
(index == 2) ? (BUTTON3_RELAY - 1) :
|
||||
(index == 3) ? (BUTTON4_RELAY - 1) :
|
||||
(index == 4) ? (BUTTON5_RELAY - 1) :
|
||||
(index == 5) ? (BUTTON6_RELAY - 1) :
|
||||
(index == 6) ? (BUTTON7_RELAY - 1) :
|
||||
(index == 7) ? (BUTTON8_RELAY - 1) : RELAY_NONE
|
||||
);
|
||||
}
|
||||
|
||||
constexpr unsigned long debounceDelay() {
|
||||
return BUTTON_DEBOUNCE_DELAY;
|
||||
}
|
||||
|
||||
constexpr unsigned long debounceDelay(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_DEBOUNCE_DELAY :
|
||||
(index == 1) ? BUTTON2_DEBOUNCE_DELAY :
|
||||
(index == 2) ? BUTTON3_DEBOUNCE_DELAY :
|
||||
(index == 3) ? BUTTON4_DEBOUNCE_DELAY :
|
||||
(index == 4) ? BUTTON5_DEBOUNCE_DELAY :
|
||||
(index == 5) ? BUTTON6_DEBOUNCE_DELAY :
|
||||
(index == 6) ? BUTTON7_DEBOUNCE_DELAY :
|
||||
(index == 7) ? BUTTON8_DEBOUNCE_DELAY : debounceDelay()
|
||||
);
|
||||
}
|
||||
|
||||
constexpr unsigned long repeatDelay() {
|
||||
return BUTTON_REPEAT_DELAY;
|
||||
}
|
||||
|
||||
constexpr unsigned long repeatDelay(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_REPEAT_DELAY :
|
||||
(index == 1) ? BUTTON2_REPEAT_DELAY :
|
||||
(index == 2) ? BUTTON3_REPEAT_DELAY :
|
||||
(index == 3) ? BUTTON4_REPEAT_DELAY :
|
||||
(index == 4) ? BUTTON5_REPEAT_DELAY :
|
||||
(index == 5) ? BUTTON6_REPEAT_DELAY :
|
||||
(index == 6) ? BUTTON7_REPEAT_DELAY :
|
||||
(index == 7) ? BUTTON8_REPEAT_DELAY : repeatDelay()
|
||||
);
|
||||
}
|
||||
|
||||
constexpr unsigned long longClickDelay() {
|
||||
return BUTTON_LNGCLICK_DELAY;
|
||||
}
|
||||
|
||||
constexpr unsigned long longClickDelay(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_LNGCLICK_DELAY :
|
||||
(index == 1) ? BUTTON2_LNGCLICK_DELAY :
|
||||
(index == 2) ? BUTTON3_LNGCLICK_DELAY :
|
||||
(index == 3) ? BUTTON4_LNGCLICK_DELAY :
|
||||
(index == 4) ? BUTTON5_LNGCLICK_DELAY :
|
||||
(index == 5) ? BUTTON6_LNGCLICK_DELAY :
|
||||
(index == 6) ? BUTTON7_LNGCLICK_DELAY :
|
||||
(index == 7) ? BUTTON8_LNGCLICK_DELAY : longClickDelay()
|
||||
);
|
||||
}
|
||||
|
||||
constexpr unsigned long longLongClickDelay() {
|
||||
return BUTTON_LNGLNGCLICK_DELAY;
|
||||
}
|
||||
|
||||
constexpr unsigned long longLongClickDelay(size_t index) {
|
||||
return (
|
||||
(index == 0) ? BUTTON1_LNGLNGCLICK_DELAY :
|
||||
(index == 1) ? BUTTON2_LNGLNGCLICK_DELAY :
|
||||
(index == 2) ? BUTTON3_LNGLNGCLICK_DELAY :
|
||||
(index == 3) ? BUTTON4_LNGLNGCLICK_DELAY :
|
||||
(index == 4) ? BUTTON5_LNGLNGCLICK_DELAY :
|
||||
(index == 5) ? BUTTON6_LNGLNGCLICK_DELAY :
|
||||
(index == 6) ? BUTTON7_LNGLNGCLICK_DELAY :
|
||||
(index == 7) ? BUTTON8_LNGLNGCLICK_DELAY : longLongClickDelay()
|
||||
);
|
||||
}
|
||||
|
||||
constexpr bool mqttSendAllEvents() {
|
||||
return (1 == BUTTON_MQTT_SEND_ALL_EVENTS);
|
||||
}
|
||||
|
||||
constexpr bool mqttSendAllEvents(size_t index) {
|
||||
return (
|
||||
(index == 0) ? (1 == BUTTON1_MQTT_SEND_ALL_EVENTS) :
|
||||
(index == 1) ? (1 == BUTTON2_MQTT_SEND_ALL_EVENTS) :
|
||||
(index == 2) ? (1 == BUTTON3_MQTT_SEND_ALL_EVENTS) :
|
||||
(index == 3) ? (1 == BUTTON4_MQTT_SEND_ALL_EVENTS) :
|
||||
(index == 4) ? (1 == BUTTON5_MQTT_SEND_ALL_EVENTS) :
|
||||
(index == 5) ? (1 == BUTTON6_MQTT_SEND_ALL_EVENTS) :
|
||||
(index == 6) ? (1 == BUTTON7_MQTT_SEND_ALL_EVENTS) :
|
||||
(index == 7) ? (1 == BUTTON8_MQTT_SEND_ALL_EVENTS) : mqttSendAllEvents()
|
||||
);
|
||||
}
|
||||
|
||||
constexpr bool mqttRetain() {
|
||||
return (1 == BUTTON_MQTT_RETAIN);
|
||||
}
|
||||
|
||||
constexpr bool mqttRetain(size_t index) {
|
||||
return (
|
||||
(index == 0) ? (1 == BUTTON1_MQTT_RETAIN) :
|
||||
(index == 1) ? (1 == BUTTON2_MQTT_RETAIN) :
|
||||
(index == 2) ? (1 == BUTTON3_MQTT_RETAIN) :
|
||||
(index == 3) ? (1 == BUTTON4_MQTT_RETAIN) :
|
||||
(index == 4) ? (1 == BUTTON5_MQTT_RETAIN) :
|
||||
(index == 5) ? (1 == BUTTON6_MQTT_RETAIN) :
|
||||
(index == 6) ? (1 == BUTTON7_MQTT_RETAIN) :
|
||||
(index == 7) ? (1 == BUTTON8_MQTT_RETAIN) : mqttRetain()
|
||||
);
|
||||
}
|
||||
|
||||
constexpr ButtonProvider provider(size_t index) {
|
||||
return (
|
||||
(index == 0) ? (BUTTON1_PROVIDER) :
|
||||
(index == 1) ? (BUTTON2_PROVIDER) :
|
||||
(index == 2) ? (BUTTON3_PROVIDER) :
|
||||
(index == 3) ? (BUTTON4_PROVIDER) :
|
||||
(index == 4) ? (BUTTON5_PROVIDER) :
|
||||
(index == 5) ? (BUTTON6_PROVIDER) :
|
||||
(index == 6) ? (BUTTON7_PROVIDER) :
|
||||
(index == 7) ? (BUTTON8_PROVIDER) : BUTTON_PROVIDER_NONE
|
||||
);
|
||||
}
|
||||
|
||||
constexpr int analogLevel(size_t index) {
|
||||
return (
|
||||
(index == 0) ? (BUTTON1_ANALOG_LEVEL) :
|
||||
(index == 1) ? (BUTTON2_ANALOG_LEVEL) :
|
||||
(index == 2) ? (BUTTON3_ANALOG_LEVEL) :
|
||||
(index == 3) ? (BUTTON4_ANALOG_LEVEL) :
|
||||
(index == 4) ? (BUTTON5_ANALOG_LEVEL) :
|
||||
(index == 5) ? (BUTTON6_ANALOG_LEVEL) :
|
||||
(index == 6) ? (BUTTON7_ANALOG_LEVEL) :
|
||||
(index == 7) ? (BUTTON8_ANALOG_LEVEL) : 0
|
||||
);
|
||||
}
|
||||
|
||||
} // namespace build
|
||||
|
||||
namespace settings {
|
||||
namespace internal {
|
||||
|
||||
template <typename T>
|
||||
T indexedThenGlobal(const String& prefix, size_t index, T defaultValue) {
|
||||
SettingsKey key{prefix, index};
|
||||
auto indexed = ::settings::internal::get(key.value());
|
||||
if (indexed) {
|
||||
return ::settings::internal::convert<T>(indexed.ref());
|
||||
}
|
||||
|
||||
auto global = ::settings::internal::get(prefix);
|
||||
if (global) {
|
||||
return ::settings::internal::convert<T>(indexed.ref());
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
unsigned char pin(size_t index) {
|
||||
return getSetting({"btnGpio", index}, build::pin(index));
|
||||
}
|
||||
|
||||
GpioType pinType(size_t index) {
|
||||
return getSetting({"btnGpioType", index}, build::pinType(index));
|
||||
}
|
||||
|
||||
ButtonProvider provider(size_t index) {
|
||||
return getSetting({"btnProv", index}, build::provider(index));
|
||||
}
|
||||
|
||||
debounce_event::types::Mode mode(size_t index) {
|
||||
return getSetting({"btnMode", index}, build::mode(index));
|
||||
}
|
||||
|
||||
debounce_event::types::PinValue defaultValue(size_t index) {
|
||||
return getSetting({"btnDefVal", index}, build::defaultValue(index));
|
||||
}
|
||||
|
||||
debounce_event::types::PinMode pinMode(size_t index) {
|
||||
return getSetting({"btnPinMode", index}, build::pinMode(index));
|
||||
}
|
||||
|
||||
ButtonAction release(size_t index) {
|
||||
return getSetting({"btnRlse", index}, build::release(index));
|
||||
}
|
||||
|
||||
ButtonAction press(size_t index) {
|
||||
return getSetting({"btnPress", index}, build::press(index));
|
||||
}
|
||||
|
||||
ButtonAction click(size_t index) {
|
||||
return getSetting({"btnClick", index}, build::click(index));
|
||||
}
|
||||
|
||||
ButtonAction doubleClick(size_t index) {
|
||||
return getSetting({"btnDclk", index}, build::doubleClick(index));
|
||||
}
|
||||
|
||||
ButtonAction tripleClick(size_t index) {
|
||||
return getSetting({"btnTclk", index}, build::tripleClick(index));
|
||||
}
|
||||
|
||||
ButtonAction longClick(size_t index) {
|
||||
return getSetting({"btnLclk", index}, build::longClick(index));
|
||||
}
|
||||
|
||||
ButtonAction longLongClick(size_t index) {
|
||||
return getSetting({"btnLLclk", index}, build::longLongClick(index));
|
||||
}
|
||||
|
||||
unsigned long debounceDelay(size_t index) {
|
||||
return internal::indexedThenGlobal("btnDebDel", index, build::debounceDelay(index));
|
||||
}
|
||||
|
||||
unsigned long longClickDelay(size_t index) {
|
||||
return internal::indexedThenGlobal("btnLclkDel", index, build::longClickDelay(index));
|
||||
}
|
||||
|
||||
unsigned long longLongClickDelay(size_t index) {
|
||||
return internal::indexedThenGlobal("btnLLclkDel", index, build::longLongClickDelay(index));
|
||||
}
|
||||
|
||||
unsigned long repeatDelay() {
|
||||
return getSetting("btnRepDel", build::repeatDelay());
|
||||
}
|
||||
|
||||
unsigned long repeatDelay(size_t index) {
|
||||
return internal::indexedThenGlobal("btnRepDel", index, build::repeatDelay(index));
|
||||
}
|
||||
|
||||
size_t relay(size_t index) {
|
||||
return getSetting({"btnRelay", index}, build::relay(index));
|
||||
}
|
||||
|
||||
bool mqttSendAllEvents(size_t index) {
|
||||
return getSetting({"btnMqttSendAll", index}, build::mqttSendAllEvents(index));
|
||||
}
|
||||
|
||||
bool mqttRetain(size_t index) {
|
||||
return getSetting({"btnMqttRetain", index}, build::mqttRetain(index));
|
||||
}
|
||||
|
||||
#if BUTTON_PROVIDER_ANALOG_SUPPORT
|
||||
int analogLevel(size_t index) {
|
||||
return getSetting({"btnLevel", index}, build::analogLevel(index));
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace settings
|
||||
} // namespace
|
||||
} // namespace button
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr ButtonAction _buttonDecodeEventAction(const ButtonActions& actions, ButtonEvent event) {
|
||||
@@ -197,10 +640,9 @@ constexpr ButtonEvent _buttonMapReleased(uint8_t count, unsigned long length, un
|
||||
|
||||
debounce_event::types::Config _buttonRuntimeConfig(size_t index) {
|
||||
return {
|
||||
getSetting({"btnMode", index}, button::build::mode(index)),
|
||||
getSetting({"btnDefVal", index}, button::build::defaultValue(index)),
|
||||
getSetting({"btnPinMode", index}, button::build::pinMode(index))
|
||||
};
|
||||
button::settings::mode(index),
|
||||
button::settings::defaultValue(index),
|
||||
button::settings::pinMode(index)};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -221,18 +663,18 @@ ButtonEventDelays::ButtonEventDelays(unsigned long debounce, unsigned long repea
|
||||
lnglngclick(lnglngclick)
|
||||
{}
|
||||
|
||||
button_t::button_t(ButtonActions&& actions_, ButtonEventDelays&& delays_) :
|
||||
Button::Button(ButtonActions&& actions_, ButtonEventDelays&& delays_) :
|
||||
actions(std::move(actions_)),
|
||||
event_delays(std::move(delays_))
|
||||
{}
|
||||
|
||||
button_t::button_t(BasePinPtr&& pin, const debounce_event::types::Config& config, ButtonActions&& actions_, ButtonEventDelays&& delays_) :
|
||||
Button::Button(BasePinPtr&& pin, const debounce_event::types::Config& config, ButtonActions&& actions_, ButtonEventDelays&& delays_) :
|
||||
event_emitter(std::make_unique<debounce_event::EventEmitter>(std::move(pin), config, delays_.debounce, delays_.repeat)),
|
||||
actions(std::move(actions_)),
|
||||
event_delays(std::move(delays_))
|
||||
{}
|
||||
|
||||
ButtonEvent button_t::loop() {
|
||||
ButtonEvent Button::loop() {
|
||||
if (event_emitter) {
|
||||
switch (event_emitter->loop()) {
|
||||
case debounce_event::types::EventPressed:
|
||||
@@ -253,7 +695,7 @@ ButtonEvent button_t::loop() {
|
||||
return ButtonEvent::None;
|
||||
}
|
||||
|
||||
static std::vector<button_t> _buttons;
|
||||
static std::vector<Button> _buttons;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -335,7 +777,7 @@ void _buttonWebSocketOnVisible(JsonObject& root) {
|
||||
|
||||
void _buttonWebSocketOnConnected(JsonObject& root) {
|
||||
if (buttonCount()) {
|
||||
root["btnRepDel"] = getSetting("btnRepDel", button::build::repeatDelay());
|
||||
root["btnRepDel"] = button::settings::repeatDelay();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -526,42 +968,15 @@ void _buttonConfigure() {
|
||||
|
||||
for (decltype(buttons) id = 0; id < buttons; ++id) {
|
||||
#if RELAY_SUPPORT
|
||||
_button_relays.push_back(getSetting({"btnRelay", id}, button::build::relay(id)));
|
||||
_button_relays.push_back(button::settings::relay(id));
|
||||
#endif
|
||||
#if MQTT_SUPPORT
|
||||
_buttons_mqtt_send_all[id] = getSetting({"btnMqttSendAll", id}, button::build::mqttSendAllEvents(id));
|
||||
_buttons_mqtt_retain[id] = getSetting({"btnMqttRetain", id}, button::build::mqttRetain(id));
|
||||
_buttons_mqtt_send_all[id] = button::settings::mqttSendAllEvents(id);
|
||||
_buttons_mqtt_retain[id] = button::settings::mqttRetain(id);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: compatibility proxy, fetch global key before indexed
|
||||
unsigned long _buttonGetDelay(const char* key, size_t index, unsigned long default_value) {
|
||||
unsigned long result { default_value };
|
||||
|
||||
bool found { false };
|
||||
auto indexed = SettingsKey(key, index);
|
||||
auto global = String(key);
|
||||
|
||||
settings::internal::foreach([&](settings::kvs_type::KeyValueResult&& kv) {
|
||||
if (found) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ((kv.key.length != indexed.length()) && (kv.key.length != global.length())) {
|
||||
return;
|
||||
}
|
||||
|
||||
auto other = kv.key.read();
|
||||
found = indexed == other;
|
||||
if (found || (global == other)) {
|
||||
result = settings::internal::convert<unsigned long>(kv.value.read());
|
||||
}
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
void buttonLoop() {
|
||||
@@ -714,13 +1129,12 @@ namespace {
|
||||
|
||||
BasePinPtr _buttonGpioPin(size_t index, ButtonProvider provider) {
|
||||
BasePinPtr result;
|
||||
|
||||
auto pin [[gnu::unused]] = getSetting({"btnGpio", index}, button::build::pin(index));
|
||||
auto pin [[gnu::unused]] = button::settings::pin(index);
|
||||
|
||||
switch (provider) {
|
||||
case ButtonProvider::Gpio: {
|
||||
#if BUTTON_PROVIDER_GPIO_SUPPORT
|
||||
auto* base = gpioBase(getSetting({"btnGpioType", index}, button::build::pinType(index)));
|
||||
auto* base = gpioBase(button::settings::pinType(index));
|
||||
if (!base) {
|
||||
break;
|
||||
}
|
||||
@@ -740,7 +1154,7 @@ BasePinPtr _buttonGpioPin(size_t index, ButtonProvider provider) {
|
||||
break;
|
||||
}
|
||||
|
||||
auto level = getSetting({"btnLevel", index}, button::build::analogLevel(index));
|
||||
auto level = button::settings::analogLevel(index);
|
||||
if (!AnalogPin::checkExpectedLevel(level)) {
|
||||
break;
|
||||
}
|
||||
@@ -759,25 +1173,23 @@ BasePinPtr _buttonGpioPin(size_t index, ButtonProvider provider) {
|
||||
|
||||
ButtonActions _buttonActions(size_t index) {
|
||||
return {
|
||||
getSetting({"btnPress", index}, button::build::press(index)),
|
||||
getSetting({"btnRlse", index}, button::build::release(index)),
|
||||
getSetting({"btnClick", index}, button::build::click(index)),
|
||||
getSetting({"btnDclk", index}, button::build::doubleClick(index)),
|
||||
getSetting({"btnLclk", index}, button::build::longClick(index)),
|
||||
getSetting({"btnLLclk", index}, button::build::longLongClick(index)),
|
||||
getSetting({"btnTclk", index}, button::build::tripleClick(index))
|
||||
};
|
||||
button::settings::press(index),
|
||||
button::settings::release(index),
|
||||
button::settings::click(index),
|
||||
button::settings::doubleClick(index),
|
||||
button::settings::longClick(index),
|
||||
button::settings::longLongClick(index),
|
||||
button::settings::tripleClick(index)};
|
||||
}
|
||||
|
||||
// Note that we use settings without indexes as default values to preserve backwards compatibility
|
||||
|
||||
ButtonEventDelays _buttonDelays(size_t index) {
|
||||
return {
|
||||
_buttonGetDelay("btnDebDel", index, button::build::debounceDelay(index)),
|
||||
_buttonGetDelay("btnRepDel", index, button::build::repeatDelay(index)),
|
||||
_buttonGetDelay("btnLclkDel", index, button::build::longClickDelay(index)),
|
||||
_buttonGetDelay("btnLLclkDel", index, button::build::longLongClickDelay(index)),
|
||||
};
|
||||
button::settings::debounceDelay(index),
|
||||
button::settings::repeatDelay(index),
|
||||
button::settings::longClickDelay(index),
|
||||
button::settings::longLongClickDelay(index)};
|
||||
}
|
||||
|
||||
bool _buttonSetupProvider(size_t index, ButtonProvider provider) {
|
||||
@@ -834,7 +1246,7 @@ void buttonSetup() {
|
||||
migrateVersion(_buttonSettingsMigrate);
|
||||
|
||||
for (size_t index = 0; index < ButtonsMax; ++index) {
|
||||
auto provider = getSetting({"btnProv", index}, button::build::provider(index));
|
||||
auto provider = button::settings::provider(index);
|
||||
if (!_buttonSetupProvider(index, provider)) {
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user