#include "ZC13.hpp"
#include "common.h"

int ZC13_INIT()
{
    uint tx_offset = pio_add_program(ZC13_PIO, &uart_tx_program);
    uart_tx_program_init(ZC13_PIO, ZC13_PIO_SM_TX, tx_offset, ZC13_PIO_TX_PIN, ZC13_PIO_SERIAL_BAUD);

    uint rx_offset = pio_add_program(ZC13_PIO, &uart_rx_program);
    uart_rx_program_init(ZC13_PIO, ZC13_PIO_SM_RX, rx_offset, ZC13_PIO_RX_PIN, ZC13_PIO_SERIAL_BAUD);

    return 0;
}

int ZC13_PIO_UART_TX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
{

    for (int i = 0; i < DATA_LEN; i++) {
        uart_tx_program_putc(pio, sm, DATA[i]);
        sleep_ms(3);
    }

    return 0;
}

int ZC13_PIO_UART_RX_DATA(PIO pio, uint sm, uint8_t * DATA, int DATA_LEN)
{
    char c = '\0';
    int received_count = 0;
    int timeout_ms = 100;       // 设置较长的超时时间

    while (received_count < DATA_LEN && timeout_ms > 0) {
        if (uart_rx_program_available(pio, sm)) {
            c = uart_rx_program_getc(pio, sm);
            DATA[received_count++] = c;
            printf("0x%X ", c);
            if (c == '\n') {
                // 接收到换行符，停止接收数据
                break;
            }
        } else {
            // 没有接收到数据，继续等待
            sleep_ms(1);
            timeout_ms--;
        }
    }

    if (received_count == 0) {
        // 没有接收到有效数据，可以进行相应的处理
        return -1;
    }

    return received_count;
}

int ZC13(const char *model)
{
    uint8_t CH4_DATA[270] = { 0 };
    int CH4_DATA_LEN = 0;

    // 发送指令
    uint8_t CH4_CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
    ZC13_PIO_UART_TX_DATA(ZC13_PIO, ZC13_PIO_SM_TX, CH4_CMD, sizeof(CH4_CMD));
    sleep_ms(200);

    // 接收指令
    CH4_DATA_LEN = ZC13_PIO_UART_RX_DATA(ZC13_PIO, ZC13_PIO_SM_RX, CH4_DATA, 9);

    printf("\n");
    printf("%d\n", CH4_DATA_LEN);

    uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节
    uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节

    // 判断最高位是否为 1
    if (highByte & 0x80) {
        // 最高位是 1，表示传感器故障
        printf("CH4 sensor malfunction!\n");
    } else {
        if (CH4_DATA[1] == 0X86) {
            if (0 == strcasecmp(model, "ZC05")) {
                // 计算气体浓度值
                uint16_t gasConcentration = (highByte & 0x3F) * 256 + lowByte;
                // 输出气体浓度值
                printf("CH4 Concentration: %uppm\n", gasConcentration);

                return gasConcentration;
            }

            if (0 == strcasecmp(model, "ZC13")) {
                // 计算气体浓度值
                uint16_t gasConcentration = (highByte & 0x1F) * 256 + lowByte;
                // 输出气体浓度值
                printf("CH4 Concentration: %uppm\n", gasConcentration);

                return gasConcentration;
            }
        }
    }

    return -1;
}

/*
int ZC13(const char *model)
{
    char CH4_DATA[9] = { 0 };
    int CH4_DATA_index = 0;

    // 发送指令
    char CH4_CMD[9] = { 0xFF, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 };
    for (int i = 0; i < 9; i++) {
        uart_tx_program_putc(ZC13_PIO, ZC13_PIO_SM_TX, CH4_CMD[i]);
        sleep_ms(1);
    }
    sleep_ms(200);

    // 接收数据，直到收到换行符或达到缓冲区大小限制
    char c = '\0';
    int timeout_ms = 100;       // 设置超时时间为100ms
    int received_count = 0;

    while (received_count < 9 && timeout_ms > 0) {
        if (uart_rx_program_available(ZC13_PIO, ZC13_PIO_SM_RX)) {
            c = uart_rx_program_getc(ZC13_PIO, ZC13_PIO_SM_RX);
            CH4_DATA[CH4_DATA_index++] = c;
            received_count++;
            timeout_ms = 100;   // 重置超时时间
        } else {
            sleep_ms(1);
            timeout_ms--;       // 减少超时时间
        }
    }

    // 处理接收到的数据
    for (int i = 0; i < 9; i++) {
        printf("0x%X ", CH4_DATA[i]);
    }
    printf("\n");

    uint8_t highByte = CH4_DATA[2]; // 假设这是气体浓度高位字节
    uint8_t lowByte = CH4_DATA[3]; // 假设这是气体浓度高位字节

    // 判断最高位是否为 1
    if (highByte & 0x80) {
        // 最高位是 1，表示传感器故障
        printf("CH4 sensor malfunction!\n");
    } else {
        if (CH4_DATA[1] == 0X86) {
			if ( 0 == strcasecmp(model, "ZC05")) {
				// 计算气体浓度值
				uint16_t gasConcentration = (highByte & 0x3F) * 256 + lowByte;
				// 输出气体浓度值
				printf("CH4 Concentration: %uppm\n", gasConcentration);
			
				return gasConcentration;
			}
			
			if ( 0 == strcasecmp(model, "ZC13")) {
				// 计算气体浓度值
				uint16_t gasConcentration = (highByte & 0x1F) * 256 + lowByte;
				// 输出气体浓度值
				printf("CH4 Concentration: %uppm\n", gasConcentration);
			
				return gasConcentration;
			}
        }
    }
	
    return -1;
}
*/
