#ifndef _USART_H_
#define _USART_H_

#include "stdint.h"
#include "Include.h"

#include "user_queue.h"

#define MAX_SLAVE_NUMBER                       ( 8 )
#define MAC_ADDRESS_LENGTH                     ( 12 )


#define BLE_USART                              ( USART1 )
#define BLE_USART_GPIO_CLK                     ( RCC_AHB1Periph_GPIOB )
#define BLE_USART_CLK                          ( RCC_APB2Periph_USART1 )


#define BLE_USART_GPIO_PORT                    ( GPIOB )

#define BLE_USART_TX_GPIO_PIN                  ( GPIO_Pin_6 )
#define BLE_USART_RX_GPIO_PIN                  ( GPIO_Pin_7 )

#define BLE_USART_BAUDRATE                     ( 115200 )

#define BLE_NVIC_IRQChannel                    ( USART1_IRQn )

/**********************复用**************************/
#define BLE_USART_TX_PinSource                 ( GPIO_PinSource6 )
#define BLE_USART_RX_PinSource                 ( GPIO_PinSource7 )
#define BLE_GPIO_AF_USART                      ( GPIO_AF_USART1 )

#define USART1_RX_BUFFER_SIZE                  ( 50 )



#define FrameHead                              ( 0xAA )
#define FrameTail                              ( 0x55 )
/*********功能码**********/
#define WaveformSet                            ( 0x71 )
#define AcquisitionCtrl                        ( 0x72 )
#define CurrentSet                             ( 0x73 )
#define EMG_DataReport                         ( 0x74 )
#define EMG_FormatSwitching                    ( 0x75 )
#define CurrentPreinstallCtrl                  ( 0x76 )
#define TriggerModeCtrl                        ( 0x77 )
#define Poll                                   ( 0x78 )
#define SliceFalLSwitch                        ( 0x79 )
#define ShutDown                               ( 0x7A )
/***********偏移***********/
#define HeadOffset                             ( 0x00 )
#define LengthOffset                           ( 0x01 )
#define FunctionCodeOffset                     ( 0x02 )
#define DeviceNumberOffset                     ( 0x03 )
#define ChannelOffset                          ( 0x04 )
/**********数据************/
#define ChannelModeAcquisition                 ( 0x01 )
#define ChannelModeStimulation                 ( 0x02 )
#define ChannelModeTrigger                     ( 0x03 )

#define AcquisitionRate4K                      ( 0x01 )
#define AcquisitionRate8K                      ( 0x02 )

#define TurnOff                                ( 0x00 )
#define TurnOn                                 ( 0x01 )
#define Pause                                  ( 0x02 )
#define Continue                               ( 0x03 )

#define RMS_Data                               ( 0x00 )
#define OriginalData                           ( 0x01 )

#define Acquisition                            ( 0x00 )
#define Stimulation                            ( 0x01 )

#define SliceFall                              ( 0x00 )
#define SliceConnect                           ( 0x01 )
#define SliceUnmonitored                       ( 0x02 )

#define IdleState                              ( 0x00 )
#define ClimbState                             ( 0x01 )
#define DownState                              ( 0x02 )
#define KeepState                              ( 0x03 )
#define RestState                              ( 0x04 )

#define Charging                               ( 0x00 )
#define BatterySupply                          ( 0x01 )

#define SlaveConnect                           ( 0x01 )                           
#define SlaveDisconnect                        ( 0x02 )

#define BLE_Disconnect                         ( 0x01 )
#define BLE_Connect                            ( 0x02 )

#define Reset                                  ( 0x00 )
#define NotReset                               ( 0x01 )

typedef struct
{
	uint8_t  mode;           //模式
	uint8_t  rate;           //采样率
	uint8_t  EMG_data_mode;  //肌电数据模式
	uint8_t  state;          //开关状态
}channel_data_t;

typedef struct
{
	channel_data_t channel1; 
	channel_data_t channel2;
	uint8_t output_current_mA;    //输出电流强度
	uint8_t preinstall_state;     //预设开关
	uint8_t stimulate_state;      //刺激开关
	uint8_t slice_state;          //极片状态
	uint8_t slice_detect_state;   //脱落检测开关
	uint8_t formwave_state;       //波形状态
	uint8_t mac_address[MAC_ADDRESS_LENGTH];       //mac
	uint8_t connection_state;     //蓝牙连接状态
	uint8_t adapter_state;        //适配器状态
	uint8_t electric_quantity;    //电量
	uint8_t reset_flag;           //复位标志位
	
	uint16_t frequency_Hz;   //频率
	uint16_t width_us;       //脉宽
	uint16_t climb_time_ms;  //上升时间
	uint16_t keep_time_ms;   //保持时间
	uint16_t down_time_ms;   //下降时间
	uint16_t rest_time_ms;   //休息时间
}device_state_t;


void ble_usart_init(uint32_t baudrate);
//void ble_usart_send(uint8_t *ucpTx_Data,uint8_t ucTx_length);

uint8_t ble_usart_send(uint8_t ucTx_length);

uint8_t Analysis_data(void);

extern uint8_t usart1_tx_done ;//串口1发送完成标志

void net_received_data_analysis(void);
void parameters_init(void);


#endif
