#pragma once

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
#include <stdbool.h>
#include "device/sx1281_driver/sx1281_driver.h"
#include "device/device.h"

/* USER INCLUDE BEGIN */

/* USER INCLUDE END */

/* Exported constants ------------------------------------------------------- */
/* Exported macro ----------------------------------------------------------- */
/*!
 * \brief Defines the buffer size, i.e. the payload size
 */
#define BUFFER_SIZE 127



/* Exported types ----------------------------------------------------------- */

typedef enum {
  APPMODE_SLEEP,
  APPMODE_TX,
  APPMODE_RXSINGLE,
  APPMODE_RXSUCCESSIVE
}SX1281_AppMode_t;

typedef enum {
  RADIOMODE_BLE,         /* Bluetooth Low Energy */
  RADIOMODE_LORA,        /* Long Range */
  RADIOMODE_GFSK,        /* Gaussian Frequency Shift Keying */
  RADIOMODE_FLRC         /* Fast Long Range Codec */
}SX1281_RadioMode_t;

typedef enum {
  LOWPOWER,
  RUNNING,
  RX_DONE,
  RX_TIMEOUT,
  RX_ERROR,
  TX_DONE,
  TX_TIMEOUT,
  LORA_CAD_DONE            
}SX1281_States_t;


typedef enum{
  RF_BAUDRATE_BLE_250K=0,
  RF_BAUDRATE_BLE_500K,
  RF_BAUDRATE_BLE_1M,
  RF_BAUDRATE_BLE_USER_CUSTOM,  /* User defined custom parameters */
  RF_BAUDRATE_BLE_NUM,
}SX1281_BLEBaudrate_t;

typedef enum{
  RF_BAUDRATE_LORA_216=0, 
  RF_BAUDRATE_LORA_001K, 
  RF_BAUDRATE_LORA_005K, 
  RF_BAUDRATE_LORA_010K,    
  RF_BAUDRATE_LORA_020K, 
  RF_BAUDRATE_LORA_061K, 
  RF_BAUDRATE_LORA_127K, 
  RF_BAUDRATE_LORA_203K,
  RF_BAUDRATE_LORA_NUM,
}SX1281_LORABaudrate_t;

typedef enum{
  RF_BAUDRATE_GFSK_125K=0,
  RF_BAUDRATE_GFSK_250K,
  RF_BAUDRATE_GFSK_500K,
  RF_BAUDRATE_GFSK_1M,
  RF_BAUDRATE_GFSK_USER_CUSTOM, /* User defined custom parameters */
  RF_BAUDRATE_GFSK_NUM,
}SX1281_GFKSBaudrate_t;

typedef enum{
  RF_BAUDRATE_FLRC_130K=0,
  RF_BAUDRATE_FLRC_260K,
  RF_BAUDRATE_FLRC_520K,
  RF_BAUDRATE_FLRC_1040K,
  RF_BAUDRATE_FLRC_USER_CUSTOM, /* User defined custom parameters */
  RF_BAUDRATE_FLRC_NUM,
}SX1281_FLRCBaudrate_t;

typedef struct {
  SX1281_BLEBaudrate_t    ble;
  SX1281_LORABaudrate_t   lora;
  SX1281_GFKSBaudrate_t   gfks;
  SX1281_FLRCBaudrate_t   flrc;
}SX1281_Baudrate_t;

typedef struct {
  struct {
  uint8_t first[5];
  uint8_t second[5];
  uint8_t third[5];
  }gfsk;
 struct {
  uint8_t first[4];
  uint8_t second[4];
  uint8_t third[4];
 }flrc;
}SX1281_SyncWord_t;

typedef struct {
  enum{
  RadioRoleNONE=0,
  RadioRoleTX,
  RadioRoleRX,
  }RadioRole;

  SX1281_RadioMode_t radioMode;

  SX1281_SyncWord_t syncWord;
  PacketParams_t packetParams;
  
  ModulationParams_t modulationParams;

  uint64_t rfFrequency;//射频中心频率

  int8_t txOutputPower;//发射功率 The range of the output power is [-18..+13] dBm
  RadioRampTimes_t rampTime;//发射斜坡时间

  SX1281_Baudrate_t baudrate;//不同调制方式的波特率

}SX1281_Params_t;

typedef struct {
  SX1281_AppMode_t appMode;

  SX1281_Params_t *param;

  SX1281_States_t appState;

  PacketStatus_t packetStatus;

  int8_t rxBuffer[BUFFER_SIZE];
}SX1281_t;


/* Exported functions prototypes -------------------------------------------- */

/* USER FUNCTION BEGIN */
int8_t SX1281_Init(SX1281_t *radio, SX1281_Params_t *param,SX1281_RadioMode_t mode);

int8_t SX1281_SetMode(SX1281_t *radio, SX1281_RadioMode_t mode);
int8_t SX1281_SetRXSingle(SX1281_t *radio);
int8_t SX1281_SetRXSuccessive(SX1281_t *radio);
int8_t SX1281_SetTX (SX1281_t *radio,uint8_t *data,uint8_t data_length);
int8_t SX1281_Running(SX1281_t *radio);

int8_t SX1281_SetRFFrequency(SX1281_t *radio, uint32_t frequency);
int8_t SX1281_SetBLEBaudrate(SX1281_t *radio, SX1281_BLEBaudrate_t baudrate);
int8_t SX1281_SetLORABaudrate(SX1281_t *radio, SX1281_LORABaudrate_t baudrate);
int8_t SX1281_SetGFSKBaudrate(SX1281_t *radio, SX1281_GFKSBaudrate_t baudrate);
int8_t SX1281_SetFLRCBaudrate(SX1281_t *radio, SX1281_FLRCBaudrate_t baudrate);

/* Direct modulation parameter setting functions (auto-switch to USER_CUSTOM mode) */
int8_t SX1281_SetBLEModulationParams(SX1281_t *radio, RadioGfskBleBitrates_t bitrateBandwidth, 
                                     RadioGfskBleModIndexes_t modulationIndex, 
                                     RadioModShapings_t modulationShaping);
int8_t SX1281_SetGFSKModulationParams(SX1281_t *radio, RadioGfskBleBitrates_t bitrateBandwidth, 
                                      RadioGfskBleModIndexes_t modulationIndex, 
                                      RadioModShapings_t modulationShaping);

void SetBLEAdvertisingPacket(SX1281_t *radio, uint8_t *data, uint8_t length);
/* USER FUNCTION END */

#ifdef __cplusplus
}
#endif
