#ifndef __TWI_H__
#define __TWI_H__

#ifdef __cplusplus
extern "C" {
#endif

#ifndef TWI_BUFFER_LENGTH
#define TWI_BUFFER_LENGTH   32
#endif

// I2C state
typedef enum 
{
  I2C_OK            = 0,
  I2C_DATA_TOO_LONG = 1,
  I2C_NACK_ADDR     = 2,
  I2C_NACK_DATA     = 3,
  I2C_ERROR         = 4,
  I2C_TIMEOUT       = 5,
  I2C_BUSY          = 6
} i2c_status_e;

uint8_t twi_init(uint8_t slaveAddress);
void twi_deinit(void);
uint8_t twi_setFrequency(uint32_t frequency, bool onInit);

uint8_t twi_masterReadFrom(uint8_t address, uint8_t* data, uint8_t length, uint8_t sendStop);
uint8_t twi_masterWriteTo(uint8_t address, uint8_t* data, uint8_t length, uint8_t sendStop);
i2c_status_e twi_slaveWrite(uint8_t *txData, uint8_t bytesNum);

void twi_attachSlaveRxEvent(void (*function)(uint8_t*, int)); 
void twi_attachSlaveTxEvent(void (*function)(void));
void twi_interruptHandler(void);

#ifdef __cplusplus
}
#endif

#endif /* __TWI_H__ */

