#include <stdio.h>
#include "esp_log.h"
#include "driver/i2c.h"
#include "gw_grayscale_sensor.h"

static const char *TAG = "gw gray i2c example";

#define I2C_MASTER_SCL_IO           22      /*!< I2C 时钟针脚号  */
#define I2C_MASTER_SDA_IO           21      /*!< I2C 数据针脚号  */
#define I2C_MASTER_NUM              0       /*!< I2C 通道       */
#define I2C_MASTER_FREQ_HZ          100000  /*!< I2C 主的时钟频率 */
#define I2C_MASTER_TX_BUF_DISABLE   0       /*!< I2C 不设输出缓冲 */
#define I2C_MASTER_RX_BUF_DISABLE   0       /*!< I2C 不设输入缓冲 */
#define I2C_MASTER_TIMEOUT_MS       0

#define GW_GRAY_ADDR GW_GRAY_ADDR_DEF /*! 使用默认地址 */

static esp_err_t gw_gray_write_cmd(uint8_t cmd)
{
	esp_err_t err;
	err = i2c_master_write_to_device(I2C_MASTER_NUM, GW_GRAY_ADDR,
	                                 &cmd, 1, pdMS_TO_TICKS(I2C_MASTER_TIMEOUT_MS));
	return err;
}

static esp_err_t gw_gray_read_byte(uint8_t *byte_read)
{
	esp_err_t err;
	err = i2c_master_read_from_device(I2C_MASTER_NUM, GW_GRAY_ADDR,
	                                  byte_read, 1, pdMS_TO_TICKS(I2C_MASTER_TIMEOUT_MS));
	return err;
}

static esp_err_t gw_gray_read(uint8_t *read_arr, size_t read_size)
{
	esp_err_t err;
	err = i2c_master_read_from_device(I2C_MASTER_NUM, GW_GRAY_ADDR,
	                                  read_arr, read_size, pdMS_TO_TICKS(I2C_MASTER_TIMEOUT_MS));
	return err;
}

static esp_err_t gw_gray_write(uint8_t *write_arr, size_t write_size)
{
	esp_err_t err;
	err = i2c_master_write_to_device(I2C_MASTER_NUM, GW_GRAY_ADDR,
	                                 write_arr, write_size, pdMS_TO_TICKS(I2C_MASTER_TIMEOUT_MS));
	return err;
}

static esp_err_t i2c_master_init(void)
{
	int i2c_master_port = I2C_MASTER_NUM;

	i2c_config_t conf = {
			.mode = I2C_MODE_MASTER,
			.sda_io_num = I2C_MASTER_SDA_IO,
			.scl_io_num = I2C_MASTER_SCL_IO,
			.sda_pullup_en = GPIO_PULLUP_ENABLE,
			.scl_pullup_en = GPIO_PULLUP_ENABLE,
			.master.clk_speed = I2C_MASTER_FREQ_HZ,
	};

	i2c_param_config(i2c_master_port, &conf);

	return i2c_driver_install(i2c_master_port, conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
}


void app_main(void)
{
	uint8_t ping_rsp = 0;
	uint8_t read_arr[8] = {0};
	uint8_t send_buff[9];
	int err;
	ESP_ERROR_CHECK(i2c_master_init());
	ESP_LOGI(TAG, "I2C initialized successfully");

	/* PING 通为止 */
	while (ping_rsp != GW_GRAY_PING_OK) {
		ping_rsp = 0;
		err = gw_gray_write_cmd(GW_GRAY_PING);
		err = gw_gray_read_byte(&ping_rsp);
		ESP_LOGI(TAG, "ping_rsp = %X", ping_rsp);
		vTaskDelay(pdMS_TO_TICKS(500));
	}
	ESP_LOGI(TAG, "PING OK");

	/* 读取探头的滞回比较器参数 */
	ESP_ERROR_CHECK_WITHOUT_ABORT(gw_gray_write_cmd(GW_GRAY_CALIBRATION_BLACK)); //GW_GRAY_CALIBRATION_WHITE
	ESP_ERROR_CHECK_WITHOUT_ABORT(gw_gray_read(read_arr, 8));

	for (int i = 0; i < 8; ++i) {
		ESP_LOGI(TAG, "read_arr[%d] : %d", i, read_arr[i]);
	}

	/* 更新传感器的校准数据, 注:断电或重启后,参数会重置 */


	// 准备数据 [0:1] = 命令 [1:9] = 校准值
	send_buff[0] = GW_GRAY_CALIBRATION_BLACK; //命令
	// 8个校准值
	for (int i = 1; i < 9; ++i) {
		send_buff[i] = 0x66; // 这里随便写了个数据, 别拿去实战啊(:
	}

	// 发送9个字节的数据(命令+8个校准值)
	ESP_ERROR_CHECK_WITHOUT_ABORT(gw_gray_write(send_buff, 9));

	// 滞回比较参数更新完毕,使用新配置
	while (1) {
		vTaskDelay(portMAX_DELAY);
	}
}