#ifndef __SOFT_CAN_H
#define __SOFT_CAN_H
#include "stdbool.h"
#include "common.h"

//雷达/流量计/称重/前板CAN通讯 CAN1
//水泵/喷头/电池/播撒器CAN通讯 CAN2

#define CANID1   1
#define CANID2   2

//莫之比避障前后
#define CAN_OBSTACLE_1 (0x000EFF11)
#define CAN_OBSTACLE_2 (0x000EFF12)
//莫之比仿地
#define CAN_UAVH30_MSG (0x000EFF0B)
//莫之比MASK
#define CAN_MICOB_FILTER_ID    (0x000EFF01)
#define CAN_MICOB_MASK_ID      ((uint32_t)0x1F0FFFE0)
//莫之比升级系列扩展ID
#define CAN_UAVRH_FILTER_ID    (0x000007E0)
#define CAN_UAVRH_MASK_ID      ((uint32_t)0x1FFFFFE0)

//恩曌仿地
#define CAN_MIMO_T_ID (0x300)
//恩曌前避障(单点)
#define CAN_MIMO_FOBS_SIG (0x301)
//恩曌后避障（单点）
#define CAN_MIMO_BOBS_SIG (0x302)
//恩曌360雷达新协议
#define CAN_360MIMO_1ID (0x501)
#define CAN_360MIMO_2ID (0x502)

//Z70转接板

#define CAN_Z70_MSG (0x7010)

//VK协议
#define FRAME_TYPE_MASK 0x1C000000 // 1 1100 0000 0000 0000 0000 0000 0000(29位扩展ID)
#define CAN_FMTYPE_DATA 0x00   // 数据帧
#define CAN_FMTYPE_REQ 0x01    // 请求帧

// 第18~12位，目标nodeID，, ID最大0x7F
// 第11~5位， 源nodeID, ID最大0x7F
#define DES_ID_MASK 0x0007F000         // 0 0000 0000 0111 1111 0000 0000 0000(29位扩展ID)
#define SRC_ID_MASK 0x00000FE0         // 0 0000 0000 0000 0000 1111 1110 0000(29位扩展ID)
#define CAN_NODEID_MC  0x01            // 主控nodeid
#define CAN_NODEID_LED 0x02            // led灯板nodeid
#define CAN_NODEID_4G  0x03            // 4G模块nodeid
#define CAN_NODEID_LIQUED 0x15         //液位计nodeid
#define CAN_NODEID_FLOW 0x16           //流量计nodeid
#define CAN_NODEID_ENGINE 0x17         //发动机nodeid
#define CAN_NODEID_LASER  0x18         //激光雷达nodeid
#define CAN_NODEID_WEIGHT 0x19         //称重传感器、播撒器 nodeid
#define CAN_NODEID_RADAR_UPDATE 0x20   //设备升级  
#define CAN_NODEID_DISTRIBUTOR  0x21   //分电板
#define CAN_NODEID_PUMP_NOZZLE  0x22   //水泵离心喷头


// 第25~19位，消息ID
#define MSG_ID_MASK 0x03F80000 // 0 0011 1111 1000 0000 0000 0000 0000(29位扩展ID)
// 飞控发出给LED的消息序号
#define CAN_MSGID_MCTOLED_BLINK 0x01 // 灯闪烁
// 飞控发出给LED的配置消息序号
#define CAN_MSGID_MCTOLED_CONF 0x02 // 底板配置
// 飞控发出给LED的AD状态消息序号
#define CAN_MSGID_MCTOLED_ADOC 0x03 // AD开关

// LED发出给飞控的消息序号
#define CAN_MSGID_LEDTOMC_VOLT 0x01 // 电压监测
// LED发出给飞控的版本序号
#define CAN_MSGID_LEDTOMC_VER 0x02 // 版本信息
// 液位计发出给飞控的消息
#define CAN_MSGID_LEDTOMC_LIQUID 0x03 // 液位计信息(目前没有到)
//流量计发出给飞控的信息
#define CAN_MSGID_FLOWTOMC_INFO  0x04 //流量计信息 0x2012c0 
#define CAN_MSGID_FLOWTOMC_ACK  0x05 //流量计ACK  
//发动机发出给飞控的信息
#define CAN_MSGID_ENGINE_INFO 0x06 //CAN发动机信息(目前没有到)

#define CAN_MSGID_RADAR_START 0X07 //雷达升级开始
#define CAN_MSGID_RADAR_ING 0X08 //雷达升级中
#define CAN_MSGID_RADAR_END 0X09 //雷达升级结束  1000 0001 0100 0010 0000
//水泵信息
#define CAN_MSGID_PUMP  0x10
//离心喷头信息
#define CAN_MSGID_NOZZLE 0x11

//防地雷达发出给飞控的信息0x901300
#define CAN_MSGID_RADAR_INFO 0x12
//飞控发出给防地雷达的信息0x981300
#define CAN_MSGID_RADAR_VERSION_INFO 0x13

