21#ifndef LSHCORE_PERIPHERALS_OUTPUT_ACTUATOR_HPP
22#define LSHCORE_PERIPHERALS_OUTPUT_ACTUATOR_HPP
35#ifndef CONFIG_USE_FAST_ACTUATORS
36 const uint8_t pinNumber;
38 const uint8_t pinMask;
39 volatile uint8_t *
const pinPort;
42 const bool defaultState;
43 bool actualState =
false;
44 uint32_t lastTimeSwitched = 0U;
45 bool isProtected =
false;
47 bool hasAutoOffTimer =
false;
48 uint32_t autoOffTimer_ms = 0U;
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)
62 digitalWrite(pin,
static_cast<uint8_t
>(normalState));
72 explicit Actuator(uint8_t pin, uint8_t uniqueId,
bool normalState =
false) noexcept : pinMask(digitalPinToBitMask(pin)), pinPort(portOutputRegister(digitalPinToPort(pin))), defaultState(normalState),
id(uniqueId)
75 uint8_t port = digitalPinToPort(pin);
76 volatile uint8_t *
const reg = portModeRegister(port);
77 const uint8_t oldSREG = SREG;
79 *reg |= this->pinMask;
85 *this->pinPort &= ~this->pinMask;
89 *this->pinPort |= this->pinMask;
97#if (__cplusplus >= 201703L) && (__GNUC__ >= 7)
105 [[nodiscard]]
auto setState(
bool state) -> bool;
112 [[nodiscard]]
auto getIndex() const -> uint8_t;
113 [[nodiscard]] auto
getId() const -> uint8_t;
114 [[nodiscard]] auto
getState() const ->
bool;
116 [[nodiscard]] auto
hasAutoOff() const ->
bool;
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.