InputEvents v1.5.2
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 AdafruitMCP23017ExpanderAdapter_h
4#define AdafruitMCP23017ExpanderAdapter_h
5
6#include <Adafruit_MCP23X17.h>
7#include "Arduino.h"
8#include "GpioExpanderAdapter/GpioExpanderAdapter.h"
9#include "PinAdapter/PinAdapter.h"
10
18
19public:
20
26
32 AdafruitMCP23017ExpanderAdapter(Adafruit_MCP23X17& _mcp)
33 : mcp(_mcp)
34 {}
35
40 void begin() override {
41 mcp.begin_I2C(); //The default
42 delayMicroseconds(400);
43 pinStates = mcp.readGPIOAB();
44 }
45
54 bool begin(uint8_t i2c_addr, TwoWire *wire = &Wire) {
55 bool ret = mcp.begin_I2C(i2c_addr, wire);
56 delayMicroseconds(400);
57 pinStates = mcp.readGPIOAB();
58 return ret;
59 }
60
61
66 void update() override {
67 pinStates = mcp.readGPIOAB();
68 }
69
76 void attachPin(byte pin, int mode = INPUT_PULLUP) override {
77 mcp.pinMode(pin, mode);
78 }
79
88 bool read(byte pin) override {
89 return bitRead(pinStates, pin);
90 }
91
99 bool updateAndRead(byte pin) {
100 return mcp.digitalRead(pin);
101 }
102
111 void write(byte pin, bool state) {
112 mcp.digitalWrite(pin, state);
113 }
114
120 bool canWrite() { return true; }
121
122 virtual ~AdafruitMCP23017ExpanderAdapter() = default;
123
124private:
125
126 Adafruit_MCP23X17 mcp;
127 uint16_t pinStates = 0;
128};
129
130
131#endif
132// #endif
An implementation of the GpioExpanderAdapter For Adafruit's MCP23017 library.
Definition: AdafruitMCP23017ExpanderAdapter.h:17
bool canWrite()
The MCP23017 can write a pins state.
Definition: AdafruitMCP23017ExpanderAdapter.h:120
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:54
bool read(byte pin) override
Returns the state of a pin on the expander.
Definition: AdafruitMCP23017ExpanderAdapter.h:88
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:99
void update() override
Update the state of all input pins (called from loop() before updating EventButtons)
Definition: AdafruitMCP23017ExpanderAdapter.h:66
AdafruitMCP23017ExpanderAdapter(Adafruit_MCP23X17 &_mcp)
Construct a AdafruitMCP23017ExpanderAdapter with an already created Adafruit_MCP23X17 instance.
Definition: AdafruitMCP23017ExpanderAdapter.h:32
void begin() override
The default begin for a GPIOExpanderAdapter will use the default I2C address for the MCP23017.
Definition: AdafruitMCP23017ExpanderAdapter.h:40
void attachPin(byte pin, int mode=INPUT_PULLUP) override
Attach a pin and set its pin mode.
Definition: AdafruitMCP23017ExpanderAdapter.h:76
AdafruitMCP23017ExpanderAdapter()
Construct a AdafruitMCP23017ExpanderAdapter. An Adafruit_MCP23X17 instance will be created for you.
Definition: AdafruitMCP23017ExpanderAdapter.h:25
void write(byte pin, bool state)
Write bool state to a pin.
Definition: AdafruitMCP23017ExpanderAdapter.h:111
The base class/interface specification for GPIO expanders.
Definition: GpioExpanderAdapter.h:10