124 lines
3.4 KiB
C++
124 lines
3.4 KiB
C++
#include "ZC13.hpp"
|
||
#include "common.hpp"
|
||
#include "HC-12.hpp"
|
||
|
||
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;
|
||
}
|
||
|
||
void CH4(void *pvParameters)
|
||
{
|
||
ZC13_INIT();
|
||
_printTaskStackHighWaterMark("CH4");
|
||
|
||
while (1)
|
||
{
|
||
|
||
char CH4_DATA[BUFER] = { 0 };
|
||
sprintf(CH4_DATA, "CH4 Concentration: %d\n", ZC13("ZC05"));
|
||
_HC_12(CH4_DATA);
|
||
|
||
//_printTaskStackHighWaterMark("CH4");
|
||
//watchdog_update(); // 喂狗
|
||
vTaskDelay(pdMS_TO_TICKS(3000)); // 非阻塞延时
|
||
}
|
||
|
||
return ;
|
||
}
|