36
36
#endif
37
37
38
38
// minihelper to keep Arduino backward compatibility
39
- static inline void wiresend (uint8_t x) {
39
+ static inline void wiresend (uint8_t x, TwoWire *theWire ) {
40
40
#if ARDUINO >= 100
41
- Wire. write ((uint8_t )x);
41
+ theWire-> write ((uint8_t )x);
42
42
#else
43
- Wire. send (x);
43
+ theWire-> send (x);
44
44
#endif
45
45
}
46
46
47
- static inline uint8_t wirerecv (void ) {
47
+ static inline uint8_t wirerecv (TwoWire *theWire ) {
48
48
#if ARDUINO >= 100
49
- return Wire. read ();
49
+ return theWire-> read ();
50
50
#else
51
- return Wire. receive ();
51
+ return theWire-> receive ();
52
52
#endif
53
53
}
54
54
@@ -70,22 +70,22 @@ uint8_t Adafruit_MCP23017::regForPin(uint8_t pin, uint8_t portAaddr,
70
70
*/
71
71
uint8_t Adafruit_MCP23017::readRegister (uint8_t addr) {
72
72
// 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 );
78
78
}
79
79
80
80
/* *
81
81
* Writes a given register
82
82
*/
83
83
void Adafruit_MCP23017::writeRegister (uint8_t regAddr, uint8_t regValue) {
84
84
// 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 ();
89
89
}
90
90
91
91
/* *
@@ -113,14 +113,16 @@ void Adafruit_MCP23017::updateRegisterBit(uint8_t pin, uint8_t pValue,
113
113
* Initializes the MCP23017 given its HW selected address, see datasheet for
114
114
* Address selection.
115
115
* @param addr Selected address
116
+ * @param theWire the I2C object to use, defaults to &Wire
116
117
*/
117
- void Adafruit_MCP23017::begin (uint8_t addr) {
118
+ void Adafruit_MCP23017::begin (uint8_t addr, TwoWire *theWire ) {
118
119
if (addr > 7 ) {
119
120
addr = 7 ;
120
121
}
121
122
i2caddr = addr;
123
+ _wire = theWire;
122
124
123
- Wire. begin ();
125
+ _wire-> begin ();
124
126
125
127
// set defaults!
126
128
// all inputs on port A and B
@@ -131,8 +133,9 @@ void Adafruit_MCP23017::begin(uint8_t addr) {
131
133
/* *
132
134
* Initializes the default MCP23017, with 000 for the configurable part of the
133
135
* address
136
+ * @param theWire the I2C object to use, defaults to &Wire
134
137
*/
135
- void Adafruit_MCP23017::begin (void ) { begin (0 ); }
138
+ void Adafruit_MCP23017::begin (TwoWire *theWire ) { begin (0 , theWire ); }
136
139
137
140
/* *
138
141
* Sets the pin mode to either INPUT or OUTPUT
@@ -152,13 +155,13 @@ uint16_t Adafruit_MCP23017::readGPIOAB() {
152
155
uint8_t a;
153
156
154
157
// 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 ();
158
161
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 );
162
165
ba <<= 8 ;
163
166
ba |= a;
164
167
@@ -173,28 +176,28 @@ uint16_t Adafruit_MCP23017::readGPIOAB() {
173
176
uint8_t Adafruit_MCP23017::readGPIO (uint8_t b) {
174
177
175
178
// read the current GPIO output latches
176
- Wire. beginTransmission (MCP23017_ADDRESS | i2caddr);
179
+ _wire-> beginTransmission (MCP23017_ADDRESS | i2caddr);
177
180
if (b == 0 )
178
- wiresend (MCP23017_GPIOA);
181
+ wiresend (MCP23017_GPIOA, _wire );
179
182
else {
180
- wiresend (MCP23017_GPIOB);
183
+ wiresend (MCP23017_GPIOB, _wire );
181
184
}
182
- Wire. endTransmission ();
185
+ _wire-> endTransmission ();
183
186
184
- Wire. requestFrom (MCP23017_ADDRESS | i2caddr, 1 );
185
- return wirerecv ();
187
+ _wire-> requestFrom (MCP23017_ADDRESS | i2caddr, 1 );
188
+ return wirerecv (_wire );
186
189
}
187
190
188
191
/* *
189
192
* Writes all the pins in one go. This method is very useful if you are
190
193
* implementing a multiplexed matrix and want to get a decent refresh rate.
191
194
*/
192
195
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 ();
198
201
}
199
202
200
203
/* !
0 commit comments