/*
 *  usr_protocol_type.h
 *  pc同主机通信
 *  Created on: 2023年9月18日
 *      Author: longtengjun
 */

#include <stdint.h>

#ifndef _USR_PROTOCOL_TYPE_
#define _USR_PROTOCOL_TYPE_

#define  ONECHIP_MINI_ONE
//#define  ONECHIP_MINI_PLUS


#define __WIFI_ENABLE__ 1   //wifi使能


/*****************define Card state*****************/
#define CardON 1
#define CardOFF 0

#define MINI7_HEADER_FALG 0x55AA
#define MINI7_TAIL_FALG 0x5A
#define MINI7_MASSAGE_SIZE 8
#define MINI7_MASSAGE_TYPE_MCUTOMCU 0x10

#define ACK_TIMEOUT 9
#define RESEND_TIME 3
#define PC_OFFLINE_TIMEOUT 30       //PC离线超时时间
#define PC_NORMAL_COM_TIME 11       //正常通信间隔

#define ARRAY_COUNT(array)	(sizeof(array) / sizeof(array[0]))

//LOG打印控制
#define LOG_LEVEL_ALL   (0xFF)
#define LOG_LEVEL_DEBUG (1 << 0)
#define LOG_LEVEL_INFO  (1 << 1)
#define LOG_LEVEL_WARN  (1 << 2)
#define LOG_LEVEL_ERROR (1 << 3)
#define LOG_LEVEL_WIFI  (1 << 4)
#define LOG_LEVEL_NO    (0x00)

#define LOG(...) my_printf( __VA_ARGS__)
#define LOG_(x, ...) if(x & log_level) my_printf( __VA_ARGS__)

#define LOG_D(...) LOG_(LOG_LEVEL_DEBUG, __VA_ARGS__)
#define LOG_I(...) LOG_(LOG_LEVEL_INFO, __VA_ARGS__)
#define LOG_W(...) LOG_(LOG_LEVEL_WARN, __VA_ARGS__)
#define LOG_E(...) LOG_(LOG_LEVEL_ERROR, __VA_ARGS__)
#define LOG_WIFI(...) LOG_(LOG_LEVEL_WIFI, __VA_ARGS__)

struct CUartMgr
{
    char *m_pLeftData;
    int m_iLeftLen;
};


#define g_uMsgCmdFrameLen (6)

enum cmd_frame_type
{
    cmd_frame_type_req,
    cmd_frame_type_resp,
};



enum light_mode_type
{
    MODE_NORMAL,    //常亮
    MODE_BLINK,     //闪烁
    MODE_BREATH,    //呼吸
};

enum SYS_STATE_DEF
{
    SYS_STATE_PREPARE,
    SYS_STATE_READY,
    SYS_STATE_CHECK,
    SYS_STATE_RUNING,
    SYS_STATE_WORKEND,
    SYS_STATE_ERROR = 0xff
};

enum light_color_type
{
    COLOR_BLUE = 0x000088,      //蓝
    COLOR_GREEN = 0x008800,     //绿
    COLOR_RED = 0x880000,       //红
    COLOR_YELLOW = 0x888800,    //黄
    COLOR_OTHER = 0xFFFFFF,    //白
};

/* host量配置参数 */
typedef struct _pidParm {
    float kp; /* 预留参数2 */
    float ki; /* 预留参数1 */
    float kd; /* 预留参数3 */
} pidParm;
/* host量配置参数 */
typedef struct _MotorParm {
    float time; /* 预留参数2 */
    float speed; /* 预留参数1 */
    float swingtime; /* 预留参数3 */
} MotorParm;
/* host量配置参数 */
typedef struct _tempAdjCouple {
    float raw;    /*原始值*/
    float offset; /*预留参数2 */
} tempAdjCouple;

typedef struct _tempAdjParm {
    unsigned int section; /* 段数 */
    tempAdjCouple *offsetlist; /*  */
} tempAdjParm;


typedef struct _miniConfigPram {    
	unsigned int partID;
    union {
        pidParm pid;
        tempAdjParm adj;
        MotorParm minf;
    }Pram;
} miniConfigPram;


struct PCActionPack
{
    enum pc_cmd_type cmd;
    int (*action)(uint8_t cmd, uint8_t *parg);
    int (*response)(struct CmdMessage *pMsg, uint8_t result);
    int datalen;
};

struct B2BActionPack
{
    enum b2b_cmd_type cmd;
    int (*action)(uint8_t cmd, uint8_t *parg);
    int (*response)(struct CmdMessage *pMsg, uint8_t result);
};

struct WIFIConfigInfo
{
    uint8_t name[32];
    uint8_t password[32];
};

extern uint32_t log_level;

void DelayMS(uint32_t ms);
void b2b_recv_data(uint8_t *pRcvData, uint16_t iRcvLen);
extern int  b2b_send_response(struct CmdMessage *pMsg, uint8_t result);

extern uint16_t b2b_send_device(enum b2b_cmd_type cmd, char *data, uint16_t len,uint8_t master);
extern void process_system(void);
extern uint16_t b2b_send_to_pc(enum pc_cmd_type cmd, char *data, uint16_t len);
uint16_t b2b_send_boardcast(enum b2b_cmd_type cmd, char *data, uint16_t len);
uint16_t data_send_to_pc(enum pc_cmd_type cmd, char *data, uint16_t len);
void data_resend_to_pc(void);
unsigned int check_to_pc_busy(void);

uint16_t union_data_send_to_pc(struct CmdMessage *pMsg);
void set_pc_chanel_recv(uint8_t channel);
void set_pc_chanel_temp();
void reload_pc_chanel();
void set_pc_chanel(uint8_t channel);
uint32_t byte_to_u32(uint8_t *p, int len);
void processB2BCmd(struct CmdMessage *pMsg);
uint16_t checkSum(const uint8_t *pInBuffer, int uLen);
int my_printf(const char* str, ...);
int hex_to_ascii(const void *hex, void *string, int len);
void b2b_send_Heat_Para(uint8_t cmd);

#endif
