#ifndef _SOFT_ENGINE
#define _SOFT_ENGINE
#include "stm32f1xx_hal.h"
#include "soft_can.h"
#include "stdio.h"
#include "common.h"

typedef enum
{
    ENGINE_DEFAULT = 0,
    ENGINE_RUISHEN = 1,
    ENGINE_AOAN = 2,
    ENGINE_VK = 3,
    ENGINE_GEELY = 4,
    ENGINE_BRO = 5,
}FAC_TYPE;


//===============奥安老发动机===============
#pragma pack(1)
typedef struct
{
    uint8_t engin_ver;       //版本号
    uint16_t engin_rev;      //转速
    uint16_t engin_thr;      //油门
    uint16_t engin_vol;      //电压 0.1v
    uint16_t engin_elect;    //电流 0.1A
    uint32_t engin_rtime;    //运行时间 min
    uint16_t engin_tcd;      //剩余保养时间 min
    uint16_t engin_lcd;      //锁机剩余时间 min（租赁方式）
    uint8_t engin_rsta;      //运行状态 0-停止，1-等待，2-运行，3-锁机
    uint16_t engin_warn;     //报警信息
    uint8_t engin_roil;      //剩余油量 %
    uint16_t engin_cyl1temp; //气缸1温度
    uint16_t engin_cyl2temp; //气缸2温度
    uint8_t engin_pcbtemp;   //控制盒PCB温度
} AOAN_TYPE;
#pragma pack()
extern AOAN_TYPE aoan_data;

short decode_aoan_engine(uint8_t *pdata, uint16_t length);

//===============VK-V03协议发动机===============
#pragma pack(1)
typedef struct
{
    uint8_t engin_ver;       //版本号
    uint8_t engin_brand[8];  //品牌
    uint32_t engin_sn;       //序列号
    uint16_t engin_rev;      //转速
    uint16_t engin_thr;      //油门
    uint16_t engin_vol;      //电压 0.1v
    uint16_t engin_elect;    //电流 0.1A
    uint32_t engin_rtime;    //运行时间 min
    uint16_t engin_tcd;      //剩余保养时间 min
    uint16_t engin_lcd;      //锁机剩余时间 min（租赁方式）
    uint8_t engin_rsta;      //运行状态 0-停止，1-等待，2-运行，3-锁机
    uint16_t engin_warn;     //报警信息
    uint8_t engin_roil;      //剩余油量 %
    uint16_t engin_cyl1temp; //气缸1温度
    uint16_t engin_cyl2temp; //气缸2温度
    uint16_t engin_pcbtemp;  //控制盒PCB温度
} VKV3_TYPE;
#pragma pack()
extern VKV3_TYPE vkv3_data;

extern Connect_check vkv3_engine_link;
extern Connect_check aoan_engine_Link;
//extern Connect_check vkv3_engine2_link;


short decode_vkv3_engine(uint8_t *pdata, uint16_t length);

//===============GEELY发动机============================
#pragma pack(1)
typedef struct
{   
    uint8_t engine_ver; //版本号 
    uint8_t engine_brand[3];        //品牌名
    uint8_t engine_sn[4];           //序列号 年月日

    uint16_t engine_rev;            //发动机转速 
    uint16_t engine_thrposition;    //节气门位置  0.1%
    uint8_t  engine_fuelposition;   //油位   0.5%
    uint8_t engine_cyltemp;         //缸体温度        偏移-40
    uint8_t engine_cooltemp;        //冷却液温度 C    偏移-40
    char    engine_status;          //发动机状态 
    
    uint16_t engine_vol;            //输出电压 0.2v
    uint16_t engine_elect;          //输出电流 0.2A
    uint16_t engine_warn;           //报警信息
    uint8_t  engine_standby;        //备用
    uint8_t  engine_fault;          //错误信息

    uint8_t engine_control;         //控制响应
    uint8_t engine_set;             //设置命令
    uint16_t engine_setpar;         //设置响应
    uint16_t engine_ctrbak;
    uint8_t engine_dronestutas:4;   //飞行器状态 4位
    uint8_t engine_counts:4;        //活动计数 4位
    uint8_t  engine_checksum;       //校验和

    uint32_t engine_runtime;        //总运行时间    min
    uint16_t engine_tcd;            //剩余保养时间 min
    uint16_t engine_tlock;          //剩余锁机时间

} GEELY;
#pragma pack()
extern GEELY geely_data;


//===============GEELY发动机第二版============================
#pragma pack(1)

typedef struct
{
    //0X1E4
    uint8_t engine_restime:6;     //响应时间
    uint8_t engine_datanormal:1;  //数据是否有效
    uint8_t engine_reserve0:1;
    uint32_t engine_reserve1:20;
    uint8_t engine_conuts:4;      //活动技术
    uint16_t check_crc;
}geely_bit1E4;

extern uint8_t geely_count;
extern uint8_t start_engine;
typedef struct
{
    //0X1EA
    uint16_t engine_TTP1:12;        //目标节气门1位置
    uint16_t engine_ATP1:12;        //实际节气门1位置
    uint16_t engine_reserve4:12;
    uint16_t engine_reserve5:12;
    uint16_t engine_baro;           //大气压力
    uint16_t engine_IAT;            //进气温度
}geely_bit1EA;

