#ifndef PROTOCOL_H
#define PROTOCOL_H

#include <cstdint>

#define FRANE_HEAD      0x5AA5 //枕头
#define WRITE_CMD       0x82   //写指令
#define READ_CMD        0x83   //读指令

#define PHASE_START_ADDR  0X0050
#define PHASE_BYTE_SIZE 12

#pragma pack(push)
#pragma pack(1)

enum DeviceRunStatus{
    Idle,
    Heat,
    ConstantTemp,
    Cooling,
    HeatingReteSelfTunning
};

enum DeviceStartMode{
    Stop,
    Start,
    StartAutoTune,  // 启动升温速率自整定
    StopAutoTune    // 停止升温速率自整定
};

typedef struct com_protocol
{
    uint16_t head;
    uint8_t len;
    uint8_t cmd;
    uint16_t addr;
    uint8_t data_buf[256];
}SerialPortProtocol;

typedef enum gas_type {
    NC,
    N2,
    O2
}GasType;

typedef struct control_data
{
    uint8_t onoff;
    float cutoff_temp;
    float temp_flow;
    uint16_t constant_temp_time_min;
    enum gas_type gas; // uint8_t
}Phase;

struct pid_data
{
    float kp;
    float ki;
    float kd;
};

typedef struct com_data
{
    uint8_t run_type;            //0,运行状态
    uint8_t current_phase;       //1,当前阶段
    float add_run_time;             //2,累计运行时间
    float add_constan_temp_time;    //6,当前阶段恒温时间
    double sample_temp;              //A,样品温度
    double dsc;                     //12,热流
    double temp_flow;               //1a,升温速率
    float  cold_temp;               //22,冷端温度
    uint8_t hardware_err;        //26,硬件错误
    uint8_t current_gas;         //27,当前气氛
    float auto_pid_temp_flow;       //28,自整定升温速率
    uint8_t run_mode;            //2c,启动模式
    uint8_t reserve_buf[35];     //2d-4f,保留
    struct control_data phase_data[6];//50-97,阶段控制数据
    uint8_t meas_type;             //98，测试类型
    uint8_t init_gas;              //99, 初始气氛
    struct pid_data temp_flow_pid_low;//9a-a5,升温速率 低pid
    struct pid_data temp_flow_pid_high;//a6-B1,升温速率 高pid
    float cold_temp_cali_standard;      //B2,冷端标准温度
    float cold_temp_cali_meas;           //B6,冷端测量温度
    uint8_t cold_cali_flag;          //B7,冷端温度计算
    float cold_temp_slope;             //B8,冷端温度倍率
    float sample_temp_calilow_standard;//BC,样品标准温度低倍率
    float sample_temp_calilow_meas;    //C0,样品测量温度低倍率
    uint8_t sample_temp_cali_low_flag;//C4,样品温度计算低倍率
    float sample_temp_slope_low;       //C5,样品温度低倍率低倍率
    float sample_temp_calimid_standard;//C9,样品标准温度中倍率
    float sample_temp_calimid_meas;    //CD,样品测量温度中倍率
    uint8_t sample_temp_cali_mid_flag;//D1,样品温度计算中倍率
    float sample_temp_slope_middle;     //D2,样品温度中倍率
    float sample_temp_calihigh_standard;//D6,样品标准温度高倍率
    float sample_temp_calihigh_meas;    //样品测量温度高倍率
    uint8_t sample_temp_cali_high_flag; //样品温度计算高倍率
    float sample_temp_slope_high;       //样品温度高倍率
    float enthalpy_equation_a;          //热焓方程式系数a
    float enthalpy_equation_b;          //热焓方程式系数b
    float enthalpy_equation_c;          //热焓方程式系数c
} CommonData;

#pragma pack(pop)

#if 0
struct com_device
{
    struct temp_control* temp;
};
struct com_data* com_data_get(void);
void com_protocol_init(struct com_device* dev);
void com_protocol_data_init(void);
#endif

#endif // PROTOCOL_H
