#include "defines.h"
#include "uart.h"
#include <avr/io.h>

#ifndef i2c_H
#define i2c_H

#define TWSR TWSR0
#define TWDR TWDR0
#define TWBR TWBR0
#define TWCR TWCR0

#define I2C_START_WRITE(device_addr)                                           \
    {                                                                          \
        if (i2c_start((device_addr << 1) | 0x00)) {                            \
            return 1;                                                          \
        }                                                                      \
    }

#define I2C_START_READ(device_addr)                                            \
    {                                                                          \
        if (i2c_start((device_addr << 1) | 0x01)) {                            \
            return 1;                                                          \
        }                                                                      \
    }

// A byte at F_SCL takes well under 100 us; anything past this means the bus is
// stuck (peripheral unpowered, SDA held low) and we must not spin forever.
#define I2C_TIMEOUT_LOOPS 20000U

void i2c_init(void);
uint8_t i2c_start(uint8_t address);

uint8_t write_one_byte(uint8_t device_addr, uint8_t register_addr,
                       uint8_t data);
uint8_t write_n_bytes(uint8_t device_addr, uint8_t register_addr, uint8_t *data,
                      uint8_t n_bytes);

uint8_t read_one_byte_16bit_addr_no_err_register(uint8_t device_addr, uint16_t register_addr);
uint8_t read_one_byte_16bit_addr(uint8_t device_addr, uint16_t register_addr,
                      uint8_t *data);
uint8_t read_n_bytes_16bit_addr(uint8_t device_addr, uint16_t register_addr, uint8_t *data,
                     uint8_t n_bytes);


uint8_t read_one_byte_no_err_register(uint8_t device_addr, uint8_t register_addr);
uint8_t read_one_byte(uint8_t device_addr, uint8_t register_addr,
                      uint8_t *data);
uint8_t read_n_bytes(uint8_t device_addr, uint8_t register_addr, uint8_t *data,
                     uint8_t n_bytes);

void i2c_stop(void);
uint8_t i2c_read_ack(void);
uint8_t i2c_read_nack(void);
uint8_t i2c_write(uint8_t data);

#endif