Changes done in ECG and BT

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of merged_code2_20sept_2017_4th_oct_2017 by nikita teggi

Revision:
45:067673dae242
Parent:
44:05dbea558c9a
Child:
46:162ed3f051b2
diff -r 05dbea558c9a -r 067673dae242 bt.cpp
--- a/bt.cpp	Tue Sep 12 04:16:41 2017 +0000
+++ b/bt.cpp	Tue Sep 12 10:08:04 2017 +0000
@@ -1,19 +1,19 @@
-
 #include "mbed.h"
 #include "sdcard.h"
 #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
-#define DATA_TRFR 0x03
-#define NACK 0xFF
-#define SOS 0xc0
-#define EOS 0xc0
-#define NO_OF_SAMPLES 600   //changed the buffer size from 150- 22/7/17- suhasini
-
+#define ACK 0x00                                /// changed //16/06 #define all the values removing structure 
+#define START_OF_FILE       0x01
+#define END_OF_FILE         0x02
+#define DATA_TRFR           0x03
+#define NACK                0xFF
+#define SOS                 0xc0
+#define EOS                 0xc0
+#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;
@@ -44,23 +44,18 @@
     int8_t check_sum=0;
     uint8_t state_rx =0;
  
-  
-
-  
-
-    switch(state_tx)
+  switch(state_tx)
           {
  
        case 1:    
                   state_tx =0; 
                   counter=0;
                   total_file_size_sent=0; 
-                  file_start=0;          
-                 // j=0; 
-                 count_send=0;  total_samples=0;  
+                  file_start=0;           
+                  count_send=0;  total_samples=0;  
                   file_pointer_position=0;
-                 // mc.printf("%d %d\n",current_file,  bt_file_tosend);
-                if(get_filepid()>get_btpid())
+    
+                if(get_filepid()>get_btpid())                           //check for pid and bluetooth pid by reading from file
                 {
                     send_initial_string(); 
                     state_rx = 1;
@@ -69,7 +64,7 @@
                          
                 else 
                 {
-                    state_rx =0;
+                    state_rx =0;                                        //no files to send
                     mc.printf("no file to send\n");  
                 }       
                 break;
@@ -77,11 +72,11 @@
                    
        case 2:   total_samples=0; 
                  file=sd_open_read(get_btpid());  
-                 total_file_size=file_size() ;             // determined the file size 
-                 sd_read1(bt_file1);               // read the sd card file
+                 total_file_size=file_size() ;                           // determined the file size 
+                 sd_read1(bt_file1);                                    // read the sd card file
                  mc.printf("sd file opened to read data in it\n");
-                 myBleMsg->bt_msg=bt_file1;         //copy the contents to the structure
-                 send_structure();                                // send the initial string
+                 myBleMsg->bt_msg=bt_file1;                              //copy the contents to the structure
+                 send_structure();                                       // send the structure string
                  blue.printf("\n");   
                  mc.printf("sent initial string\n");                    
                  total_samples= myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp;
@@ -135,7 +130,7 @@
                           send_data_str2(check_sum);
                     }   
                      
-                       total_file_size_sent=file_size1();
+                        total_file_size_sent=file_size1();
                         blue.printf("\n");  
                   break;        
                
@@ -182,14 +177,14 @@
 uint8_t bt_receive (uint8_t state_rx)
 {
        
- unsigned char state_tx =1;      //state for transmitting 
+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;  
  
- while (blue.readable())
+ while (blue.readable())                                            // reading from bluetooth app
     {
         blue.scanf("%02x", &sos_rx);
         blue.scanf("%02x", &ack_rx);
@@ -206,7 +201,7 @@
           case 1:                                       // wait for the ack
                       state_rx=0;
                       myBleMsg->proto=ACK;
-                 if (ack_rx == ACK)
+                 if ((ack_rx == ACK) && (chksum_rx == CHKSUM))                    // check for ack and checksum
                
                      {
                        
@@ -224,7 +219,7 @@
  
          case 2:      state_rx =0;                        //wait for the ack
                   
-                    if (ack_rx == ACK)
+                    if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
                         {
                 
                           state_tx=3;
@@ -245,12 +240,12 @@
                 
      case 3:       
                    state_rx =0;
-                 if (ack_rx == ACK)
+                 if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
                       {
                      
-                        state_tx=3;
+                         state_tx=3;
                          count_send=count_send+1;
-                       file_pointer_position=total_file_size_sent;
+                         file_pointer_position=total_file_size_sent;
                       }
                     
                   else 
@@ -266,11 +261,11 @@
                                             
      case 4:       
                    state_rx =0;
-                 if (ack_rx == ACK)
+                 if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
                       {
-                        state_tx=4;
+                         state_tx=4;
                          count_send=count_send+1;
-                        file_pointer_position=total_file_size_sent;
+                         file_pointer_position=total_file_size_sent;
                       }
                     
                   else 
@@ -285,10 +280,10 @@
                  
 
       case 5:      state_rx = 0;
-                if (ack_rx == ACK)
+                if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
                 {
                     sd_close();
-                    increment_btpid();
+                    increment_btpid();                // increment the bluetooth pid and write in the file
                     mc.printf("next file");       
                     state_tx=1;
                           
@@ -314,31 +309,31 @@
              
 void send_structure()                                 // function to send the structure to bluetooth
 {
-                myBleMsg->start_of_string=SOS;
-                myBleMsg->length_file=26;
-                myBleMsg->proto=DATA_TRFR;                      // data transfer mode
-                blue.printf("%02x",myBleMsg->start_of_string); 
-                blue.printf("%02x",myBleMsg->proto);
-                blue.printf("%08x",myBleMsg->length_file);      //changed the sequence 
-                blue.printf("%08x",myBleMsg->bt_msg.length); 
-                blue.printf("%02x",myBleMsg->bt_msg.device_id); 
-                blue.printf("%08x",myBleMsg->bt_msg.patient_id);    
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.date);     
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.month);  
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.year);  
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.hour);      
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.mins);  
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.sec);  
-                blue.printf("%02x",myBleMsg->bt_msg.test_type);   
-                blue.printf("%04x",myBleMsg->bt_msg.sampling_freq); 
-                blue.printf("%04x",myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy); 
-                blue.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp); 
-                blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy);   
-                blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp); \
-                myBleMsg->end_of_string=EOS;  
-                myBleMsg->chk_sum = checksum_struct((uint8_t *)myBleMsg, sizeof(Point));
-                blue.printf("%02x",myBleMsg->end_of_string);
-                blue.printf("%02x",myBleMsg->chk_sum);
+    myBleMsg->start_of_string=SOS;
+    myBleMsg->length_file = STRUCTURE_LENGTH;
+    myBleMsg->proto=DATA_TRFR;                      // data transfer mode
+    blue.printf("%02x",myBleMsg->start_of_string); 
+    blue.printf("%02x",myBleMsg->proto);
+    blue.printf("%08x",myBleMsg->length_file);      //changed the sequence 
+    blue.printf("%08x",myBleMsg->bt_msg.length); 
+    blue.printf("%02x",myBleMsg->bt_msg.device_id); 
+    blue.printf("%08x",myBleMsg->bt_msg.patient_id);    
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.date);     
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.month);  
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.year);  
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.hour);      
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.mins);  
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.sec);  
+    blue.printf("%02x",myBleMsg->bt_msg.test_type);   
+    blue.printf("%04x",myBleMsg->bt_msg.sampling_freq); 
+    blue.printf("%04x",myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy); 
+    blue.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp); 
+    blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy);   
+    blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp); 
+    myBleMsg->end_of_string=EOS;  
+    myBleMsg->chk_sum = checksum_struct((uint8_t *)myBleMsg, sizeof(Point));
+    blue.printf("%02x",myBleMsg->end_of_string);
+    blue.printf("%02x",myBleMsg->chk_sum);
                
     
     }
