#include "DS18B20.hpp"
#include "HC-12.hpp"
#include "common.hpp"

QueueHandle_t xQueue;


// 温度传感器
void DS18B20(void *pvParameters)
{
    // One_wire 第三方库方法
    float TEMPERATURE = -1;
    char TEMPERATURE_TEMP[BUFER] = { 0 };
    One_wire one_wire(DS18B20_PIN);
    one_wire.init();
    rom_address_t address {
        
    };


    _printTaskStackHighWaterMark("DS18B20");

    while (1) {

    
        one_wire.single_device_read_rom(address);
        int delay_time = one_wire.convert_temperature(address, true, false);
        //printf("Conversion delay time: %d ms\n", delay_time);

        TEMPERATURE = one_wire.temperature(address);

        printf("Device Address: %02x%02x%02x%02x%02x%02x%02x%02x DS18B20 Temperature: %3.1f°C\n", \
                address.rom[0], address.rom[1], address.rom[2], address.rom[3], address.rom[4], \
                address.rom[5], address.rom[6], address.rom[7], one_wire.temperature(address));
                
        if (TEMPERATURE == -1000) {
            continue;
        }
        
        if (TEMPERATURE != 85) {
                
                sprintf(TEMPERATURE_TEMP, "Temperature: %.3f°C\n", TEMPERATURE);
                _HC_12(TEMPERATURE_TEMP);
                memset(TEMPERATURE_TEMP, 0, BUFER);
                TEMPERATURE = -1;
        }
/*
        // 发送数据到队列
        xQueueSend(xQueue, &TEMPERATURE, portMAX_DELAY);
        vTaskDelay(pdMS_TO_TICKS(1000));
*/
        //_printTaskStackHighWaterMark("DS18B20");
        //watchdog_update();                  // 喂狗
        vTaskDelay(pdMS_TO_TICKS(5000));    // 非阻塞延时
    }
    
    return ;
}
