////////////////////////////////////////////////////////////////////////
//ModBluetooth V1.0 Test Code - www.microingenia.com        12/02/2010//
//                                                                    //
//Simple code to show how to use a communication over bluetooth.      //
//You must link your Bluetooth dongle with ModBluetooth first.        //
//Pswd: 0000, this catch an ascii to turn on/off led                  //
//                                                                    //
//Released under the cc by-nc-sa 3.0 License.                         //
//http://creativecommons.org/licenses/by-nc-sa/3.0/                   //
////////////////////////////////////////////////////////////////////////
#include <18F2550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,NOBROWNOUT,USBDIV,PLL5,CPUDIV1,VREGEN,PUT,MCLR
#use delay(clock=48000000)

// MiE USB HID Bootloader
#define CODE_START   0x1000
#build(reset=CODE_START, interrupt=CODE_START+0x08)
#org 0, CODE_START-1 {}

// Definitions for LEDs on Board
#define  ON    output_high
#define  OFF   output_low

// Connections in 18F4553Trainer
#define  LedV   PIN_B0
#define  LedR   PIN_B1
#define  Led   PIN_B2

// Connections between ModBluetooth and 18F4553Trainer
#define  Rx     PIN_C7
#define  Tx     PIN_C6

#use rs232(UART1, baud=9600, xmit=Tx, rcv=Rx, STREAM=BLUE)
#include "string.h"

int8 CR = 0x0d; // Carriage Return

void main()
{  
   ON(LedR);      // Red: No connection
   OFF(LedV);
   
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
      
   delay_ms(3000);
   fprintf(BLUE,"$$$");                // Get into command mode
      
   delay_ms(250);
   fprintf(BLUE,"SA,1");               // Enable authentication
   fputc(CR,BLUE);

   delay_ms(250);
   fprintf(BLUE,"SP,0000");            // Set Password
   fputc(CR,BLUE);
   
   delay_ms(250);
   fprintf(BLUE,"SN,ModBluetooth");    // Device name
   fputc(CR,BLUE);
   
   delay_ms(250);
   fprintf(BLUE,"SS,ModBluePort");     // Service name
   fputc(CR,BLUE);

   delay_ms(250);
   fprintf(BLUE,"ST,0");               // Enable vinculation
   fputc(CR,BLUE);                

   delay_ms(250);
   fprintf(BLUE,"---");                // Get out command mode
   fputc(CR,BLUE);
   
   ON(LedV);      // Green: Bluetooth ready! Link it now.
   OFF(LedR);
      
 
  while(true);

} 
#INT_RDA
void rda_isr(void)
{  
   char c;
   c = fgetc(blue);

   
   if(c == '$')
   {     
      ON(led);
   
  }   
   if(c == '%')
   {     
      OFF(led);
   }   
  
}

  