#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT,BROWNOUT


#use delay (clock=4000000)

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PC_HW)

#byte PORTD = 0x08
#byte TRISD = 0x88
#byte TRISC = 0x87
#define LCDPORT   PORTD
#define LCDPORT_TRIS TRISD
#define LCDCONTROL_TRIS TRISC
#define RSLCD   PIN_C0
#define ENLCD   PIN_C1


void delay1(void);
void delay2(void);
void delay3(void);
void pulse(void);
void lcdInit(void);
void lcdClear(void);
void lcdGoto(unsigned char);
void lcdWrite(unsigned char);
int flagdata;
int flag = 1;
int flag2 = 0;
int flag3 = 0;
int p_index=0;
int8 phone[12];
int8 phone2[12];
int8 count=0;
int8 count2=0;
int8 response[15];
int8 password[6];

#INT_RDA
void receiveData()
{
   int j;
   int receiveTemp;
   receiveTemp = getc(PC_HW);
   //lcdWrite(receiveTemp);
   if(flagdata==1){
   if(count<25)
      count++;
   if(count==14 && receiveTemp==0xa)
      count=0;
   if(count==25 && count2<6){
   password[count2]=receiveTemp;
   count2++;
   }
   if(count2==6 && password[0]=='r' && password[1]=='e'&& password[2]=='d'&& password[3]=='c'&& password[4]=='a' && password[5]=='t'){
   putc('1',PC_HW);
   count2++;
   }
   else if(count2==6){
   putc('0',PC_HW);
   count2=0;
   count=0;
   }
   if(count==25 && count2==7){
   lcdWrite('L');
   lcdWrite('o');
   lcdWrite('a');
   lcdWrite('d');
   lcdWrite('i');
   lcdWrite('n');
   lcdWrite('g');
   lcdWrite(' ');
   lcdWrite('I');
   lcdWrite('m');
   lcdWrite('a');
   lcdWrite('g');
   lcdWrite('e');
   lcdWrite('.');
   lcdWrite('.');
   lcdWrite('.');
   count++;
   count2=0;
   }
   else if(count>25 && count2==0){
   //lcdWrite('?');
   PORTD = receiveTemp;
   if(receiveTemp=='$'){
   count=0;
   flagdata=0;
   }
   }
   }
   else{
   if(p_index>1 && p_index<14)
      response[p_index] = receiveTemp;
   
   p_index++;
   
   if(receiveTemp==0xa) 
      count++;
   
   if(count==2) {
      count=0;
      p_index=0;
   
   if(response[2]=='O' && response[3]=='K' && flag3==1){
   flag2=0;
   flag3=2;
   }
   else if(response[2]=='O' && response[3]=='K'){
   flag2=0;
   }
   else if(flag3==2 && count2==0){
   for(j=0; j<12;j++){
   phone[j] = response[j+2];
   }
   count2++;
   }
   else if(flag3==2 && count2==1){
   for(j=0; j<12;j++){
   phone2[j] = response[j+2];
   }
   count2++;
   }
   else if(response[2]=='D' && response[3]=='O' && response[4]=='N' && response[5]=='E'){
   flag3=0;
   count2=0;
   }
   else if(response[2]=='E' && response[3]=='R' && response[4]=='R' && response[5]=='O' && response[6]=='R'){
   flag3=0;
   count2=0;
   flag2=0;
   flag=0;
   }
   else if(response[2]=='C' && response[3]=='O' && response[4]=='N' && response[5]=='N' && response[6]=='E' && response[7]=='C' && response[8]=='T'){
   flag=0;
   }
   else if(response[2]=='N' && response[3]=='O' && response[5]=='A' && response[6]=='N' && response[7]=='S' && response[8]=='W' && response[9]=='E' && response[10]=='R'){
   flag=0;
   }
   }
   lcdWrite(receiveTemp);
   }
}

void delay1(void)
{
   int i;
   for(i = 0; i < 125; i++);
}

void delay2(void)
{
   int i;
   for(i = 0; i < 40; i++)
      delay1();
}

void delay3(void)
{
   int i;
   for(i = 0; i < 10; i++)
      delay2();
}

void pulse(void)
{
   output_high(ENLCD);
   delay_cycles(1);
   output_low(ENLCD);
   //ENLCD = 0;
   delay1();
}

void lcdInit(void)
{
   LCDPORT_TRIS = 0;
   LCDCONTROL_TRIS = 0;
   output_low(ENLCD);
   //ENLCD = 0;
   output_low(RSLCD);
   //RSLCD = 0;
   delay1();
   LCDPORT = 56;
   pulse();
   LCDPORT = 15;//1DCB
   pulse();
   LCDPORT = 1;//clear lcd and return the cursor to the home
   pulse();
   delay2();
}

