#include "stm32f10x.h"
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include "Sx1276-1278.h"

#include "gpio.h"
#include "delay.h"
#include "spi.h"

#include "crc.h"

/**************************************************************************************************************************************
Demo 程序流程  EnableMaster=true  为主机端，主机端发送一个"PING"数据后切换到接收，等待从机返回的应答"PONG"数据LED闪烁

               EnableMaster=false 为从机端，从机端接收到主机端发过来的"PING"数据后LED闪烁并发送一个"PONG"数据作为应答
***************************************************************************************************************************************/
bool  EnableMaster=false;//主从选择 true为主 false 为从

tRadioDriver *Radio = 0;
const uint8_t PingMsg[] = "PING";
const uint8_t PongMsg[] = "PONG";
uint16_t num_rx=0;
uint8_t   RXBuffer[RF_BUFFER_SIZE];  //RX buffer			
uint8_t   TXBuffer[RF_BUFFER_SIZE];  //TX buffer
uint16_t  crc_value;

void HW_Int()//MCU外围资源初始化
{
  GPIO_int();
  SPI2_Int();
}


void LedToggle(void)
{
  //GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_RESET);//LED闪烁
  HAL_Delay_nMs (1);
  //GPIO_WriteBit( LED1_PORT, LED1_PIN,Bit_SET);
}

void lora_data_send(char *tx_data, uint32_t tx_len)
{
	memcpy(TXBuffer, tx_data, tx_len);
	crc_value=RadioComputeCRC(TXBuffer,4,CRC_TYPE_IBM);//计算得出要发送数据包CRC值
	TXBuffer[tx_len]=crc_value>>8;
	TXBuffer[tx_len + 1]= crc_value;

	Radio->SetTxPacket( TXBuffer, tx_len + 2);	
}
/*
 * Manages the master operation
 */
void OnMaster( void )
{  
	switch(Radio->Process())
	{
		case RF_TX_DONE:
			Radio->StartRx( ); 
			break;
		case RF_RX_DONE:
			Radio->GetRxPacket( RXBuffer, ( uint16_t* )&num_rx );

			if(num_rx > 0) {
				printf("master RF_RX_DONE  %s\r\n", RXBuffer);
				crc_value=RXBuffer[num_rx-2];
				crc_value<<=8;
				crc_value|=RXBuffer[num_rx-1];
				if(crc_value==RadioComputeCRC(RXBuffer,num_rx-2,CRC_TYPE_IBM)) {
					if(strncmp(( const char* )RXBuffer, ( const char* )PongMsg,4)==0) {
						LedToggle();//LED闪烁
						lora_data_send("PING", 4);
					}
				}
			}            
			break;
		case RF_RX_TIMEOUT:
			printf("RF_RX_TIMEOUT  \r\n");
			lora_data_send("PING", 4);
			break;
		default:
			break;
	}
}

/*
 * Manages the slave operation
 */
void OnSlave( void )
{
	switch(Radio->Process())
	{
		case RF_RX_DONE:
			Radio->GetRxPacket(RXBuffer,(uint16_t* )&num_rx );
			if(num_rx >0) {
				printf("slave RF_RX_DONE  %s\r\n", RXBuffer);
				crc_value=RXBuffer[num_rx-2];
				crc_value<<=8;
				crc_value|=RXBuffer[num_rx-1];
				if(crc_value==RadioComputeCRC(RXBuffer,num_rx-2,CRC_TYPE_IBM)) {
					if( strncmp( ( const char* )RXBuffer, ( const char* )PingMsg, 4 ) == 0 ) {
						LedToggle();//LED闪烁
						lora_data_send("PONG", 4);
					} 
				}
			}
			break;
		case RF_TX_DONE:
			Radio->StartRx( ); 
			break;
		default:
			break;
	}
}

int lora_test(void)
{
	static uint8_t init = 0;
	
	if(init == 0) {
		HW_Int();//MCU初始化
		Radio = RadioDriverInit();
		Radio->Init();
		if(EnableMaster == true) {
			lora_data_send("PING", 4);
		} else {
			Radio->StartRx();
		}
		init = 1;
	}

	if(EnableMaster == true) {		
		OnMaster();
	} else {
		OnSlave();
	}

	return 0;
}

void lora_init(void)
{
	Radio = RadioDriverInit();
	Radio->Init();
}
