LSH-Core
A robust C++ framework for Arduino-based home automation nodes
Loading...
Searching...
No Matches
clickable.hpp
Go to the documentation of this file.
1
21#ifndef LSHCORE_PERIPHERALS_INPUT_CLICKABLE_HPP
22#define LSHCORE_PERIPHERALS_INPUT_CLICKABLE_HPP
23
24#include <etl/vector.h>
25#include <stdint.h>
26
31
37{
38private:
43 enum class State : uint8_t
44 {
45 IDLE,
46 DEBOUNCING,
47 PRESSED,
48 RELEASED
49 };
50
55 enum class ActionFired : uint8_t
56 {
57 NONE,
58 LONG,
59 SUPER_LONG
60 };
61
62 // Bitfield for configuration flags to save RAM. These are "cold" data, mostly read after setup.
63 struct ClickableConfigFlags
64 {
65 uint8_t isShortClickable : 1 = true;
66 uint8_t isLongClickable : 1 = false;
67 uint8_t isSuperLongClickable : 1 = false;
68 uint8_t isNetworkLongClickable : 1 = false;
69 uint8_t isNetworkSuperLongClickable : 1 = false;
70 uint8_t isQuickClickable : 1 = false;
71 uint8_t isValid : 1 = false;
72 uint8_t isChecked : 1 = false;
73 };
74
75#ifndef CONFIG_USE_FAST_CLICKABLES
76 const uint8_t pinNumber;
77#else
78 const uint8_t pinMask;
79 const volatile uint8_t *const pinPort;
80#endif
81 uint8_t index = 0U;
82 const uint8_t id;
83
84 ClickableConfigFlags configFlags;
85
86 // Non-boolean configuration properties remain separate.
91
92 // State variables for the FSM remain separate for fast access ("hot" data).
93 State currentState = State::IDLE;
94 uint32_t stateChangeTime = 0U;
95 ActionFired lastActionFired = ActionFired::NONE;
96
97 // Attached actuators
98 etl::vector<uint8_t, CONFIG_MAX_ACTUATORS> actuatorsShort{};
99 etl::vector<uint8_t, CONFIG_MAX_ACTUATORS> actuatorsLong{};
100 etl::vector<uint8_t, CONFIG_MAX_ACTUATORS> actuatorsSuperLong{};
101
102 // Timings
103 uint16_t longClick_ms = constants::timings::CLICKABLE_LONG_CLICK_TIME_MS;
104 uint16_t superLongClick_ms = constants::timings::CLICKABLE_SUPER_LONG_CLICK_TIME_MS;
105
106protected:
107 uint8_t debounce_ms = constants::timings::CLICKABLE_DEBOUNCE_TIME_MS;
108
109public:
110#ifndef CONFIG_USE_FAST_CLICKABLES
117 explicit constexpr Clickable(uint8_t pin, uint8_t uniqueId) noexcept : pinNumber(pin), id(uniqueId) {}
118#else
125 explicit Clickable(uint8_t pin, uint8_t uniqueId) noexcept : pinMask(digitalPinToBitMask(pin)), pinPort(portInputRegister(digitalPinToPort(pin))), id(uniqueId) {}
126#endif
127
128// Delete copy constructor, copy assignment operator, move constructor and move assignment operator
129#if (__cplusplus >= 201703L) && (__GNUC__ >= 7)
130 Clickable(const Clickable &) = delete;
131 Clickable(Clickable &&) = delete;
132 auto operator=(const Clickable &) -> Clickable & = delete;
133 auto operator=(Clickable &&) -> Clickable & = delete;
134#endif // (__cplusplus >= 201703L) && (__GNUC__ >= 7)
135
136 [[nodiscard]] auto getState() -> bool; // Get the state of the clickable
137
138 void setIndex(uint8_t indexToSet); // Set the Clickable index on Clickables namespace Array
139
140 // Clickability setters
141 auto setClickableShort(bool shortClickable) -> Clickable &; // Set the short clickability of the clickable
142 auto setClickableLong(bool longClickable,
144 bool networkClickable = false,
145 constants::NoNetworkClickType fallback = constants::NoNetworkClickType::LOCAL_FALLBACK) -> Clickable &; // Set long click network clickability
146 auto setClickableSuperLong(bool superLongClickable,
148 bool networkClickable = false,
149 constants::NoNetworkClickType fallback = constants::NoNetworkClickType::LOCAL_FALLBACK) -> Clickable &; // Set super long click network clickability
150
151 // Actuators setter
152 auto addActuator(uint8_t actuatorIndex, constants::ClickType actuatorType) -> Clickable &; // Add an actuator
153 auto addActuatorShort(uint8_t actuatorIndex) -> Clickable &; // Add a short click actuator
154 auto addActuatorLong(uint8_t actuatorIndex) -> Clickable &; // Add a long click actuator
155 auto addActuatorSuperLong(uint8_t actuatorIndex) -> Clickable &; // Add a super long click actuator
156
157 // Timing setters
158 auto setDebounceTime(uint8_t timeToSet_ms) -> Clickable &; // Set debounce time in ms (0-255)
159 auto setLongClickTime(uint16_t timeToSet_ms) -> Clickable &; // Set long click time in ms (0-65535)
160 auto setSuperLongClickTime(uint16_t timeToSet_ms) -> Clickable &; // Set super long click time in ms (0-65535)
161
162 // Getters
163 [[nodiscard]] auto getIndex() const -> uint8_t; // Get the Clickable index on Clickables namespace Array
164 [[nodiscard]] auto getId() const -> uint8_t; // Return unique ID of the clickable
165 [[nodiscard]] auto getActuators(constants::ClickType actuatorType) const -> const etl::ivector<uint8_t> *; // Returns attached actuators of one kind
166 [[nodiscard]] auto getTotalActuators(constants::ClickType actuatorType) const -> uint8_t; // Returns the total number of a kind of actuators
167 [[nodiscard]] auto getLongClickType() const -> constants::LongClickType; // Returns the type of long click
168 [[nodiscard]] auto isNetworkClickable(constants::ClickType clickType) const -> bool; // Returns if the clickable is network clickable for long or super long click actions
169 [[nodiscard]] auto getNetworkFallback(constants::ClickType clickType) const -> constants::NoNetworkClickType; // Returns the fallback type for a give network click type
170 [[nodiscard]] auto getSuperLongClickType() const -> constants::SuperLongClickType; // Returns the type of super long click
171
172 // Utilities
173 auto check() -> bool; // Check for clickable coherence, it has to be clickable, it has to be connected to something
174 [[nodiscard]] auto shortClick() const -> bool; // Perform a short click action
175 [[nodiscard]] auto longClick() const -> bool; // Perform a long click action
176 [[nodiscard]] auto superLongClickSelective() const -> bool; // Perform a selective super long click;
177 [[nodiscard]] auto clickDetection() -> constants::ClickResult; // Processes input state and determines the click type using an internal Finite State Machine (FSM).
178 void resizeVectors(); // Resize vectors to actual needed size
179};
180
181#endif // LSHCORE_PERIPHERALS_INPUT_CLICKABLE_HPP
A class that represents a clickable object, like a button, and its associated logic.
Definition clickable.hpp:37
auto getActuators(constants::ClickType actuatorType) const -> const etl::ivector< uint8_t > *
Get the const vector of attached actuators.
Definition clickable.cpp:226
uint8_t debounce_ms
Debounce time in ms.
Definition clickable.hpp:107
auto setClickableSuperLong(bool superLongClickable, constants::SuperLongClickType clickType=constants::SuperLongClickType::NORMAL, bool networkClickable=false, constants::NoNetworkClickType fallback=constants::NoNetworkClickType::LOCAL_FALLBACK) -> Clickable &
Set clickable super long clickability locally and over network.
Definition clickable.cpp:93
auto longClick() const -> bool
Perform a long click action.
Definition clickable.cpp:385
auto setClickableLong(bool longClickable, constants::LongClickType clickType=constants::LongClickType::NORMAL, bool networkClickable=false, constants::NoNetworkClickType fallback=constants::NoNetworkClickType::LOCAL_FALLBACK) -> Clickable &
Set clickable long clickability locally and over network.
Definition clickable.cpp:75
auto isNetworkClickable(constants::ClickType clickType) const -> bool
Get if the clickable performs network clicks.
Definition clickable.cpp:279
auto setClickableShort(bool shortClickable) -> Clickable &
Set clickable short clickability.
Definition clickable.cpp:60
auto getState() -> bool
Get the state of the clickable if configured as INPUT with its external pulldown resistor (PIN -> BUT...
Definition clickable.cpp:35
auto getId() const -> uint8_t
Get the unique ID of the clickable.
Definition clickable.cpp:215
constexpr Clickable(uint8_t pin, uint8_t uniqueId) noexcept
Construct a new Clickable object, conventional IO version.
Definition clickable.hpp:117
void setIndex(uint8_t indexToSet)
Store the Clickable index on Clickables namespace Array.
Definition clickable.cpp:49
auto addActuatorLong(uint8_t actuatorIndex) -> Clickable &
Add a long click actuator.
Definition clickable.cpp:146
auto getSuperLongClickType() const -> constants::SuperLongClickType
Get the type of super long click.
Definition clickable.cpp:317
auto setSuperLongClickTime(uint16_t timeToSet_ms) -> Clickable &
Set super long click time.
Definition clickable.cpp:194
auto setDebounceTime(uint8_t timeToSet_ms) -> Clickable &
Set debounce time.
Definition clickable.cpp:170
auto addActuatorSuperLong(uint8_t actuatorIndex) -> Clickable &
Add a super long click actuator.
Definition clickable.cpp:158
auto setLongClickTime(uint16_t timeToSet_ms) -> Clickable &
Set long click time.
Definition clickable.cpp:182
auto shortClick() const -> bool
Perform a short click action.
Definition clickable.cpp:358
auto clickDetection() -> constants::ClickResult
Perform the clickable state detection.
Definition clickable.cpp:465
auto addActuatorShort(uint8_t actuatorIndex) -> Clickable &
Add a short click actuator.
Definition clickable.cpp:134
void resizeVectors()
Resize vectors to actual needed size.
Definition clickable.cpp:569
auto superLongClickSelective() const -> bool
Perform a super long click selective action.
Definition clickable.cpp:441
auto getIndex() const -> uint8_t
Get the Clickable index on Clickables namespace Array.
Definition clickable.cpp:205
auto getNetworkFallback(constants::ClickType clickType) const -> constants::NoNetworkClickType
Get the fallback type for a network click type.
Definition clickable.cpp:298
auto getTotalActuators(constants::ClickType actuatorType) const -> uint8_t
Get the number of attached actuators of one kind.
Definition clickable.cpp:247
auto check() -> bool
Validates the clickable's configuration.
Definition clickable.cpp:331
auto getLongClickType() const -> constants::LongClickType
Get the type of long click.
Definition clickable.cpp:267
auto addActuator(uint8_t actuatorIndex, constants::ClickType actuatorType) -> Clickable &
Add an actuator to a list of actuators controlled by the clickable.
Definition clickable.cpp:109
Defines the ClickResult enum for the clickable state machine.
Defines enums for various click types and fallback behaviors.
namespace for constants.
Definition config.hpp:33
SuperLongClickType
Super long click behaviors.
Definition clicktypes.hpp:60
@ NORMAL
Turns off all controllino unprotected actuators.
@ NONE
Never use this, it's just a default parameter.
LongClickType
Long click behaviors.
Definition clicktypes.hpp:48
@ NORMAL
Turns on or off secondary attached actuators based on how many are on or off.
@ NONE
Never use this, it's just a default parameter.
NoNetworkClickType
Fallback for a network click that expired or receives a failover.
Definition clicktypes.hpp:71
@ LOCAL_FALLBACK
Apply local logic.
@ NONE
Never use this, it's just a default parameter.
ClickType
Clickable (like button) click types.
Definition clicktypes.hpp:36
Centralizes all build-time configurable timing constants for the framework.
Internal bridge that imports user-defined macros into the library's scope.