//前避障雷达发出给飞控的信息
#define CAN_MSGID_FRADAR_INFO 0x14
//飞控发出给前避障雷达的信息
#define CAN_MSGID_FRADAR_VERSION_INFO 0x15

//后避障雷达发出给飞控的信息
#define CAN_MSGID_BRADAR_INFO 0x16
//飞控发出给后避障雷达的信息
#define CAN_MSGID_BRADAR_VERSION_INFO 0x17

// CANfilterID,（扩展帧 29位）
// filterID中，按照目标和源ID进行过滤，目标ID为自己的固定不变，源ID需要把所有的需要接收的node合并一起组成
#define CAN_MC_FILTER_ID   ((unsigned int)(CAN_FMTYPE_DATA<<26 | CAN_MSGID_LEDTOMC_VOLT<<19 | \
                                         CAN_NODEID_MC<<12 | CAN_NODEID_LED<<5))

// CANmaskID, (扩展帧 29位) 0
// maskID中为1的位，需要filterID和消息帧中对应位的值完全一样，才能通过过滤
// 消息类型位全置1，目标nodeID的位全置1, 消息ID以及源节点ID处寻找相同位的做过滤
#define CAN_MC_MASK_ID   ((unsigned int)(FRAME_TYPE_MASK | \
(MSG_ID_MASK & (((0x7f & (~(0x00 | CAN_MSGID_LEDTOMC_VOLT | CAN_MSGID_LEDTOMC_VER | CAN_MSGID_ENGINE_INFO \
 | CAN_MSGID_FLOWTOMC_INFO | CAN_MSGID_FLOWTOMC_ACK | CAN_MSGID_RADAR_START| CAN_MSGID_RADAR_INFO |CAN_MSGID_FRADAR_INFO | CAN_MSGID_BRADAR_INFO |CAN_MSGID_RADAR_ING | CAN_MSGID_RADAR_END)))|\
 (0x7f & CAN_MSGID_LEDTOMC_VOLT & CAN_MSGID_LEDTOMC_VER & CAN_MSGID_FLOWTOMC_INFO & CAN_MSGID_BRADAR_INFO & CAN_MSGID_RADAR_INFO & CAN_MSGID_FRADAR_INFO & CAN_MSGID_FLOWTOMC_ACK & CAN_MSGID_ENGINE_INFO \
  & CAN_MSGID_RADAR_START& CAN_MSGID_RADAR_ING & CAN_MSGID_RADAR_END))<<19)) | \
                            DES_ID_MASK | \
(SRC_ID_MASK & (((0x7f & (~(0x00 | CAN_NODEID_LED | CAN_NODEID_4G | CAN_NODEID_LIQUED | CAN_NODEID_FLOW | \
    CAN_NODEID_ENGINE | CAN_NODEID_LASER |CAN_NODEID_WEIGHT | CAN_NODEID_RADAR_UPDATE | CAN_NODEID_DISTRIBUTOR | CAN_NODEID_PUMP_NOZZLE))) |\
 (0x7f & CAN_NODEID_LED & CAN_NODEID_4G & CAN_NODEID_LIQUED & CAN_NODEID_FLOW & \
  CAN_NODEID_ENGINE & CAN_NODEID_LASER & CAN_NODEID_WEIGHT & CAN_NODEID_RADAR_UPDATE & CAN_NODEID_DISTRIBUTOR & CAN_NODEID_PUMP_NOZZLE))<<5))))


//海盈智能电池
#define HEREWIN_FILTER_ID  0x0000FE00    //               1111 1110 0000 0000
#define HEREWIN_MASK_ID    0x0300F0FF    //0011 0000 0000 1111 0000 1111 1111

//正方
#define CAN_ZHENGFANG (0x15359826)
#define CAN_ZHENGFANG_MASk (0x1FFF0000)

//格式
#define CAN_TATTU_MSG (0x01109216)
#define CAN_NEWTATTU_MSG (0x01109200)
#define CAN_NEWTATTU_MSG2 (0x011092FF)

//EZ雷达协议升级
#define CAN_EZ_R_UPDATE (0x3D)

extern uint8_t RxData[8];
extern CAN_RxHeaderTypeDef RxHeader;

#pragma pack(1)
typedef struct 
{   
    char Head[3];
    uint32_t Id;
    uint32_t Ide;
    uint32_t Dlc;
    uint32_t FilterIndex;
    uint8_t Data[8];

    bool complete_flag;
}canpack;
extern canpack canbuffer;
#pragma pack()

void CAN1_filter_init(void);
void CAN2_filter_init(void);

void Can_Send_Msg_Func(uint8_t can_num, unsigned char *data, unsigned char length, unsigned int id,
                  uint32_t id_type);
void L1L2_GPIO_check();
void put_date_to_can(uint8_t *buf,uint8_t par1,uint8_t par2,uint8_t par3,uint8_t par4,uint8_t par5,
                        uint8_t par6,uint8_t par7,uint8_t par8);
#endif