Skip to content

Commit ebde707

Browse files
authored
Merge pull request #3 from yanzixiang/master
Add support for other Wire buses
2 parents 2c697bb + a00fc5c commit ebde707

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ Unlike most Arduino library, no default instance is created when the library is
2020
MCP23017 mcp = MCP23017(0x24);
2121
```
2222

23+
Additionaly, you can specify the `Wire` instance to use as a second argument. For instance `MCP23017(0x24, Wire1)`.
2324
See included examples for furher usage.

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MCP23017
2-
version=1.0.1
2+
version=1.1.0
33
author=Bertrand Lemasle
44
maintainer=Bertrand Lemasle
55
sentence=MCP23017 I2C Port expander library.

src/MCP23017.cpp

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "MCP23017.h"
22

3-
MCP23017::MCP23017(uint8_t address) {
3+
MCP23017::MCP23017(uint8_t address, TwoWire& bus = Wire) {
44
_deviceAddr = address;
5+
_bus = &bus;
56
}
67

78
MCP23017::~MCP23017() {}
@@ -100,39 +101,39 @@ uint16_t MCP23017::read()
100101

101102
void MCP23017::writeRegister(MCP23017_REGISTER reg, uint8_t value)
102103
{
103-
Wire.beginTransmission(_deviceAddr);
104-
Wire.write(static_cast<uint8_t>(reg));
105-
Wire.write(value);
106-
Wire.endTransmission();
104+
_bus->beginTransmission(_deviceAddr);
105+
_bus->write(static_cast<uint8_t>(reg));
106+
_bus->write(value);
107+
_bus->endTransmission();
107108
}
108109

109110
void MCP23017::writeRegister(MCP23017_REGISTER reg, uint8_t portA, uint8_t portB)
110111
{
111-
Wire.beginTransmission(_deviceAddr);
112-
Wire.write(static_cast<uint8_t>(reg));
113-
Wire.write(portA);
114-
Wire.write(portB);
115-
Wire.endTransmission();
112+
_bus->beginTransmission(_deviceAddr);
113+
_bus->write(static_cast<uint8_t>(reg));
114+
_bus->write(portA);
115+
_bus->write(portB);
116+
_bus->endTransmission();
116117
}
117118

118119

119120
uint8_t MCP23017::readRegister(MCP23017_REGISTER reg)
120121
{
121-
Wire.beginTransmission(_deviceAddr);
122-
Wire.write(static_cast<uint8_t>(reg));
123-
Wire.endTransmission();
124-
Wire.requestFrom(_deviceAddr, (uint8_t)1);
125-
return Wire.read();
122+
_bus->beginTransmission(_deviceAddr);
123+
_bus->write(static_cast<uint8_t>(reg));
124+
_bus->endTransmission();
125+
_bus->requestFrom(_deviceAddr, (uint8_t)1);
126+
return _bus->read();
126127
}
127128

128129
void MCP23017::readRegister(MCP23017_REGISTER reg, uint8_t& portA, uint8_t& portB)
129130
{
130-
Wire.beginTransmission(_deviceAddr);
131-
Wire.write(static_cast<uint8_t>(reg));
132-
Wire.endTransmission();
133-
Wire.requestFrom(_deviceAddr, (uint8_t)2);
134-
portA = Wire.read();
135-
portB = Wire.read();
131+
_bus->beginTransmission(_deviceAddr);
132+
_bus->write(static_cast<uint8_t>(reg));
133+
_bus->endTransmission();
134+
_bus->requestFrom(_deviceAddr, (uint8_t)2);
135+
portA = _bus->read();
136+
portB = _bus->read();
136137
}
137138

138139
#ifdef _MCP23017_INTERRUPT_SUPPORT_

src/MCP23017.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ inline MCP23017_REGISTER operator+(MCP23017_REGISTER a, MCP23017_PORT b) {
5858
class MCP23017
5959
{
6060
private:
61+
TwoWire* _bus;
6162
uint8_t _deviceAddr;
6263
public:
6364
/**
6465
* Instantiates a new instance to interact with a MCP23017 at the specified address.
6566
*/
66-
MCP23017(uint8_t address);
67+
MCP23017(uint8_t address, TwoWire& bus = Wire);
6768
~MCP23017();
6869
#ifdef _DEBUG
6970
void debug();

0 commit comments

Comments
 (0)