2626#include < hardware/i2c.h>
2727#include " Wire.h"
2828
29- TwoWireRP2040::TwoWireRP2040 (i2c_inst_t *i2c, pin_size_t sda, pin_size_t scl) {
29+ TwoWire::TwoWire (i2c_inst_t *i2c, pin_size_t sda, pin_size_t scl) {
3030 _sda = sda;
3131 _scl = scl;
3232 _i2c = i2c;
@@ -36,28 +36,28 @@ TwoWireRP2040::TwoWireRP2040(i2c_inst_t *i2c, pin_size_t sda, pin_size_t scl) {
3636 _buffLen = 0 ;
3737}
3838
39- bool TwoWireRP2040 ::setSDA (pin_size_t pin) {
39+ bool TwoWire ::setSDA (pin_size_t pin) {
4040 if (sdaAllowed (pin)) {
4141 _sda = pin;
4242 return true ;
4343 }
4444 return false ;
4545}
4646
47- bool TwoWireRP2040 ::setSCL (pin_size_t pin) {
47+ bool TwoWire ::setSCL (pin_size_t pin) {
4848 if (sclAllowed (pin)) {
4949 _scl = pin;
5050 return true ;
5151 }
5252 return false ;
5353}
5454
55- void TwoWireRP2040 ::setClock (uint32_t hz) {
55+ void TwoWire ::setClock (uint32_t hz) {
5656 _clkHz = hz;
5757}
5858
5959// Master mode
60- void TwoWireRP2040 ::begin () {
60+ void TwoWire ::begin () {
6161 if (_begun) {
6262 // ERROR
6363 return ;
@@ -76,8 +76,8 @@ void TwoWireRP2040::begin() {
7676}
7777
7878// Slave mode
79- void TwoWireRP2040 ::begin (uint8_t addr) {
80- // Slave moce isn't documented in the SDK, need to twiddle raw registers
79+ void TwoWire ::begin (uint8_t addr) {
80+ // Slave mode isn't documented in the SDK, need to twiddle raw registers
8181 // and use bare interrupts. TODO to implement, for now.
8282#if 0
8383 if (_begun) {
@@ -94,7 +94,7 @@ void TwoWireRP2040::begin(uint8_t addr) {
9494#endif
9595}
9696
97- void TwoWireRP2040 ::end () {
97+ void TwoWire ::end () {
9898 if (!_begun) {
9999 // ERROR
100100 return ;
@@ -104,7 +104,7 @@ void TwoWireRP2040::end() {
104104 _txBegun = false ;
105105}
106106
107- void TwoWireRP2040 ::beginTransmission (uint8_t addr) {
107+ void TwoWire ::beginTransmission (uint8_t addr) {
108108 if (!_begun || _txBegun) {
109109 // ERROR
110110 return ;
@@ -114,7 +114,7 @@ void TwoWireRP2040::beginTransmission(uint8_t addr) {
114114 _txBegun = true ;
115115}
116116
117- bool TwoWireRP2040 ::sdaAllowed (pin_size_t pin) {
117+ bool TwoWire ::sdaAllowed (pin_size_t pin) {
118118 switch (i2c_hw_index (_i2c)) {
119119 case 0 :
120120 switch (pin) {
@@ -138,7 +138,7 @@ bool TwoWireRP2040::sdaAllowed(pin_size_t pin) {
138138 return false ;
139139}
140140
141- bool TwoWireRP2040 ::sclAllowed (pin_size_t pin) {
141+ bool TwoWire ::sclAllowed (pin_size_t pin) {
142142 switch (i2c_hw_index (_i2c)) {
143143 case 0 :
144144 switch (pin) {
@@ -162,7 +162,7 @@ bool TwoWireRP2040::sclAllowed(pin_size_t pin) {
162162 return false ;
163163}
164164
165- size_t TwoWireRP2040 ::requestFrom (uint8_t address, size_t quantity, bool stopBit) {
165+ size_t TwoWire ::requestFrom (uint8_t address, size_t quantity, bool stopBit) {
166166 if (!_begun || _txBegun || !quantity || (quantity > sizeof (_buff))) {
167167 return 0 ;
168168 }
@@ -173,7 +173,7 @@ size_t TwoWireRP2040::requestFrom(uint8_t address, size_t quantity, bool stopBit
173173 return _buffLen;
174174}
175175
176- size_t TwoWireRP2040 ::requestFrom (uint8_t address, size_t quantity) {
176+ size_t TwoWire ::requestFrom (uint8_t address, size_t quantity) {
177177 return requestFrom (address, quantity, true );
178178}
179179
@@ -183,7 +183,7 @@ size_t TwoWireRP2040::requestFrom(uint8_t address, size_t quantity) {
183183// 2 : NACK on transmit of address
184184// 3 : NACK on transmit of data
185185// 4 : Other error
186- uint8_t TwoWireRP2040 ::endTransmission (bool stopBit) {
186+ uint8_t TwoWire ::endTransmission (bool stopBit) {
187187 if (!_begun || !_txBegun || !_buffLen) {
188188 return 4 ;
189189 }
@@ -194,19 +194,19 @@ uint8_t TwoWireRP2040::endTransmission(bool stopBit) {
194194 return (ret == len) ? 0 : 4 ;
195195}
196196
197- uint8_t TwoWireRP2040 ::endTransmission () {
197+ uint8_t TwoWire ::endTransmission () {
198198 return endTransmission (true );
199199}
200200
201- size_t TwoWireRP2040 ::write (uint8_t ucData) {
201+ size_t TwoWire ::write (uint8_t ucData) {
202202 if (!_begun || !_txBegun || (_buffLen == sizeof (_buff))) {
203203 return 0 ;
204204 }
205205 _buff[_buffLen++] = ucData;
206206 return 1 ;
207207}
208208
209- size_t TwoWireRP2040 ::write (const uint8_t *data, size_t quantity) {
209+ size_t TwoWire ::write (const uint8_t *data, size_t quantity) {
210210 for (size_t i = 0 ; i < quantity; ++i) {
211211 if (!write (data[i])) {
212212 return i;
@@ -216,40 +216,40 @@ size_t TwoWireRP2040::write(const uint8_t *data, size_t quantity) {
216216 return quantity;
217217}
218218
219- int TwoWireRP2040 ::available (void ) {
219+ int TwoWire ::available (void ) {
220220 return _begun ? _buffLen - _buffOff : 0 ;
221221}
222222
223- int TwoWireRP2040 ::read (void ) {
223+ int TwoWire ::read (void ) {
224224 if (available ()) {
225225 return _buff[_buffOff++];
226226 }
227227 return -1 ; // EOF
228228}
229229
230- int TwoWireRP2040 ::peek (void ) {
230+ int TwoWire ::peek (void ) {
231231 if (available ()) {
232232 return _buff[_buffOff];
233233 }
234234 return -1 ; // EOF
235235}
236236
237- void TwoWireRP2040 ::flush (void ) {
237+ void TwoWire ::flush (void ) {
238238 // Do nothing, use endTransmission(..) to force
239239 // data transfer.
240240}
241241
242242
243- void TwoWireRP2040 ::onReceive (void (*function)(int ))
243+ void TwoWire ::onReceive (void (*function)(int ))
244244{
245245 _onReceiveCallback = function;
246246}
247247
248- void TwoWireRP2040 ::onRequest (void (*function)(void ))
248+ void TwoWire ::onRequest (void (*function)(void ))
249249{
250250 _onRequestCallback = function;
251251}
252252
253- TwoWireRP2040 Wire (i2c0, 0 , 1 );
254- TwoWireRP2040 Wire1 (i2c1, 4 , 5 );
253+ TwoWire Wire (i2c0, 0 , 1 );
254+ TwoWire Wire1 (i2c1, 4 , 5 );
255255
0 commit comments