void lcdClear(void)
{
   output_low(ENLCD);
   //ENLCD = 0;
   output_low(RSLCD);
   //RSLCD = 0;
   delay1();
   LCDPORT = 1;
   pulse();
   delay2();
}

void lcdGoto(unsigned char c)
{
   LCDPORT = c;
   output_low(RSLCD);
   //RSLCD = 0;
   delay2();
   pulse();
}

void lcdWrite(unsigned char c)
{
   LCDPORT = c;
   output_high(RSLCD);
   //RSLCD = 1;
   pulse();
}

void helper2(){
   flag2=1;
   putc('a',PC_HW);
   putc('t',PC_HW);
   putc('u',PC_HW);
   putc('c',PC_HW);
   putc('l',PC_HW);
   putc(0x0d,PC_HW);
   while(flag2!=0);
}

void helper(){
   flag2=1;
   putc('a',PC_HW);
   putc('t',PC_HW);
   putc(0x0d,PC_HW);
   while(flag2!=0);
}


void connect(char *p){
   int i;
   helper();
   helper2();
   flag=1;
   lcdWrite('G');
   putc('a',PC_HW);
   putc('t',PC_HW);
   putc('d',PC_HW);
   putc('m',PC_HW);
   putc(',',PC_HW);
   for(i=0; i<12; i++){
   putc(p[i],PC_HW);
   }
   putc(',',PC_HW);
   putc('1',PC_HW);
   putc('1',PC_HW);
   putc('0',PC_HW);
   putc('5',PC_HW);
   putc(0x0d,PC_HW);
   while(flag!=0);
   lcdWrite('L');
}

void disconnect(){

   putc('+',PC_HW);
   putc('+',PC_HW);
   putc('+',PC_HW);
   putc(0x0d,PC_HW);
   delay_ms(500);
   putc('a',PC_HW);
   putc('t',PC_HW);
   putc('d',PC_HW);
   putc('h',PC_HW);
   putc(0x0d,PC_HW);
}

void connectObex(int *p){

   int i;
   for(i=0; i<7; i++)
      putc(p[i],PC_HW);  
}
void broadcast(){
   int i = 0;
   helper();
   helper2();
   flag2=1;
   flag3=1;
   putc('a', PC_HW);
   putc('t', PC_HW);
   putc('d', PC_HW);
   putc('i', PC_HW);
   putc(',', PC_HW);
   putc('2', PC_HW);
   putc(',', PC_HW);
   for(; i<8; i++)
      putc('0', PC_HW);
   putc(0xd, PC_HW);
   while(flag2!=0);
   while(flag3!=0);
}

