#pragma once

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ----------------------------------------------------------------- */
#include <stdint.h>
#include <stdbool.h>
#include "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 255



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

typedef enum {
  MODE_BLE,         /* Bluetooth Low Energy */
  MODE_LORA,        /* Long Range */
  MODE_GFSK,        /* Gaussian Frequency Shift Keying */
  MODE_FLRC         /* Fast Long Range Codec */
}SX1281_Mode_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_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_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_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 {
  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;//LORA,FLRC模式下不同调制方式的波特率

}SX1281_Params_t;

typedef struct {

  SX1281_Params_t param;

  SX1281_Mode_t mode;

  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_Mode_t mode);
int8_t SX1281_SetRXSingle(void);
int8_t SX1281_SetRXSuccessive(void);
int8_t SX1281_SetTX (uint8_t *data);
int8_t SX1281_Running(SX1281_t *radio);

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

#ifdef __cplusplus
}
#endif
