
#ifndef TwoWire_h
#define TwoWire_h

#include "Stream.h"
#include "Arduino.h"

#define BUFFER_LENGTH 32

// WIRE_HAS_END means Wire has end()
#define WIRE_HAS_END      1

class TwoWire : public Stream
{
  private:
    static uint8_t rxBuffer[];
    static uint8_t rxBufferIndex;
    static uint8_t rxBufferLength;

    static uint8_t txAddress;
    static uint8_t txBuffer[];
    static uint8_t txBufferIndex;
    static uint8_t txBufferLength;

    static uint8_t transmitting;
    static uint8_t slaveAddress;
    static void (*user_onRequest)(void);
    static void (*user_onReceive)(int numBytes);
    static void onRequestService(void);
    static void onReceiveService(uint8_t* inBytes, int numBytes);
  public:
    TwoWire();
    void begin();
    void begin(uint8_t);
    void begin(int);
    void end();
    void setClock(uint32_t frequency);

    void beginTransmission(uint8_t address);
    void beginTransmission(int address);
    uint8_t endTransmission(void);
    uint8_t endTransmission(uint8_t sendStop);

    uint8_t requestFrom(uint8_t address, uint8_t quantity);
    uint8_t requestFrom(uint8_t address, uint8_t quantity, uint8_t sendStop);
    uint8_t requestFrom(uint8_t address, uint8_t quantity, uint32_t iaddress, uint8_t isize, uint8_t sendStop);
    uint8_t requestFrom(int address, int quantity);
    uint8_t requestFrom(int address, int quantity, int sendStop);

    virtual int available(void);
    virtual int read(void);
    virtual int peek(void);
    virtual void flush(void);

    virtual size_t write(uint8_t data);
    virtual size_t write(const uint8_t * data, size_t quantity);
    inline size_t write(unsigned long n)  { return write((uint8_t)n); }
    inline size_t write(long n)           { return write((uint8_t)n); }
    inline size_t write(unsigned int n)   { return write((uint8_t)n); }
    inline size_t write(int n)            { return write((uint8_t)n); }
    using Print::write;
    
    void onReceive( void (*)(int) );
    void onRequest( void (*)(void) );
};

extern TwoWire Wire;

#endif