@@ -346,19 +341,18 @@
 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;
-                myBleMsg->proto=START_OF_FILE;
-               //  myBleMsg->bt_msg=NULL;
-                sum_init=checksum_init();
-                myBleMsg->chk_sum=sum_init;
-                blue.printf("%02x",myBleMsg->start_of_string);
-                blue.printf("%02x",myBleMsg->proto); 
-                blue.printf("%08x",myBleMsg->length_file);     // chnged
-                myBleMsg->end_of_string=EOS;  
-                blue.printf("%02x",myBleMsg->end_of_string);
-                blue.printf("%02x",myBleMsg->chk_sum);
+    uint8_t sum_init=0  ;               
+    myBleMsg->start_of_string=SOS;
+    myBleMsg->length_file=0;
+    myBleMsg->proto=START_OF_FILE;
+    sum_init=checksum_init();
+    myBleMsg->chk_sum=sum_init;
+    blue.printf("%02x",myBleMsg->start_of_string);
+    blue.printf("%02x",myBleMsg->proto); 
+    blue.printf("%08x",myBleMsg->length_file);     // chnged
+    myBleMsg->end_of_string=EOS;  
+    blue.printf("%02x",myBleMsg->end_of_string);
+    blue.printf("%02x",myBleMsg->chk_sum);
     
     }
   
@@ -366,70 +360,61 @@
 uint8_t checksum_init()          // function to calculate the checksum for the initial string
  {
   
-  uint32_t sum=0;
-  uint8_t chksum_init=0;
+  uint32_t sum = 0;
+  uint8_t chksum_init = 0;
   sum=SOS+EOS+START_OF_FILE; 
-  chksum_init=sum & 0x000000FF;
+  chksum_init = sum & 0x000000FF;
   return chksum_init;
   }
 
 uint8_t checksum_last()        // function to calculate the checksum for the last string
  {
   
-  uint32_t sum=0;
-  uint8_t chksum_last=0;
+  uint32_t sum = 0;
+  uint8_t chksum_last = 0;
   sum=SOS+EOS+END_OF_FILE; // changed //16/06 #define all the values removing structure 
   chksum_last=sum & 0x000000FF;
   return chksum_last;
   }    
     
