InputEvents v1.6.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
AdafruitPCF8574ExpanderAdapter.h
1#ifndef INPUT_EVENTS_ADAFRUIT_PCF8574_EXPANDER_ADAPTER_H
2#define INPUT_EVENTS_ADAFRUIT_PCF8574_EXPANDER_ADAPTER_H
3
4#include <Arduino.h>
5#include <new>
6#include <Adafruit_PCF8574.h>
7#include "GpioExpanderAdapter/GpioExpanderAdapter.h"
8#include "PinAdapter/PinAdapter.h"
9
17
18public:
19
28 // We have to create the instance on the stack because Adafruit does 'clever' stuff with Wire and
29 // tries to delete an instance that doesn't yet exist...
30 //NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
31 pcf = new (&pcfStorage) Adafruit_PCF8574();
32 }
33
39 AdafruitPCF8574ExpanderAdapter(Adafruit_PCF8574& _pcf)
40 : pcf(&_pcf)
41 {}
42
47 void begin() override {
48 pcf->begin(); //Use defaults
49 delayMicroseconds(400);
50 pinStates = pcf->digitalReadByte();
51 }
52
61 bool begin(uint8_t i2c_addr, TwoWire *wire = &Wire) {
62 bool ret = pcf->begin(i2c_addr, wire);
63 delayMicroseconds(400);
64 pinStates = pcf->digitalReadByte();
65 return ret;
66 }
67
68
73 void update() override {
74 pinStates = pcf->digitalReadByte();
75 }
76
83 void attachPin(byte pin, int mode = INPUT_PULLUP) override {
84 pcf->pinMode(pin, mode);
85 }
86
95 bool read(byte pin) override {
96 return bitRead(pinStates, pin);
97 }
98
106 bool updateAndRead(byte pin) {
107 return pcf->digitalRead(pin);
108 }
109
118 void write(byte pin, bool state) {
119 pcf->digitalWrite(pin, state);
120 }
121
127 bool canWrite() { return true; }
128
129 virtual ~AdafruitPCF8574ExpanderAdapter() = default;
130
131private:
132
133 alignas(Adafruit_PCF8574) uint8_t pcfStorage[sizeof(Adafruit_PCF8574)];
134 Adafruit_PCF8574* pcf;
135 uint8_t pinStates = 0;
136};
137
138
139#endif
An implementation of the GpioExpanderAdapter For Adafruit's PCF8574 library.
Definition: AdafruitPCF8574ExpanderAdapter.h:16
void attachPin(byte pin, int mode=INPUT_PULLUP) override
Attach a pin and set its pin mode.
Definition: AdafruitPCF8574ExpanderAdapter.h:83
void update() override
Update the state of all input pins (called from loop() before updating EventButtons)
Definition: AdafruitPCF8574ExpanderAdapter.h:73
void begin() override
The default begin for a GPIOExpanderAdapter will use the default I2C address for the PCF8574.
Definition: AdafruitPCF8574ExpanderAdapter.h:47
bool begin(uint8_t i2c_addr, TwoWire *wire=&Wire)
Use this method instead of the default begin() to specify a different I2C addreess and optional Wire.
Definition: AdafruitPCF8574ExpanderAdapter.h:61
AdafruitPCF8574ExpanderAdapter()
Construct a AdafruitPCF8574ExpanderAdapter. An Adafruit_PCF8574 instance will be created for you.
Definition: AdafruitPCF8574ExpanderAdapter.h:27
bool updateAndRead(byte pin)
Update the expander over I2C and return a pin state. Not recommended, use a single uptate() and then ...
Definition: AdafruitPCF8574ExpanderAdapter.h:106
bool canWrite()
The PCF8574 can write a pins state.
Definition: AdafruitPCF8574ExpanderAdapter.h:127
void write(byte pin, bool state)
Write bool state to a pin.
Definition: AdafruitPCF8574ExpanderAdapter.h:118
AdafruitPCF8574ExpanderAdapter(Adafruit_PCF8574 &_pcf)
Construct a AdafruitPCF8574ExpanderAdapter with an already created Adafruit_PCF8574 instance.
Definition: AdafruitPCF8574ExpanderAdapter.h:39
bool read(byte pin) override
Returns the state of a pin on the expander.
Definition: AdafruitPCF8574ExpanderAdapter.h:95
The base class/interface specification for GPIO expanders.
Definition: GpioExpanderAdapter.h:10