/********************************************************************
  Copyright (c) 2021 Xiangyu Medical Co.，Ltd. All rights reserved.
  FileName : drv_uart.h
  Author   : zhangdawei
  Version  : V1.0
  Date     :
  Note     :
  History  :
********************************************************************/
/* Includes ------------------------------------------------------*/
#ifndef DRV_UART_H__
#define DRV_UART_H__
#include "sdk_config.h"
#include "ble_nus.h"
#include "ble_link_ctx_manager.h"
/* Public define -------------------------------------------------*/
#define ONE_SECOND                             ( 1000000 )  /* 1s = 1000000us */
/* 功能码定义 */
#define RUN_ROLL                               ( 0x81 ) //运行轮询
#define VERSION_INQUIRY                        ( 0x82 ) //版本查询
#define STIM_PARA_CONTROL                      ( 0x83 ) //模式、脉宽、频率、斜坡时间设置
#define START_STOP_CONTROL                     ( 0x84 ) //控制
#define PRE_ADJUST                             ( 0x85 ) //预调节
#define CURRENT_CONTROL                        ( 0x86 ) //电流设置
#define EEG_DATA                               ( 0x87 ) //肌电数据上报
#define TRIG_COLLECT_START_CONTROL             ( 0x88 ) //触发采集开始
#define TRIG_STIM_START_CONTROL                ( 0x89 ) //触发刺激开始
#define STATUS_INQUIRY                         ( 0x8A ) //轮询
#define MAC_QUERY                              ( 0x8B ) //MAC地址查询
#define STATE_UPLOAD                           ( 0x92 ) //状态上传（电极片状态和适配器状态）
#define SCHEME_QUERY                           ( 0x91 ) //方案查询
/* 刺激启停控制数据偏移定义 */
#define StartStopOffset                        ( 4 )
/* 刺激启停控制数据描述 */
#define CH_STOP                                ( 0x00 )
#define CH_START                               ( 0x01 )
#define CH_PAUSE                               ( 0x02 )
#define CH_CONTINUE                            ( 0x03 )
/* 刺激参数数据偏移定义 */
#define HeadOffset                             ( 0 )
#define DataLengthOffset                       ( 1 )
#define FunctionCodeOffset                     ( 2 )
#define ChannelNumOffset                       ( 3 )
#define SchemeCategoryOffset                   ( 4 )
#define SchemeIDMSBOffset                      ( 5 )
#define SchemeIDLSBOffset                      ( 6 )
#define FreqMSBOffset                          ( 7 )
#define FreqLSBOffset                          ( 8 )
#define WidthMSBOffset                         ( 9 ) 
#define WidthLSBOffset                         ( 10 )
#define RampUpTimeLSBOffset                    ( 12 )
#define RampUpTimeMSBOffset                    ( 11 )
#define RampSmoothTimeLSBOffset                ( 14 )
#define RampSmoothTimeMSBOffset                ( 13 )
#define RampDownTimeLSBOffset                  ( 16 )
#define RampDownTimeMSBOffset                  ( 15 )
#define RampBreakTimeLSBOffset                 ( 18 )
#define RampBreakTimeMSBOffset                 ( 17 )
/* 状态轮询数据偏移定义 */
#define ElectrodeStatusOffset                  ( 4 )
#define StimStatusOffset                       ( 5 )
#define StimCurrentOffset                      ( 6 )
#define ResetOffset                            ( 7 )
#define DataCrcOffset                          ( 8 )
#define TailOffset                             ( 9 )
/* 运行轮询数据偏移定义 */
#define AdapterStatusOffset                    ( 5 )
#define BatteryLevelOffset                     ( 4 )
/* 刺激电流数据偏移定义 */
#define PRE_ADJUST_OFFSET                      ( 4 )
/* 预调节数据定义 */
#define START_PRE_ADJUST                       ( 0x01 )
#define STOP_PRE_ADJUST                        ( 0x00 )
/* 方案存储属性定义 */
#define TEMP_STORAGE                           ( 1 )      // 临时存储
#define PERMANENT_STORAGE                      ( 2 )      // 永久存储
#define FDS_DATA_LENGTH                        ( 20 )     // 存储空间长度

//头尾帧
#define FRAME_HEADER                          (0xAA)
#define FRAME_TAIL                            (0x55)

//功能码
// Function Codes (Converted to UPPER_SNAKE_CASE)
#define D_PARAMETER_SET                       (0x71)  // Original: D_ParameterSet
#define D_CHANNEL_CTRL                        (0x72)  // Original: D_ChannelCtrl
#define D_CURRENT_SET                         (0x73)  // Original: D_CurrentSet
#define D_EMG_DATA_REPORT                     (0x74)  // Original: D_EmgDataReport
#define D_DATA_MODE_SWITCH                    (0x75)  // Original: D_DataModeSwitch
#define D_PREINSTALL_CTRL                     (0x76)  // Original: D_PreinstallCtrl
#define D_TRIGGER_MODE_CTRL                   (0x77)  // Original: D_TriggerModeCtrl
#define D_POLL_CMD                            (0x78)  // Original: D_PollCmd
#define D_SLICE_FALL_DETECT_SWITCH            (0x79)  // Original: D_SliceFallDetectSwitch
#define D_TURN_OFF_CMD                        (0x7A)  // Original: D_TurnOffCmd
#define D_BLE_SCAN_SWITCH                     (0x7B)  // Original: D_BleScanSwitch
#define D_MAC_ADDR_REPORT                     (0x7C)  // Original: D_MacAddrReport
#define D_BLE_CONNECT_CMD                     (0x7D)  // Original: D_BleConnectCmd

