/*
加入队列 处理
*/
#ifndef __VIPER__H
#define __VIPER__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"


#include <string.h>
#include "bsp_uart.h"
 
#define Viper_USE_MAX485 0
#define Viper_USE_MAX3160_232 0
#define Viper_USE_MAX3160_485 1
 
#define  Viper_Rcv_Buf_Size   (3+244+2) 
// #define  Viper_Command_Number       1

static void Viper_MAX3160_232(void) 
{
  HAL_GPIO_WritePin(SEL_232_485_GPIO_Port,SEL_232_485_Pin, RESET);
  HAL_GPIO_WritePin(HDPLX_GPIO_Port,HDPLX_Pin, RESET);
}

typedef enum 
{
  Viper_Event_Ready ,
  Viper_Event_Test,
  Viper_Event_Test_OK  ,
  Viper_Event_Get_Data,
  Viper_Event_Check_Data  ,
} Module_Viper_Event_TypeDef;

typedef enum 
{
  Viper_State_Waiting  ,
  Viper_State_Test ,
  Viper_State_Test_Start ,
  Viper_State_SN_Getting ,
  Viper_State_SN_OK ,
  Viper_State_Ready  ,
  Viper_State_Get_WAVELENGTH  ,
//   Viper_State_Get_WAVELENGTH_Wait  ,
//   Viper_State_Get_WAVELENGTH_OK,
//   Viper_State_Get_WAVELENGTH_Check  ,
  Viper_State_Get_DATA  ,
  Viper_State_Get_DATA_Trans,
  Viper_State_Get_DATA_Wait  ,
  Viper_State_Get_DATA_OK,
  Viper_State_Get_DATA_Check  ,
  Viper_State_Get_Queue_MSG  ,
  Viper_State_Stop ,
  Viper_State_Timeout ,
  Viper_State_Error   ,
} Module_Viper_State_TypeDef;

extern UART_HELPER_TypeDef *Viper_uart_helper;
typedef struct 
{
    int (*init)(void);
    void (*port)(void);
    int (*test)(void);
    void (*start)(void);
    void (*stop)(void);

    osMessageQueueId_t transQueue;  // 队列可选

    volatile Module_Viper_State_TypeDef state; 
    uint8_t data_ok;                 /* 接收数据完整可以处理 */

    uint8_t result_buf[1024];
    uint8_t size_received;

    uint64_t  timebase_ticks;
    uint64_t  timeout_ticks;

    uint8_t		event_flag;
	uint8_t		wavelength_ok;

    Record_Trans_Rcv_TypeDef transRecord[3];

    uint8_t vipersn_buf[15];
    uint8_t Viper_Wavelength_Buf[3][ Viper_Rcv_Buf_Size ];
    uint8_t Viper_Data_Buf[3][ Viper_Rcv_Buf_Size ];

}Viper_TypeDef;

extern Viper_TypeDef viper ;

int Viper_Init( void );
void Viper_Task( void);
void Viper_Port( void);
int Viper_Test( );
void Viper_Start( );
void Viper_Stop( );

void Viper_Wavelength( );

void Viper_Set_Sendbuf(   uint8_t * buf, uint16_t size );
void Viper_Set_Rcvbuf(   uint8_t * buf, uint16_t size );
void Viper_Set_Timeout_ms(    uint64_t ms_ticks );
int Viper_Validate(   );


int Viper_Transmit();
int Viper_Receive();

void Viper_Trans_GPIO(void) ;

void Viper_Trans_Cplt_GPIO(void) ;

int Viper_CallBack( Viper_TypeDef *pViper , uint8_t *buf, uint16_t size );


#ifdef __cplusplus
	}
#endif
#endif


/*
    temp.init();
    temp.port();
    temp.test();

    uint64_t ticks = osKernelGetTickCount();

    while ( temp.state != PH_State_Ready )
    {
        if ( ( osKernelGetTickCount() -ticks) > 3000 )
        {
            log_e(" pH test error.... ") ;
            break;
        }
    }
    log_w(" pH test ok.... ") ;


// osMessageQueueId_t TestQQueueueueHandle;
// osMessageQueueId_t osMessageQueueNew (uint32_t msg_count, uint32_t msg_size, const osMessageQueueAttr_t *attr);
// osMessageQueueGet(TestQueueHandle,osWaitForever);
// osStatus_t osMessageQueueGet ( osMessageQueueId_t  mq_id,
// void *  msg_ptr,       //储存读取结果的变量地址
// uint8_t *  msg_prio,  // ==NULL
// uint32_t  timeout    //阻塞超时时间
// );
// osStatus_t osMessageQueuePut ( osMessageQueueId_t  mq_id,
// const void *  msg_ptr,     //储存写入内容的变量地址
// uint8_t  msg_prio,        //==0U
// uint32_t  timeout         //阻塞超时时间
// );


// uint8_t Viper_SN_command[8] = {0x01, 0x03, 0x00, 0x0A, 0x00, 0x05, 0xA5, 0xCB};

// uint8_t Viper_Wavelength_sendbuf[3][8]   = {
//         
//         // 01 03 08 36 00 7A 26 47    (2102  122)
//         // 01 03 08 B0 00 7A C7 AE    (2224  122)
//         // 01 03 09 2A 00 7A E6 7D    (2346  122)
//         
//         { 0x01, 0x03, 0x08, 0x36, 0x00, 0x7A, 0x26, 0x47 },
//         { 0x01, 0x03, 0x08, 0xB0, 0x00, 0x7A, 0xC7, 0xAE },
//         { 0x01, 0x03, 0x09, 0x2A, 0x00, 0x7A, 0xE6, 0x7D }
//     };
// uint8_t Viper_Data_sendbuf[3][8]   = {
//         
//         //01 03 0A 36 00 7A 27 FF    (2614  122)
//         //01 03 0A B0 00 7A C6 16    (2736  122)
//         //01 03 0B 2A 00 7A E7 C5    (2858  122)
//         
//         { 0x01, 0x03, 0x0A, 0x36, 0x00, 0x7A, 0x27, 0xFF },
//         { 0x01, 0x03, 0x0A, 0xB0, 0x00, 0x7A, 0xC6, 0x16 },
//         { 0x01, 0x03, 0x0B, 0x2A, 0x00, 0x7A, 0xE7, 0xC5 }
//     };
// uint8_t Viper_sn_buf[15] = {0};


*/