LSH-Core
A robust C++ framework for Arduino-based home automation nodes
Loading...
Searching...
No Matches
actuator.hpp
Go to the documentation of this file.
1
21#ifndef LSHCORE_PERIPHERALS_OUTPUT_ACTUATOR_HPP
22#define LSHCORE_PERIPHERALS_OUTPUT_ACTUATOR_HPP
23
24#include <stdint.h>
25
27
33{
34private:
35#ifndef CONFIG_USE_FAST_ACTUATORS
36 const uint8_t pinNumber;
37#else
38 const uint8_t pinMask;
39 volatile uint8_t *const pinPort;
40#endif
41 uint8_t index = 0U;
42 const bool defaultState;
43 bool actualState = false;
44 uint32_t lastTimeSwitched = 0U;
45 bool isProtected = false;
46 uint8_t id;
47 bool hasAutoOffTimer = false;
48 uint32_t autoOffTimer_ms = 0U;
49
50public:
51#ifndef CONFIG_USE_FAST_ACTUATORS
59 explicit constexpr Actuator(uint8_t pin, uint8_t uniqueId, bool normalState = false) noexcept : pinNumber(pin), defaultState(normalState), id(uniqueId)
60 {
61 pinMode(pin, OUTPUT); // PinMode to Output
62 digitalWrite(pin, static_cast<uint8_t>(normalState)); // Set the default state
63 }
64#else
72 explicit Actuator(uint8_t pin, uint8_t uniqueId, bool normalState = false) noexcept : pinMask(digitalPinToBitMask(pin)), pinPort(portOutputRegister(digitalPinToPort(pin))), defaultState(normalState), id(uniqueId)
73 {
74 // PinMode to OUTPUT
75 uint8_t port = digitalPinToPort(pin);
76 volatile uint8_t *const reg = portModeRegister(port);
77 const uint8_t oldSREG = SREG;
78 cli();
79 *reg |= this->pinMask;
80 SREG = oldSREG;
81
82 // Set the default state
83 if (!normalState)
84 {
85 *this->pinPort &= ~this->pinMask;
86 }
87 else
88 {
89 *this->pinPort |= this->pinMask;
90 }
91 }
92#endif
93
94 /* Workaround for https://stackoverflow.com/questions/28788353/clang-wweak-vtables-and-pure-abstract-class
95 and https://stackoverflow.com/questions/28786473/clang-no-out-of-line-virtual-method-definitions-pure-abstract-c-class/40550578 */
96
97#if (__cplusplus >= 201703L) && (__GNUC__ >= 7)
98 Actuator(const Actuator &) = delete;
99 Actuator(Actuator &&) = delete;
100 auto operator=(const Actuator &) -> Actuator & = delete;
101 auto operator=(Actuator &&) -> Actuator & = delete;
102#endif // (__cplusplus >= 201703L) && (__GNUC__ >= 7)
103
104 // Setters
105 [[nodiscard]] auto setState(bool state) -> bool; // Sets the new state of the actuator, respecting debounce time.
106
107 void setIndex(uint8_t indexToSet); // Set the actuator index on Actuators namespace Array
108 auto setAutoOffTimer(uint32_t time_ms) -> Actuator &; // Set "turn off" timer in ms
109 auto setProtected(bool hasProtection) -> Actuator &; // Set protection against global "turn-off" actions (e.g., a general super long click).
110
111 // Getters
112 [[nodiscard]] auto getIndex() const -> uint8_t; // Get the actuator index on Actuators namespace Array
113 [[nodiscard]] auto getId() const -> uint8_t; // Return unique ID of the actuator
114 [[nodiscard]] auto getState() const -> bool; // Returns the state of the actuator (false=OFF, true=ON)
115 [[nodiscard]] auto getDefaultState() const -> bool; // Returns the default state of the actuator
116 [[nodiscard]] auto hasAutoOff() const -> bool; // Returns if the actuators has a timer set
117 [[nodiscard]] auto getAutoOffTimer() const -> uint32_t; // Returns the timer of the actuator
118 [[nodiscard]] auto hasProtection() const -> bool; // Returns true if the actuator is protected from global "turn-off" actions.
119
120 // Utils
121 [[nodiscard]] auto toggleState() -> bool; // Switch the actuator
122 [[nodiscard]] auto checkAutoOffTimer() -> bool; // Checks if auto off timer is over, turn off if it's over
123};
124
125#endif // LSHCORE_PERIPHERALS_OUTPUT_ACTUATOR_HPP
Represents an actuator (relay) attached to a digital pin.
Definition actuator.hpp:33
void setIndex(uint8_t indexToSet)
store the actuator index in Actuators namespace array.
Definition actuator.cpp:69
auto setProtected(bool hasProtection) -> Actuator &
Set protection against some turn ON/OFF behaviour.
Definition actuator.cpp:93
auto toggleState() -> bool
Switch the state of the actuator (if it was OFF is going to be ON and vice versa).
Definition actuator.cpp:179
auto getId() const -> uint8_t
Get the unique ID of the actuator.
Definition actuator.cpp:114
auto getState() const -> bool
Get the state of the actuator.
Definition actuator.cpp:125
auto setAutoOffTimer(uint32_t time_ms) -> Actuator &
Set turn off timer, it represents the timer after that the actuator will be switched off.
Definition actuator.cpp:80
auto hasAutoOff() const -> bool
Get if the actuator has a timer set.
Definition actuator.cpp:147
constexpr Actuator(uint8_t pin, uint8_t uniqueId, bool normalState=false) noexcept
Construct a new Actuator object, conventional IO version.
Definition actuator.hpp:59
auto setState(bool state) -> bool
Set the new actuator state if the new state can be set.
Definition actuator.cpp:33
auto getDefaultState() const -> bool
Get the default state of the actuator.
Definition actuator.cpp:136
auto getIndex() const -> uint8_t
Get the actuator index on Actuators namespace array.
Definition actuator.cpp:104
auto checkAutoOffTimer() -> bool
Checks if auto off timer is over, switch OFF the actuator if it's over.
Definition actuator.cpp:190
auto hasProtection() const -> bool
Get if the actuators is protected against some turn ON/OFF behaviour.
Definition actuator.cpp:168
auto getAutoOffTimer() const -> uint32_t
Get the timer of the actuator in ms.
Definition actuator.cpp:157
Internal bridge that imports user-defined macros into the library's scope.