/*
 * state_mini.h
 *
 *  Created on: 2026年6月29日
 *      Author: dell
 */

#ifndef __STATE_MINI_H
#define __STATE_MINI_H

#define MAX_DATA_BUF_SIZE  36
#define SystemChecking    0
#define SystemOK          1
#define SystemError       2

#define SKIP_CHECK_CARDINSERT (1 << 0)  //开始检测插卡正确
#define SKIP_CHECK_CARDREMOVE (1 << 1) //运行过程中检测插卡拔卡
#define SKIP_HEATONCHECK_OK   (1 << 2) //跳过加热，不管加热状态
#define SKIP_SAMPLIGHT_ACC   (1 << 3)  //上报加速
#define SKIP_SCANQRCODE    (1 << 4)  //上报加速
#define SKIP_FARSTRUN    (1 << 5)  //上报加速
#define SKIP_SLAVEDATASCOP    (1 << 6)  //从模块datascop上报


#define HEAT_OFF 0x00    //关加热
#define HEAT_ON 0x01     //开加热
#define HEAT_STOP 0x02     //停止加热
#define FAN_ON 0x02     //启用风扇


#define sampleTotalTimes  181
typedef enum StatusWord
{
    // 所有板卡准备自检，包含通道预加热
    SW_Read_To_Board_Selftest = -1,
    // 准备和PC握手
    SW_Ready_To_Shake_On = 0,
    // 故障
    SW_Fault = 1,
    // 获取温度，两通道通道加热
    SW_Switch_On_Pre_Heat = 8,
    // 快速停止
    SW_Quick_stop = 30,
    // 等温度稳定后
    SW_Target_reached = 10,
    // 等待扫码
    SW_Scan_Qrcode = 12,
    // 插卡，通道检测
    SW_Chip_Id = 14,
    // 条码信息匹配，下发
    SW_Qrcode_publish = 16,
    // 采样，30min定时
    SW_Sample_timeout = 18,
    // 计算，阈值范围，所有孔的结果（阴阳性、无效）
    // 结果上传&数据上传
    SW_result_report = 25,
    SW_move_card = 26,      //拔卡，进入下一次流程循环
    SW_SystemError
} StatusWord_t;
// pc Controlword commands
typedef enum Controlword
{
    Board_Selftest_cmd,
    Hand_Shake_cmd,
    Scan_ready_Qrcode_cmd,
    Scan_Qrcode_cmd,
    Pre_Heat_cmd,
    Target_reached_cmd,
    Chip_Id_cmd,
    Qrcode_publish_cmd,
    Sample_timeout_cmd,
    result_report_cmd,
    result_move_card
}  Controlword_t;

//故障码
enum ERROR_CODE
{
    ERROR_HEAT_INSERT_CARD = 101,      //温度稳定前插卡
    ERROR_NO_QRCODE = 102,             //未扫码就插卡
    ERROR_SAMP_MOVE_CARD = 103,        //检测中拔卡
    ERROR_SENSOR_SELF_CHECK= 104,      //光电自检失败
    ERROR_HEAT_SELF_CHECK= 105,        //加热自检失败
    ERROR_MCU_LINK_TIMEOUT = 106,             //mcut通讯超时
    ERROR_PRE_HEAT_TIMEOUT = 107,             //预热超时
    ERROR_HEAT_TIMEOUT = 108,                 //加热超时
    ERROR_MOTOR_FIALED = 109,                 //电机
    ERROR_TUPE_MOVE = 110,                 //检测过程中拔试管
    ERROR_TUPE_EXIST = 111,                 //检测过程中拔试管
    ERROR_HEATFUC_EER = 112,               //加热模块无反应
    ERROR_TEMPSENSOR_EER = 113,            //温度传感器异常
};

typedef struct inner_action_flags_t
{
    uint8_t selftest_allowed : 1;
    uint8_t scan_qrcode_allowed : 1;
    uint8_t pre_heat_enabled : 1;
    uint8_t target_reached : 1;
    uint8_t check_chipid_enabled : 1;
    uint8_t qrcode_info_applied : 1;
}  inner_action_flags_t;


