Advertisement
yOPERO

Untitled

May 19th, 2012
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. /*
  2.   TwoWire.h - TWI/I2C library for Arduino & Wiring
  3.   Copyright (c) 2006 Nicholas Zambetti.  All right reserved.
  4.  
  5.   This library is free software; you can redistribute it and/or
  6.   modify it under the terms of the GNU Lesser General Public
  7.   License as published by the Free Software Foundation; either
  8.   version 2.1 of the License, or (at your option) any later version.
  9.  
  10.   This library is distributed in the hope that it will be useful,
  11.   but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.   Lesser General Public License for more details.
  14.  
  15.   You should have received a copy of the GNU Lesser General Public
  16.   License along with this library; if not, write to the Free Software
  17.   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  18. */
  19.  
  20. #ifndef TwoWire_h
  21. #define TwoWire_h
  22.  
  23. #include <inttypes.h>
  24. #include "Stream.h"
  25.  
  26. #define BUFFER_LENGTH 32
  27.  
  28. class TwoWire : public Stream
  29. {
  30.   private:
  31.     static uint8_t rxBuffer[];
  32.     static uint8_t rxBufferIndex;
  33.     static uint8_t rxBufferLength;
  34.  
  35.     static uint8_t txAddress;
  36.     static uint8_t txBuffer[];
  37.     static uint8_t txBufferIndex;
  38.     static uint8_t txBufferLength;
  39.  
  40.     static uint8_t transmitting;
  41.     static void (*user_onRequest)(void);
  42.     static void (*user_onReceive)(int);
  43.     static void onRequestService(void);
  44.     static void onReceiveService(uint8_t*, int);
  45.   public:
  46.     TwoWire();
  47.     void begin();
  48.     void begin(uint8_t);
  49.     void begin(int);
  50.     void beginTransmission(uint8_t);
  51.     void beginTransmission(int);
  52.     uint8_t endTransmission(void);
  53.     uint8_t requestFrom(uint8_t, uint8_t);
  54.     uint8_t requestFrom(int, int);
  55.     virtual size_t write(uint8_t);
  56.     virtual size_t write(const uint8_t *, size_t);
  57.     virtual int available(void);
  58.     virtual int read(void);
  59.     virtual int peek(void);
  60.     virtual void flush(void);
  61.     void onReceive( void (*)(int) );
  62.     void onRequest( void (*)(void) );
  63.    
  64.     inline size_t write(unsigned long n) { return write((uint8_t)n); }
  65.     inline size_t write(long n) { return write((uint8_t)n); }
  66.     inline size_t write(unsigned int n) { return write((uint8_t)n); }
  67.     inline size_t write(int n) { return write((uint8_t)n); }
  68.  
  69.     using Print::write;
  70. };
  71.  
  72. extern TwoWire Wire;
  73.  
  74. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement