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 // Data structures definitions
64 struct ClickableConfigFlags
65 {
66 uint8_t isShortClickable : 1 = true;
67 uint8_t isLongClickable : 1 = false;
68 uint8_t isSuperLongClickable : 1 = false;
69 uint8_t isNetworkLongClickable : 1 = false;
70 uint8_t isNetworkSuperLongClickable : 1 = false;
71 uint8_t isQuickClickable : 1 = false;
72 uint8_t isValid : 1 = false;
73 uint8_t isChecked : 1 = false;
74 };
75
76 // Bitfield for configuration flags to save RAM. "Hot" data, read every detection.
77 ClickableConfigFlags configFlags;
78
79#ifndef CONFIG_USE_FAST_CLICKABLES
80 const uint8_t pinNumber;
81#else
82 const uint8_t pinMask;
83 const volatile uint8_t *const pinPort;
84#endif
85 uint8_t index = 0U;
86 const uint8_t id;
87
88 // State variables for the FSM ("hot" data, move to top for <64 byte offset optimization).
89 State currentState = State::IDLE;
90 uint32_t stateChangeTime = 0U;
91 ActionFired lastActionFired = ActionFired::NONE;
92
93 // Timings ("hot" configuration, move to top)
94 uint16_t longClick_ms = constants::timings::CLICKABLE_LONG_CLICK_TIME_MS;
95 uint16_t superLongClick_ms = constants::timings::CLICKABLE_SUPER_LONG_CLICK_TIME_MS;
96protected:
97 uint8_t debounce_ms = constants::timings::CLICKABLE_DEBOUNCE_TIME_MS;
98private:
99
100 // Non-boolean configuration properties (Cold data, rarely accessed in tight loops)
105
106 // Attached actuators (HUGE size due to static storage)
107 // Keep at the bottom to avoid pushing hot variables out of the 64-byte displacement range.
108 etl::vector<uint8_t, CONFIG_MAX_ACTUATORS> actuatorsShort{};
109 etl::vector<uint8_t, CONFIG_MAX_ACTUATORS> actuatorsLong{};
110 etl::vector<uint8_t, CONFIG_MAX_ACTUATORS> actuatorsSuperLong{};
111
112public:
113#ifndef CONFIG_USE_FAST_CLICKABLES
120 explicit constexpr Clickable(uint8_t pin, uint8_t uniqueId) noexcept : pinNumber(pin), id(uniqueId) {}
121#else
128 explicit Clickable(uint8_t pin, uint8_t uniqueId) noexcept : pinMask(digitalPinToBitMask(pin)), pinPort(portInputRegister(digitalPinToPort(pin))), id(uniqueId) {}
129#endif
130
131// Delete copy constructor, copy assignment operator, move constructor and move assignment operator
132#if (__cplusplus >= 201703L) && (__GNUC__ >= 7)
133 Clickable(const Clickable &) = delete;
134 Clickable(Clickable &&) = delete;
135 auto operator=(const Clickable &) -> Clickable & = delete;
136 auto operator=(Clickable &&) -> Clickable & = delete;
137#endif // (__cplusplus >= 201703L) && (__GNUC__ >= 7)
138
145 inline auto getState() const -> bool
146 {
147#ifdef CONFIG_USE_FAST_CLICKABLES
148 return (*this->pinPort & this->pinMask) != 0U;
149#else
150 return (static_cast<bool>(digitalRead(this->pinNumber)));
151#endif
152 }
153
154 void setIndex(uint8_t indexToSet); // Set the Clickable index on Clickables namespace Array
155
156 // Clickability setters
157 auto setClickableShort(bool shortClickable) -> Clickable &; // Set the short clickability of the clickable
158 auto setClickableLong(bool longClickable,
160 bool networkClickable = false,
161 constants::NoNetworkClickType fallback = constants::NoNetworkClickType::LOCAL_FALLBACK) -> Clickable &; // Set long click network clickability
162 auto setClickableSuperLong(bool superLongClickable,
164 bool networkClickable = false,
165 constants::NoNetworkClickType fallback = constants::NoNetworkClickType::LOCAL_FALLBACK) -> Clickable &; // Set super long click network clickability
166
167 // Actuators setter
168 auto addActuator(uint8_t actuatorIndex, constants::ClickType actuatorType) -> Clickable &; // Add an actuator
169 auto addActuatorShort(uint8_t actuatorIndex) -> Clickable &; // Add a short click actuator
170 auto addActuatorLong(uint8_t actuatorIndex) -> Clickable &; // Add a long click actuator
171 auto addActuatorSuperLong(uint8_t actuatorIndex) -> Clickable &; // Add a super long click actuator
172
173 // Timing setters
174 auto setDebounceTime(uint8_t timeToSet_ms) -> Clickable &; // Set debounce time in ms (0-255)
175 auto setLongClickTime(uint16_t timeToSet_ms) -> Clickable &; // Set long click time in ms (0-65535)
176 auto setSuperLongClickTime(uint16_t timeToSet_ms) -> Clickable &; // Set super long click time in ms (0-65535)
177
178 // Getters
179 [[nodiscard]] auto getIndex() const -> uint8_t; // Get the Clickable index on Clickables namespace Array
180 [[nodiscard]] auto getId() const -> uint8_t; // Return unique ID of the clickable
181 [[nodiscard]] auto getActuators(constants::ClickType actuatorType) const -> const etl::ivector<uint8_t> *; // Returns attached actuators of one kind
182 [[nodiscard]] auto getTotalActuators(constants::ClickType actuatorType) const -> uint8_t; // Returns the total number of a kind of actuators
183 [[nodiscard]] auto getLongClickType() const -> constants::LongClickType; // Returns the type of long click
184 [[nodiscard]] auto isNetworkClickable(constants::ClickType clickType) const -> bool; // Returns if the clickable is network clickable for long or super long click actions
185 [[nodiscard]] auto getNetworkFallback(constants::ClickType clickType) const -> constants::NoNetworkClickType; // Returns the fallback type for a give network click type
186 [[nodiscard]] auto getSuperLongClickType() const -> constants::SuperLongClickType; // Returns the type of super long click
187
188 // Utilities
189 auto check() -> bool; // Check for clickable coherence, it has to be clickable, it has to be connected to something
190 [[nodiscard]] auto shortClick() const -> bool; // Perform a short click action
191 [[nodiscard]] auto longClick() const -> bool; // Perform a long click action
192 [[nodiscard]] auto superLongClickSelective() const -> bool; // Perform a selective super long click;
193 [[nodiscard]] auto clickDetection() -> constants::ClickResult; // Processes input state and determines the click type using an internal Finite State Machine (FSM).
194
195};
196
197#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:213
uint8_t debounce_ms
Debounce time in ms.
Definition clickable.hpp:97
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:80
auto longClick() const -> bool
Perform a long click action.
Definition clickable.cpp:372
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:62
auto isNetworkClickable(constants::ClickType clickType) const -> bool
Get if the clickable performs network clicks.
Definition clickable.cpp:266
auto setClickableShort(bool shortClickable) -> Clickable &
Set clickable short clickability.
Definition clickable.cpp:47
auto getId() const -> uint8_t
Get the unique ID of the clickable.
Definition clickable.cpp:202
constexpr Clickable(uint8_t pin, uint8_t uniqueId) noexcept
Construct a new Clickable object, conventional IO version.
Definition clickable.hpp:120
void setIndex(uint8_t indexToSet)
Store the Clickable index on Clickables namespace Array.
Definition clickable.cpp:36
auto addActuatorLong(uint8_t actuatorIndex) -> Clickable &
Add a long click actuator.
Definition clickable.cpp:133
auto getSuperLongClickType() const -> constants::SuperLongClickType
Get the type of super long click.
Definition clickable.cpp:304
auto setSuperLongClickTime(uint16_t timeToSet_ms) -> Clickable &
Set super long click time.
Definition clickable.cpp:181
auto setDebounceTime(uint8_t timeToSet_ms) -> Clickable &
Set debounce time.
Definition clickable.cpp:157
auto addActuatorSuperLong(uint8_t actuatorIndex) -> Clickable &
Add a super long click actuator.
Definition clickable.cpp:145
auto setLongClickTime(uint16_t timeToSet_ms) -> Clickable &
Set long click time.
Definition clickable.cpp:169
auto shortClick() const -> bool
Perform a short click action.
Definition clickable.cpp:345
auto clickDetection() -> constants::ClickResult
Perform the clickable state detection.
Definition clickable.cpp:452
auto addActuatorShort(uint8_t actuatorIndex) -> Clickable &
Add a short click actuator.
Definition clickable.cpp:121
auto superLongClickSelective() const -> bool
Perform a super long click selective action.
Definition clickable.cpp:428
auto getIndex() const -> uint8_t
Get the Clickable index on Clickables namespace Array.
Definition clickable.cpp:192
auto getNetworkFallback(constants::ClickType clickType) const -> constants::NoNetworkClickType
Get the fallback type for a network click type.
Definition clickable.cpp:285
auto getTotalActuators(constants::ClickType actuatorType) const -> uint8_t
Get the number of attached actuators of one kind.
Definition clickable.cpp:234
auto getState() const -> bool
Get the state of the clickable if configured as INPUT with its external pulldown resistor (PIN -> BUT...
Definition clickable.hpp:145
auto check() -> bool
Validates the clickable's configuration.
Definition clickable.cpp:318
auto getLongClickType() const -> constants::LongClickType
Get the type of long click.
Definition clickable.cpp:254
auto addActuator(uint8_t actuatorIndex, constants::ClickType actuatorType) -> Clickable &
Add an actuator to a list of actuators controlled by the clickable.
Definition clickable.cpp:96
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.