/**
 * @file iic_drv.h
 * @author June6 (https://github.com/Junedoubling6)
 * @brief
 * @version 0.1
 * @date 2023-08-16
 *
 * @copyright Copyright (c) 2022 - , All rights reserved.
 *
 */
#ifndef IIC_DRV_H__
#define IIC_DRV_H__

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "driver/gpio.h"
#include "err_code.h"

#ifdef __cplusplus
extern "C" {
#endif

/*!< IIC master iic port number, the number of iic peripheral interfaces
 * available will depend on the chip */
#define IIC_MASTER_NUM 0
#define IIC_PIN_NUM_SCL (GPIO_NUM_20)
#define IIC_PIN_NUM_SDA (GPIO_NUM_19)
#define IIC_MASTER_TIMEOUT_MS 1000

/**
 * @brief Config iic master.
 *
 * @param port_num
 * @param pin_scl
 * @param pin_sda
 * @param clk_speed
 * @return esp_err_t
 */
esp_err_t iic_master_config(int port_num, int pin_scl, int pin_sda,
                            uint32_t clk_speed);

/**
 * @brief IIC master read to slave.
 *
 * @param port_num
 * @param dev_addr
 * @param reg_addr
 * @param data
 * @param len
 * @param ack_enable
 * @return esp_err_t
 */
esp_err_t iic_master_read_slave(int port_num, uint8_t dev_addr,
                                uint8_t reg_addr, uint8_t* data, size_t len,
                                bool ack_enable);

/**
 * @brief Write a byte to a sensor register.
 *
 * @param port_num
 * @param dev_addr
 * @param reg_addr
 * @param data
 * @param len
 * @param ack_enable
 * @return esp_err_t
 * @details The maximum size of data written is 128 bytes.
 */
esp_err_t iic_master_write_slave(int port_num, uint8_t dev_addr,
                                 uint8_t reg_addr, uint8_t* data, size_t len,
                                 bool ack_enable);

#ifdef __cplusplus
}
#endif

#endif  // IIC_DRV_H__