41 AnalogMultiplex(pin_t analogPin, const Array<pin_t, N> &addressPins,
42 pin_t enablePin = NO_PIN)
43 : analogPin(analogPin), addressPins(addressPins), enablePin(enablePin) {
61 void pinMode(pin_t pin, PinMode_t mode) override;
66 void pinModeBuffered(pin_t pin, PinMode_t mode) override;
72 void digitalWrite(pin_t, PinStatus_t) override
73 __attribute__((deprecated)) {}
78 void digitalWriteBuffered(pin_t, PinStatus_t) override
79 __attribute__((deprecated)) {}
87 PinStatus_t digitalRead(pin_t pin) override;
92 PinStatus_t digitalReadBuffered(pin_t pin) override;
100 analog_t analogRead(pin_t pin) override;
105 analog_t analogReadBuffered(pin_t pin) override;
111 void analogWrite(pin_t, analog_t) override
112 __attribute__((deprecated)) {}
117 void analogWriteBuffered(pin_t, analog_t) override
118 __attribute__((deprecated)) {}
124 void begin() override;
130 void updateBufferedOutputs() override {}
136 void updateBufferedInputs() override {}
142 void discardFirstReading(bool discardFirstReading_) {
143 this->discardFirstReading_ = discardFirstReading_;
147 const pin_t analogPin;
148 const Array<pin_t, N> addressPins;
149 const pin_t enablePin;
150 bool discardFirstReading_ = true;
159 void setMuxAddress(uint8_t address);
167 void prepareReading(uint8_t address);
175 constexpr static auto MUX_ENABLED = LOW;
176 constexpr static auto MUX_DISABLED = HIGH;
185using CD74HC4067 = AnalogMultiplex<4>;
193using CD74HC4051 = AnalogMultiplex<3>;
198void AnalogMultiplex<N>::pinMode(pin_t, PinMode_t mode) {
199 ExtIO::pinMode(analogPin, mode);
203void AnalogMultiplex<N>::pinModeBuffered(pin_t, PinMode_t mode) {
204 AnalogMultiplex<N>::pinMode(analogPin, mode);
208PinStatus_t AnalogMultiplex<N>::digitalRead(pin_t pin) {
210 PinStatus_t result = ExtIO::digitalRead(analogPin);
216PinStatus_t AnalogMultiplex<N>::digitalReadBuffered(pin_t pin) {
217 return AnalogMultiplex<N>::digitalRead(pin);
221analog_t AnalogMultiplex<N>::analogRead(pin_t pin) {
223 if (discardFirstReading_)
224 (void)ExtIO::analogRead(analogPin);
225 analog_t result = ExtIO::analogRead(analogPin);
231analog_t AnalogMultiplex<N>::analogReadBuffered(pin_t pin) {
232 return AnalogMultiplex<N>::analogRead(pin);
236void AnalogMultiplex<N>::begin() {
237 for (const pin_t &addressPin : addressPins)
238 ExtIO::pinMode(addressPin, OUTPUT);
239 if (enablePin != NO_PIN) {
240 ExtIO::pinMode(enablePin, OUTPUT);
241 ExtIO::digitalWrite(enablePin, MUX_DISABLED);
246void AnalogMultiplex<N>::setMuxAddress(uint8_t address) {
248 for (const pin_t &addressPin : addressPins) {
249 ExtIO::digitalWrite(addressPin, (address & mask) != 0 ? HIGH : LOW);
252#if !defined(__AVR__) && defined(ARDUINO)
253 delayMicroseconds(SELECT_LINE_DELAY);
258void AnalogMultiplex<N>::prepareReading(uint8_t address) {
259 setMuxAddress(address);
260 if (enablePin != NO_PIN)
261 ExtIO::digitalWrite(enablePin, MUX_ENABLED);
265void AnalogMultiplex<N>::afterReading() {
266 if (enablePin != NO_PIN)
267 ExtIO::digitalWrite(enablePin, MUX_DISABLED);
#define BEGIN_AH_NAMESPACE
A class for reading multiplexed analog inputs.
A class for ExtendedIOElements with a fixed size.