- void send_last_string()                 // function to send the last string to the bluetooth // which determines the end of file
+void send_last_string()                 // function to send the last string to the bluetooth // which determines the end of file
 {
-                uint8_t sum_last=0;
-                myBleMsg->start_of_string=SOS;
-                myBleMsg->length_file=0;
-                myBleMsg->proto=END_OF_FILE;
-               //  myBleMsg->bt_msg=NULL;
-                sum_last=checksum_last();
-                myBleMsg->chk_sum=sum_last;
-                blue.printf("%02x",myBleMsg->start_of_string);
-                blue.printf("%02x",myBleMsg->proto); 
-                blue.printf("%08x",myBleMsg->length_file);     // chnged
-                 mc.printf("%02x",myBleMsg->start_of_string);
-                mc.printf("%02x",myBleMsg->proto); 
-               mc.printf("%08x",myBleMsg->length_file); 
-                myBleMsg->end_of_string=EOS;  
-                blue.printf("%02x",myBleMsg->end_of_string);
-                blue.printf("%02x",myBleMsg->chk_sum);
-                 mc.printf("%02x",myBleMsg->end_of_string);
-               mc.printf("%02x",myBleMsg->chk_sum);
-    
-    }
+    uint8_t sum_last=0;
+    myBleMsg->start_of_string = SOS;
+    myBleMsg->length_file = 0;
+    myBleMsg->proto=END_OF_FILE;
+    sum_last=checksum_last();
+    myBleMsg->chk_sum=sum_last;
+    blue.printf("%02x",myBleMsg->start_of_string);
+    blue.printf("%02x",myBleMsg->proto); 
+    blue.printf("%08x",myBleMsg->length_file);     // chnged
+    myBleMsg->end_of_string=EOS;  
+    blue.printf("%02x",myBleMsg->end_of_string);
+    blue.printf("%02x",myBleMsg->chk_sum);
+                
+}
        
- void send_data_str1()       // function to send a part of the structure // after this raw data is sent//
-   {
-                 myBleMsg->start_of_string=SOS;
-                 myBleMsg->length_file=(counter*4);
-                 myBleMsg->proto=DATA_TRFR;
-                 blue.printf("%02x",myBleMsg->start_of_string); 
-                 blue.printf("%02x",myBleMsg->proto);                   // chnaged
-                 blue.printf("%08x",myBleMsg->length_file);
-                 mc.printf("%02x",myBleMsg->start_of_string); 
-                 mc.printf("%02x",myBleMsg->proto);                   // chnaged
-                 mc.printf("%08x",myBleMsg->length_file);
-    }
+void send_data_str1()       // function to send a part of the structure // after this raw data is sent//
+{
+    myBleMsg->start_of_string=SOS;
+    myBleMsg->length_file=(counter*4);
+    myBleMsg->proto=DATA_TRFR;
+    blue.printf("%02x",myBleMsg->start_of_string); 
+    blue.printf("%02x",myBleMsg->proto);                   // chnaged
+    blue.printf("%08x",myBleMsg->length_file);
+
+}
     
     
     
- void send_data_str2(uint8_t check_sum)          // after raw data// this last part of structure is sent
-    {          
-                myBleMsg->chk_sum=check_sum;
-                myBleMsg->end_of_string=EOS;  
-                blue.printf("%02x",myBleMsg->end_of_string);
-                blue.printf("%02x",myBleMsg->chk_sum);
-                mc.printf("%02x",myBleMsg->end_of_string);
-                mc.printf("%02x",myBleMsg->chk_sum);
-    }
+void send_data_str2(uint8_t check_sum)          // after raw data// this last part of structure is sent
+{          
+    myBleMsg->chk_sum=check_sum;
+    myBleMsg->end_of_string=EOS;  
+    blue.printf("%02x",myBleMsg->end_of_string);
+    blue.printf("%02x",myBleMsg->chk_sum);
+    
+}
     
 
 uint8_t checksum_struct(const uint8_t data[], uint16_t numBytes)         // calculating checksum for 
@@ -451,27 +436,26 @@
 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;
-uint32_t index1 = 0;
+    uint32_t sum=0;
+    uint8_t  chksum_datatrfr=0;
+    uint32_t index = 0;
+    uint32_t index1 = 0;
     sum += SOS;
     sum += DATA_TRFR;  
-   index1=(numBytes*4);
-   sum +=(index1>>(8*0))&0xFF;
+    index1=(numBytes*4);
+    sum +=(index1>>(8*0))&0xFF;
     sum +=(index1>>(8*1))&0xFF; 
 
-for (index=0U; index<(numBytes*4); index++)
-{ 
+    for (index=0U; index<(numBytes*4); index++)
+    { 
     sum += data[index];
-
-}
+    }
 
-  sum+=EOS;
+    sum+=EOS;
 
-  chksum_datatrfr = sum & 0x000000FF;
+    chksum_datatrfr = sum & 0x000000FF;
  //mc.printf("chksum_datatrfr=%02x\n",chksum_datatrfr);
-  return chksum_datatrfr;
+    return chksum_datatrfr;
 }