118 lines
2.4 KiB
C
118 lines
2.4 KiB
C
#include "main.h"
|
|
#include "sensor.h"
|
|
#include "string.h"
|
|
#include "usart_user.h"
|
|
|
|
volatile uint8_t THRESHOLD_CTRL = 0;
|
|
extern volatile uint8_t co_threshold;
|
|
sensor_parse_ctrl_t co;
|
|
|
|
//³õʼ»¯½á¹¹Ìå
|
|
void InitSensorCtrlData(sensor_parse_ctrl_t *sensor_parse_ctrl, uint8_t data_length, uint8_t type)
|
|
{
|
|
sensor_parse_ctrl->type = type;
|
|
|
|
RingBuff_Init(&sensor_parse_ctrl->recvRingBuff);
|
|
|
|
sensor_parse_ctrl->data_length = data_length;
|
|
|
|
sensor_parse_ctrl->function = NULL;
|
|
}
|
|
|
|
void SensorModeInitSet(void)
|
|
{
|
|
|
|
}
|
|
|
|
void GetSensorData(void)
|
|
{
|
|
|
|
}
|
|
|
|
uint8_t ReadSensorData(sensor_parse_ctrl_t *sensor_parse_ctrl)
|
|
{
|
|
uint32_t data_length = 0;
|
|
uint8_t recdata[1];
|
|
|
|
if(Read_Length_RingBuff(&sensor_parse_ctrl->recvRingBuff) < sensor_parse_ctrl->data_length)
|
|
{
|
|
while(Read_RingBuff(&sensor_parse_ctrl->recvRingBuff,recdata)){};
|
|
}
|
|
|
|
else
|
|
{
|
|
while(Read_RingBuff(&sensor_parse_ctrl->recvRingBuff,recdata))
|
|
{
|
|
sensor_parse_ctrl->sensorData[data_length] = recdata[0];
|
|
data_length++;
|
|
if(data_length >= sensor_parse_ctrl->data_length)
|
|
{
|
|
while(Read_RingBuff(&sensor_parse_ctrl->recvRingBuff,recdata)){};
|
|
}
|
|
}
|
|
}
|
|
return data_length;
|
|
|
|
}
|
|
|
|
static void general_data_check(sensor_parse_ctrl_t *sensor_parse_ctrl)
|
|
{
|
|
if((sensor_parse_ctrl->sensorData[0] != 0xFF) || (sensor_parse_ctrl->sensorData[1] != 0x04))
|
|
{
|
|
memset(sensor_parse_ctrl->sensorData, 0, sizeof(sensor_parse_ctrl->sensorData));
|
|
return;
|
|
}
|
|
|
|
uint8_t temp = 0x00;
|
|
for(int i = 1; i<sensor_parse_ctrl->data_length-1; i++)
|
|
{
|
|
temp += sensor_parse_ctrl->sensorData[i];
|
|
}
|
|
temp = (~temp)+1;
|
|
if((temp&0xFF) == sensor_parse_ctrl->sensorData[sensor_parse_ctrl->data_length-1])
|
|
{
|
|
uint32_t CO = (sensor_parse_ctrl->sensorData[4]*256+sensor_parse_ctrl->sensorData[5])*0.1;
|
|
#ifdef DEBUG
|
|
printf("CO is %d\r\n", CO);
|
|
#endif
|
|
if(CO >= co_threshold)
|
|
{
|
|
THRESHOLD_CTRL |= BIT_1;
|
|
}
|
|
else
|
|
{
|
|
THRESHOLD_CTRL &= ~BIT_1;
|
|
}
|
|
}
|
|
|
|
memset(sensor_parse_ctrl->sensorData, 0, sizeof(sensor_parse_ctrl->sensorData));
|
|
}
|
|
|
|
void AnalysisSensorData(sensor_parse_ctrl_t *sensor_parse_ctrl)
|
|
{
|
|
general_data_check(sensor_parse_ctrl);
|
|
}
|
|
|
|
|
|
void Mq2Data(void)
|
|
{
|
|
static uint8_t last_status = LL_COMP_OUTPUT_LEVEL_LOW;
|
|
|
|
uint8_t status = LL_COMP_ReadOutputLevel(COMP1);
|
|
|
|
if(last_status == status)
|
|
{
|
|
switch(status)
|
|
{
|
|
case LL_COMP_OUTPUT_LEVEL_LOW:
|
|
THRESHOLD_CTRL &= ~BIT_0;
|
|
break;
|
|
case LL_COMP_OUTPUT_LEVEL_HIGH:
|
|
THRESHOLD_CTRL |= BIT_0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
last_status = status;
|
|
}
|