@@ -38,71 +38,36 @@ extern "C" {
38
38
#error Wire library is not supported on this board
39
39
#endif
40
40
41
- // Initialize Class Variables //////////////////////////////////////////////////
42
-
43
- uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
44
- uint8_t TwoWire::rxBufferIndex = 0 ;
45
- uint8_t TwoWire::rxBufferLength = 0 ;
46
-
47
- uint8_t TwoWire::txAddress = 0 ;
48
- uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
49
- uint8_t TwoWire::txBufferIndex = 0 ;
50
- uint8_t TwoWire::txBufferLength = 0 ;
51
-
52
- uint8_t TwoWire::transmitting = 0 ;
53
- void (*TwoWire::user_onRequest)(void );
54
- void (*TwoWire::user_onReceive)(size_t );
55
-
56
- static int default_sda_pin = SDA;
57
- static int default_scl_pin = SCL;
58
-
59
41
// Constructors ////////////////////////////////////////////////////////////////
60
42
61
- TwoWire::TwoWire () {}
43
+ TwoWire::TwoWire (uint8_t rxBufferSize, uint8_t txBufferSize) :
44
+ rxBuffer{new uint8_t [rxBufferSize]},
45
+ txBuffer{new uint8_t [txBufferSize]}
46
+ {
47
+ this ->txBufferSize = txBufferSize;
48
+ this ->rxBufferSize = rxBufferSize;
49
+ }
62
50
63
51
// Public Methods //////////////////////////////////////////////////////////////
64
52
65
53
void TwoWire::begin (int sda, int scl)
66
54
{
67
- default_sda_pin = sda;
68
- default_scl_pin = scl;
69
- twi_init (sda, scl);
70
- flush ();
71
- }
72
-
73
- void TwoWire::begin (int sda, int scl, uint8_t address)
74
- {
75
- default_sda_pin = sda;
76
- default_scl_pin = scl;
77
- twi_setAddress (address);
78
- twi_init (sda, scl);
79
- twi_attachSlaveTxEvent (onRequestService);
80
- twi_attachSlaveRxEvent (onReceiveService);
55
+ twi.init (sda, scl);
81
56
flush ();
82
57
}
83
58
84
59
void TwoWire::pins (int sda, int scl)
85
60
{
86
- default_sda_pin = sda;
87
- default_scl_pin = scl;
88
61
}
89
62
90
63
void TwoWire::begin (void )
91
64
{
92
- begin (default_sda_pin, default_scl_pin);
93
- }
94
-
95
- void TwoWire::begin (uint8_t address)
96
- {
97
- twi_setAddress (address);
98
- twi_attachSlaveTxEvent (onRequestService);
99
- twi_attachSlaveRxEvent (onReceiveService);
100
- begin ();
65
+ begin (SDA, SCL);
101
66
}
102
67
103
68
uint8_t TwoWire::status ()
104
69
{
105
- return twi_status ();
70
+ return twi. status ();
106
71
}
107
72
108
73
void TwoWire::begin (int address)
@@ -112,21 +77,21 @@ void TwoWire::begin(int address)
112
77
113
78
void TwoWire::setClock (uint32_t frequency)
114
79
{
115
- twi_setClock (frequency);
80
+ twi. setClock (frequency);
116
81
}
117
82
118
83
void TwoWire::setClockStretchLimit (uint32_t limit)
119
84
{
120
- twi_setClockStretchLimit (limit);
85
+ twi. setClockStretchLimit (limit);
121
86
}
122
87
123
88
size_t TwoWire::requestFrom (uint8_t address, size_t size, bool sendStop)
124
89
{
125
- if (size > BUFFER_LENGTH )
90
+ if (size > rxBufferSize )
126
91
{
127
- size = BUFFER_LENGTH ;
92
+ size = rxBufferSize ;
128
93
}
129
- size_t read = (twi_readFrom (address, rxBuffer, size, sendStop) == 0 ) ? size : 0 ;
94
+ size_t read = (twi. readFrom (address, rxBuffer. get () , size, sendStop) == 0 ) ? size : 0 ;
130
95
rxBufferIndex = 0 ;
131
96
rxBufferLength = read ;
132
97
return read ;
@@ -167,7 +132,7 @@ void TwoWire::beginTransmission(int address)
167
132
168
133
uint8_t TwoWire::endTransmission (uint8_t sendStop)
169
134
{
170
- int8_t ret = twi_writeTo (txAddress, txBuffer, txBufferLength, sendStop);
135
+ int8_t ret = twi. writeTo (txAddress, txBuffer. get () , txBufferLength, sendStop);
171
136
txBufferIndex = 0 ;
172
137
txBufferLength = 0 ;
173
138
transmitting = 0 ;
@@ -183,7 +148,7 @@ size_t TwoWire::write(uint8_t data)
183
148
{
184
149
if (transmitting)
185
150
{
186
- if (txBufferLength >= BUFFER_LENGTH )
151
+ if (txBufferLength >= txBufferSize )
187
152
{
188
153
setWriteError ();
189
154
return 0 ;
@@ -194,7 +159,8 @@ size_t TwoWire::write(uint8_t data)
194
159
}
195
160
else
196
161
{
197
- twi_transmit (&data, 1 );
162
+ setWriteError ();
163
+ return 0 ;
198
164
}
199
165
return 1 ;
200
166
}
@@ -213,7 +179,8 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity)
213
179
}
214
180
else
215
181
{
216
- twi_transmit (data, quantity);
182
+ setWriteError ();
183
+ return 0 ;
217
184
}
218
185
return quantity;
219
186
}
@@ -261,72 +228,6 @@ void TwoWire::flush(void)
261
228
txBufferLength = 0 ;
262
229
}
263
230
264
- void TwoWire::onReceiveService (uint8_t * inBytes, size_t numBytes)
265
- {
266
- // don't bother if user hasn't registered a callback
267
- if (!user_onReceive)
268
- {
269
- return ;
270
- }
271
- // // don't bother if rx buffer is in use by a master requestFrom() op
272
- // // i know this drops data, but it allows for slight stupidity
273
- // // meaning, they may not have read all the master requestFrom() data yet
274
- // if(rxBufferIndex < rxBufferLength){
275
- // return;
276
- // }
277
-
278
- // copy twi rx buffer into local read buffer
279
- // this enables new reads to happen in parallel
280
- for (uint8_t i = 0 ; i < numBytes; ++i)
281
- {
282
- rxBuffer[i] = inBytes[i];
283
- }
284
-
285
- // set rx iterator vars
286
- rxBufferIndex = 0 ;
287
- rxBufferLength = numBytes;
288
-
289
- // alert user program
290
- user_onReceive (numBytes);
291
- }
292
-
293
- void TwoWire::onRequestService (void )
294
- {
295
- // don't bother if user hasn't registered a callback
296
- if (!user_onRequest)
297
- {
298
- return ;
299
- }
300
-
301
- // reset tx buffer iterator vars
302
- // !!! this will kill any pending pre-master sendTo() activity
303
- txBufferIndex = 0 ;
304
- txBufferLength = 0 ;
305
-
306
- // alert user program
307
- user_onRequest ();
308
- }
309
-
310
- void TwoWire::onReceive (void (*function)(int ))
311
- {
312
- // arduino api compatibility fixer:
313
- // really hope size parameter will not exceed 2^31 :)
314
- static_assert (sizeof (int ) == sizeof (size_t ), " something is wrong in Arduino kingdom" );
315
- user_onReceive = reinterpret_cast <void (*)(size_t )>(function);
316
- }
317
-
318
- void TwoWire::onReceive (void (*function)(size_t ))
319
- {
320
- user_onReceive = function;
321
- twi_enableSlaveMode ();
322
- }
323
-
324
- void TwoWire::onRequest (void (*function)(void ))
325
- {
326
- user_onRequest = function;
327
- twi_enableSlaveMode ();
328
- }
329
-
330
231
// Preinstantiate Objects //////////////////////////////////////////////////////
331
232
332
233
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_TWOWIRE)
0 commit comments