typedef struct
{
    bool version2_flag;

    //0X1E2
    uint8_t engine_ver; //版本号 
    uint8_t engine_brand[3];        //品牌名
    uint8_t engine_sn[4];           //序列号 年月日

    //0X1E3
    uint32_t engine_runtime;        //总运行时间    min
    uint16_t engine_tcd;            //剩余保养时间 min
    uint16_t engine_tlock;          //剩余锁机时间

    //0X1E4
    geely_bit1E4 Bit1E4;

    //0X1E8
    uint32_t engine_time;        //时间戳
    uint8_t  engine_Mcontrol:1;  //电机控制
    uint8_t  engine_Mresponse:1;  //电机响应
    uint8_t  engine_reserve2:2;
    uint8_t  engine_controlcmd:2; //来自系统的外部控制命令
    uint8_t  engine_state:2;      //发动机工作状态
    uint8_t  engine_cooltemp;        //冷却液1温度 C    偏移-40
    uint8_t  engine_cooltemp1;       //冷却液2温度 C    偏移-40
    uint8_t  engine_GWT;          //线圈温度 C    偏移-40   

    //0X1E9
    uint16_t engine_rev;            //发动机转速
    uint16_t engine_vol;            //输出电压 0.01v
    short engine_elect;          //输出电流 0.05A -400
    uint16_t engine_bmselect;       //电池电流 0.05A -400

    //0X1EA
    geely_bit1EA Bit1EA;

    //0x1EB
    uint16_t FuelConsumption;       //瞬时油耗
    uint8_t  GPS;                   //汽油液位
    uint8_t  B12V;                  //板上 12V 电压
    uint8_t  B5V1;                  //板上 5.1V 电压
    uint8_t  B7V4;                  //板上舵机驱动电压
    uint8_t  BBAT;                  //MCU VBAT 电压
    uint8_t  VREF;                  //MCU VREF 电压

    //0X1EC
    uint16_t EmgST0;                 //报警信息  新协议
    uint8_t ErrST0;                 //一般故障状态0
    uint8_t ErrST1;                 //一般故障状态1
    uint8_t ErrST2;                 //一般故障状态2
    uint8_t ErrST3;                 //一般故障状态3
    uint8_t engine_reserve6;
    uint8_t engine_reserve7;

    //0X1ED
    uint8_t AlmST0;                 //异常报警0
    uint8_t AlmST1;                 //异常报警1
    uint8_t AlmST2;                 //异常报警2
    uint8_t AlmST3;                 //异常报警3 
    uint8_t SysST0;                 //系统状态0
    uint8_t SysST1;                 //系统状态1
    uint8_t engine_reserve8;      
    uint8_t engine_reserve9;
    
    //0X1EE
    uint16_t Gear1Position;         //舵机1输出位置
    uint8_t engine_reserve10;
    uint8_t engine_reserve11;
    uint8_t OCT;                    //MCU 芯片温度
    uint8_t engine_reserve12;
    uint8_t engine_reserve13;

} GEELY2;
#pragma pack()
extern GEELY2 geely_data2;
typedef struct 
{
    uint8_t msg1c3_last:4;
    uint32_t send_1a0;
    uint32_t recv_1c3;
    uint32_t msg1c3_sumerr;
    uint32_t msg1c3_indexerr;
    
}GEELY_MONI;
extern GEELY_MONI geely_moni; 
extern  Connect_check geely_engin_link;

#pragma pack(1)
typedef struct 
{
    uint8_t ExtenderControl;        //增程器启停控制
    uint8_t ExtenderSetting;        //增程器设置命令
    uint16_t SettingParameter;      //设置命令参数
    uint16_t ExtenderBak;           //备用
    uint8_t DroneStatus:4;          //飞行器状态
    uint8_t MessageCount:4;         //活动计数
    uint8_t CheckSum;               //校验和

}STO_GEELY;
#pragma pack()
extern STO_GEELY sendto_geely;


void EngGeelyCanRecvHookFunction(uint32_t cellCanID, uint8_t data[], uint8_t len);


//================飞控收集后的发动机信息结构体=============
extern comp_status engine_link_status;

#pragma pack(1)
struct ENGINE_DATA
{
    uint8_t engine_type;       //类型
    uint16_t engine_rev;       //转速
    uint16_t engine_thr;       //油门
    uint16_t engine_vol;       //电压 0.1v
    short engine_elect;     //电流 0.1A
    uint32_t engine_rtime;     //运行时间 min
    uint16_t engine_tcd;       //剩余保养时间 min
    uint16_t engine_lcd;       //锁机剩余时间 min（租赁方式）
    uint8_t engine_rsta;       //运行状态 0-停止，1-等待，2-运行，3-锁机
    uint16_t engine_warn;      //报警信息
    uint8_t engine_roil;       //剩余油量 %
    uint16_t engine_cyl1temp;  //气缸1温度
    uint16_t engine_cyl2temp;  //气缸2温度
    uint16_t engine_pcbtemp;    //控制盒PCB温度
    uint8_t engine_brand[8];   //品牌
    uint32_t engine_sn;        //序列号
    uint8_t engine_lockstatus; //发送机解锁状态
    uint8_t engine_fault;      //发动机错误状态
};
#pragma pack()
extern struct ENGINE_DATA engine_data;

extern short geely_set_counts;
extern bool geely_set_tcd,geely_set_tcd_re, 
     geely_set_tlock, geely_set_tlock_re,geely_set_protect;

void send_msg_to_geely(void);
void update_engine_data(void);
void engine_function(void);
void pmu_to_con_engine_data(void);
void send_msg_to_vk3(void);
#endif
