Skip to content

Commit e92db5b

Browse files
authored
Merge pull request #49 from SunboX/master
add possibility to pass in a Wire instance
2 parents 8235d27 + 4d065c4 commit e92db5b

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

Adafruit_MCP23017.cpp

+38-35
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,19 @@
3636
#endif
3737

3838
// minihelper to keep Arduino backward compatibility
39-
static inline void wiresend(uint8_t x) {
39+
static inline void wiresend(uint8_t x, TwoWire *theWire) {
4040
#if ARDUINO >= 100
41-
Wire.write((uint8_t)x);
41+
theWire->write((uint8_t)x);
4242
#else
43-
Wire.send(x);
43+
theWire->send(x);
4444
#endif
4545
}
4646

47-
static inline uint8_t wirerecv(void) {
47+
static inline uint8_t wirerecv(TwoWire *theWire) {
4848
#if ARDUINO >= 100
49-
return Wire.read();
49+
return theWire->read();
5050
#else
51-
return Wire.receive();
51+
return theWire->receive();
5252
#endif
5353
}
5454

@@ -70,22 +70,22 @@ uint8_t Adafruit_MCP23017::regForPin(uint8_t pin, uint8_t portAaddr,
7070
*/
7171
uint8_t Adafruit_MCP23017::readRegister(uint8_t addr) {
7272
// read the current GPINTEN
73-
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
74-
wiresend(addr);
75-
Wire.endTransmission();
76-
Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
77-
return wirerecv();
73+
_wire->beginTransmission(MCP23017_ADDRESS | i2caddr);
74+
wiresend(addr, _wire);
75+
_wire->endTransmission();
76+
_wire->requestFrom(MCP23017_ADDRESS | i2caddr, 1);
77+
return wirerecv(_wire);
7878
}
7979

8080
/**
8181
* Writes a given register
8282
*/
8383
void Adafruit_MCP23017::writeRegister(uint8_t regAddr, uint8_t regValue) {
8484
// Write the register
85-
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
86-
wiresend(regAddr);
87-
wiresend(regValue);
88-
Wire.endTransmission();
85+
_wire->beginTransmission(MCP23017_ADDRESS | i2caddr);
86+
wiresend(regAddr, _wire);
87+
wiresend(regValue, _wire);
88+
_wire->endTransmission();
8989
}
9090

9191
/**
@@ -113,14 +113,16 @@ void Adafruit_MCP23017::updateRegisterBit(uint8_t pin, uint8_t pValue,
113113
* Initializes the MCP23017 given its HW selected address, see datasheet for
114114
* Address selection.
115115
* @param addr Selected address
116+
* @param theWire the I2C object to use, defaults to &Wire
116117
*/
117-
void Adafruit_MCP23017::begin(uint8_t addr) {
118+
void Adafruit_MCP23017::begin(uint8_t addr, TwoWire *theWire) {
118119
if (addr > 7) {
119120
addr = 7;
120121
}
121122
i2caddr = addr;
123+
_wire = theWire;
122124

123-
Wire.begin();
125+
_wire->begin();
124126

125127
// set defaults!
126128
// all inputs on port A and B
@@ -131,8 +133,9 @@ void Adafruit_MCP23017::begin(uint8_t addr) {
131133
/**
132134
* Initializes the default MCP23017, with 000 for the configurable part of the
133135
* address
136+
* @param theWire the I2C object to use, defaults to &Wire
134137
*/
135-
void Adafruit_MCP23017::begin(void) { begin(0); }
138+
void Adafruit_MCP23017::begin(TwoWire *theWire) { begin(0, theWire); }
136139

137140
/**
138141
* Sets the pin mode to either INPUT or OUTPUT
@@ -152,13 +155,13 @@ uint16_t Adafruit_MCP23017::readGPIOAB() {
152155
uint8_t a;
153156

154157
// read the current GPIO output latches
155-
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
156-
wiresend(MCP23017_GPIOA);
157-
Wire.endTransmission();
158+
_wire->beginTransmission(MCP23017_ADDRESS | i2caddr);
159+
wiresend(MCP23017_GPIOA, _wire);
160+
_wire->endTransmission();
158161

159-
Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 2);
160-
a = wirerecv();
161-
ba = wirerecv();
162+
_wire->requestFrom(MCP23017_ADDRESS | i2caddr, 2);
163+
a = wirerecv(_wire);
164+
ba = wirerecv(_wire);
162165
ba <<= 8;
163166
ba |= a;
164167

@@ -173,28 +176,28 @@ uint16_t Adafruit_MCP23017::readGPIOAB() {
173176
uint8_t Adafruit_MCP23017::readGPIO(uint8_t b) {
174177

175178
// read the current GPIO output latches
176-
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
179+
_wire->beginTransmission(MCP23017_ADDRESS | i2caddr);
177180
if (b == 0)
178-
wiresend(MCP23017_GPIOA);
181+
wiresend(MCP23017_GPIOA, _wire);
179182
else {
180-
wiresend(MCP23017_GPIOB);
183+
wiresend(MCP23017_GPIOB, _wire);
181184
}
182-
Wire.endTransmission();
185+
_wire->endTransmission();
183186

184-
Wire.requestFrom(MCP23017_ADDRESS | i2caddr, 1);
185-
return wirerecv();
187+
_wire->requestFrom(MCP23017_ADDRESS | i2caddr, 1);
188+
return wirerecv(_wire);
186189
}
187190

188191
/**
189192
* Writes all the pins in one go. This method is very useful if you are
190193
* implementing a multiplexed matrix and want to get a decent refresh rate.
191194
*/
192195
void Adafruit_MCP23017::writeGPIOAB(uint16_t ba) {
193-
Wire.beginTransmission(MCP23017_ADDRESS | i2caddr);
194-
wiresend(MCP23017_GPIOA);
195-
wiresend(ba & 0xFF);
196-
wiresend(ba >> 8);
197-
Wire.endTransmission();
196+
_wire->beginTransmission(MCP23017_ADDRESS | i2caddr);
197+
wiresend(MCP23017_GPIOA, _wire);
198+
wiresend(ba & 0xFF, _wire);
199+
wiresend(ba >> 8, _wire);
200+
_wire->endTransmission();
198201
}
199202

200203
/*!

Adafruit_MCP23017.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
class Adafruit_MCP23017 {
2525
public:
26-
void begin(uint8_t addr);
27-
void begin(void);
26+
void begin(uint8_t addr, TwoWire *theWire = &Wire);
27+
void begin(TwoWire *theWire = &Wire);
2828

2929
void pinMode(uint8_t p, uint8_t d);
3030
void digitalWrite(uint8_t p, uint8_t d);
@@ -42,6 +42,7 @@ class Adafruit_MCP23017 {
4242

4343
private:
4444
uint8_t i2caddr;
45+
TwoWire *_wire; //!< pointer to a TwoWire object
4546

4647
uint8_t bitForPin(uint8_t pin);
4748
uint8_t regForPin(uint8_t pin, uint8_t portAaddr, uint8_t portBaddr);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#include <Wire.h>
2+
#include "Adafruit_MCP23017.h"
3+
4+
// Sample for usage of two MCP23017 I/O expander with an ESP32
5+
// public domain!
6+
7+
Adafruit_MCP23017 mcp;
8+
9+
void setup() {
10+
11+
Wire.setClock(1700000); // set frequency to 1.7mhz
12+
13+
mcp.begin(&Wire); // use default address 0
14+
}
15+
16+
void loop() {
17+
}

0 commit comments

Comments
 (0)