1717 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
1919 Modified 2012 by Todd Krein ([email protected] ) to implement repeated starts 20+ Modified 2017 by Michael Mayer to plain C for use with Sduino
2021*/
2122
22- extern " C" {
23+ // extern "C" {
2324 #include <stdlib.h>
2425 #include <string.h>
25- #include < inttypes.h>
26+ // #include <inttypes.h>
2627 #include "utility/twi.h"
27- }
28+ // }
2829
2930#include "Wire.h"
3031
3132// Initialize Class Variables //////////////////////////////////////////////////
3233
33- uint8_t TwoWire:: rxBuffer[BUFFER_LENGTH];
34- uint8_t TwoWire:: rxBufferIndex = 0 ;
35- uint8_t TwoWire:: rxBufferLength = 0 ;
34+ static uint8_t rxBuffer [BUFFER_LENGTH ];
35+ static uint8_t rxBufferIndex = 0 ;
36+ static uint8_t rxBufferLength = 0 ;
3637
37- uint8_t TwoWire:: txAddress = 0 ;
38- uint8_t TwoWire:: txBuffer[BUFFER_LENGTH];
39- uint8_t TwoWire:: txBufferIndex = 0 ;
40- uint8_t TwoWire:: txBufferLength = 0 ;
38+ static uint8_t txAddress = 0 ;
39+ static uint8_t txBuffer [BUFFER_LENGTH ];
40+ static uint8_t txBufferIndex = 0 ;
41+ static uint8_t txBufferLength = 0 ;
4142
42- uint8_t TwoWire::transmitting = 0 ;
43+ static uint8_t transmitting = 0 ;
44+ /*
4345void (*TwoWire::user_onRequest)(void);
4446void (*TwoWire::user_onReceive)(int);
47+ */
4548
4649// Constructors ////////////////////////////////////////////////////////////////
4750
51+ /*
4852TwoWire::TwoWire()
4953{
5054}
55+ */
5156
5257// Public Methods //////////////////////////////////////////////////////////////
5358
54- void TwoWire::begin (void )
59+ void Wire_begin (void )
5560{
5661 rxBufferIndex = 0 ;
5762 rxBufferLength = 0 ;
@@ -62,6 +67,7 @@ void TwoWire::begin(void)
6267 twi_init ();
6368}
6469
70+ /*
6571void TwoWire::begin(uint8_t address)
6672{
6773 twi_setAddress(address);
@@ -79,12 +85,40 @@ void TwoWire::end(void)
7985{
8086 twi_disable();
8187}
88+ */
8289
90+ /*
8391void TwoWire::setClock(uint32_t clock)
8492{
8593 twi_setFrequency(clock);
8694}
95+ */
96+
97+ uint8_t Wire_requestFrom (uint8_t address , uint8_t quantity )
98+ {
99+ return Wire_requestFrom3 (address , quantity , (uint8_t )true);
100+ }
101+
102+
103+ uint8_t Wire_requestFrom3 (uint8_t address , uint8_t quantity , uint8_t sendStop )
104+ {
105+ uint8_t read ;
106+
107+ // clamp to buffer length
108+ if (quantity > BUFFER_LENGTH ){
109+ quantity = BUFFER_LENGTH ;
110+ }
111+ // perform blocking read into buffer
112+ read = twi_readFrom (address , rxBuffer , quantity , sendStop );
113+ // set rx buffer iterator vars
114+ rxBufferIndex = 0 ;
115+ rxBufferLength = read ;
116+
117+ return read ;
118+ }
119+
87120
121+ /*
88122uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop)
89123{
90124 if (isize > 0) {
@@ -136,8 +170,9 @@ uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop)
136170{
137171 return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
138172}
173+ */
139174
140- void TwoWire::beginTransmission (uint8_t address)
175+ void Wire_beginTransmission (uint8_t address )
141176{
142177 // indicate that we are transmitting
143178 transmitting = 1 ;
@@ -148,10 +183,12 @@ void TwoWire::beginTransmission(uint8_t address)
148183 txBufferLength = 0 ;
149184}
150185
186+ /*
151187void TwoWire::beginTransmission(int address)
152188{
153189 beginTransmission((uint8_t)address);
154190}
191+ */
155192
156193//
157194// Originally, 'endTransmission' was an f(void) function.
@@ -166,7 +203,7 @@ void TwoWire::beginTransmission(int address)
166203// no call to endTransmission(true) is made. Some I2C
167204// devices will behave oddly if they do not see a STOP.
168205//
169- uint8_t TwoWire::endTransmission (uint8_t sendStop)
206+ uint8_t Wire_endTransmission1 (uint8_t sendStop )
170207{
171208 // transmit buffer (blocking)
172209 uint8_t ret = twi_writeTo (txAddress , txBuffer , txBufferLength , 1 , sendStop );
@@ -178,24 +215,26 @@ uint8_t TwoWire::endTransmission(uint8_t sendStop)
178215 return ret ;
179216}
180217
218+ /*
181219// This provides backwards compatibility with the original
182220// definition, and expected behaviour, of endTransmission
183221//
184222uint8_t TwoWire::endTransmission(void)
185223{
186224 return endTransmission(true);
187225}
226+ */
188227
189228// must be called in:
190229// slave tx event callback
191230// or after beginTransmission(address)
192- size_t TwoWire::write (uint8_t data)
231+ size_t Wire_write (uint8_t data )
193232{
194233 if (transmitting ){
195234 // in master transmitter mode
196235 // don't bother if buffer is full
197236 if (txBufferLength >= BUFFER_LENGTH ){
198- setWriteError ();
237+ //FIXME setWriteError();
199238 return 0 ;
200239 }
201240 // put byte in tx buffer
@@ -211,6 +250,7 @@ size_t TwoWire::write(uint8_t data)
211250 return 1 ;
212251}
213252
253+ /*
214254// must be called in:
215255// slave tx event callback
216256// or after beginTransmission(address)
@@ -228,22 +268,23 @@ size_t TwoWire::write(const uint8_t *data, size_t quantity)
228268 }
229269 return quantity;
230270}
271+ */
231272
232273// must be called in:
233274// slave rx event callback
234275// or after requestFrom(address, numBytes)
235- int TwoWire::available (void )
276+ int Wire_available (void )
236277{
237278 return rxBufferLength - rxBufferIndex ;
238279}
239280
240281// must be called in:
241282// slave rx event callback
242283// or after requestFrom(address, numBytes)
243- int TwoWire::read (void )
284+ int Wire_read (void )
244285{
245286 int value = -1 ;
246-
287+
247288 // get each successive byte on each call
248289 if (rxBufferIndex < rxBufferLength ){
249290 value = rxBuffer [rxBufferIndex ];
@@ -253,6 +294,7 @@ int TwoWire::read(void)
253294 return value ;
254295}
255296
297+ /*
256298// must be called in:
257299// slave rx event callback
258300// or after requestFrom(address, numBytes)
@@ -328,3 +370,4 @@ void TwoWire::onRequest( void (*function)(void) )
328370
329371TwoWire Wire = TwoWire();
330372
373+ */
0 commit comments