SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
actuator/AW9364LedDriver.hpp
Go to the documentation of this file.
1
30#pragma once
31
32#include "../SensorBuildOpt.h"
33#if !SENSORLIB_EXCLUDE_AW9364
34
35#include "../expander/IoExpanderBase.hpp"
36
38constexpr uint8_t MAX_BRIGHTNESS_STEPS = 16;
39
52{
53public:
59 void begin(uint8_t pin)
60 {
61 _driver_pin = pin;
62 _base = nullptr;
63 this->_pinMode(_driver_pin, OUTPUT);
64 this->_digitalWrite(_driver_pin, LOW);
65 }
66
77 void begin(IoExpanderBase &base, uint8_t pin)
78 {
79 _base = &base;
80 _driver_pin = pin;
81 this->_pinMode(_driver_pin, OUTPUT);
82 this->_digitalWrite(_driver_pin, LOW);
83 }
84
99 void setBrightness(uint8_t value)
100 {
101 if (_brightness == value) {
102 return;
103 }
104 if (value > MAX_BRIGHTNESS_STEPS) {
105 value = MAX_BRIGHTNESS_STEPS;
106 }
107
108 // Turn off if brightness is zero
109 if (value == 0) {
110 this->_digitalWrite(_driver_pin, 0);
111 _brightness = 0;
112 return;
113 }
114
115 // If coming from zero, start with a HIGH pulse to enable output
116 if (_brightness == 0) {
117 this->_digitalWrite(_driver_pin, 1);
118 _brightness = MAX_BRIGHTNESS_STEPS;
119 }
120
121 // Calculate number of pulses needed to change from current brightness
122 int from = MAX_BRIGHTNESS_STEPS - _brightness;
123 int to = MAX_BRIGHTNESS_STEPS - value;
124 int num = (MAX_BRIGHTNESS_STEPS + to - from) % MAX_BRIGHTNESS_STEPS;
125
126 /* // Optional speed boost when using I2C expanders
127 // uint32_t frequency = 0;
128 // if (_base) {
129 // // Increasing I2C speed may be necessary for enough pulse rate.
130 // // Tested with XL9555: can run at 1000 kHz despite 400 kHz spec.
131 // frequency = _base->getClock();
132 // _base->setClock(1000000UL);
133 // }
134 */
135
136 // Generate the required number of pulses
137 for (int i = 0; i < num; i++) {
138 this->_digitalWrite(_driver_pin, 0);
139 this->_digitalWrite(_driver_pin, 1);
140 }
141
142 /* // Restore original communication speed
143 // if (_base) {
144 // _base->setClock(frequency);
145 // }
146 */
147
148 _brightness = value;
149 }
150
156 uint8_t getBrightness() const
157 {
158 return _brightness;
159 }
160
161protected:
168 void _pinMode(uint8_t pin, uint8_t mode)
169 {
170 if (_base) {
171 _base->pinMode(pin, mode);
172 } else {
173 pinMode(pin, mode);
174 }
175 }
176
183 void _digitalWrite(uint8_t pin, uint8_t value)
184 {
185 if (_base) {
186 _base->digitalWrite(pin, value);
187 } else {
188 digitalWrite(pin, value);
189 }
190 }
191
192private:
193 IoExpanderBase *_base;
194 uint8_t _driver_pin;
195 uint8_t _brightness = 0;
196};
197
198#endif
constexpr uint8_t MAX_BRIGHTNESS_STEPS
Maximum brightness steps (0‑16).
Controls LED brightness by generating a specific number of pulses on a GPIO pin.
void _digitalWrite(uint8_t pin, uint8_t value)
Internal helper to write digital value, handling both native and expander pins.
uint8_t getBrightness() const
Get the current brightness setting.
void setBrightness(uint8_t value)
Set the LED brightness level.
void _pinMode(uint8_t pin, uint8_t mode)
Internal helper to set pin mode, handling both native and expander pins.
void begin(IoExpanderBase &base, uint8_t pin)
Initialize the driver for use with a GPIO expander pin.
void begin(uint8_t pin)
Initialize the driver for use with a native Arduino pin.
Abstract base class for GPIO expander devices.
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.
uint8_t i