//
// Created by dell on 24-9-26.
//

#include "mlx90614.h"
#include "gd32e23x.h"
#include "systick.h"
#include <stdio.h>

void i2c_config(void) {
    rcu_periph_clock_enable(RCU_I2C0);
    rcu_periph_clock_enable(RCU_GPIOF);

    gpio_af_set(GPIOF, GPIO_AF_1, GPIO_PIN_0 | GPIO_PIN_1);
    gpio_output_options_set(GPIOF, GPIO_OTYPE_OD, GPIO_OSPEED_50MHZ, GPIO_PIN_0 | GPIO_PIN_1);

    i2c_clock_config(I2C0, 400000U, I2C_DTCY_2);
    i2c_mode_addr_config(I2C0, I2C_I2CMODE_ENABLE, I2C_ADDFORMAT_7BITS, 0xA0);
    i2c_enable(I2C0);
    i2c_ack_config(I2C0, I2C_ACK_ENABLE);

    i2c_start_on_bus(I2C0);

    // nvic_irq_enable(I2C0_EV_IRQn, 2);
    // nvic_irq_enable(I2C0_ER_IRQn, 2);
}

int read_ir_mlx90614(void) {
    uint8_t Data[5];
    int inttemp_ir = 0;
    uint32_t TIMEOUT = 0;
    // while(TIMEOUT<10000&&I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY))
    while (TIMEOUT < 10000 && i2c_flag_get(I2C0, I2C_FLAG_I2CBSY))
        TIMEOUT++;
    if (TIMEOUT >= 10000) {
        printf("ERROR0\r\n");
        return -410;
    }
    TIMEOUT = 0;
    // I2C_GenerateSTART(I2C0, ENABLE);
    i2c_start_on_bus(I2C0);
    // while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECT))
    /* 等待发送模式
     * I2C_EVENT_MASTER_MODE_SELECT --> BUSY, MSL and SB flags
     *   ||
     * !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_MASTER) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_SBSEND)
     */
    while (TIMEOUT < 10000
           && !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)
           && !i2c_flag_get(I2C0, I2C_FLAG_MASTER)
           && !i2c_flag_get(I2C0, I2C_FLAG_SBSEND))
        TIMEOUT++;
    if (TIMEOUT >= 10000) {
        printf("ERROR1\r\n");
        return -410;
    }
    //	I2C_AcknowledgeConfig(I2C0,DISABLE);
    TIMEOUT = 0;
    // I2C_Send7bitAddress(I2C0, 0XB4, I2C_Direction_Transmitter);
    i2c_master_addressing(I2C0, SLAVE_ADDR, I2C_TRANSMITTER);
    // while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
    /* 等待发送地址完成
     * I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED --> BUSY, MSL, ADDR, TXE and TRA flags
     *   ||
     * !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_MASTER) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_TBE) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_TR)
     */
    while (TIMEOUT < 10000 &&
           !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) &&
           !i2c_flag_get(I2C0, I2C_FLAG_MASTER) &&
           !i2c_flag_get(I2C0, I2C_FLAG_ADDSEND) &&
           !i2c_flag_get(I2C0, I2C_FLAG_TBE) &&
           !i2c_flag_get(I2C0, I2C_FLAG_TR))
        TIMEOUT++;
    if (TIMEOUT >= 10000) {
        printf("ERROR2\r\n");
        return -410;
    }
    // I2C_SendData(I2C0, 0x07);
    i2c_data_transmit(I2C0, REG_ADDR_OBJ_TEMP);

    TIMEOUT = 0;
    // I2C_GenerateSTART(I2C0, ENABLE);
    i2c_start_on_bus(I2C0);
    // while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_MODE_SELECT))
    /* 等待发送模式
     * I2C_EVENT_MASTER_MODE_SELECT --> BUSY, MSL and SB flags
     *   ||
     * !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_MASTER) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_SBSEND)
     */
    while (TIMEOUT < 10000 && !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)
           && !i2c_flag_get(I2C0, I2C_FLAG_MASTER)
           && !i2c_flag_get(I2C0, I2C_FLAG_SBSEND))
        TIMEOUT++;
    if (TIMEOUT >= 10000) {
        printf("ERROR3\r\n");
        return -410;
    }
    TIMEOUT = 0;
    // I2C_Send7bitAddress(I2C0, 0XB4, I2C_Direction_Receiver);
    i2c_master_addressing(I2C0, SLAVE_ADDR, I2C_RECEIVER);
    // while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED))
    /* 等待接收完成
     * I2C_EVENT_MASTER_BYTE_RECEIVED --> BUSY, MSL, RXNE flags
     *   ||
     * !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_MASTER) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_RBNE)
     */
    while (TIMEOUT < 10000 && !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)
           && !i2c_flag_get(I2C0, I2C_FLAG_MASTER)
           && !i2c_flag_get(I2C0, I2C_FLAG_RBNE))
        TIMEOUT++;
    if (TIMEOUT >= 10000) {
        printf("ERROR4\r\n");
        return -410;
    }
    //I2C_AcknowledgeConfig(I2C0,DISABLE);
    TIMEOUT = 0;
    // Data[0] = I2C_ReceiveData(I2C0);
    Data[0] = i2c_data_receive(I2C0);
    // while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED))
    /* 等待接收完成
     * I2C_EVENT_MASTER_BYTE_RECEIVED --> BUSY, MSL, RXNE flags
     *   ||
     * !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_MASTER) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_RBNE)
     */
    while (TIMEOUT < 10000 && !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)
           && !i2c_flag_get(I2C0, I2C_FLAG_MASTER)
           && !i2c_flag_get(I2C0, I2C_FLAG_RBNE))
        TIMEOUT++;
    if (TIMEOUT >= 10000) {
        printf("ERROR5\r\n");
        return -410;
    }

    TIMEOUT = 0;
    // Data[1] = I2C_ReceiveData(I2C0);
    Data[1] = i2c_data_receive(I2C0);
    // while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED))
    /* 等待接收完成
     * I2C_EVENT_MASTER_BYTE_RECEIVED --> BUSY, MSL, RXNE flags
     *   ||
     * !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_MASTER) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_RBNE)
     */
    while (TIMEOUT < 10000 && !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)
           && !i2c_flag_get(I2C0, I2C_FLAG_MASTER)
           && !i2c_flag_get(I2C0, I2C_FLAG_RBNE))
        TIMEOUT++;
    if (TIMEOUT >= 10000) {
        printf("ERROR6\r\n");
        return -410;
    }

    TIMEOUT = 0;
    // Data[2] = I2C_ReceiveData(I2C0);
    Data[2] = i2c_data_receive(I2C0);
    // while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED))
    /* 等待接收完成
     * I2C_EVENT_MASTER_BYTE_RECEIVED --> BUSY, MSL, RXNE flags
     *   ||
     * !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_MASTER) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_RBNE)
     */
    while (TIMEOUT < 10000 && !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)
           && !i2c_flag_get(I2C0, I2C_FLAG_MASTER)
           && !i2c_flag_get(I2C0, I2C_FLAG_RBNE))
        TIMEOUT++;
    if (TIMEOUT >= 10000) {
        //printf("ERROR7\r\n");
        return -410;
    }


    // I2C_AcknowledgeConfig(I2C0, DISABLE);
    i2c_ack_config(I2C0, I2C_ACK_DISABLE);
    // Data[3] = I2C_ReceiveData(I2C0);
    Data[3] = i2c_data_receive(I2C0);
    // while (TIMEOUT < 10000 && !I2C_CheckEvent(I2C0, I2C_EVENT_MASTER_BYTE_RECEIVED))
    /* 等待接收完成
     * I2C_EVENT_MASTER_BYTE_RECEIVED --> BUSY, MSL, RXNE flags
     *   ||
     * !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_MASTER) &&
     * !i2c_flag_get(I2C0, I2C_FLAG_RBNE)
     */
    while (TIMEOUT < 10000 && !i2c_flag_get(I2C0, I2C_FLAG_I2CBSY)
           && !i2c_flag_get(I2C0, I2C_FLAG_MASTER)
           && !i2c_flag_get(I2C0, I2C_FLAG_RBNE))
        TIMEOUT++;
    if (TIMEOUT >= 10000) {
        //printf("ERROR11\r\n");
        return -410;
    }


    // I2C_GenerateSTOP(I2C0, ENABLE);
    i2c_stop_on_bus(I2C0);
    // I2C_AcknowledgeConfig(I2C0, ENABLE);
    i2c_ack_config(I2C0, I2C_ACK_ENABLE);
    printf("data:%x,%x,%x\r\n",Data[0],Data[1],Data[2]);
    // inttemp_ir = (int) ((Data[0] + Data[1] * 255) * 0.2 - 2731.5);
    // Data[0] = 0x41;
    // Data[1] = 0x39;
    inttemp_ir = (((Data[1] << 8) | Data[0]) * 2 - 27315);
    //		printf("temp:%d\r\n",inttemp_ir);


    // if (inttemp_ir < -4000)
    //     inttemp_ir = -4000;
    // if (inttemp_ir > 8500)
    //     inttemp_ir = 8500;
    //
    return inttemp_ir;
}
