#ifndef _IC2_H_
#define _I2C_H_

//------------------------------------------------------------------
// qfixI2C.h
//
// This class is used for low-level I2C communication.
//
// For TW_ constants see compat/twi.h
//
// Copyright 2005-2006 by KTB mechatronics GmbH
// Author: Stefan Enderle, Florian Schrapp
//------------------------------------------------------------------



#include "../global.h"
#include <compat/twi.h>

const int ACTION_SEND = 1;
const int ACTION_GET  = 2;
const int ACTION_UNKNOWN  = 3;


const uint8_t ERROR_NO_ACK   = 1;
const uint8_t ERROR_NO_START = 2;
const uint8_t ERROR_NOT_SENT = 3;     // send() could not send byte(s)



class I2C
{
private:
  uint8_t err;
  uint8_t adr;

  void sendStartSLA_W(uint8_t address);
  void sendStartSLA_R(uint8_t address);
  void sendByte(uint8_t data);
  void sendStop();
  void readByte(uint8_t& data);

public:
  I2C();
  uint8_t error();

// master side //

  void send(uint8_t address, uint8_t data);
  void send(uint8_t address, uint8_t* data, int length);
  void get(uint8_t address, uint8_t* data, int length);

// slave side //

  void setSlaveAdress(uint8_t address);
  bool isAction();
  bool isActionSend();  // true if master sent something
  bool isActionGet();   // true if master wants to get something
  int receive(uint8_t* data); // if action is send, receive the bytes
  void returnBytes(uint8_t* data, int len, bool lastOne);
};






#endif