void main() {
   int i=0;
   int16 j=0;
   int message[] = {0x80, 0x0, 0x7, 0x10, 0x0, 0x20, 0x0};
   const int card1[70] = {0x82, 0x01, 0x79, 0x01, 0x00, 0x15, 0x00, 
                        0x64, 0x00, 0x65, 0x00, 0x6d, 0x00, 0x6f, 
                        0x00, 0x2e, 0x00, 0x76, 0x00, 0x63, 0x00, 
                        0x73, 0x00, 0x00, 0x49, 0x01, 0x61, 0x42, 
                        0x45, 0x47, 0x49, 0x4e, 0x3a, 0x56, 0x43, 
                        0x41, 0x4c, 0x45, 0x4e, 0x44, 0x41, 0x52, 
                        0x0d, 0x0a, 0x56, 0x45, 0x52, 0x53, 0x49, 
                        0x4f, 0x4e, 0x3a, 0x32, 0x2e, 0x30, 0x0d, 
                        0x0a, 0x50, 0x52, 0x4f, 0x44, 0x49, 0x44, 
                        0x3a, 0x2d, 0x2f, 0x2f, 0x72, 0x45, 0x44};
                        
   const int card2[70] = {0x63, 0x41, 0x54, 0x2f, 0x2f, 0x4e, 0x4f, 
                        0x4e, 0x53, 0x47, 0x4d, 0x4c, 0x20, 0x76, 
                        0x31, 0x2e, 0x30, 0x2f, 0x2f, 0x45, 0x4e, 
                        0x0d, 0x0a, 0x42, 0x45, 0x47, 0x49, 0x4e, 
                        0x3a, 0x56, 0x45, 0x56, 0x45, 0x4e, 0x54, 
                        0x0d, 0x0a, 0x44, 0x54, 0x53, 0x54, 0x41, 
                        0x52, 0x54, 0x3a, 0x32, 0x30, 0x30, 0x37, 
                        0x30, 0x36, 0x30, 0x38, 0x54, 0x31, 0x38, 
                        0x34, 0x37, 0x31, 0x37, 0x5a, 0x0d, 0x0a, 
                        0x44, 0x54, 0x45, 0x4e, 0x44, 0x3a, 0x32};
                        
   const int card3[70] = {0x30, 0x30, 0x37, 0x30, 0x36, 0x30, 0x38, 
                        0x54, 0x31, 0x38, 0x34, 0x37, 0x31, 0x37, 
                        0x5a, 0x0d, 0x0a, 0x53, 0x55, 0x4d, 0x4d, 
                        0x41, 0x52, 0x59, 0x3a, 0x49, 0x74, 0x20, 
                        0x69, 0x73, 0x20, 0x61, 0x20, 0x74, 0x65,
                        0x73, 0x74, 0x20, 0x64, 0x61, 0x74, 0x61,
                        0x20, 0x61, 0x6e, 0x64, 0x20, 0x76, 0x63, 
                        0x61, 0x6c, 0x65, 0x6e, 0x64, 0x61, 0x72, 
                        0x20, 0x73, 0x75, 0x62, 0x6a, 0x65, 0x63, 
                        0x74, 0x2e, 0x20, 0x53, 0x65, 0x6e, 0x64};
                        
   const int card4[70] = {0x65, 0x64, 0x20, 0x62, 0x79, 0x20, 0x72, 
                        0x45, 0x44, 0x63, 0x41, 0x54, 0x21, 0x20, 
                        0x20, 0x0d, 0x0a, 0x44, 0x45, 0x53, 0x43, 
                        0x52, 0x49, 0x50, 0x54, 0x49, 0x4f, 0x4e, 
                        0x3a, 0x44, 0x45, 0x53, 0x43, 0x52, 0x49, 
                        0x50, 0x54, 0x49, 0x4f, 0x4e, 0x20, 0x31, 
                        0x20, 0x44, 0x45, 0x53, 0x43, 0x52, 0x49, 
                        0x50, 0x54, 0x49, 0x4f, 0x4e, 0x20, 0x31, 
                        0x44, 0x45, 0x53, 0x43, 0x52, 0x49, 0x50, 
                        0x54, 0x49, 0x4f, 0x4e, 0x20, 0x31, 0x20};
                        
   const int card5[70] = {0x44, 0x45, 0x53, 0x43, 0x52, 0x49, 0x50, 
                        0x54, 0x49, 0x4f, 0x4e, 0x20, 0x31, 0x44, 
                        0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 
                        0x69, 0x6f, 0x6e, 0x20, 0x32, 0x20, 0x44, 
                        0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 
                        0x69, 0x6f, 0x6e, 0x20, 0x32, 0x20, 0x44, 
                        0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 
                        0x69, 0x6f, 0x6e, 0x20, 0x32, 0x20, 0x44, 
                        0x65, 0x73, 0x63, 0x72, 0x69, 0x70, 0x74, 
                        0x69, 0x6f, 0x6e, 0x20, 0x32, 0x0d, 0x0a};
                        
   const int card6[27] = {0x45, 0x4e, 0x44, 0x3a, 0x56, 0x45, 0x56, 
                        0x45, 0x4e, 0x54, 0x0d, 0x0a, 0x45, 0x4e, 
                        0x44, 0x3a, 0x56, 0x43, 0x41, 0x4c, 0x45, 
                        0x4e, 0x44, 0x41, 0x52, 0x0d, 0x0a};
                        
   delay_ms(50);
   lcdInit();
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_RDA);
   count=0;
   delay_ms(500);
   flagdata=1;
   while(flagdata!=0);
   lcdInit();
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_RDA);
   delay_ms(500);
   disconnect();
   delay_ms(500);
   broadcast();
   delay_ms(500);
   while(i<2){
   
   if(i==1){
   connect(phone2);
   }
   
   else if(i==0){
   connect(phone);
   }
   i++;
   
   //lcdWrite('?');
   
   connectObex(message);
   delay_ms(500);
   for(j=0; j<70; j++)
      putc(card1[j],PC_HW);
   for(j=0; j<70; j++)
      putc(card2[j],PC_HW);
   for(j=0; j<70; j++)
      putc(card3[j],PC_HW);
   for(j=0; j<70; j++)
      putc(card4[j],PC_HW);
   for(j=0; j<70; j++)
      putc(card5[j],PC_HW);
   for(j=0; j<27; j++)
      putc(card6[j],PC_HW);
      
   delay_ms(500);
   disconnect();
   delay_ms(500);
}
//}
   while(TRUE)
   {
      output_high(PIN_A2);
      output_high(PIN_D0);
      delay_ms(500);
      output_LOW(PIN_D0);
      delay_ms(500);
      
   }
}