typedef struct
{
	uint8_t    Devicenum;               //设备编号
	char   	   card_num[10];            //卡盒编号

	uint8_t    hole1_item;              //孔1检测项目
	uint8_t    hole1_item_result;       //孔1检测项目结果
	uint8_t    hole1_result;            //孔1检测结果
	uint8_t      hole1_Tt[2];                //孔1 Tt

	uint8_t    hole2_item;              //孔2检测项目
	uint8_t    hole2_item_result;       //孔2检测项目结果
	uint8_t    hole2_result;            //孔2检测结果
	uint8_t     hole2_Tt[2] ;                //孔2 Tt

	uint8_t    hole3_item;              //孔3检测项目
	uint8_t    hole3_item_result;       //孔3检测项目结果
	uint8_t    hole3_result;            //孔3检测结果
	uint8_t      hole3_Tt[2];                //孔3 Tt

	uint8_t    hole4_item;              //孔4检测项目
	uint8_t    hole4_item_result;       //孔4检测项目结果
	uint8_t    hole4_result;            //孔4检测结果
	uint8_t      hole4_Tt[2];                //孔4 Tt

	uint8_t    hole5_item;              //孔5检测项目
	uint8_t    hole5_item_result;       //孔5检测项目结果
	uint8_t    hole5_result;            //孔5检测结果
	uint8_t      hole5_Tt[2];                //孔5 Tt

	uint8_t    hole6_item;              //孔6检测项目
	uint8_t    hole6_item_result;       //孔6检测项目结果
	uint8_t    hole6_result;            //孔6检测结果
	uint8_t      hole6_Tt[2];                //孔6 Tt

	uint8_t    hole7_item;              //孔7检测项目
	uint8_t    hole7_item_result;       //孔7检测项目结果
	uint8_t    hole7_result;            //孔7检测结果
	uint8_t      hole7_Tt[2];                //孔7 Tt

	uint8_t    hole8_item;              //孔8检测项目
	uint8_t    hole8_item_result;       //孔8检测项目结果
	uint8_t    hole8_result;            //孔8检测结果
	uint8_t      hole8_Tt[2];                //孔8 Tt

	uint8_t    hole9_item;              //孔9检测项目
	uint8_t    hole9_item_result;       //孔9检测项目结果
	uint8_t    hole9_result;            //孔9检测结果
	uint8_t      hole9_Tt[2];                //孔9 Tt

	uint8_t    hole10_item;             //孔10检测项目
	uint8_t    hole10_item_result;      //孔10检测项目结果
	uint8_t    hole10_result;           //孔10检测结果
	uint8_t      hole10_Tt[2];               //孔10 Tt

}test_result_struct;






typedef struct str_error_info
{
    uint8_t id; //板卡id
    uint8_t error_code; //错误码 
    char   	SN[10];
    uint8_t error_report; //错误码 
} str_error_info_t;


typedef struct str_hole_info
{
    uint8_t    item;
    uint8_t TtMax[2];
    uint8_t TtMin[2];
} str_hole_info_t;
//str_hole_info_t hole1,hole2,hole3,hole4,hole5,hole6,hole7,hole8,hole9,hole10;
typedef struct str_card_param_info
{
    char   pid[10];
    uint8_t temp[2];
    struct str_hole_info hole_info[10];
} str_card_param_info_t;

typedef struct
{
	uint8_t   id;            		//设备编号
	uint8_t   heat_state;            		//串口控制加热状态
	uint8_t   temp_stable_sta;            	//串口控制加热状态
	float     heater_temp;                  //加热体温度
	uint8_t   fan_state;   					//风扇状态
	float heater_independent_temp;

}heat_ctrl_struct;
typedef struct  //由于flash_write是按4字节写入，所以本结构体内部都按4字节定义和对齐
{
	float    presetTempVal;   //拉升温度
	uint32_t presetTempTime;   //拉升保持时间
	float    setTempVal;      //设定温度
	float    holdTemp;       //稳定温度
	float	tempOffset;		//温度补偿
	uint32_t preHeatTime;	//上电预热时间,min
	float readyTemp;		//就绪温度
	uint32_t log_level;		//日志等级
	uint32_t skip_CheckOrRun;		//插入光变化检测
	float adjtemp;		//调整温度

}CFG_PARAM_STRUCT;


typedef struct ProcessStateMachine
{
    StatusWord_t state;
    inner_action_flags_t flags; 
    float DataBufRDR[MAX_DATA_BUF_SIZE][10];
    uint32_t DataSendHeader;    //串口超时
    uint32_t DataSendLast;    //串口超时   
    unsigned int sendconter;
    unsigned int updateresult;
    unsigned int cursendok;
    unsigned int sendTime ;
    unsigned int  sampleStart;
    unsigned int  sampleTimes;
    unsigned int  heat_timeout; //加热超时
    unsigned int  scan_qrcode_done;
    unsigned int  statusword; 
    unsigned int  controlword;     
	str_card_param_info_t card_param_info; 
} ProcessStateMachine_t;


void updateData_next(unsigned int recdex,unsigned char * part);
void clean_reult_next(unsigned char * part);
void resetSampleData(void);
uint8_t error_set(uint8_t error);
void error_clear(void);
void usr_state_machine(ProcessStateMachine_t *sts);
#endif