/* Public typedef ------------------------------------------------*/
/* 方案信息结构体 */
typedef struct
{
    uint8_t StorageMode;      //存储模式
    uint8_t SchemeIDMSB;      //方案ID
    uint8_t SchemeIDLSB;
    uint8_t FreqMSB;          //频率
    uint8_t FreqLSB;
    uint8_t WidthMSB;         //脉宽
    uint8_t WidthLSB; 
    uint8_t RampUpTimeMSB;    //波升
    uint8_t RampUpTimeLSB;
    uint8_t RampSmoothTimeMSB;//保持
    uint8_t RampSmoothTimeLSB; 
    uint8_t RampDownTimeMSB;  //波降
    uint8_t RampDownTimeLSB;
    uint8_t RampBreakTimeMSB; //休息
    uint8_t RampBreakTimeLSB;
}SchemeData_t;

#define RMS_USER_DATA_LENGTH                   4//110///4 //用户数据长度
#pragma pack(1)

typedef struct
{
  uint8_t   frameHeader;          //帧头
  uint8_t   frameLength;          //帧长度
  uint8_t   functionCode;         //功能码
  uint8_t   myNumber;            //编号
  uint8_t   channel;              //通道号
  uint16_t  rmsDataBuffer[RMS_USER_DATA_LENGTH];             //数据内容
  uint8_t   checkSum;             //校验和
  uint8_t   frameTail;            //帧尾
} rms_data_t;

typedef struct
{
    uint8_t   frameHeader;          //帧头
    uint8_t   frameLength;          //帧长度
    uint8_t   functionCode;         //功能码
    uint8_t   channel;              //通道号
    uint8_t   ChargeState;          //充电状态
    uint8_t   BatteryLevelA;        //A 电池电量
    uint8_t   BatteryLevelB;        //B 电池电量
    uint8_t   BatteryLevelC;        //C 电池电量

    uint8_t   reserve[5];               //保留字段

    uint8_t   checkSum;             //校验和
    uint8_t   frameTail;            //帧尾
} reply_run_status_t;

#define MAX_VERSION_LEN 15
typedef struct
{
    uint8_t   frameHeader;          //帧头
    uint8_t   frameLength;          //帧长度
    uint8_t   functionCode;         //功能码
    uint8_t   channel;              //通道号
    uint8_t   VersionDes[MAX_VERSION_LEN];       //版本描述

    uint8_t   checkSum;             //校验和
    uint8_t   frameTail;            //帧尾
} check_version_t;


#pragma pack()


//设备连接状态
typedef enum
{
    DisconnectState = 0,
    ConnectState
}ConnectStateInfo_e;
// 电极片状态
typedef enum
{
    ElectrodeFalloff,
    ElectrodeConnectted
}ElectrodeStatusInfo_e;
// 适配器连接状态
typedef enum
{
    AdapterNotConnected = 0,
    AdapterConnected
}AdapterStateInfo_e;
//FDS异步操作标志结构体
typedef struct
{
	bool scheme_update;       //scheme_record记录更新标志
	bool read;       //读记录标志
	bool gc;         //碎片收集标志
    bool busy;       //FDS忙标志
}my_fds_info_t;

//记录scheme的id和内容
typedef struct
{
    uint8_t text[FDS_DATA_LENGTH];
}__attribute__((aligned(4)))SchemePara_t;
/* Public constants ----------------------------------------------*/
/* Public variables ----------------------------------------------*/
extern uint16_t m_conn_handle;
extern uint16_t m_ble_nus_max_data_len;
extern uint8_t CurrentFlag;
extern ConnectStateInfo_e DeviceConnectState;
extern ElectrodeStatusInfo_e ElectrodeStatusInfo;
extern my_fds_info_t my_fds_info;
extern SchemePara_t SchemePara;
extern SchemeData_t SchemeData;
extern uint8_t BLE_MAC[BLE_GAP_ADDR_LEN];
extern AdapterStateInfo_e AdapterState;
extern AdapterStateInfo_e LastAdapterState;
extern SchemeData_t PreStorageSchemeData;
extern ElectrodeStatusInfo_e LastElectrodeStatusInfo;
extern uint16_t ccrvaluebuf[70];
extern uint8_t eegflag;
/* Public function prototypes ------------------------------------*/
void service_nus_init(void);
void StartManage(void);
void PauseManage(void);
void RecoverManage(void);
void StopManage(void);
void CloseOutput(void);
void SetStandardCurrent(uint8_t* AnalysisDataBfferIn_t);
void RunRoll(void);
void CheckVersion(void);
void UpdateCurrent(uint8_t CurrentSend);
void JudgeLedMode(void);
void DisconnectControl(void);
void SchemeQuery(uint8_t idMSB,uint8_t idLSB);
void UpdateControlStatus(uint8_t ControlStatus);
void StateUpLoad(AdapterStateInfo_e AdapterStateTemp , ElectrodeStatusInfo_e ElectrodeStatusTemp);
void EegDataSend(void);
void user_ble_or_uart_send(char * txBufferP, uint16_t Length);

void ble_send_rms_data(uint16_t rms_data);
#endif
/*************************** END OF FILE ***************************/


