#ifndef __PH_H
#define __PH_H

#ifdef __cplusplus
	extern "C" {
#endif



#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include "usart.h"
 
 
#include "elog.h"

#ifdef __cplusplus
	extern "C" {
#endif

#include <string.h>
#include "uart_helper.h"
#include "uart_interface.h"
// #include "config.h"
 
#define  PH_Rcv_Buf_Size   9
#define  PH_Command_Number       1

typedef enum 
{
  PH_Status_Waiting  ,
  PH_Status_Ready  ,
  PH_Status_Sending ,
  PH_Status_Send_Cplt  ,
  PH_Status_RCV_OK  ,
  PH_Status_DATA_OK  ,
  PH_Status_Timeout ,
  PH_Status_Error   ,
} PH_Status_TypeDef;

typedef struct 
{
	UartHelper_TypeDef *uarthelper;
    // UART_HandleTypeDef *huart;
    uint8_t interface_type; /* 0: common, 1: 485 ,2:3160*/
	uint8_t *send_buf;
	uint16_t size_send;
	uint8_t *rcv_buf;
	uint16_t size_rcv;

           
	uint8_t mode_232_485; /* 0: common, 1: 485  */
    uint8_t trans_type;  /* 0 :polling, 1: IT  2: DMA*/
    uint8_t rcv_type;  /* 0 :polling, 1: IT  2: DMA*/
    uint8_t idle_enable_disable;    /* 0 不开启空闲中断 ， 1 开启空闲中断 */
	uint8_t trans_mode;         /* 0 :polling, 1: IT  2: DMA*/

	uint64_t timebase;
	uint16_t timeout_ms;
	volatile uint8_t send_flag;  		/* 发送标志位 */
	Uart_Status_TypeDef send_status;  /* 发送状态 1：正在发送 busy，0：发送ok，*/
	Uart_Status_TypeDef status;  /* 发送状态 1：正在发送 busy，0：发送ok，*/

    uint8_t command_seq ;      /* 命令序号 */
    uint8_t mode;  /* 0 : normal , 1 : get wavelength ,  2 : get sn  */

    uint8_t vipersn_buf[15];
    uint8_t PH_Wavelength_Buf[2][ PH_Rcv_Buf_Size ];
    uint8_t PH_Data_Buf[2][ PH_Rcv_Buf_Size ];

    uint8_t rcv_ok;                 /* 接收数据完整可以处理 */
    uint8_t data_begin_flag;                 /* 接收数据完整可以处理 */
    uint8_t data_ok;                 /* 接收数据完整可以处理 */

    volatile PH_Status_TypeDef state;

	// int (*Init)(void);			//初始化触摸屏控制器
	// // uint8_t (*Setup)( );				//扫描触摸屏.0,屏幕扫描;1,物理坐标;	 
	// uint8_t (*Port)( );				//扫描触摸屏.0,屏幕扫描;1,物理坐标;	 


}PH_TypeDef;

extern PH_TypeDef *ph ;

int PH_Init(   );
int PH_Test( );
void PH_Start( );
void PH_Stop( );

void PH_Set_Uarthelper( PH_TypeDef *ph,  UartHelper_TypeDef * uarthelper );
void PH_Set_Interface( PH_TypeDef *ph,  UartInterface_TypeDef * interface  );
void PH_Set_Huart( PH_TypeDef *ph,  UART_HandleTypeDef * huart  );
void PH_Set_Interface_Type( PH_TypeDef *ph,   Uart_Interface_Type_Typedef interface_type  );
void PH_Set_Trans_Type( PH_TypeDef *ph,  Uart_Transmode_TypeDef trans_type  );
void PH_Set_Rcv_Type( PH_TypeDef *ph,  Uart_Transmode_TypeDef rcv_type  );
void PH_Set_Idle_Enable( PH_TypeDef *ph,  Uart_IDLE_Enable_TypeDef idle_enable_disable  );
void PH_Set_TransMode( PH_TypeDef *ph,  Uart_Transmode_TypeDef trans_mode  );
void PH_Set_RsMode_232_485( PH_TypeDef *ph,  Uart_RS_Mode_TypeDef rs_232_485  );
void PH_Set_Timeout( PH_TypeDef *ph,    uint16_t timeout_ms );
void PH_Set_Sendbuf( PH_TypeDef *ph,  uint8_t * buf, uint16_t size );
void PH_Set_Rcvbuf( PH_TypeDef *ph,  uint8_t * buf, uint16_t size );

int PH_Transmit( PH_TypeDef *ph,  uint8_t * buf, uint16_t size);


void PH_Begin_Rcv(PH_TypeDef *ph,  uint8_t * buf, uint16_t size);

extern  void PH_Rcv_Cplt_Callback( PH_TypeDef *ph  );
extern  void PH_Send_Cplt_Callback(  PH_TypeDef *ph );

int  PH_Get_Data_OK(PH_TypeDef *ph   );
void PH_Set_Send_Flag(  PH_TypeDef *ph );

// extern void PH_Setup(    );
extern void PH_Port( );
// extern void PH_SN( );
// extern void PH_Wavelength( );
void PH_Task( );
 
/* 再封装演示 */ 
typedef struct 
{
	uint8_t (*init)( );			 
	uint8_t (*port)(void);	
	int (*test)(void);
	void (*start)(void);
	int (*stop)(void);
    				//扫描触摸屏.0,屏幕扫描;1,物理坐标;	 
    GPIO_TypeDef *sel_gpio;	 
    GPIO_TypeDef *dplx_gpio;	 
    GPIO_TypeDef *de_gpio;	 
    uint16_t sel_pin;
    uint16_t dplx_pin;
    uint16_t de_pin;

    Uart_Interface_Type_Typedef interface_type; /* 0: common, 1: 485 ,2:3160*/
    Uart_RS_Mode_TypeDef mode_232_485;       /* 0 commome  1:485*/
    Uart_Transmode_TypeDef trans_type;     /* 0 :polling, 1: IT  2: DMA*/
    Uart_Transmode_TypeDef rcv_type;     /* 0 :polling, 1: IT  2: DMA*/
    Uart_IDLE_Enable_TypeDef idle_enable_disable;     /* 0 :不启用空闲 , 1: 启用空闲*/

    uint16_t ph_val;
    uint16_t temp_val;
    PH_TypeDef *ph; /*不是常量  不能直接初始化*/


}My_PH_TypeDef;
extern My_PH_TypeDef my_pH ;

 

#ifdef __cplusplus
	}
#endif
#endif
