SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
IoExpanderBase.hpp
Go to the documentation of this file.
1
29#pragma once
30
31#include "SensorPlatform.hpp"
32
43{
44public:
55 {
56 _pinModes = std::unique_ptr<uint8_t[]>(new (std::nothrow) uint8_t[pinCount]());
57 _outputStates = std::unique_ptr<bool[]>(new (std::nothrow) bool[pinCount]());
58 if (!_pinModes || !_outputStates) {
59 _pinCount = 0;
60 }
61 }
62
69 virtual ~IoExpanderBase() = default;
70
77 virtual void deinit()
78 {
79 }
80
99 void configPins(uint16_t pinMask, uint8_t mode)
100 {
101 if (_pinModes == nullptr) {
102 SENSORLIB_LOG_E("Pin modes array not initialized");
103 return;
104 }
105 if (mode != INPUT && mode != OUTPUT) {
106 SENSORLIB_LOG_E("Invalid pin mode");
107 return;
108 }
109 configPinsImpl(pinMask, mode);
110 }
111
112
122 void pinMode(uint8_t pin, uint8_t mode)
123 {
124 if (pin >= _pinCount) {
125 SENSORLIB_LOG_E("Invalid pin number");
126 return;
127 }
128 if (_pinModes == nullptr) {
129 SENSORLIB_LOG_E("Pin modes array not initialized");
130 return;
131 }
132 if (mode != INPUT && mode != OUTPUT) {
133 SENSORLIB_LOG_E("Invalid pin mode");
134 return;
135 }
136
137 _pinModes[pin] = mode;
138 pinModeImpl(pin, mode);
139 }
140
150 void digitalWrite(uint8_t pin, bool value)
151 {
152 if (pin >= _pinCount) {
153 SENSORLIB_LOG_E("Invalid pin number");
154 return;
155 }
156 if (_pinModes == nullptr) {
157 SENSORLIB_LOG_E("Pin modes array not initialized");
158 return;
159 }
160 if (_outputStates == nullptr) {
161 SENSORLIB_LOG_E("Output states array not initialized");
162 return;
163 }
164 if (_pinModes[pin] != OUTPUT) {
165 SENSORLIB_LOG_E("Pin %d is not configured as OUTPUT", pin);
166 return;
167 }
168
169 _outputStates[pin] = value;
170 digitalWriteImpl(pin, value);
171 }
172
182 bool digitalRead(uint8_t pin)
183 {
184 if (pin >= _pinCount) {
185 SENSORLIB_LOG_E("Invalid pin number");
186 return false;
187 }
188 if (_pinModes == nullptr) {
189 SENSORLIB_LOG_E("Pin modes array not initialized");
190 return false;
191 }
192 if (_pinModes[pin] != INPUT) {
193 SENSORLIB_LOG_E("Pin %d is not configured as INPUT", pin);
194 return false;
195 }
196
197 return digitalReadImpl(pin);
198 }
199
207 void digitalToggle(uint8_t pin)
208 {
209 if (!_pinModes || !_outputStates) {
210 SENSORLIB_LOG_E("Expander not properly initialized");
211 return;
212 }
213 if (_pinModes[pin] != OUTPUT) {
214 SENSORLIB_LOG_E("Pin %d is not configured as OUTPUT", pin);
215 return;
216 }
217 int state = 1 - _outputStates[pin];
218 digitalWrite(pin, state);
219 }
220
231 virtual void digitalWritePort(uint16_t mask, uint16_t values)
232 {
233 for (uint8_t i = 0; i < _pinCount; i++) {
234 if (mask & (1UL << i)) {
235 digitalWrite(i, (values >> i) & 1);
236 }
237 }
238 }
239
248 virtual uint16_t digitalReadPort()
249 {
250 uint16_t result = 0;
251 if (!_pinModes || !_outputStates) {
252 SENSORLIB_LOG_E("Expander not properly initialized");
253 return 0;
254 }
255 for (uint8_t i = 0; i < _pinCount; i++) {
256 if (_pinModes[i] == INPUT) {
257 result |= (digitalRead(i) ? (1UL << i) : 0);
258 }
259 }
260 return result;
261 }
262
268 uint8_t pinCount() const
269 {
270 return _pinCount;
271 }
272
273
285 uint16_t pinToMask(uint8_t pin) const
286 {
287 if (pin >= _pinCount) {
288 SENSORLIB_LOG_E("Invalid pin number");
289 return 0;
290 }
291 return (1UL << pin);
292 }
293
294protected:
306 virtual bool initImpl(uint8_t param) = 0;
307
314 virtual void configPinsImpl(uint16_t pinMask, uint8_t mode) = 0;
315
324 virtual void pinModeImpl(uint8_t pin, uint8_t mode) = 0;
325
334 virtual void digitalWriteImpl(uint8_t pin, bool value) = 0;
335
345 virtual bool digitalReadImpl(uint8_t pin) = 0;
346
347 uint8_t _pinCount;
348 std::unique_ptr<uint8_t[]> _pinModes;
349 std::unique_ptr<bool[]> _outputStates;
350};
#define SENSORLIB_LOG_E(...)
@license MIT License
Abstract base class for GPIO expander devices.
virtual ~IoExpanderBase()=default
Virtual destructor.
std::unique_ptr< bool[]> _outputStates
Cached output states (true = HIGH)
virtual void deinit()
Deinitialize the expander.
void digitalToggle(uint8_t pin)
Toggle the state of an output pin.
virtual bool initImpl(uint8_t param)=0
Chip‑specific initialization.
bool digitalRead(uint8_t pin)
Read the digital value from an input pin.
virtual void digitalWriteImpl(uint8_t pin, bool value)=0
Hardware‑specific implementation of digitalWrite().
std::unique_ptr< uint8_t[]> _pinModes
Cached pin modes (INPUT/OUTPUT)
virtual uint16_t digitalReadPort()
Read the values of all input pins.
virtual void pinModeImpl(uint8_t pin, uint8_t mode)=0
Hardware‑specific implementation of pinMode().
uint8_t _pinCount
Number of GPIO pins.
void pinMode(uint8_t pin, uint8_t mode)
Set the direction of a GPIO pin.
void digitalWrite(uint8_t pin, bool value)
Write a digital value to an output pin.
void configPins(uint16_t pinMask, uint8_t mode)
Configure multiple pins as input or output simultaneously.
virtual void digitalWritePort(uint16_t mask, uint16_t values)
Write to multiple pins simultaneously using a mask.
uint8_t pinCount() const
Get the total number of pins provided by this expander.
virtual bool digitalReadImpl(uint8_t pin)=0
Hardware‑specific implementation of digitalRead().
IoExpanderBase(uint8_t pinCount)
Construct a new IoExpanderBase object.
virtual void configPinsImpl(uint16_t pinMask, uint8_t mode)=0
Hardware‑specific implementation of batch pin mode configuration.
uint16_t pinToMask(uint8_t pin) const
Convert a pin number to its corresponding bitmask.
uint8_t i