eBoard ๐Ÿ‰  โ‘ โ‘งโ‘จ
Written for SIA 2017/2018
eagle_TwoWire.h
Go to the documentation of this file.
1 #ifndef EAGLE_EBOARD_HELPLIB_TWOWIRE
2 #define EAGLE_EBOARD_HELPLIB_TWOWIRE
3 
4  #include <inttypes.h>
5  #include "Stream.h"
6 
7  #define BUFFER_LENGTH 32
8  namespace eagle_impl {
25  class TwoWire : public Stream {
26  private:
28  static uint8_t rxBuffer[];
30  static uint8_t rxBufferIndex;
32  static uint8_t rxBufferLength;
33 
35  static uint8_t txAddress;
37  static uint8_t txBuffer[];
39  static uint8_t txBufferIndex;
41  static uint8_t txBufferLength;
42 
44  static uint8_t transmitting;
46  static void (*user_onRequest)(void);
51  static void (*user_onReceive)(int numBytes);
53  static void onRequestService(void);
59  static void onReceiveService(uint8_t* inBytes, int numBytes);
60 
61  public:
63  TwoWire();
65  void begin();
70  void begin(uint8_t address);
76  void begin(int address);
81  void beginTransmission(uint8_t address);
87  void beginTransmission(int address);
98  uint8_t endTransmission(void);
109  uint8_t endTransmission(uint8_t sendStop);
117  uint8_t requestFrom(uint8_t address, uint8_t quantity);
125  uint8_t requestFrom(uint8_t address , uint8_t quantity, uint8_t sendStop);
133  uint8_t requestFrom(int address, int quantity);
142  uint8_t requestFrom(int address, int quantity, int sendStop);
151  virtual size_t write(uint8_t data);
159  virtual size_t write(const uint8_t *data, size_t quantity);
164  virtual int available(void);
170  virtual int read(void);
176  virtual int peek(void);
183  void onReceive( void (*function)(int) );
190  void onRequest( void (*function)(void) );
191 
192  /* Removed due to: not needed
193  inline size_t write(unsigned long n) { return write((uint8_t)n); }
194  inline size_t write(long n) { return write((uint8_t)n); }
195  inline size_t write(unsigned int n) { return write((uint8_t)n); }
196  inline size_t write(int n) { return write((uint8_t)n); }
197  */
198  using Print::write;
199  };
200  }
201  extern "C" {
202  #include <stdlib.h>
203  #include <string.h>
204  #include <inttypes.h>
205  //#include "twi.h"
206  }
207 
209  extern TwoWire Wire;
210  uint8_t TwoWire::rxBuffer[BUFFER_LENGTH];
211  uint8_t TwoWire::rxBufferIndex = 0;
212  uint8_t TwoWire::rxBufferLength = 0;
213 
214  uint8_t TwoWire::txAddress = 0;
215  uint8_t TwoWire::txBuffer[BUFFER_LENGTH];
216  uint8_t TwoWire::txBufferIndex = 0;
217  uint8_t TwoWire::txBufferLength = 0;
218 
219  uint8_t TwoWire::transmitting = 0;
220  void (*TwoWire::user_onRequest)(void);
221  void (*TwoWire::user_onReceive)(int);
222 
223  TwoWire::TwoWire() {}
224 
225  void TwoWire::begin(void) {
226  rxBufferIndex = 0;
227  rxBufferLength = 0;
228 
229  txBufferIndex = 0;
230  txBufferLength = 0;
231 
232  twi_init();
233  }
234 
235  void TwoWire::begin(uint8_t address) {
236  twi_setAddress(address);
237  twi_attachSlaveTxEvent(onRequestService);
238  twi_attachSlaveRxEvent(onReceiveService);
239  begin();
240  }
241 
242  void TwoWire::begin(int address) {
243  begin((uint8_t)address);
244  }
245 
246  uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop) {
247  if(quantity > BUFFER_LENGTH){
248  quantity = BUFFER_LENGTH;
249  }
250  uint8_t read = twi_readFrom(address, rxBuffer, quantity, sendStop);
251  rxBufferIndex = 0;
252  rxBufferLength = read;
253 
254  return read;
255  }
256 
257  uint8_t TwoWire::requestFrom(uint8_t address, uint8_t quantity) {
258  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
259  }
260 
261  uint8_t TwoWire::requestFrom(int address, int quantity) {
262  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)true);
263  }
264 
265  uint8_t TwoWire::requestFrom(int address, int quantity, int sendStop) {
266  return requestFrom((uint8_t)address, (uint8_t)quantity, (uint8_t)sendStop);
267  }
268 
269  void TwoWire::beginTransmission(uint8_t address) {
270  transmitting = 1;
271  txAddress = address;
272  txBufferIndex = 0;
273  txBufferLength = 0;
274  }
275 
276  void TwoWire::beginTransmission(int address) {
277  beginTransmission((uint8_t)address);
278  }
279 
280  uint8_t TwoWire::endTransmission(uint8_t sendStop) {
281  int8_t ret = twi_writeTo(txAddress, txBuffer, txBufferLength, 1, sendStop);
282  txBufferIndex = 0;
283  txBufferLength = 0;
284  transmitting = 0;
285  return ret;
286  }
287 
288  uint8_t TwoWire::endTransmission(void){
289  return endTransmission(true);
290  }
291 
292  size_t TwoWire::write(uint8_t data) {
293  if(transmitting) {
294  if(txBufferLength >= BUFFER_LENGTH) {
295  setWriteError();
296  return 0;
297  }
298  txBuffer[txBufferIndex] = data;
299  ++txBufferIndex;
300  txBufferLength = txBufferIndex;
301  }else{
302  twi_transmit(&data, 1);
303  }
304  return 1;
305  }
306 
307  size_t TwoWire::write(const uint8_t *data, size_t quantity) {
308  if(transmitting){
309  for(size_t i = 0; i < quantity; ++i) {
310  write(data[i]);
311  }
312  }else{
313  twi_transmit(data, quantity);
314  }
315  return quantity;
316  }
317 
318  int TwoWire::available(void) {
319  return rxBufferLength - rxBufferIndex;
320  }
321 
322  int TwoWire::read(void) {
323  int8_t value = -1;
324 
325  if(rxBufferIndex < rxBufferLength) {
326  value = rxBuffer[rxBufferIndex];
327  ++rxBufferIndex;
328  }
329 
330  return value;
331  }
332 
333  int TwoWire::peek(void) {
334  int value = -1;
335 
336  if(rxBufferIndex < rxBufferLength) {
337  value = rxBuffer[rxBufferIndex];
338  }
339 
340  return value;
341  }
342 
343  void TwoWire::onReceiveService(uint8_t* inBytes, int numBytes) {
344  if(!user_onReceive){
345  return;
346  }
347 
348  if(rxBufferIndex < rxBufferLength) {
349  return;
350  }
351 
352  for(uint8_t i = 0; i < numBytes; ++i) {
353  rxBuffer[i] = inBytes[i];
354  }
355  rxBufferIndex = 0;
356  rxBufferLength = numBytes;
357  user_onReceive(numBytes);
358  }
359 
360  void TwoWire::onRequestService(void) {
361  if(!user_onRequest) return;
362 
363  txBufferIndex = 0;
364  txBufferLength = 0;
365  user_onRequest();
366  }
367 
368  void TwoWire::onReceive( void (*function)(int) ) {
369  user_onReceive = function;
370  }
371 
372  void TwoWire::onRequest( void (*function)(void) ) {
373  user_onRequest = function;
374  }
375 
378 
379  TwoWire Wire = TwoWire();
380 
381  #define TwoWire_h
382 #endif
void onRequest(void(*function)(void))
this will set the user_onRequest method
virtual size_t write(uint8_t data)
this will write a single unsigned 8-bit value to address
static void(* user_onReceive)(int numBytes)
twi slave [Rx]receive-event user def handler
Definition: eagle_TwoWire.h:51
void twi_setAddress(uint8_t)
static uint8_t rxBufferIndex
this defines the rxBuffer Index - current position in rxBuffer array
Definition: eagle_TwoWire.h:30
static uint8_t txBufferIndex
this defines the txBuffer Index - current position in txBuffer array
Definition: eagle_TwoWire.h:39
static void onRequestService(void)
twi slave [Tx]transmitting-event handler
This is used to avoid path resolving issues and defines the common known Arduino Wire-Interface &#160;&#160;&#160;...
Definition: eagle_TwoWire.h:25
static uint8_t transmitting
&#39;boolean&#39; value. Set to 1 if transmitting => in master write mode
Definition: eagle_TwoWire.h:44
uint8_t twi_transmit(const uint8_t *, uint8_t)
uint8_t twi_readFrom(uint8_t, uint8_t *, uint8_t, uint8_t)
static uint8_t rxBufferLength
this defines the length of rxBuffer
Definition: eagle_TwoWire.h:32
static uint8_t txAddress
this defines the txAddress the transmitting Dta
Definition: eagle_TwoWire.h:35
static uint8_t txBuffer[]
this defines the txBuffer used to enable delayed read
Definition: eagle_TwoWire.h:37
this namespace contains all the Don&#39;t use manually classes ;)
#define BUFFER_LENGTH
Definition: eagle_TwoWire.h:7
virtual int read(void)
this will read a single byte from rxBuffer and increment the Index
uint8_t twi_writeTo(uint8_t, uint8_t *, uint8_t, uint8_t, uint8_t)
uint8_t requestFrom(uint8_t address, uint8_t quantity)
this will read a specific quantity of bytes from a specific address
static void onReceiveService(uint8_t *inBytes, int numBytes)
twi slave [Rx]receive-event handler
void begin()
begin the TwoWire communcation without any data set
TwoWire()
The constructor of the TwoWire class.
void beginTransmission(uint8_t address)
this will start a new transmission to a specific address => master mode
virtual int peek(void)
this will read a single byte from rxBuffer without increment the Index
void twi_attachSlaveRxEvent(void(*)(uint8_t *, int))
void onReceive(void(*function)(int))
this will set the user_onReceive method
static uint8_t txBufferLength
this defines the length of txBuffer
Definition: eagle_TwoWire.h:41
TwoWire Wire
this is the well-known Arduino Wire Interface, just a little bit &#39;modified&#39; ;P
void twi_init(void)
uint8_t endTransmission(void)
this will end the transmission and send the STOP-sequence
virtual int available(void)
this will return the amount of rxBuffer left
static void(* user_onRequest)(void)
twi slave [Tx]transmitting-event user def handler
Definition: eagle_TwoWire.h:46
static uint8_t rxBuffer[]
this defines the rxBuffer used to enable delayed read
Definition: eagle_TwoWire.h:28
void twi_attachSlaveTxEvent(void(*)(void))