////////////////////////////////////////////////////////////////////////////////
//                        Laipac RF-24G / TXRX24G
//                2.4GHz Wireless Transceiver Serial Link
//
// Based upon original by micro222@yahoo.com
//
// Filename     : 18LF2550 RF-24G Serial.c
// Programmer   : Steven Cholewiak, www.semifluid.com
// Version      : Version 1.0 - 01/30/2006
// Remarks      : Datasheets for RF-24G / TXRX24G are available from:
//                http://www.sparkfun.com/datasheets/RF/RF-24G_datasheet.pdf
//                http://www.sparkfun.com/datasheets/RF/RF-24G.pdf
//                http://www.sparkfun.com/datasheets/RF/nRF2401rev1_1.pdf
//                http://store.qkits.com/moreinfo.cfm/txrx24g.pdf
//
//                More information on the circuit can be found at:
//                http://www.semifluid.com/PIC18LF2550_RF24G_serial.html
////////////////////////////////////////////////////////////////////////////////

#include <18F2550.h>
#device ADC=10
#use delay(clock=10000000)

#build(reset=0x1, interrupt=0x8)          // Necessary for Bootloader
#ORG 0x0F00,0x0FFF {}                     // Necessary for Bootloader

#use rs232(stream=PC, baud=57600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <RF-24G.c>

#define BUFFER_SIZE 256                   // RF transmit buffer
int8 q[BUFFER_SIZE];                      // RF transmit buffer
int8 i=0;                                 // Add pointer
int8 j=0;                                 // Take pointer
int8 k=0;
int8 unsent_bytes = 0;

#INT_RDA
void serial_isr()                         // Serial Interrupt
{
   int8 uReceive;

   disable_interrupts(INT_RDA);           // Disable Serial Recieve Interrupt
   disable_interrupts(GLOBAL);            // Disable Global Interrupts

   uReceive = fgetc(PC);

   switch (uReceive) {
      case 0x12: {
            if (fgetc(PC) == 0x34 & fgetc(PC) == 0x56 & fgetc(PC) == 0x78 & fgetc(PC) == 0x90) #asm reset #endasm
         }
         break;
      default: {
            if(unsent_bytes + 1 < BUFFER_SIZE){
               q[i] = uReceive;
               i++;
               if(i >= BUFFER_SIZE) i = 0;
               unsent_bytes++;
            }
         }
         break;
   }

   enable_interrupts(INT_RDA);            // Enable Serial Recieve Interrupt
   enable_interrupts(GLOBAL);             // Enable Global Interrupts
}

void main() {
   SETUP_ADC_PORTS(NO_ANALOGS);
   SETUP_ADC(ADC_OFF);
   SETUP_TIMER_0(RTCC_INTERNAL|RTCC_DIV_1);
   SETUP_TIMER_1(T1_DISABLED);
   SETUP_TIMER_2(T2_DISABLED, 127, 1);
   SETUP_TIMER_3(T3_DISABLED);
   SETUP_CCP1(CCP_OFF);
   SETUP_CCP2(CCP_OFF);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);

   printf("Initializing Port B...\n\r");
   RF_24G_initPorts();
   printf("Configuring RF-24G...\n\r");
   RF_24G_Config();                       // Set up transceiver
   RF_24G_SetRx();                        // Switch to receive
   buf[0] = 'A';
   buf[1] = 'B';                          // Unused in current implementation
   printf("OK!\n\r");

   while(1) {
      if(INPUT(RF_24G_DR1)){
         getBuffer();                     // Get packet
         putc(buf[0]);                    // Put character on serial line
      }

      if(i!=j){                           // If there are characters to transmit
         buf[0] = q[j];
         j++;
         if(j>=BUFFER_SIZE)j=0;
         unsent_bytes--;
         
         RF_24G_SetTx();                  // Switch to transmit
         delay_ms(1);                     // Delay needed for transceiver
         putBuffer();                     // Send packet
         delay_ms(1);                     // Delay needed for transceiver
         RF_24G_SetRx();                  // Switch to receive
         delay_ms(1);                     // Delay needed for transceiver
      }
   }
}
