/*
==========================未经授权，转载需保留此信息============================
Function : MAIN.C file Operation for cc1101
Taobao website:http://zeyaotech.taobao.com/
Official website:http://www.ashining.com/
Written by:Lijian
TEL:028-64891123(Sales),028-64891129(Technology),e-mail: service@ashining.com
================================================================================
*/

#include "main.h"
#include "CC1101.h"
#include "board.h"
#include "OLED.h"

volatile INT8U rxBuffer[10] = {0}, i;
INT8U txBuffer[5] = {0,1,2,3,4 };
INT8U testbuffer[32] = {'0','0','0','0','0', 0};
INT16U rx_couter = 0, itmp;
INT32U x;


void display_rx(void)
{
	  LCD_Init( );            //OLED初始化
    LCD_Dis_Logo( );
    LCD_Dis_Str( 2,24,"STM8 board" );
    LCD_Dis_Str( 4,0,"www.ashining.com" );
    LCD_Dis_Str( 6,0,"CC1101:RX " );
    LCD_Dis_Str( 6, 80, (char*)testbuffer );
}
/*
void USART_Initial(void )

{
	CLK_PeripheralClockConfig(CLK_Peripheral_USART, ENABLE);
	USART_DeInit();
	USART_Init(9600,USART_WordLength_8D,USART_StopBits_1,USART_Parity_No,USART_Mode_Tx | USART_Mode_Rx);
	
	USART_Cmd(ENABLE);
	
	GPIO_Init( GPIOC, GPIO_Pin_2,GPIO_Mode_In_PU_No_IT );
	GPIO_Init( GPIOC, GPIO_Pin_3, GPIO_Mode_Out_PP_High_Slow );
	
	
}
*/
void DISPLAY_COUNTER(void)
{
	//Display the received count
            rx_couter ++;
            itmp = rx_couter;
            testbuffer[0] = ( itmp / 10000 ) + '0';
            itmp %= 10000;
            testbuffer[1] = ( itmp / 1000 ) + '0';
            itmp %= 1000;
            testbuffer[2] = ( itmp / 100 ) + '0';
            itmp %= 100;
            testbuffer[3] = ( itmp / 10 ) + '0';
            itmp %= 10;
            testbuffer[4] = itmp + '0';
            testbuffer[5] = 0;
            LCD_Dis_Str( 6, 80, (char*)testbuffer );
}

void RCV_PKT(void)
{
	     GS_CC1101ClrRXBuff();
	     GS_CC1101SetTRMode( RX_MODE );
        while( GPIO_ReadInputDataBit( GPIOB, GPIO_Pin_3 ) != 0 );
        while( GPIO_ReadInputDataBit( GPIOB, GPIO_Pin_3 )  == 0);
        //如果接收到数据包，LED状态翻转一次

/*
        i = GS_CC1101RecPacket( rxBuffer );
        if( i == 18 )
        {
					  for( i = 0; i < 18; i ++ )
								{
								  while((USART_GetFlagStatus(USART_FLAG_TXE)) != SET);
	                USART_SendData8(rxBuffer[i]);
								}
				}
*/				
				
}

main()
{
    

    SPI_Initial( );
    GPIO_Initial( );
//		USART_Initial();   
    display_rx();		
		GS_CC1101Init(  );                        // Write RF settings to config reg
    GS_CC1101SetTRMode( RX_MODE );

    while( 1 )
    {
            //receive packet
						RCV_PKT();
						
            LED_Toggle( );
						///send packet
            //GS_CC1101SetTRMode( TX_MODE );
            //GS_CC1101SendPacket( txBuffer,5, ADDRESS_CHECK );
						///counter++
            DISPLAY_COUNTER();
            
        
    }
}