/***
 * @Author: mbw
 * @Date: 2024-10-09 08:42:14
 * @LastEditors: mbw && 1600520629@qq.com
 * @LastEditTime: 2024-11-07 14:51:57
 * @FilePath: \JT-DT-YD4N02A_RTT_MRS-4G\bsp\inc\bsp_h308.h
 * @Description:
 * @
 * @Copyright (c) 2024 by ${git_name_email}, All Rights Reserved.
 */
#ifndef __BSP_H308_H__
#define __BSP_H308_H__

#include "board.h"
#include "rtdef.h"
#include "drv_gpio.h"

#define TSET_BSP_H308

#define H308_SAMPLING_TIMS (8U)
/*浓度异常采样周期*/
#define H308_SAMPLING_ABNORMAL (60U)
#define H308_PWR_PIN           GET_PIN(B, 9)
#define H308_RST_PIN           GET_PIN(C, 12)

#define H308_PWR_ON            rt_pin_write(H308_PWR_PIN, PIN_HIGH)
#define H308_PWR_OFF           rt_pin_write(H308_PWR_PIN, PIN_LOW)

#define H308_RST_ON            rt_pin_write(H308_RST_PIN, PIN_LOW)
#define H308_RST_OFF           rt_pin_write(H308_RST_PIN, PIN_HIGH)

#define H308_THREAD_STACK_SIZE (2048)
#define H308_THREAD_PRIORITY   (8)
#define H308_THREAD_TIMESLICE  (5)

typedef enum
{
    // kH308Normal = 0,        // 传感器工作正常
    kH308TempCtrlErr,    // 温控异常
    kH308EnvTempErr,     // 环境温度异常
    kH308LaserWeak,      // 激光信号弱
    kH308LaserStrong,    // 激光信号强
    kH308LaserErr,       // 激光信号异常
    kH308LaserWeakErr,   // 激光信号弱+激光信号异常
    kH308LaserStrongErr, // 激光信号强+激光信号异常
} TeH308FaultCode;

typedef enum
{
    kH308Normal = 0,
    kH308Alarm  = 1,
    kH308Fault,
    kH308EndOfLife,
    kH308Abnormal,
} TeH308Status;

typedef struct
{
    char vol[16];             // 浓度
    char temp[16];            // 温度
    char laser_temp[16];      // 激光器温度
    char signal_strength[16]; // 信号强度
    char gain[16];            // 放大系数
    char fault_code[4];       // 传感器状态
    char checksum[4];         // 校验
} TsH308StrData;

typedef struct __attribute__((packed))
{
    float lel;             // 浓度
    float temp;            // 温度
    float laser_temp;      // 激光器温度
    float signal_strength; // 信号强度
    float gain;            // 放大系数
    rt_uint8_t fault_code; // 故障码
    rt_uint8_t checksum;   // 校验
} TsH308Data;

typedef struct
{
    TsH308Data Data;
    TeH308FaultCode fault_code;  // 故障码
    uint32_t expiration_seconds; // 传感器到期时间，s
    uint8_t alarm_value;         // 报警器阈值 LEL

    TeH308Status status_buffer[H308_SAMPLING_TIMS]; //  采样缓存数组

    uint8_t detection_flag; // 实时检测的标志

    uint8_t end_of_life; // 寿命到期
    uint8_t last_life;   // 上次状态
    uint8_t to_be_calib; // 待标定
} TsH308;

extern TsH308 H308;
int BSP_H308_Init(void);
void H308_ShowData(TsH308Data data);
uint8_t IS_H308_EndOfLife(void);
#endif // !__BSP_H308_H__
