8#include "ExtendedInputOutput.hpp"
44 AnalogMultiplex(pin_t analogPin, const Array<pin_t, N> &addressPins,
45 pin_t enablePin = NO_PIN)
46 : analogPin(analogPin), addressPins(addressPins), enablePin(enablePin) {
64 void pinMode(pin_t pin, PinMode_t mode) override;
69 void pinModeBuffered(pin_t pin, PinMode_t mode) override;
75 void digitalWrite(pin_t, PinStatus_t) override
76 __attribute__((deprecated)) {}
81 void digitalWriteBuffered(pin_t, PinStatus_t) override
82 __attribute__((deprecated)) {}
90 PinStatus_t digitalRead(pin_t pin) override;
95 PinStatus_t digitalReadBuffered(pin_t pin) override;
103 analog_t analogRead(pin_t pin) override;
108 analog_t analogReadBuffered(pin_t pin) override;
114 void analogWrite(pin_t, analog_t) override
115 __attribute__((deprecated)) {}
120 void analogWriteBuffered(pin_t, analog_t) override
121 __attribute__((deprecated)) {}
127 void begin() override;
133 void updateBufferedOutputs() override {}
139 void updateBufferedInputs() override {}
145 void discardFirstReading(bool discardFirstReading_) {
146 this->discardFirstReading_ = discardFirstReading_;
150 const pin_t analogPin;
151 const Array<pin_t, N> addressPins;
152 const pin_t enablePin;
153 bool discardFirstReading_ = true;
162 void setMuxAddress(uint8_t address);
170 void prepareReading(uint8_t address);
178 constexpr static auto MUX_ENABLED = LOW;
179 constexpr static auto MUX_DISABLED = HIGH;
188using CD74HC4067 = AnalogMultiplex<4>;
196using CD74HC4051 = AnalogMultiplex<3>;
201void AnalogMultiplex<N>::pinMode(pin_t, PinMode_t mode) {
202 ExtIO::pinMode(analogPin, mode);
206void AnalogMultiplex<N>::pinModeBuffered(pin_t, PinMode_t mode) {
207 AnalogMultiplex<N>::pinMode(analogPin, mode);
211PinStatus_t AnalogMultiplex<N>::digitalRead(pin_t pin) {
213 PinStatus_t result = ExtIO::digitalRead(analogPin);
219PinStatus_t AnalogMultiplex<N>::digitalReadBuffered(pin_t pin) {
220 return AnalogMultiplex<N>::digitalRead(pin);
224analog_t AnalogMultiplex<N>::analogRead(pin_t pin) {
226 if (discardFirstReading_)
227 (void)ExtIO::analogRead(analogPin);
228 analog_t result = ExtIO::analogRead(analogPin);
234analog_t AnalogMultiplex<N>::analogReadBuffered(pin_t pin) {
235 return AnalogMultiplex<N>::analogRead(pin);
239void AnalogMultiplex<N>::begin() {
240 for (const pin_t &addressPin : addressPins)
241 ExtIO::pinMode(addressPin, OUTPUT);
242 if (enablePin != NO_PIN) {
243 ExtIO::pinMode(enablePin, OUTPUT);
244 ExtIO::digitalWrite(enablePin, MUX_DISABLED);
249void AnalogMultiplex<N>::setMuxAddress(uint8_t address) {
251 for (const pin_t &addressPin : addressPins) {
252 ExtIO::digitalWrite(addressPin, (address & mask) != 0 ? HIGH : LOW);
255#if !defined(__AVR__) && defined(ARDUINO)
256 delayMicroseconds(1);
261void AnalogMultiplex<N>::prepareReading(uint8_t address) {
262 setMuxAddress(address);
263 if (enablePin != NO_PIN)
264 ExtIO::digitalWrite(enablePin, MUX_ENABLED);
268void AnalogMultiplex<N>::afterReading() {
269 if (enablePin != NO_PIN)
270 ExtIO::digitalWrite(enablePin, MUX_DISABLED);
#define BEGIN_AH_NAMESPACE
#define AH_DIAGNOSTIC_WERROR()
A class for reading multiplexed analog inputs.
A class for ExtendedIOElements with a fixed size.