LSH-Core
Deterministic firmware core for Controllino-based Labo Smart Home nodes
 
Loading...
Searching...
No Matches
indicator.hpp
Go to the documentation of this file.
1
21#ifndef LSH_CORE_PERIPHERALS_OUTPUT_INDICATOR_HPP
22#define LSH_CORE_PERIPHERALS_OUTPUT_INDICATOR_HPP
23
24#include <stdint.h>
25
27#include "internal/pin_tag.hpp"
29#ifdef CONFIG_USE_FAST_INDICATORS
31#endif
32
38{
39private:
40#ifndef CONFIG_USE_FAST_INDICATORS
41 const uint8_t pinNumber;
42#else
43 const uint8_t pinMask;
44 volatile uint8_t *const pinPort;
45
52 explicit Indicator(lsh::core::avr::FastOutputPinBinding binding) noexcept : pinMask(binding.mask), pinPort(binding.pinPort)
53 {
54 // Indicators default to OFF. Prime the output latch before enabling
55 // OUTPUT so setup cannot briefly expose an inherited HIGH level.
56 const uint8_t oldSREG = SREG;
57 cli();
58 *this->pinPort &= ~this->pinMask;
59 *binding.modePort |= this->pinMask;
60 SREG = oldSREG;
61 }
62#endif
63#if defined(LSH_DEBUG) || defined(LSH_STATIC_CONFIG_RUNTIME_CHECKS)
64 uint8_t index = UINT8_MAX;
65#endif
66 bool actualState = false;
67
68public:
69#ifndef CONFIG_USE_FAST_INDICATORS
74 explicit LSH_OPTIONAL_CONSTEXPR_CTOR Indicator(uint8_t pin) noexcept : pinNumber(pin)
75 {
76 digitalWrite(pin, LOW);
77 pinMode(pin, OUTPUT);
78 }
79
86 template <uint8_t Pin>
87 explicit LSH_OPTIONAL_CONSTEXPR_CTOR Indicator(lsh::core::PinTag<Pin>) noexcept : Indicator(static_cast<uint8_t>(Pin))
88 {}
89#else
94 explicit Indicator(uint8_t pin) noexcept : Indicator(lsh::core::avr::makeFastOutputPinBinding(pin))
95 {}
96
103 template <uint8_t Pin>
104 explicit Indicator(lsh::core::PinTag<Pin>) noexcept : Indicator(lsh::core::avr::makeFastOutputPinBinding(lsh::core::PinTag<Pin>{}))
105 {}
106#endif
107
108#if LSH_USING_CPP17
109 Indicator(const Indicator &) = delete;
110 Indicator(Indicator &&) = delete;
111 auto operator=(const Indicator &) -> Indicator & = delete;
112 auto operator=(Indicator &&) -> Indicator & = delete;
113#endif // LSH_USING_CPP17
114
120 void setState(bool stateToSet)
121 {
122#ifdef CONFIG_USE_FAST_INDICATORS
123 if (!stateToSet)
124 {
125 *this->pinPort &= ~this->pinMask;
126 }
127 else
128 {
129 *this->pinPort |= this->pinMask;
130 }
131#else
132 digitalWrite(this->pinNumber, static_cast<uint8_t>(stateToSet));
133#endif
134 }
135 void applyComputedState(bool newState)
136 {
137 // Generated static profiles compute the indicator expression directly.
138 // Keep the state-change guard here so refreshIndicators() can avoid the
139 // old index-based Indicator::check() path in release builds.
140 if (newState == this->actualState)
141 {
142 return;
143 }
144 this->actualState = newState;
145 this->setState(newState);
146 }
147 void setIndex(uint8_t indexToSet); // Set the indicator index on Indicators namespace Array
148 void check(); // Perform the actual check
149
150 [[nodiscard]] auto getIndex() const -> uint8_t; // Get the indicator index on Indicators namespace Array
151};
152
153#endif // LSH_CORE_PERIPHERALS_OUTPUT_INDICATOR_HPP
Typed helpers for AVR fast I/O access without Arduino macro casts.
Represents a state indicator for one or more attached actuators, indicators are normally connected to...
Definition indicator.hpp:38
LSH_OPTIONAL_CONSTEXPR_CTOR Indicator(uint8_t pin) noexcept
Construct a new Indicator object using standard I/O.
Definition indicator.hpp:74
void setIndex(uint8_t indexToSet)
Set the indicator index on Indicators namespace Array.
Definition indicator.cpp:31
auto getIndex() const -> uint8_t
Get the indicator index on Indicators namespace Array.
Definition indicator.cpp:68
void setState(bool stateToSet)
Set the state of the indicator.
Definition indicator.hpp:120
LSH_OPTIONAL_CONSTEXPR_CTOR Indicator(lsh::core::PinTag< Pin >) noexcept
Construct an indicator from a compile-time pin tag on the slow-I/O path.
Definition indicator.hpp:87
void check()
Switch the indicator based on controlled actuators status.
Definition indicator.cpp:55
Centralized C++ feature-detection macros for lsh-core.
Compile-time pin tag used to route peripheral construction through constant pin paths.
Type-level wrapper around one Arduino pin number.
Definition pin_tag.hpp:41
Resolved fast-output binding for one Arduino pin.
Definition avr_fast_io.hpp:62
volatile uint8_t * modePort
Final AVR DDR register used to configure the pin direction.
Definition avr_fast_io.hpp:65
volatile uint8_t * pinPort
Final AVR output register used to drive the pin.
Definition avr_fast_io.hpp:64
uint8_t mask
Final bit mask for the pin inside the AVR port.
Definition avr_fast_io.hpp:63
Internal bridge that imports static profile resources into the library's scope.