InputEvents v1.6.0
An easy to use but comprehensive Event Library for Buttons, Encoders, Encoder Buttons, Analog Inputs, Joysticks and Switches.
AdafruitMCP23017ExpanderAdapter.h
1// #ifdef __ADAFRUIT_MCP23XXX_H__
2
3#ifndef INPUT_EVENTS_ADAFRUIT_MCP23017_EXPANDER_ADAPTERCP_H
4#define INPUT_EVENTS_ADAFRUIT_MCP23017_EXPANDER_ADAPTERCP_H
5
6#include <Arduino.h>
7#include <new>
8#include <Adafruit_MCP23X17.h>
9#include "GpioExpanderAdapter/GpioExpanderAdapter.h"
10#include "PinAdapter/PinAdapter.h"
11
19
20public:
21
30 // We have to create the instance on the stack because Adafruit does interesting stuff with Wire and
31 // tries to delete an instance that doesn't yet exist...
32 //NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
33 mcp = new (&mcpStorage) Adafruit_MCP23X17();
34 }
35
41 AdafruitMCP23017ExpanderAdapter(Adafruit_MCP23X17& _mcp)
42 : mcp(&_mcp)
43 {}
44
49 void begin() override {
50 mcp->begin_I2C(); //The default
51 delayMicroseconds(400);
52 pinStates = mcp->readGPIOAB();
53 }
54
63 bool begin(uint8_t i2c_addr, TwoWire *wire = &Wire) {
64 bool ret = mcp->begin_I2C(i2c_addr, wire);
65 delayMicroseconds(400);
66 pinStates = mcp->readGPIOAB();
67 return ret;
68 }
69
70
75 void update() override {
76 pinStates = mcp->readGPIOAB();
77 }
78
85 void attachPin(byte pin, int mode = INPUT_PULLUP) override {
86 mcp->pinMode(pin, mode);
87 }
88
97 bool read(byte pin) override {
98 return bitRead(pinStates, pin);
99 }
100
108 bool updateAndRead(byte pin) {
109 return mcp->digitalRead(pin);
110 }
111
120 void write(byte pin, bool state) {
121 mcp->digitalWrite(pin, state);
122 }
123
129 bool canWrite() { return true; }
130
131 virtual ~AdafruitMCP23017ExpanderAdapter() = default;
132
133 private:
134
135 alignas(Adafruit_MCP23X17) uint8_t mcpStorage[sizeof(Adafruit_MCP23X17)];
136 Adafruit_MCP23X17* mcp;
137 uint16_t pinStates = 0;
138};
139
140
141#endif
142// #endif
An implementation of the GpioExpanderAdapter For Adafruit's MCP23017 library.
Definition: AdafruitMCP23017ExpanderAdapter.h:18
bool canWrite()
The MCP23017 can write a pins state.
Definition: AdafruitMCP23017ExpanderAdapter.h:129
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: AdafruitMCP23017ExpanderAdapter.h:63
bool read(byte pin) override
Returns the state of a pin on the expander.
Definition: AdafruitMCP23017ExpanderAdapter.h:97
bool updateAndRead(byte pin)
Update the expander over I2C and return a pin state. Not recommended, use a single uptate() and then ...
Definition: AdafruitMCP23017ExpanderAdapter.h:108
void update() override
Update the state of all input pins (called from loop() before updating EventButtons)
Definition: AdafruitMCP23017ExpanderAdapter.h:75
AdafruitMCP23017ExpanderAdapter(Adafruit_MCP23X17 &_mcp)
Construct a AdafruitMCP23017ExpanderAdapter with an already created Adafruit_MCP23X17 instance.
Definition: AdafruitMCP23017ExpanderAdapter.h:41
void begin() override
The default begin for a GPIOExpanderAdapter will use the default I2C address for the MCP23017.
Definition: AdafruitMCP23017ExpanderAdapter.h:49
void attachPin(byte pin, int mode=INPUT_PULLUP) override
Attach a pin and set its pin mode.
Definition: AdafruitMCP23017ExpanderAdapter.h:85
AdafruitMCP23017ExpanderAdapter()
Construct a AdafruitMCP23017ExpanderAdapter. An Adafruit_MCP23X17 instance will be created for you.
Definition: AdafruitMCP23017ExpanderAdapter.h:29
void write(byte pin, bool state)
Write bool state to a pin.
Definition: AdafruitMCP23017ExpanderAdapter.h:120
The base class/interface specification for GPIO expanders.
Definition: GpioExpanderAdapter.h:10