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:
31:0f67acfc6236
Parent:
28:65f2d80b7eb7
Child:
32:76892fdf5e4c
--- a/bt.cpp	Sat Jul 08 12:06:16 2017 +0000
+++ b/bt.cpp	Sat Jul 15 05:39:45 2017 +0000
@@ -9,7 +9,7 @@
 #define NACK 0xFF
 #define sos 0xc0
 #define eos 0xc0
-#define no_of_samples 1500
+#define no_of_samples 150
 #include "eeprom_pgm.h"
 //#define len 6000
 
@@ -61,8 +61,9 @@
                   file_pointer_position=0;
                   bt_file_tosend=eprom_read(6);            // reading the file number(pid) from eeprom which has to be sent to bluetooth
                   current_file=eprom_read(1);  
-                  bt_file_tosend=bt_file_tosend+1;
-                  eprom_write(6,bt_file_tosend);            // read the current pid 
+                 // current_file=2;
+                  //bt_file_tosend=bt_file_tosend+1;
+                  //eprom_write(6,bt_file_tosend);            // read the current pid 
                   mc.printf("eeprom file=%d",bt_file_tosend);  
                   mc.printf("currentfile=%d",current_file);  
                   if (bt_file_tosend>current_file)       // if the file sent to bluetooth and current file are same,
@@ -74,7 +75,7 @@
                        file=sd_open_read(bt_file_tosend);
                        if(file==0)
                         {
-                           state_tx =6; 
+                           state_rx =6; 
                            }
                          else 
                          {   
@@ -116,7 +117,7 @@
                          }
                          
                          
-                          check_sum=checksum(v1);               // calculating checksum
+                          check_sum=checksum((uint8_t *)v1,counter);               // calculating checksum            // calculating checksum
                           state_tx =0;
                           state_rx =3;
                           send_data_str2(check_sum);            // send the remaining part of the string
@@ -140,6 +141,7 @@
                                                                    
                           state_tx=0;
                           state_rx=4;
+                          check_sum=checksum((uint8_t *)v1,counter);               // calculating checksum
                           send_data_str2(check_sum);
                     }   
                      
@@ -284,11 +286,14 @@
                     break;
                    
       case 6:       state_rx =0;
+                  bt_file_tosend=bt_file_tosend+1;
+                  eprom_write(6,bt_file_tosend);     
+                   mc.printf("next file");       
                    /*myBleMsg->proto=ACK;
                     blue.scanf("%02x%02x%08x%02x",&sos_rx,&ack_rx,&eos_rx,&chksum_rx);
                    if (ack_rx==myBleMsg->proto)
                       {*/
-                      if (bt_file_tosend!=current_file)     
+                      if (bt_file_tosend<=current_file)     
                             {      sd_close();
                                 // bt_file_tosend=bt_file_tosend+1;
                                  // eprom_write(6,bt_file_tosend);
@@ -352,7 +357,7 @@
                 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();
+                myBleMsg->chk_sum = checksum_struct((uint8_t *)myBleMsg, sizeof(Point));
                 blue.printf("%02x",myBleMsg->end_of_string);
                 blue.printf("%02x",myBleMsg->chk_sum);
     
@@ -437,7 +442,7 @@
                 blue.printf("%02x",myBleMsg->chk_sum);
     }
     
-uint8_t checksum(uint32_t  *v1)          // this function to calculate the checksum for the raw data
+/*uint8_t checksum(uint32_t  *v1)          // this function to calculate the checksum for the raw data
 {
 
 uint32_t sum=0;
@@ -466,4 +471,48 @@
   sum=myBleMsg->start_of_string+myBleMsg->proto+myBleMsg->bt_msg.length+myBleMsg->bt_msg.device_id+myBleMsg->bt_msg.date_time.date+myBleMsg->bt_msg.date_time.month+myBleMsg->bt_msg.date_time.year+myBleMsg->bt_msg.date_time.hour+myBleMsg->bt_msg.date_time.mins+myBleMsg->bt_msg.date_time.sec+myBleMsg->bt_msg.test_type+myBleMsg->bt_msg.sampling_freq+myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp+myBleMsg->bt_msg.cal_data.cal_sbp_dummy+myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp+myBleMsg->end_of_string+myBleMsg->chk_sum;
   chksum_struct=sum & 0x000000FF;
   return chksum_struct;
+}*/
+
+uint8_t checksum_struct(const uint8_t data[], uint16_t numBytes)         // calculating checksum for 
+{
+    uint32_t chksum_struct = 0;
+    uint16_t index = 0;
+    
+    for (index=0U; index < (numBytes-1); index++)
+    {
+          chksum_struct += data[index];  
+          //blue.printf("data=%02x\n",data[index]);
+        
+    }
+  // blue.printf("%08x\n",chksum_struct);  
+  return chksum_struct;
 }
+
+
+uint8_t checksum(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;
+uint8_t  i = 0;
+uint32_t index = 0;
+uint32_t index1 = 0;
+    sum += sos;
+    sum += data_trfr;  
+   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;
+}
+