#include "main.h"

#define SEND_MODE 1

const uint8_t SendMsg[] = "SendTest_1";
#define SendMsgSIZE 10

int nums = 0;
PacketStatus_t PacketStatus;

void radio_init()
{
  ModulationParams_t modulationParams;
  modulationParams.PacketType = PACKET_TYPE_LORA;
  modulationParams.Params.LoRa.SpreadingFactor = LORA_SF10;
  modulationParams.Params.LoRa.Bandwidth = LORA_BW_1600;
  modulationParams.Params.LoRa.CodingRate = LORA_CR_4_7;

  packetParams.PacketType = PACKET_TYPE_LORA;
  packetParams.Params.LoRa.PreambleLength = 20;
  packetParams.Params.LoRa.HeaderType = LORA_PACKET_VARIABLE_LENGTH;
  packetParams.Params.LoRa.PayloadLength = BUFFER_SIZE;
  packetParams.Params.LoRa.CrcMode = LORA_CRC_ON;
  packetParams.Params.LoRa.InvertIQ = LORA_IQ_NORMAL;
  
  Radio.SetStandby( STDBY_XOSC );			//���ô���ģʽ   STDBY_RC / STDBY_XOSC
  Radio.WriteRegister(0x093C,0x1);    //In all cases 0x1 must be written to the Frequency Error Compensation mode register 0x093C
  
      switch(modulationParams.Params.LoRa.SpreadingFactor){
        case LORA_SF5:
        case LORA_SF6:
            Radio.WriteRegister(0x0925,0x1E);
            break;
        case LORA_SF7:
        case LORA_SF8:
            Radio.WriteRegister(0x0925,0x37);
            break;
        case LORA_SF9:
        case LORA_SF10:
        case LORA_SF11:
        case LORA_SF12:
            Radio.WriteRegister(0x0925,0x32);
            break;
    }
      
  Radio.SetPacketType( modulationParams.PacketType );			//�趨��װ����PACKET_TYPE_GFSK,PACKET_TYPE_LORA,PACKET_TYPE_FLRC, PACKET_TYPE_BLE
  Radio.SetModulationParams( &modulationParams );					//���õ��Ʋ����� �������������Ʋ����Ľṹ��
  Radio.SetPacketParams( &packetParams );									//�������ݰ��������������������ݰ������Ľṹ��
  Radio.SetRfFrequency( RF_FREQUENCY );										//������ƵƵ�ʣ�������Ƶ��ֵ
  Radio.SetBufferBaseAddresses( 0x00, 0x00 );							//���÷��ͺͽ��յ����ݻ�������ַ
  Radio.SetTxParams( TX_OUTPUT_POWER, RADIO_RAMP_02_US );	//���÷��Ͳ���  ����
}

int main(void)
{	
	HwInit();

  printf("\r\n");
  printf("/***********************************************\r\n");
  printf("************************************************\r\n");
  printf("*               SX1281 LoRa_RX                 *\r\n");
  printf("* version:%-37s*\r\n",FACTORY_TEST_VERSION);
	printf("* compile time:%13s-%-18s*\r\n",__DATE__,__TIME__);
	printf("************************************************\r\n");
	printf("************************************************/\r\n"); 
  
  Radio.Init( &Callbacks );
  Radio.SetRegulatorMode( USE_DCDC ); // Can also be set in LDO mode but consume more power
  memset( &Buffer, 0x00, BufferSize );
  radio_init();
  printf("The version: 0x%x\r\n",Radio.GetFirmwareVersion());
  Radio.SetDioIrqParams(RxIrqMask,IRQ_RADIO_NONE,IRQ_RADIO_NONE,IRQ_RADIO_NONE);
#if (0 == SEND_MODE)
  Radio.SetRx( ( TickTime_t ) { RX_TIMEOUT_TICK_SIZE, 0xFFFF } );
#elif (1 == SEND_MODE)
  memcpy(Buffer, SendMsg, SendMsgSIZE);
  Radio.SendPayload(Buffer, BufferSize, (TickTime_t){RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE});
#endif
  while(1)
  {
    while(!Radio.GetIrqStatus())
    {
      Delay_ms(1);
    }
    SX1281ProcessIrqs();  
  }
}


void OnTxDone( void )
{
  AppState = APP_TX;
  printf("<>>>>>>>>OnTxDone\r\n");

#if (1 == SEND_MODE)
  Radio.SendPayload((uint8_t *)"12345", 5, (TickTime_t){RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE});
#endif
  
}

void OnRxDone( void )
{
  AppState = APP_RX;
  nums++;
  BufferSize = 0;
  printf("<>>>>>>>>OnRxDone��Order number: %d\n",nums);
}

void OnTxTimeout( void )
{
  AppState = APP_TX_TIMEOUT;
  printf( "<>>>>>>>>TX_TIMEOUT\n\r" ); 
  Radio.SendPayload((uint8_t*)"12345",10, ( TickTime_t ){ RX_TIMEOUT_TICK_SIZE, TX_TIMEOUT_VALUE });

}

void OnRxTimeout( void )
{
  AppState = APP_RX_TIMEOUT;
  printf( "<>>>>>>>>OnRxTimeout\n\r" );
}

void OnRxError( IrqErrorCode_t errorCode )
{
  AppState = APP_RX_ERROR;
  printf( "RXE<>>>>>>>>\n\r" );
}

void OnCadDone( bool channelActivityDetected )
{
  printf( "<>>>>>>>>OnCadDone\n\r" );
}

