InputEvents v1.6.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
AdafruitPCF8575ExpanderAdapter.h
1#ifndef INPUT_EVENTS_ADAFRUIT_PCF8575_EXPANDER_ADAPTER_H
2#define INPUT_EVENTS_ADAFRUIT_PCF8575_EXPANDER_ADAPTER_H
3
4#include <Arduino.h>
5#include <new> // required for placement new on AVR
6#include <Adafruit_PCF8575.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_PCF8575();
32 ownsPcf = true;
33 }
34
40 AdafruitPCF8575ExpanderAdapter(Adafruit_PCF8575& _pcf)
41 : pcf(&_pcf)
42 {}
43
48 void begin() override {
49 pcf->begin(); //Use defaults
50 delayMicroseconds(400);
51 pinStates = pcf->digitalReadWord();
52 }
53
62 bool begin(uint8_t i2c_addr, TwoWire *wire = &Wire) {
63 bool ret = pcf->begin(i2c_addr, wire);
64 delayMicroseconds(400);
65 pinStates = pcf->digitalReadWord();
66 return ret;
67 }
68
69
74 void update() override {
75 pinStates = pcf->digitalReadWord();
76 }
77
84 void attachPin(byte pin, int mode = INPUT_PULLUP) override {
85 pcf->pinMode(pin, mode);
86 }
87
96 bool read(byte pin) override {
97 return bitRead(pinStates, pin);
98 }
99
107 bool updateAndRead(byte pin) {
108 return pcf->digitalRead(pin);
109 }
110
119 void write(byte pin, bool state) {
120 pcf->digitalWrite(pin, state);
121 }
122
128 bool canWrite() { return true; }
129
130 virtual ~AdafruitPCF8575ExpanderAdapter() = default;
131
132private:
133
134 alignas(Adafruit_PCF8575) uint8_t pcfStorage[sizeof(Adafruit_PCF8575)];
135 Adafruit_PCF8575* pcf = nullptr;
136 bool ownsPcf = false;
137 uint16_t pinStates = 0;
138};
139
140
141#endif
An implementation of the GpioExpanderAdapter For Adafruit's PCF8575 library.
Definition: AdafruitPCF8575ExpanderAdapter.h:16
AdafruitPCF8575ExpanderAdapter()
Construct a AdafruitPCF8575ExpanderAdapter. An Adafruit_PCF8575 instance will be created for you.
Definition: AdafruitPCF8575ExpanderAdapter.h:27
bool read(byte pin) override
Returns the state of a pin on the expander.
Definition: AdafruitPCF8575ExpanderAdapter.h:96
bool updateAndRead(byte pin)
Update the expander over I2C and return a pin state. Not recommended, use a single uptate() and then ...
Definition: AdafruitPCF8575ExpanderAdapter.h:107
bool begin(uint8_t i2c_addr, TwoWire *wire=&Wire)
Use this method instead of the default begin() to specify a different I2C addreess.
Definition: AdafruitPCF8575ExpanderAdapter.h:62
void write(byte pin, bool state)
Write bool state to a pin.
Definition: AdafruitPCF8575ExpanderAdapter.h:119
void update() override
Update the state of all input pins (called from loop() before updating EventButtons)
Definition: AdafruitPCF8575ExpanderAdapter.h:74
void attachPin(byte pin, int mode=INPUT_PULLUP) override
Attach a pin and set its pin mode.
Definition: AdafruitPCF8575ExpanderAdapter.h:84
void begin() override
The default begin for a GPIOExpanderAdapter will use the default I2C address for the PCF8575.
Definition: AdafruitPCF8575ExpanderAdapter.h:48
bool canWrite()
The PCF8575 can write a pins state.
Definition: AdafruitPCF8575ExpanderAdapter.h:128
AdafruitPCF8575ExpanderAdapter(Adafruit_PCF8575 &_pcf)
Construct a AdafruitPCF8575ExpanderAdapter with an already created Adafruit_PCF8575 instance.
Definition: AdafruitPCF8575ExpanderAdapter.h:40
The base class/interface specification for GPIO expanders.
Definition: GpioExpanderAdapter.h:10