/*
 * @Author       : stark1898y 1658608470@qq.com
 * @Date         : 2024-12-12 15:55:38
 * @LastEditors  : stark1898y 1658608470@qq.com
 * @LastEditTime : 2025-05-30 16:33:29
 * @FilePath     : \BLE_TYQ_CH584M\BSP\inc\bsp_valve.h
 * @Description  :
 *
 * Copyright (c) 2024 by yzy, All Rights Reserved.
 */
#ifndef __BSP_VALVE_H__
#define __BSP_VALVE_H__

#include "CONFIG.h"

extern tmosTaskID vavle_task_id;

extern volatile uint8_t boost_en_flag;


#define VAVLE_RX_DATA_EVT                   (0x0001 << 0)
#define VAVLE_TX_DATA_EVT                   (0x0001 << 1)

#define VAVLE_CLOSE_START_EVT               (0x0001 << 2) // 关阀，开始充电
#define VAVLE_CLOSE_ACTION_EVT              (0x0001 << 3) // 关阀，动作
#define VAVLE_CLOSE_END_EVT                 (0x0001 << 4) // 关阀结束

#define VAVLE_DECT_SWITCH_START_EVT         (0x0001 << 5) // 检测电磁铁
#define VAVLE_DECT_SWITCH_END_EVT           (0x0001 << 6) // 检测电磁铁

#define VAVLE_LOW_VBAT_ALARM_EVT            (0x0001 << 7) // 低电压提醒

#define VAVLE_LOOP_DECT_EVT                 (0x0001 << 8) // 循环检测

#define VAVLE_DECT_SWITCH_CALIB_EVT         (0x0001 << 9) // 标定电磁铁

#define VAVLE_UPDATE_EVT      (0x0001 << 10) // 上传更行阀门状态



// #define VALVE_LOW_VBAT_ALARM_PERIOD_MS      (1000 * 60 * 10)

#define VALVE_DECT_PERIOD_MS      (1000 * 60 * 3)


#define CHARGE_TIME_MS (2000)

// 2.2V
// #define LOW_VABAT_ALARM_VALUE  23

// #define LOW_VABAT_CLOSE_VALUE  2000

#define LOW_VABAT_CLOSE_MV  2200


#define HIGN_TEMP_CLOSE_C   60

#define FRAME_HEADER 0xAA
#define FRAME_TAIL   0x55

// 0xAA CMD/DATA/ DATA_LEN (DATA) checksum 0x55

typedef enum
{
    kCmdCfg = 0X01,
    kCmdCloseVavle,
    kCmdOpenVavle,
    kCmdData,
} TeFrameCmd;

typedef struct __attribute__((packed))
{
    TeFrameCmd cmd; // CMD/DATA
    uint8_t len;      // DATA_LEN
    // TsValveData data;
    uint8_t data[];
} TsFrameData;

typedef struct __attribute__((packed))
{
    uint8_t len;
    uint8_t buf[64];
} TsRawFrameData;

extern TsRawFrameData RelyData;

// 阀门类型
typedef enum
{
    kTyq = 0X01,
    kZbf,
    kDcf,
} TeVavleType;

typedef enum
{
    kUnknown = 0,
    kClosed,
    kOpened,
    kDisconnect,
} TeValveSwitchStatus;

extern uint16_t valve_switch_on_freq;
extern uint16_t valve_switch_off_freq;
extern uint16_t valve_switch_freq;

extern volatile uint8_t valve_switch_dect_flag;

typedef struct __attribute__((packed))
{
    // uint8_t status; // 1B 阀门工作状态  超欠压、过流
    // uint8_t connct_status; // 1B 阀门连接状态 （电磁阀BLE控制盒） 0=未连接 1=已连接
    uint8_t switch_status; // 1B 阀门开关状态 0=未知 1=关闭=关闭过，未按按键恢复 2=关闭过，按下了恢复键 = 打开
    int8_t temp; // 1B 阀门温度 有符号整数 25 ℃

    uint32_t in_pressure;    // 4B 阀门入口压力 Pa
    uint32_t out_pressure;    // 4B 阀门出口压力 Pa
    uint32_t atm_pressure;    // 4B 阀门大气压力 Pa
    uint8_t bat; // 1B 电池电压 30=3V,18=1.8V
    uint8_t type; // 阀门类型
    uint8_t humi; // 1B 阀门湿度 %RH
    int8_t rssi;
} TsValveData;
extern TsValveData gValveData;
extern uint16_t bat_mv;

void LDect_Deinit(void);
void LDect_Init(void);

uint8_t CheckSum(const uint8_t *data, size_t len);

void VavleDect_Switch(void);
uint16_t VavleDect_GetFreq(void);
void VavleDect_Start(void);

void BSP_VAVLE_Init(void);

TsFrameData* BSP_VAVLE_GetFrameData(uint8_t *data, uint8_t len);

uint8_t GenerateRawFrame(TsRawFrameData *pRawData, uint8_t cmd, const uint8_t *p_src, uint8_t src_len);

void BSP_VALVE_Generate_Data(TsRawFrameData *pRawData, uint8_t switch_status, uint8_t bat, int8_t temp, uint8_t humi);
void BSP_VALVE_Generate_ValveResponse(TsRawFrameData *pRawData, TeFrameCmd cmd, uint8_t status);

void BSP_VALVE_Generate_UploadData(TsRawFrameData *pRawData);

#endif // ! __BSP_VALVE_H__
