2024-12-01 13:49:43 +08:00
|
|
|
|
/*
|
|
|
|
|
* @Author : stark1898y 1658608470@qq.com
|
|
|
|
|
* @Date : 2024-06-18 15:48:01
|
|
|
|
|
* @LastEditors: mbw && 1600520629@qq.com
|
2025-02-21 15:33:29 +08:00
|
|
|
|
* @LastEditTime: 2025-02-21 11:27:34
|
2024-12-01 13:49:43 +08:00
|
|
|
|
* @FilePath: \ble_bjq_ch303rct6_ml307\bsp\src\bsp_mq.c
|
|
|
|
|
* @Description :
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2024 by yzy, All Rights Reserved.
|
|
|
|
|
*/
|
|
|
|
|
/*
|
|
|
|
|
* @Author : yzy
|
|
|
|
|
* @Date : 2023-01-30 15:40:07
|
|
|
|
|
* @LastEditors : stark1898y 1658608470@qq.com
|
|
|
|
|
* @LastEditTime : 2023-09-19 16:25:03
|
|
|
|
|
* @FilePath : \JT-DT-YD1F01_RTT_Nano\bsp\src\bsp_mq.c
|
|
|
|
|
* @Description :
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2023 by yzy, All Rights Reserved.
|
|
|
|
|
*/
|
|
|
|
|
#include "bsp_mq.h"
|
|
|
|
|
#include "bsp_adc.h"
|
|
|
|
|
#include "bsp_flash.h"
|
|
|
|
|
#include "bsp_rtc.h"
|
|
|
|
|
#include "user_sys.h"
|
2024-12-13 19:07:17 +08:00
|
|
|
|
#include "finsh.h"
|
2025-02-21 15:33:29 +08:00
|
|
|
|
#include "bsp_led.h"
|
|
|
|
|
#include "at_device_ml307.h"
|
2024-12-13 19:07:17 +08:00
|
|
|
|
|
2024-12-30 17:51:13 +08:00
|
|
|
|
#define LOG_TAG "bsp_mq" // 该模块对应的标签。不定义时,默认:NO_TAG
|
|
|
|
|
#define LOG_LVL LOG_LVL_DBG // 该模块对应的日志输出级别。不定义时,默认:调试级别
|
|
|
|
|
#include <ulog.h> // 必须在 LOG_TAG 与 LOG_LVL 下面
|
2024-12-01 13:49:43 +08:00
|
|
|
|
|
|
|
|
|
ALIGN(RT_ALIGN_SIZE)
|
|
|
|
|
static char sensor_thread_stack[GAS_SNESOR_THREAD_STACK_SIZE];
|
|
|
|
|
static struct rt_thread Sensor_Thread;
|
2025-02-21 15:33:29 +08:00
|
|
|
|
rt_uint8_t alarm_flag = 0, fault_flag = 0;
|
2024-12-01 13:49:43 +08:00
|
|
|
|
TsSensor_t Sensor_device;
|
|
|
|
|
|
|
|
|
|
uint16_t Get_Gas_VoltageInt1000x(void)
|
|
|
|
|
{
|
|
|
|
|
uint16_t voltage = (Get_ADC_Average(kGasAdc) * 3.3 / 4096) * MQ_VOLTAGE_RATIO * 1000;
|
2024-12-30 17:51:13 +08:00
|
|
|
|
LOG_D("Get_Gas_VoltageInt1000x = %04d", voltage);
|
2024-12-01 13:49:43 +08:00
|
|
|
|
return voltage;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-17 15:42:58 +08:00
|
|
|
|
uint16_t Get_Gas_VoltageAdcInt1000x(void)
|
|
|
|
|
{
|
|
|
|
|
rt_uint16_t voltage_adc = (Get_ADC_Average(kGasAdc) * 3.3 / 4096) * 1000;
|
2025-05-22 17:46:17 +08:00
|
|
|
|
LOG_D("Get_Gas_VoltageAdcInt1000x = %04d", voltage_adc);
|
2024-12-17 15:42:58 +08:00
|
|
|
|
return voltage_adc;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-01 13:49:43 +08:00
|
|
|
|
#ifdef TEST_ENABLE
|
|
|
|
|
static void TEST_Get_Gas_VoltageInt1000x(void)
|
|
|
|
|
{
|
|
|
|
|
LOG_D("Get_Gas_VoltageInt1000x = %04d", Get_Gas_VoltageInt1000x());
|
|
|
|
|
}
|
|
|
|
|
MSH_CMD_EXPORT(TEST_Get_Gas_VoltageInt1000x, TEST_Get_Gas_VoltageInt1000x);
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
static uint8_t Sensor_Count(const rt_uint8_t *buffer, uint8_t value, uint8_t size)
|
|
|
|
|
{
|
|
|
|
|
uint8_t count = 0;
|
|
|
|
|
for (uint8_t i = 0; i < size; i++)
|
|
|
|
|
{
|
|
|
|
|
if (buffer[i] == value)
|
|
|
|
|
{
|
|
|
|
|
count++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 15:33:29 +08:00
|
|
|
|
static void Sensor_HandleAlarm(uint8_t count, uint8_t threshold)
|
2024-12-01 13:49:43 +08:00
|
|
|
|
{
|
2025-02-21 15:33:29 +08:00
|
|
|
|
if (count >= threshold && alarm_flag == 0)
|
2024-12-01 13:49:43 +08:00
|
|
|
|
{
|
2025-02-21 15:33:29 +08:00
|
|
|
|
alarm_flag = 1;
|
2024-12-01 13:49:43 +08:00
|
|
|
|
Sensor_device.detection_flag = kSensorAlarm;
|
|
|
|
|
Send_Laser_Alarm_Event(kAlarmEvent);
|
2025-05-22 17:46:17 +08:00
|
|
|
|
|
2024-12-01 13:49:43 +08:00
|
|
|
|
}
|
2025-02-21 15:33:29 +08:00
|
|
|
|
else if (alarm_flag == 1 && count == 0)
|
2024-12-01 13:49:43 +08:00
|
|
|
|
{
|
2025-02-21 15:33:29 +08:00
|
|
|
|
alarm_flag = 0;
|
2024-12-30 17:51:13 +08:00
|
|
|
|
Sensor_device.detection_flag = kSensorNormal;
|
2024-12-01 13:49:43 +08:00
|
|
|
|
Send_Laser_Alarm_Event(kAlarmRcyEvent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 15:33:29 +08:00
|
|
|
|
static void Sensor_HandleFault(uint8_t count, uint8_t threshold)
|
2024-12-01 13:49:43 +08:00
|
|
|
|
{
|
2025-02-21 15:33:29 +08:00
|
|
|
|
if (count >= threshold && fault_flag != 1)
|
2024-12-01 13:49:43 +08:00
|
|
|
|
{
|
2025-02-21 15:33:29 +08:00
|
|
|
|
fault_flag = 1;
|
2024-12-01 13:49:43 +08:00
|
|
|
|
Sensor_device.detection_flag = kSensorFault;
|
|
|
|
|
Send_Laser_Alarm_Event(kFaultEvent);
|
|
|
|
|
}
|
2025-02-21 15:33:29 +08:00
|
|
|
|
else if (fault_flag == 1 && count == 0)
|
2024-12-01 13:49:43 +08:00
|
|
|
|
{
|
2025-02-21 15:33:29 +08:00
|
|
|
|
fault_flag = 0;
|
2024-12-30 17:51:13 +08:00
|
|
|
|
Sensor_device.detection_flag = kSensorNormal;
|
2024-12-01 13:49:43 +08:00
|
|
|
|
Send_Laser_Alarm_Event(kFaultRcyEvent);
|
|
|
|
|
}
|
2025-02-21 15:33:29 +08:00
|
|
|
|
else if (rt_pin_read(LED_Y_PIN) == PIN_HIGH && (count == 0))
|
|
|
|
|
{
|
|
|
|
|
fault_flag = 0;
|
|
|
|
|
Sensor_device.detection_flag = kSensorNormal;
|
|
|
|
|
}
|
2024-12-01 13:49:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static uint8_t Sensor_CheckData(void)
|
|
|
|
|
{
|
|
|
|
|
static rt_uint8_t alarm_count = 0, fault_count = 0;
|
2024-12-30 17:51:13 +08:00
|
|
|
|
static rt_uint8_t index = 0;
|
2024-12-01 13:49:43 +08:00
|
|
|
|
static rt_uint8_t alarm_status_buffer[SENSOR_SAMPLING_TIMS] = {0};
|
2024-12-30 17:51:13 +08:00
|
|
|
|
static rt_uint8_t fault_buf[SENSOR_SAMPLING_TIMS] = {0};
|
|
|
|
|
rt_uint16_t voltage = Get_Gas_VoltageAdcInt1000x();
|
2024-12-01 13:49:43 +08:00
|
|
|
|
|
2024-12-17 15:42:58 +08:00
|
|
|
|
alarm_status_buffer[index] = ((voltage > Sensor_device.alarm_value) && (voltage < MQ_VOLTAGE_HIGH_LIMIT)) ? kSensorAlarm : kSensorNormal;
|
2024-12-30 17:51:13 +08:00
|
|
|
|
fault_buf[index] = ((voltage < MQ_VOLTAGE_LOW_LIMIT) || (voltage > MQ_VOLTAGE_HIGH_LIMIT)) ? kSensorFault : kSensorNormal;
|
2024-12-01 13:49:43 +08:00
|
|
|
|
index++;
|
|
|
|
|
if (index >= SENSOR_SAMPLING_TIMS)
|
|
|
|
|
{
|
|
|
|
|
index = 0;
|
|
|
|
|
}
|
2024-12-30 17:51:13 +08:00
|
|
|
|
|
2024-12-01 13:49:43 +08:00
|
|
|
|
alarm_count = Sensor_Count(alarm_status_buffer, kSensorAlarm, SENSOR_SAMPLING_TIMS);
|
|
|
|
|
fault_count = Sensor_Count(fault_buf, kSensorFault, SENSOR_SAMPLING_TIMS);
|
|
|
|
|
|
2025-02-21 15:33:29 +08:00
|
|
|
|
Sensor_HandleAlarm(alarm_count, SENSOR_SAMPLING_TIMS);
|
|
|
|
|
Sensor_HandleFault(fault_count, SENSOR_SAMPLING_TIMS);
|
2024-12-01 13:49:43 +08:00
|
|
|
|
|
|
|
|
|
return RT_EOK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// TODO: 寿命检测
|
|
|
|
|
uint8_t IS_EndOfLife(void)
|
|
|
|
|
{
|
2025-02-21 15:33:29 +08:00
|
|
|
|
if (ntp_flag) // 是否同步网络时间
|
|
|
|
|
{
|
|
|
|
|
ntp_flag = 0;
|
|
|
|
|
RTC_GetTime();
|
|
|
|
|
|
|
|
|
|
if ((RtcDateTime.year >= 2030) && (work_duration >= 43800)) // 5年后到期
|
|
|
|
|
{
|
|
|
|
|
if (RTC_GetCounter() >= Sensor_device.expiration_seconds)
|
|
|
|
|
{
|
|
|
|
|
Sensor_device.end_of_life = 1;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Sensor_device.end_of_life = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Sensor_device.end_of_life = 0;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2024-12-01 13:49:43 +08:00
|
|
|
|
{
|
2025-02-21 15:33:29 +08:00
|
|
|
|
Sensor_device.end_of_life = 0;
|
|
|
|
|
return 0;
|
2024-12-01 13:49:43 +08:00
|
|
|
|
}
|
|
|
|
|
return Sensor_device.end_of_life;
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 15:33:29 +08:00
|
|
|
|
|
2024-12-01 13:49:43 +08:00
|
|
|
|
// MQ检测线程函数
|
|
|
|
|
static void Sensor_detection_thread_entry(void *param)
|
|
|
|
|
{
|
2025-04-08 14:40:25 +08:00
|
|
|
|
uint8_t calibration_flag = 0;
|
|
|
|
|
while (1)//等待标定
|
|
|
|
|
{
|
|
|
|
|
calibration_flag = Flash_Get_Calibration_State();
|
|
|
|
|
if(calibration_flag == 1)
|
|
|
|
|
break;
|
|
|
|
|
rt_thread_mdelay(1000);
|
2025-05-22 17:46:17 +08:00
|
|
|
|
Get_Gas_VoltageAdcInt1000x();
|
2025-04-08 14:40:25 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-02-21 15:33:29 +08:00
|
|
|
|
rt_uint16_t alarm_value = Flash_Get_SysCfg(kAlarmLValueId); // 获取系统报警阈值;
|
2025-04-08 14:40:25 +08:00
|
|
|
|
if ((alarm_value > 4095)||(alarm_value < 300))
|
2024-12-01 13:49:43 +08:00
|
|
|
|
{
|
2025-02-21 15:33:29 +08:00
|
|
|
|
alarm_value = 1600;
|
2024-12-01 13:49:43 +08:00
|
|
|
|
}
|
2025-02-21 15:33:29 +08:00
|
|
|
|
Sensor_device.alarm_value = alarm_value;
|
|
|
|
|
LOG_D("报警阈值:%d", Sensor_device.alarm_value);
|
2024-12-01 13:49:43 +08:00
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
if (SysControl.status > kPreheatingEvent)
|
|
|
|
|
{
|
|
|
|
|
Sensor_CheckData();
|
|
|
|
|
}
|
2024-12-30 17:51:13 +08:00
|
|
|
|
rt_thread_mdelay(500);
|
2024-12-01 13:49:43 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int BSP_MQ_Init(void)
|
|
|
|
|
{
|
2024-12-30 17:51:13 +08:00
|
|
|
|
rt_thread_init(&Sensor_Thread, // 可以用定时器做,没必要线程
|
|
|
|
|
"sensor_thread",
|
|
|
|
|
Sensor_detection_thread_entry,
|
|
|
|
|
RT_NULL,
|
|
|
|
|
&sensor_thread_stack[0],
|
|
|
|
|
sizeof(sensor_thread_stack),
|
|
|
|
|
GAS_SENSOR_THREAD_PRIORITY, GAS_SENSOR_THREAD_TIMESLICE);
|
2024-12-01 13:49:43 +08:00
|
|
|
|
rt_thread_startup(&Sensor_Thread);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
#ifdef RT_USING_COMPONENTS_INIT
|
2025-01-04 17:19:34 +08:00
|
|
|
|
// INIT_DEVICE_EXPORT(BSP_MQ_Init);
|
2024-12-01 13:49:43 +08:00
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef TEST_ENABLE
|
2025-01-04 17:56:37 +08:00
|
|
|
|
void TEST_MQ_EndOfLife(void)
|
2024-12-01 13:49:43 +08:00
|
|
|
|
{
|
|
|
|
|
Sensor_device.end_of_life = 1;
|
|
|
|
|
}
|
2025-01-04 17:19:34 +08:00
|
|
|
|
// MSH_CMD_EXPORT(TEST_MQ_EndOfLife, TEST_MQ_EndOfLife);
|
2024-12-01 13:49:43 +08:00
|
|
|
|
#endif
|