created separate function for hex to char

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of SS_SensePOC2P0_11Dec2017_USERPID by rashmi v

Revision:
52:7d9ff7781bdc
Parent:
48:f90edb35a20e
Child:
54:f2a413d5dffd
--- a/bt.cpp	Wed Sep 20 07:48:55 2017 +0000
+++ b/bt.cpp	Wed Sep 20 10:33:17 2017 +0000
@@ -3,7 +3,7 @@
 #include "struct.h"
 #include "eeprom_pgm.h"
 #include "main.h"
-
+ 
 #define ACK 0x00                                /// changed //16/06 #define all the values removing structure 
 #define START_OF_FILE       0x01
 #define END_OF_FILE         0x02
@@ -14,10 +14,10 @@
 #define NO_OF_SAMPLES       600                   //changed the buffer size from 150- 22/7/17- suhasini
 #define CHKSUM              0x80 
 #define STRUCTURE_LENGTH    26
-
+ 
 BLEMsg_info *bt_file;
 Point *myBleMsg;
-
+ 
 Serial mc(USBTX,USBRX);                  // serial terminal
 Serial blue(PTC4,PTC3);                 // communicating bluetooth through serial 
                  
@@ -33,8 +33,8 @@
 static uint32_t counter=0;
 static  uint32_t v1[NO_OF_SAMPLES];  
 uint8_t file=0;
-
-
+ 
+ 
 uint8_t bt_send(uint8_t state_tx)
 {
    
@@ -165,10 +165,10 @@
                         }   
                        
    
-
-
+ 
+ 
          }
-
+ 
    return state_rx;     
         
   }           
@@ -179,31 +179,33 @@
 uint8_t bt_receive (uint8_t state_rx)
 {
        
-unsigned char state_tx =1;      //state for transmitting 
-uint8_t ack_rx=0;                        // varibale to receive ack from the bluetooth 
-uint8_t sos_rx=0;              // sos received from app
-uint8_t eos_rx=0;              // eos received from app
-uint32_t length_rx=0;
-uint8_t chksum_rx=0;  
+unsigned char state_tx =1;                                                      //state for transmitting 
+uint8_t string_length_rx=0;
+char ack_rx[2];                                                                 // varibale to receive ack from the bluetooth 
+char chksum_rx[3]; 
+char btdata_received[17];
+ 
  
- while (blue.readable())                                            // reading from bluetooth app
-    {
-        blue.scanf("%02x", &sos_rx);
-        blue.scanf("%02x", &ack_rx);
-        blue.scanf("%08x", &length_rx);
-        blue.scanf("%02x", &eos_rx);
-        blue.scanf("%02x", &chksum_rx);
-        break;
-    } 
- //   mc.printf("%02x%02x%08x%02x%02x", sos_rx,ack_rx,length_rx,eos_rx,chksum_rx); 
+while (blue.readable()!= NULL)                                                    // reading string from bluetooth app
+   {
+       blue.scanf("%17s", btdata_received);
+       mc.printf("%s\r\n", btdata_received);
+
+    }                                 
+   
+        string_length_rx = strlen(btdata_received);                                // storing the length of the string received
+        memcpy(ack_rx, btdata_received+1, 2);                                       // reading the ack received
+        memcpy(chksum_rx, btdata_received+(string_length_rx-2), 3);                  // reading the checksum received
+        mc.printf("%s\r\n",ack_rx);
+        mc.printf("%s\r\n",chksum_rx);
     
      switch(state_rx)
          {
                 
-          case 1:                                       // wait for the ack
+          case 1:                                                                   // wait for the ack
                       state_rx=0;
                       myBleMsg->proto=ACK;
-                 if ((ack_rx == ACK) && (chksum_rx == CHKSUM))                    // check for ack and checksum
+                 if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL))                             // check for ack and checksum
                
                      {
                        
@@ -221,7 +223,7 @@
  
          case 2:      state_rx =0;                        //wait for the ack
                   
-                    if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
+                   if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL))  
                         {
                 
                           state_tx=3;
@@ -239,11 +241,11 @@
                         }
                     //blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
                     break;
-
+ 
                 
      case 3:       
                    state_rx =0;
-                 if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
+                if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL))  
                       {
                      
                          state_tx=3;
@@ -264,7 +266,7 @@
                                             
      case 4:       
                    state_rx =0;
-                 if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
+               if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL))  
                       {
                          state_tx=4;
                          count_send=count_send+1;
@@ -281,9 +283,9 @@
                       } 
                    break;                
                  
-
+ 
       case 5:      state_rx = 0;
-                if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
+               if ((strcmp(ack_rx ,"00") == NULL ) && (strcmp(chksum_rx ,"80" ) == NULL))  
                 {
                     sd_close();
                     increment_btpid();                // increment the bluetooth pid and write in the file
@@ -299,9 +301,9 @@
                        
                 }           
                 break;
-
+ 
   }
-
+ 
    return state_tx;          
   }           
    
@@ -344,7 +346,7 @@
     
 void send_initial_string()                // function to send the initial string at the start f communication to bluetooth
 {              
-
+ 
     uint8_t sum_init=0  ;               
     myBleMsg->start_of_string=SOS;
     myBleMsg->length_file=0;
@@ -370,7 +372,7 @@
   chksum_init = sum & 0x000000FF;
   return chksum_init;
   }
-
+ 
 uint8_t checksum_last()        // function to calculate the checksum for the last string
  {
   
@@ -406,7 +408,7 @@
     blue.printf("%02x",myBleMsg->start_of_string); 
     blue.printf("%02x",myBleMsg->proto);                   // chnaged
     blue.printf("%08x",myBleMsg->length_file);
-
+ 
 }
     
     
@@ -420,7 +422,7 @@
     
 }
     
-
+ 
 uint8_t checksum_struct(const uint8_t data[], uint16_t numBytes)         // calculating checksum for 
 {
     uint32_t chksum_struct = 0;
@@ -435,11 +437,11 @@
   // blue.printf("%08x\n",chksum_struct);  
   return chksum_struct;
 }
-
-
+ 
+ 
 uint8_t checksum_rawdata(const uint8_t data[], uint32_t numBytes)          // this function to calculate the checksum for the raw data
 {
-
+ 
     uint32_t sum=0;
     uint8_t  chksum_datatrfr=0;
     uint32_t index = 0;
@@ -449,19 +451,17 @@
     index1=(numBytes*4);
     sum +=(index1>>(8*0))&0xFF;
     sum +=(index1>>(8*1))&0xFF; 
-
+ 
     for (index=0U; index<(numBytes*4); index++)
     { 
     sum += data[index];
     }
-
+ 
     sum+=EOS;
-
+ 
     chksum_datatrfr = sum & 0x000000FF;
  //mc.printf("chksum_datatrfr=%02x\n",chksum_datatrfr);
     return chksum_datatrfr;
 }
-
-
-
-
+ 
+ 
\ No newline at end of file