#ifndef _TAX_CTRL_H_
#define _TAX_CTRL_H_

#include "stm32f10x.h"
#include "includes.h"

#define UART_RS485 UART3_ID

extern int rs485_send_flag;

typedef int (*rx_data_proc) (void *puser, uint8_t *data, uint8_t data_len);

typedef struct _tYTSFCommFrame
{
	uint8_t fhead;   							//帧头(1Byte)
	uint8_t flen;        						//帧长度(1Byte)
	uint8_t findex;        						//帧序号(1Byte)
	uint8_t fport;        						//税控口编号(1Byte)，0税控口1，1税控口2
	uint8_t fcmd;        						//命令码(1Byte)
//	uint16_t fcrc;        						//CRC校验(2Byte)
}tYTSFCommFrame_t;

typedef struct _tTaxCommFrame
{
	//前导码+长度码 千帧号+命令码 +参数 +校验码
	uint8_t fhead;   							//帧头(1Byte)
	uint8_t flen;        						//帧长度(1Byte)
	uint8_t findex;        						//帧序号(1Byte)
	uint8_t fcmd;        						//命令码(1Byte)
//	uint16_t fcrc;        						//CRC校验(2Byte)
}tTaxCommFrame_t;

typedef struct _gun_info {
	uint8_t fail_count;
	uint8_t quality;				//油品
	uint8_t id;						//编号
	uint8_t status;					//工作状态
	uint64_t total_price;			//总累计金额
	uint64_t total_oil_volume;		//总体积
	uint32_t last_unit_price;		//最后一次加油的单价
	uint32_t last_oil_volume;		//最后一次加油的油量
	uint32_t last_price;			//最后一次加油的油价
	uint8_t gun_type;  //加油枪的数据类型：0当次数据；1 累计数据
	uint32_t gun_msgid; //加油枪序列号
        uint8_t time_day;
        uint8_t time_hour;
        uint8_t time_minute;
//	uint8_t monitor_serino[16];
//	uint8_t gun_num;
//	uint8_t factory;			/* 1: 英泰赛福 2: 拓盛 */
//	uint8_t	taxpayer_reg_no[20];
} gun_info_t;

typedef struct _tax_info {
	uint8_t monitor_serino[16];
	uint8_t coder_serino[20];
	uint8_t dispaly_serino[16];
	uint8_t gun_num;
	uint8_t factory;			/* 1: 英泰赛福 2: 拓盛 */
	uint8_t fail_count;
	uint8_t status;
	uint8_t gun_valid;
	uint8_t	taxpayer_reg_no[20];
	gun_info_t gun[8];

	//for  debug
	uint32_t tx_pkts;
	uint32_t rx_pkts;
	uint32_t rx_errs;
} tax_info_t ;

/**********************显示屏参数*********************/
#define Write_LockScreen      0x81  //写锁屏
#define Read_LockScreen       0x01  //读锁屏
#define Write_LockAddress     0x82  //写地址锁定
#define Read_LockAddress      0x02  //读地址锁定
#define Write_Timesync        0x83  //写时间同步

typedef struct _txspCommFrame
{
	//帧头+长度码+帧号+地址码+指令码 +参数 +校验码
	uint8_t fhead;             //帧头(1Byte)
	uint8_t flen;              //帧长度(1Byte)
	uint8_t findex;            //帧序号(1Byte)
	uint8_t faddress;          //地址码(1Byte)
	uint8_t fcmd;              //指令码(1Byte)
}txspCommFrame_t;

/*******************************************/

typedef struct _rcv_data_0x8C_{
    uint8_t monitor_serino[10];
    uint8_t data14[64];
    uint8_t data14len;
    uint8_t data11[64];
    uint8_t data11len;
    uint16_t timer86;
    uint8_t crc86;
    uint8_t crc89;
}__attribute__((packed)) rcv_data_0x8C_t;
extern rcv_data_0x8C_t g_tax8Cda;



#define GB_TAX_HEADER_LEN 		10
#define GB_TAX_INFO_LEN 		53
#define GB_GUN_INFO_LEN 		22

#define YTSF_GPIO_RS485_RESET()  {GPIO_ResetBits(GPIOD, GPIO_Pin_3);}
#define YTSF_GPIO_RS485_SET()    {GPIO_SetBits(GPIOD, GPIO_Pin_3);}

#define YTSF_GPIO_AB_RESET()  {GPIO_ResetBits(GPIOD, GPIO_Pin_4);}
#define YTSF_GPIO_AB_SET()    {GPIO_SetBits(GPIOD, GPIO_Pin_4);}

#define YTSF_GPIO_CD_RESET()  {GPIO_ResetBits(GPIOD, GPIO_Pin_5);}
#define YTSF_GPIO_CD_SET()    {GPIO_SetBits(GPIOD, GPIO_Pin_5);}

#define YTSF_GPIO_YTSF_RESET()  {GPIO_ResetBits(GPIOD, GPIO_Pin_6);}
#define YTSF_GPIO_YTSF_SET()    {GPIO_SetBits(GPIOD, GPIO_Pin_6);}

//#define YTSF_GPIO_REV2_RESET()  {GPIO_ResetBits(GPIOD, GPIO_Pin_7);}
//#define YTSF_GPIO_REV2_SET()    {GPIO_SetBits(GPIOD, GPIO_Pin_7);}

//#define YTSF_GPIO_SET()  {GPIO_SetBits(GPIOB, GPIO_Pin_5);}
//#define YTSF_GPIO_RST()  {GPIO_ResetBits(GPIOB, GPIO_Pin_5);}

//#define YTSF_GPIO_DETECT  GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_6)

//#define YTSF_GPIO_EN()  {GPIO_ResetBits(GPIOB, GPIO_Pin_7);}
//#define YTSF_GPIO_DN()  {GPIO_SetBits(GPIOB, GPIO_Pin_7);}


/************************************************************/
#define YTSF_GPIO_REV2_RESET()  {GPIO_ResetBits(GPIOC, GPIO_Pin_0);}
#define YTSF_GPIO_REV2_SET()    {GPIO_SetBits(GPIOC, GPIO_Pin_0);}

#define YTSF_GPIO_SET()  {GPIO_SetBits(GPIOC, GPIO_Pin_1);}
#define YTSF_GPIO_RST()  {GPIO_ResetBits(GPIOC, GPIO_Pin_1);}

#define YTSF_GPIO_DETECT  GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_2)

#define YTSF_GPIO_EN()  {GPIO_ResetBits(GPIOC, GPIO_Pin_3);}
#define YTSF_GPIO_DN()  {GPIO_SetBits(GPIOC, GPIO_Pin_3);}
/************************************************************/

#define BIT_SET(p, bit)		((p) |= (1<<(bit)))
#define BIT_CLC(p, bit)		((p) &= ~(1<<(bit)))

#define TAX_UART	UART1_ID
#define FAIL_THRESHOLD	3
#define TAX_MSG_PERIOD 10

extern uint16_t _crc16_get(uint8_t *_buff,uint32_t _len);

extern uint8_t tax_rcv_analyze_0x8C(uint8_t *data,void *puser,uint8_t *len);
extern int tax_0x70_anaylse(void *puser, uint8_t *data, uint8_t data_len);


#endif
