/**
 * \file
 *
 * \brief Common User Interface for HID Mouse application
 *
 * Copyright (c) 2009-2018 Microchip Technology Inc. and its subsidiaries.
 *
 * \asf_license_start
 *
 * \page License
 *
 * Subject to your compliance with these terms, you may use Microchip
 * software and any derivatives exclusively with Microchip products.
 * It is your responsibility to comply with third party license terms applicable
 * to your use of third party software (including open source software) that
 * may accompany Microchip software.
 *
 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
 * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
 * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
 * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
 * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
 * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
 * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
 * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
 * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
 *
 * \asf_license_stop
 *
 */
/*
 * Support and FAQ: visit <a href="https://www.microchip.com/support/">Microchip Support</a>
 */

#ifndef _UI_H_
#define _UI_H_

//! \brief Initializes the user interface
void ui_init(void);

//! \brief Enters the user interface in power down mode
void ui_powerdown(void);

//! \brief Enables the asynchronous interrupts of the user interface
void ui_wakeup_enable(void);

//! \brief Disables the asynchronous interrupts of the user interface
void ui_wakeup_disable(void);

//! \brief Exits the user interface of power down mode
void ui_wakeup(void);

/*! \brief This process is called each 1ms
 * It is called only if the USB interface is enabled.
 *
 * \param framenumber  Current frame number
 */
void ui_process(uint16_t framenumber);

//! This function is called when a HID report is received
//!
void ui_led_change(uint8_t *report);
void ui_cmd_process(uint8_t *report);
void ChipErase(uint8_t chip , uint8_t Ers_HV);
void SectorErase(uint8_t chip, uint8_t Ers_HV, uint16_t sector, uint8_t num);
void Dig_Prog(uint8_t chip, uint8_t PRGDIG_HV,uint8_t ENIP_PRGDIG,uint8_t ENBIAS_PRGDIG, uint32_t * src, uint16_t num,uint16_t xa, uint8_t ya);
void EsfRead(uint8_t chip, uint16_t xa, uint8_t ya, uint16_t length, uint8_t * dst);
#define CMD_BYTE_NUM	1
#define LEN_BYTE_NUM	2
#define TYP_BYTE_NUM	3
#define DATA_BYTE_NUM	4

#define CMD_SET		0x1
	// type below CMS_SET
	#define TYP_MUX_CTRL		0x01	//data byte 0: Control MUX bit 0--enable, bit1 Select
	#define TYP_RST_CTRL		0x02	// initial a POR process
	#define TYP_LED_CTRL		0x03	// control LED


#define CMD_GET		0x2

#define CMD_READ	0x4
	// type below CMD_READ
	#define TYP_DIG_RD			0x01	// digital read
	#define TYP_VER_INFO		0x27

#define CMD_PRG		0x9
	// type below CMD_PRG
	#define TYP_CHIP_ERASE		0x01	// erase: data0: chip, data1:trim data
	#define TYP_SECT_ERASE		0x02	// erase
	#define TYP_WR_PACKET		0x03	// input data packet
	#define TYP_DIG_PRG			0x04	// program to flash
	#define TYP_TEST_PRG		0x05	// a test program for debugging


#define TYP_MUX_CTRL		0x01	//data byte 0: Control MUX bit 0--enable, bit1 Select
#define TYP_RST_CTRL		0x02	// initial a POR process
#define TYP_LED_CTRL		0x03	// initial a POR process

#define MAX_CHIP_NUM		4

#define TYP_FAN_CTRL		0x03	//  Fan control
#define TYP_FAN_READ        0x0A    //  Fan status read
#define TYP_FAN_GAP_SET		0x4
#define TYP_FAN_GAP_READ	0x5


#define ERR_NONE			0
#define ERR_LENGTH			1
#define ERR_CMD				2
#define ERR_ROW				3
#define ERR_CHKSUM			4
#define ERR_TYP				5
#define BAD_DATA			6
#define OUT_RANGE			7
#define ERR_UNDEF			8

#define ERR_BUSY			9

#define HEADER				0xAA
#define TAIL				0x17

#define MAX_LENGTH			64
#define MIN_LENGTH			1	

#define MAX_RPL_DATA_LENGTH	57

#define IDLE				0
#define PENDING				1
#define BUSY				2

#define FPGA_IF				0
#define MCU_IF				1	
typedef struct  
{
	uint8_t report_valid;
	uint8_t report_len;
	uint8_t buf[64];
}report_send_typ;

typedef struct
{
	uint8_t cmd_valid;
	uint8_t cmd;
	uint8_t typ;
	uint8_t reply_status;
	uint8_t data_len;
	uint8_t buf[64];
}cmd_op_typ;


extern report_send_typ Report_send_struct;
extern cmd_op_typ Cmd_received_struct;
extern uint8_t	   Connection_Status;
void Push_Send_Report(uint8_t status, uint8_t cmd, uint8_t length, uint8_t * src);
void Push_cmd_Op(uint8_t cmd, uint8_t typ, uint8_t length, uint8_t * src, cmd_op_typ * p);
void MUX_Select(uint8_t sel, uint8_t state);
bool Check_Op_status(void);
bool Is_Cmd_Pending(void);
void cmd_Op(cmd_op_typ * p);

#define MAX_DATA_LENTH 57


//#define __IN_DEBUG
#endif // _UI_H_
