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

Files at this revision

API Documentation at this revision

Comitter:
nikitateggi
Date:
Fri Jun 23 07:25:24 2017 +0000
Parent:
21:7ef1b0b40841
Child:
23:e3b533aef4b4
Commit message:
Code with Battery monitoring(commented), glc modified, bt modified, pid modified

Changed in this revision

battery.cpp Show annotated file Show diff for this revision Revisions of this file
battery.h Show annotated file Show diff for this revision Revisions of this file
bp.cpp Show annotated file Show diff for this revision Revisions of this file
bp.h Show annotated file Show diff for this revision Revisions of this file
bt.cpp Show annotated file Show diff for this revision Revisions of this file
display_modules.cpp Show annotated file Show diff for this revision Revisions of this file
display_modules.h Show annotated file Show diff for this revision Revisions of this file
ecg_dec.h Show annotated file Show diff for this revision Revisions of this file
ecgg.cpp Show annotated file Show diff for this revision Revisions of this file
eeprom.lib Show annotated file Show diff for this revision Revisions of this file
eeprom_pgm.cpp Show annotated file Show diff for this revision Revisions of this file
eeprom_pgm.h Show annotated file Show diff for this revision Revisions of this file
glc.cpp Show annotated file Show diff for this revision Revisions of this file
glc.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
sdcard.cpp Show annotated file Show diff for this revision Revisions of this file
sdcard.h Show annotated file Show diff for this revision Revisions of this file
struct.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/battery.cpp	Fri Jun 23 07:25:24 2017 +0000
@@ -0,0 +1,17 @@
+#include "mbed.h"
+#include "battery.h"
+
+AnalogIn A(PTB1);
+
+float battery_voltage() {
+   // GLC_PWR=1;
+   // Enable=1;Shutdown=1;
+float b_voltage;
+  //unsigned int  samples1[4];
+  //unsigned int data2n;
+ 
+         b_voltage = A.read_u16()*(3.3/65535)*2;
+        wait(0.001f);
+    
+return   b_voltage;
+} 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/battery.h	Fri Jun 23 07:25:24 2017 +0000
@@ -0,0 +1,9 @@
+#ifndef BATTERY_H_
+#define BATTERY_H_
+
+
+
+float battery_voltage();
+
+
+#endif
\ No newline at end of file
--- a/bp.cpp	Wed Jun 21 08:39:10 2017 +0000
+++ b/bp.cpp	Fri Jun 23 07:25:24 2017 +0000
@@ -33,7 +33,7 @@
 //FILE *ecg;
 //FILE *ppg;
     
- void bp(int pid) {
+ void bp(int32_t pid) {
     
     uint8_t uch_dummy;
     uint32_t ppgdata; 
--- a/bp.h	Wed Jun 21 08:39:10 2017 +0000
+++ b/bp.h	Fri Jun 23 07:25:24 2017 +0000
@@ -15,7 +15,7 @@
 #define BPLEN_BYT 10778 // TOTAL length in BYTEs for BP inclu Should change if BUffer is changed
 //#define 
 
-void bp(int pid);
+void bp(int32_t pid);
 
 
 #endif
\ No newline at end of file
--- a/bt.cpp	Wed Jun 21 08:39:10 2017 +0000
+++ b/bt.cpp	Fri Jun 23 07:25:24 2017 +0000
@@ -1,23 +1,39 @@
+// added newline, changed the sequence of cmd and length, added send last function
+
+
+
+
+
 #include "mbed.h"
 #include "sdcard.h"
 #include "struct.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 len 6000
 
-BLEMsg_info *bt_file;              //pointer to structure 
-Point *myBleMsg;                   // pointer to bluetooth structure
+
+
+BLEMsg_info *bt_file;
+Point *myBleMsg;
 //bt_st *as;
-Serial mc(USBTX,USBRX);            // serial terminal
-Serial blue(PTC4,PTC3);            // bluetooth serial terminal
-                  
+Serial mc(USBTX,USBRX);
+Serial blue(PTC4,PTC3);
+                 // buffer for data
 uint32_t total_samples=0;              // variable to hold total number of samples
 //Timer t;                               // timer 
 uint8_t ack_rx;                        // varibale to receive ack from the bluetooth 
 Point b;                               // structure copy
 BLEMsg_info bt_file1;                  // structure copy
-
-uint32_t total_file_size=0;             // to determine the size of the file 
-uint32_t total_file_read=0; 
+uint32_t counter=0;
+ uint32_t total_file_size=0;        // to determine the size of the file 
+ uint32_t total_file_read=0; 
                   
-uint32_t file_start=0;                   // variable for 'for loop'
+uint32_t file_start=0;           // variable for 'for loop'
 uint32_t j=0;
 uint8_t k=0; 
 uint32_t file_pointer_position=0;
@@ -27,10 +43,11 @@
 
 uint8_t bt_send(uint8_t state_tx)
 {
-  uint32_t v1[150]; 
-  blue.baud(115200);                  // baud rate of bluetooth
+  uint32_t v1[1500];    
+  mc.baud(115200);                  // baud rate of bluetooth
   bt_file=&bt_file1;                      
   myBleMsg=&b; 
+  int8_t check_sum=0;
   static  uint8_t state_rx =0;
 //t.start();                             // timer start
   
@@ -39,9 +56,10 @@
           {
  
        case 1:   sendinit();  
-                 sd_open_read(1);                       // open the file for read
+                  mc.printf("\n");
+                 sd_open_read(66029);                       // open the file for read
                  total_file_size=file_size() ;             // determined the file size  
-                // mc.printf("filesize=%d",total_file_size);              
+              //   mc.printf("filesize=%d",total_file_size);                // TO SEND INITIAL STRING
                  state_rx =1;
                  state_tx =0;                
                  break;
@@ -50,72 +68,84 @@
        case 2:   total_samples=0;   
                  sd_read1(bt_file1);               // read the sd card file
                  myBleMsg->bt_msg=bt_file1;         //copy the contents to the structure
-                 send();                             // send the structure
-                 total_samples= myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp;
+                 send();                                // send the initial string
+                 mc.printf("\n");                       
+                 total_samples= myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp;
+                 
                  state_tx =0;
                  state_rx =2;
                  k=0;
-                 mc.printf("samples=%d", total_samples);
-                 total_file_read=file_size1();         // read the file size
+                // mc.printf("samples=%d", total_samples);
+                 total_file_read=file_size1();
                  //mc.printf("fileread=%d",total_file_read);
                  //mc.printf("pointer=%d\n",file_pointer_position);
                  break;                 
  
  
-       case 3:      
+       case 3:      counter=0;
                  //mc.printf("kkkk=%d\n",k);     
-                 //mc.printf("samples=%d", total_samples);            // send the raw data
-                if(k<(total_samples/150))
+                 //mc.printf("samples=%d", total_samples);         // send the raw data
+                if(k<(total_samples/1500))
                   {
-                    sd_read(v1,150);                                  //read 1500 samples
-                    send_data_str1();                                 // send the initial structure
-                    //mc.printf("file_start,j=%d %d\n",file_start,j);                     
-                      for(file_start=j;file_start<(j+150);file_start++)
+                    sd_read(v1,1500);      
+                     counter=j+1500;               //read 1500 samples
+                    send_data_str1(); 
+                   
+                    //mc.printf("file_start,j=%d %d\n",file_start,j);                    //send the strt of the string 
+                      for(file_start=j;file_start<(j+1500);file_start++)
                          {     
                        
-                             blue.printf("%08x", v1[file_start]);
-                            
+                             mc.printf("%08x", v1[file_start]);
+                           // counter++;
                          }
+                         
+                         
+                          check_sum=checksum(v1);               // calculating checksum
+                         
                             //  blue.printf("\n");
                        // mc.printf("iiii,hjjjj=%d %d\n",file_start,j);   
                     state_tx =0;
                     state_rx =3;
-                    send_data_str2();
+                    send_data_str2(check_sum);
                   
                    }
                
                else 
                      {
-                        for(int l=0;l<150;l++)
+                        for(int l=0;l<1500;l++)
                           {    
                             v1[l]=NULL;
                            }
-                        
+                            counter=(total_samples-(1500*k));
                            send_data_str1();
-                           sd_read(v1,(total_samples-(150*k)));
-                        for(file_start=j;file_start<(total_samples-(150*k));file_start++)
+                           sd_read(v1,(total_samples-(1500*k)));
+                        for(file_start=j;file_start<(total_samples-(1500*k));file_start++)
                           {     
-                             blue.printf("%08x", v1[file_start]);
+                             mc.printf("%08x", v1[file_start]);
+                             //counter++;
                            }
                            
                           // blue.printf("\n");
                           state_tx=0;
                           state_rx=4;
-                          send_data_str2();
+                          send_data_str2(check_sum);
                     }   
                      
                        total_file_read=file_size1();
+                        mc.printf("\n"); 
                    //   mc.printf("fileread=%d",total_file_read );
                       //  mc.printf("pointer=%d\n",file_pointer_position);
                 break;        
                
          
         case 4:   total_file_read=file_size1();
-                  mc.printf("fileread=%d",total_file_read);
+                 // mc.printf("fileread=%d",total_file_read);
                  if(total_file_read==total_file_size)
                      {
                     
                         state_rx=6;
+                        sendlast();
+                        mc.printf("\n"); 
                         sd_close();
                       //  mc.printf("exit");
                      }
@@ -147,25 +177,29 @@
             
 Timer t;                               // timer 
 uint8_t ack_rx;                        // varibale to receive ack from the bluetooth 
-//Point b;                               // structure copy                     
-//myBleMsg=&b;
+Point b;                               // structure copy                     
+myBleMsg=&b;
 t.start();                             // timer start
+int x2=0;
+int x3=0;
+int x4=0;
+int x5=0;
 
      switch(state_rx)
          {
                 
           case 1:                                       // wait for the ack
-                      state_rx=0; 
+                      state_rx=0;
                       myBleMsg->proto=ACK;
-                      scanf("%02x",&ack_rx);            // read the ack/nack from serial terminal
-                      mc.printf("%02x",ack_rx);         
-                   if (ack_rx==myBleMsg->proto)         // check for ack
+                    mc.scanf("%02x%02x%08x%02x",&x2,&ack_rx,&x3,&x4);
+                //  mc.printf("%02x%02x",ack_rx, x2); 
+                   if (ack_rx==myBleMsg->proto)
                      {
                        
                        state_tx=2;
                       }
                     
-                    else                                // its a nack
+                    else 
                     
                       { 
                        
@@ -177,7 +211,7 @@
          case 2:      state_rx =0;                        //wait for the ack
                       myBleMsg->proto=ACK;
                 
-                      scanf("%02x", &ack_rx);
+                       mc.scanf("%02x%02x%08x%02x",&x2,&ack_rx,&x3,&x4);
                      if (ack_rx==myBleMsg->proto)
                         {
                 
@@ -190,7 +224,7 @@
                         { 
                       
                         state_tx=2;
-                        setpos1(file_pointer_position);          // set the file pointer to send the previous data
+                        setpos1(file_pointer_position);
                      //   mc.printf("pointer=%d\n",file_pointer_position);
                         }
             
@@ -200,7 +234,7 @@
      case 3:       
                    state_rx =0;
                    myBleMsg->proto=ACK;
-                   scanf("%02x", &ack_rx);
+                  mc.scanf("%02x%02x%08x%02x",&x2,&ack_rx,&x3,&x4);
                  if (ack_rx==myBleMsg->proto)
                       {
                      
@@ -215,8 +249,9 @@
                        
                         state_tx=3;
                       //  mc.printf("j=%d\n",j);
-                        file_start=file_start-150;
-                        //mc.printf("j=%d\n",j); 
+                        file_start=file_start-1500;
+                        //mc.printf("j=%d\n",j);
+                      //  
                        // mc.printf("file_pointer_position=%d\n",file_pointer_position);
                         setpos1(file_pointer_position);
                       }
@@ -226,7 +261,7 @@
      case 4:       
                    state_rx =0;
                    myBleMsg->proto=ACK;
-                   scanf("%02x", &ack_rx);
+                    mc.scanf("%02x%02x%08x%02x",&x2,&ack_rx,&x3,&x4);
                    if (ack_rx==myBleMsg->proto)
                       {
                        // bt_r.printf("sent");
@@ -240,16 +275,19 @@
                       { 
                         //bt_r.printf("no-ack");
                         state_tx=3;
-                        file_start=file_start-150;
-                        mc.printf("file_pointer_position=%d\n",file_pointer_position);
+                        file_start=file_start-1500;
+                       // mc.printf("file_pointer_position=%d\n",file_pointer_position);
                         setpos1(file_pointer_position);
                       } 
                    break;                
                  
       case 5:      state_tx=2;
+                   
                    break;
                    
       case 6:      state_tx=0;
+                   
+      
                    mc.printf("out");
                    break;
    
@@ -270,13 +308,12 @@
              
 void send()
 {
-                myBleMsg->start_end_of_string=sos;
-                myBleMsg->length_file=28;
-                myBleMsg->proto=start_of_file;
-                myBleMsg->chk_sum=32;
-                mc.printf("%02x",myBleMsg->start_end_of_string); 
-                mc.printf("%08x",myBleMsg->length_file);
+                myBleMsg->start_of_string=sos;
+                myBleMsg->length_file=26;
+                myBleMsg->proto=data_trfr;                      // data transfer mode
+                mc.printf("%02x",myBleMsg->start_of_string); 
                 mc.printf("%02x",myBleMsg->proto);
+                mc.printf("%08x",myBleMsg->length_file);      //changed the sequence 
                 mc.printf("%08x",myBleMsg->bt_msg.length); 
                 mc.printf("%02x",myBleMsg->bt_msg.device_id); 
                 mc.printf("%08x",myBleMsg->bt_msg.patient_id);    
@@ -292,50 +329,119 @@
                 mc.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp); 
                 mc.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy);   
                 mc.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp);   
-                myBleMsg->start_end_of_string=eos;  
-                mc.printf("%02x",myBleMsg->start_end_of_string);
+                myBleMsg->end_of_string=eos;  
+                myBleMsg->chk_sum=checksum_struct();
+                mc.printf("%02x",myBleMsg->end_of_string);
                 mc.printf("%02x",myBleMsg->chk_sum);
     
     }
     
 void sendinit()
-{
-                myBleMsg->start_end_of_string=sos;
-                myBleMsg->length_file=3;
+{              
+
+                uint8_t sum_init=0  ;               
+                myBleMsg->start_of_string=sos;
+                myBleMsg->length_file=0;
                 myBleMsg->proto=start_of_file;
                //  myBleMsg->bt_msg=NULL;
-                myBleMsg->chk_sum=3;
-                mc.printf("%02x",myBleMsg->start_end_of_string); 
-                mc.printf("%08x",myBleMsg->length_file);
-                mc.printf("%02x",myBleMsg->proto);
-                myBleMsg->start_end_of_string=eos;  
-                mc.printf("%02x",myBleMsg->start_end_of_string);
+                sum_init=checksum_init();
+                myBleMsg->chk_sum=sum_init;
+                mc.printf("%02x",myBleMsg->start_of_string);
+                mc.printf("%02x",myBleMsg->proto); 
+                mc.printf("%08x",myBleMsg->length_file);     // chnged
+                myBleMsg->end_of_string=eos;  
+                mc.printf("%02x",myBleMsg->end_of_string);
                 mc.printf("%02x",myBleMsg->chk_sum);
     
     }
+  
+   
+uint8_t checksum_init()
+ {
+  
+  uint32_t sum=0;
+  uint8_t chksum_init=0;
+  sum=sos+eos+start_of_file; 
+  chksum_init=sum & 0x000000FF;
+  return chksum_init;
+  }
+
+uint8_t checksum_last()
+ {
+  
+  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 sendlast()
+{
+                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;
+                mc.printf("%02x",myBleMsg->start_of_string);
+                mc.printf("%02x",myBleMsg->proto); 
+                mc.printf("%08x",myBleMsg->length_file);     // chnged
+                myBleMsg->end_of_string=eos;  
+                mc.printf("%02x",myBleMsg->end_of_string);
+                mc.printf("%02x",myBleMsg->chk_sum);
+    
+    }
+       
+ void send_data_str1()
+   {
+                 myBleMsg->start_of_string=sos;
+                 myBleMsg->length_file=(counter*4);
+                 myBleMsg->proto=data_trfr;
+                 mc.printf("%02x",myBleMsg->start_of_string); 
+                 mc.printf("%02x",myBleMsg->proto);                   // chnaged
+                 mc.printf("%08x",myBleMsg->length_file);
+               
+    }
     
     
     
- void send_data_str1()
-   {
-                 myBleMsg->start_end_of_string=sos;
-                 myBleMsg->length_file=3;
-                 myBleMsg->proto=start_of_file;
-                 mc.printf("%02x",myBleMsg->start_end_of_string); 
-                 mc.printf("%08x",myBleMsg->length_file);
-                 mc.printf("%02x",myBleMsg->proto);
-    }
-    
-    
-    
- void send_data_str2()
+ void send_data_str2(uint8_t check_sum)
     {          
-                myBleMsg->chk_sum=3;
-                myBleMsg->start_end_of_string=eos;  
-                mc.printf("%02x",myBleMsg->start_end_of_string);
+                myBleMsg->chk_sum=check_sum;
+                myBleMsg->end_of_string=eos;  
+                mc.printf("%02x",myBleMsg->end_of_string);
                 mc.printf("%02x",myBleMsg->chk_sum);
     }
     
+uint8_t checksum(uint32_t  *v1)
+{
+
+uint32_t sum=0;
+uint8_t chksum_datatrfr=0;
+ 
+ for (int i=0;i<1500;i++)
+ {
+     sum=sum+v1[i];
+  }
+  
+  sum=sos+eos+sum+counter+data_trfr;
+  chksum_datatrfr=sum & 0x000000FF;
+  return chksum_datatrfr;
+}
 
 
 
+
+
+uint8_t checksum_struct()
+{
+
+ uint32_t sum=0;
+ uint8_t chksum_struct=0;
+ 
+  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;
+}
--- a/display_modules.cpp	Wed Jun 21 08:39:10 2017 +0000
+++ b/display_modules.cpp	Fri Jun 23 07:25:24 2017 +0000
@@ -65,7 +65,7 @@
      
     
     
-    void screen_main_2(int pid)     // test main screen
+    void screen_main_2(int32_t pid)     // test main screen
  {
     char buff2[10];     
     backlight = 0 ;
@@ -153,14 +153,14 @@
       
       
                 
- void screen_ecg_2(float heart_rate)   // ECG screen 2
+ void screen_ecg_2(uint16_t heart_rate)   // ECG screen 2
    {
     char buf[5];
     backlight = 0 ;
     TFT.BusEnable(true) ;
     TFT.background(White) ;
     TFT.foreground(Green) ;
-    sprintf (buf, "%.0f bpm",heart_rate);
+    sprintf (buf, "%d bpm",heart_rate);
     TFT.fillrect(5,90,230,150,White);  
     TFT.locate(3,100) ;  
     TFT.printf(" ECG Tst Done  ") ;   
@@ -495,5 +495,41 @@
     TFT.BusEnable(false) ;
     backlight = 1 ; 
    }
-   
+  
+  
+  void bat_screen_nocharge()
+   {
+        
+       /* lcd.FillRect(10,10,20,40,COLOR_RED); 
+       lcd.FillRect(5,127,230,200,COLOR_BLACK); 
+        lcd.FillRoundRect(60,230,180,300,COLOR_BLACK);
+        lcd.Print( "Low battery",60,130 ,COLOR_RED,COLOR_BLACK, 0);
+        lcd.Print( "Connect",60,160 ,COLOR_RED,COLOR_BLACK, 0);
+        lcd.Print( "Charger",30,205,COLOR_RED,COLOR_BLACK,0);      */
+        
+        backlight = 0 ;
+    TFT.BusEnable(true) ;
+     TFT.fillrect(10,10,20,40,Red);
+    TFT.fillrect(5,127,230,200,White);
+    TFT.fillrect(60,230,180,300,White);
+    TFT.locate(60,130) ;  
+    TFT.printf("Low battery") ;
+    
+    
+    
+         TFT.BusEnable(false) ;
+    backlight = 1 ; 
+          
+ } 
+    
+ void bat_screen_charge()
+   {
+        
+       // lcd.FillRect(10,10,20,40,COLOR_GREEN); 
+            backlight = 0 ;
+    TFT.BusEnable(true) ;
+     TFT.fillrect(10,10,20,40,Green);
+    TFT.BusEnable(false) ;
+    backlight = 1 ;    
+    }     
    
\ No newline at end of file
--- a/display_modules.h	Wed Jun 21 08:39:10 2017 +0000
+++ b/display_modules.h	Fri Jun 23 07:25:24 2017 +0000
@@ -5,12 +5,12 @@
 
 void screen_main();//main screen
 void screen_main_1();
-void screen_main_2(int pid);
+void screen_main_2(int32_t pid);
 void screen_ecg();
 void screen_bp();
 void screen_glc();
 void init_screen();
-void screen_ecg_2(float heart_rate);
+void screen_ecg_2(uint16_t heart_rate);
 //void screen_ecg_3();
  void screen_ecg_bp(); //changed the name 
 void screen_ecg_lead();
@@ -23,6 +23,7 @@
 void screen_again();
 void screen_glc_2();
 void screen_bp1(float SBP, float DBP) ;
- void screen_ecg_lead_bp_fingerdetect();
-
+void screen_ecg_lead_bp_fingerdetect();
+void bat_screen_nocharge();
+void bat_screen_charge();
 #endif 
--- a/ecg_dec.h	Wed Jun 21 08:39:10 2017 +0000
+++ b/ecg_dec.h	Fri Jun 23 07:25:24 2017 +0000
@@ -3,86 +3,5 @@
 #ifndef ECG_DEC_H_
 #define ECG_DEC_H_
 
-/*#include "mbed.h"
-//#include "rtos.h"
-#define PIN_MOSI        PTA16
-#define PIN_MISO        PTA17 
-#define PIN_SCLK        PTD1 
-#define MA4_SIZE  4 // DO NOT CHANGE
-//SDFileSystem sd(PTE1, PTE3, PTE2, PTE4, "sd");
-
-//SPI commands
-#define CMD_WAKEUP 0x02
-#define CMD_STANDBY 0x04
-#define CMD_RESET 0x06
-#define CMD_START 0x08
-#define CMD_STOP 0x0A
-#define CMD_OFFSET_CAL 0x1A
-#define CMD_RDATAC 0x10
-#define CMD_SDATAC 0x11
-#define CMD_RDATA 0x12
-//Note the following commands are 3 bits and have the following structure
-// CMD_REGr_rrrr; where r_rrrr is the register address given below
-#define CMD_WREG  0x02
-#define CMD_RREG 0x01
-
-//SPI registers addresses, 5 bits width
-#define REG_ID  0x00
-#define REG_CONFIG1 0x01
-#define REG_CONFIG2 0x02
-#define REG_LOFF 0x03
-#define REG_CH1SET 0x04
-#define REG_CH2SET 0x05
-#define REG_RLD_SENS 0x06
-#define REG_LOFF_SENS 0x07
-#define REG_LOFF_STAT 0x08
-#define REG_MISC1 0x09
-#define REG_MISC2 0x0A
-#define REG_GIPO 0x0B
-
-//Register bit configurations
-#define sps500 0x02   //CONFIG1 REG bits 010
-#define sps125 0x00   //CONFIG1 REG bits 000
-#define sps1k 0x03    //CONFIG1 REG bits 011
-#define no_mode 0xA8    //CONFIG2 REG set to zero for device check
-#define test_mode 0xA3  //CONFIG2 REG bits set to test mode, to generate a square wave
-#define default_mode 0xE0   //CONFIG2 REG bits set to default mode, to generate ECG signal from an external input
-#define loff_conf 0xF0  //LOFF register bits set to F0 to configure LOFF reg for leadoff detection
-#define offset_meas 0x01    //CH1SET REG set to Gain = 6 and offset measurement
-#define test_inp 0x05       //CH1SET REG set to Gain = 6 and generate test signal
-#define elec_inp 0x00       //CH1SET REG set to Gain = 6 and take in electrode input
-#define rld_sens_sig 0x23
-#define loff_sens_sig 0x03 
-#define misc1_inp 0x02
-#define misc2_inp 0x03
-
-#define LSHIFT_8 8    //left shift the obtained 8bit data by 8 values
-#define LSHIFT_16 16  ////left shift the obtained 8bit data by 16 values
-#define N_ERR 100
-#define N_TEST 500
-#define N_ECG 2000
-SPI mySpi(PIN_MOSI, PIN_MISO, PIN_SCLK) ;
-//PIN DECLARATIONS
-Serial pc(USBTX,USBRX);
-DigitalIn DRDY_BAR(PTC8);
-DigitalIn INTR(PTC7); 
-DigitalOut CHIPSEL_BAR(PTD0);
-DigitalOut ADS_START(PTC16);
-DigitalOut RESET_BAR(PTC17);
-
-DigitalOut myled(LED1);
-
-PwmOut led(PTB18);
-//SPI SETTINGS
-#define baud_rate 115200
-#define freq 1000000
-#define bits 8 
-#define mode 1 
-#define loop 1000000 
-
-//PPG DECLARATION
-#define MAX_BRIGHTNESS 255 // BP ADD
-int ecg(int pid);
-*/
-float ecg(int pid);
+uint16_t ecg(int32_t pid);
 #endif
\ No newline at end of file
--- a/ecgg.cpp	Wed Jun 21 08:39:10 2017 +0000
+++ b/ecgg.cpp	Fri Jun 23 07:25:24 2017 +0000
@@ -18,8 +18,8 @@
 Serial pc(USBTX,USBRX);  
 
 
-
-float ecg(int pid) 
+uint16_t ecg(int32_t pid) 
+//float ecg(int pid) 
  {
     
     uint8_t lead_reg=0;      // added on 14/06 to check lead -off 
@@ -83,7 +83,7 @@
     BLEMsg_info_ecg.device_id = 01;                   // Device ID fixed
     BLEMsg_info_ecg.patient_id = (uint32_t)pid;       // Patient ID
     BLEMsg_info_ecg.sampling_freq = 500;              // sampling frrquency
-    BLEMsg_info_ecg.length = 4026;                   //Total length of data in bytes  22 B+10752 B
+    BLEMsg_info_ecg.length = 4022;                   //Total length of data in bytes  22 B+ 4000 data
     
     /*
     //Loading number of samples
@@ -104,44 +104,52 @@
     ecg_ptr = &concatenate_value2; // Pointer to pass for ECG write into SD card Nidhin 1/6/2017
     
  //   int32_t sample_sum; // static int32_t avg[1500];
-    int32_t count = 0; int32_t fs = 500; 
-    uint32_t ecg_buf[N_ECG]; // Moved out of func() Nidhin 9/6/17
-    Timer t;
+    int32_t count = 0; 
+    //int32_t fs = 500; //COMMENTED NIDHIN 17/6/17
+    //uint32_t ecg_buf[N_ECG]; //COMMENTED NIDHIN 17/6/17
+    //Timer t; //COMMENTED NIDHIN 17/6/17
     
     //------------------ Declaration for Peak value detection ------------------------------------
- 
+ /*
  uint32_t hi_val;uint32_t pk_val[20];unsigned int pk_pos[20]={0};unsigned int a; //uint32_t pk=0;
  int hi_dif = 0;int j=0; // int count1 = N_ECG/fs, a_dif=0, fs1 = fs ,h=0;
     // ------------------------- Declaration for Heart Rate calculation --------------------------
  int n=0;    //int n=10; 
  float pos_dif, HR[2], HR1,t_pos_dif;int t_sec = 60; float HR_sum = 0,HR_avg;
+ */  //WHOLE SECTION COMMENTED NIDHIN 17/6/17
     // -------------------------------------------------------------------------------------------
     
   //  unsigned char chk = 1;
     pc.baud(baud_rate);
     freqset();               // setting the frequency
-    setupfunc();              
-    lead_reg=ecgsetupfunc();                 // added a return function to read leadoff // 14/06
+    setupfunc();    
+    //ecgtestsetupfunc();          // For test set up of 1Hz square wave signal
+   lead_reg= ecgsetupfunc();
  //   chk = 1;
+ 
+ 
+   //ORIGINAL sd_open_ECGfile(pid);        // opening the ecg file  COMMENTED Nidhin 1/6/2017
  if (lead_reg==0)                    // checking for proper lead contact// 14/06
- {
-   //ORIGINAL sd_open_ECGfile(pid);        // opening the ecg file  COMMENTED Nidhin 1/6/2017
+ {  
    sd_open_ECGfilee(pid);  // REPLACED Nidhin 1/6/2017 Nidhin 
    
   pc.printf( "Raw data is = \n"); 
  for(int i=0; i<N_ECG; i++)
    {
      concatenate_value2= readvalue();
+     pc.printf( "%d\n", concatenate_value2); //ADDED Nidhin 21/6/2017
      
      //ORIGINAL sd_write(concatenate_value2); // writing into the sd card  COMMENTED Nidhin 1/6/2017
      sd_ecgwrite(ecg_ptr); // REPLACED Nidhin 1/6/2017
-     
-     ecg_buf[i] = concatenate_value2;
-  ////   pc.printf( "%d\n", concatenate_value2); //COMMENTED Nidhin 1/6/2017
+     //pc.printf( "%d\n", *ecg_ptr);
+     //ecg_buf[i] = concatenate_value2;         //COMMENTED Nidhin 10/6/2017
+     //pc.printf( "%d\n", concatenate_value2); //COMMENTED Nidhin 10/6/2017
    } 
   
    //sd_close();          // closing the file COMMENTED Nidhin 1/6/2017
     sd_close_ecg();          // closing the ECG file REPLACED Nidhin 1/6/2017 Nidhin
+    
+    
 /*   
  // ---------- reading back SD data for processing --------------------
  sd_read_file(15);
@@ -158,7 +166,7 @@
  // -------------------------------------- PEAK DETECTION -------------------------------------------------------------
  
   // ------------------ Main loop ---------------------------------
- 
+/* 
 for(int i=0;i<N_ECG-10;i++){
 if(ecg_buf[i]>ecg_buf[i+1])
 {
@@ -179,9 +187,9 @@
    //  if(a_dif <= 0) ------------------------ add this condition if needed ----------------------
    //  {
      pk_val[j] = hi_val; //if condition satisfied, put the "pk" value into "pk_val" buffer
-   ////  pc.printf("peak value= %d\n",pk_val[j]);
+     pc.printf("peak value= %d\n",pk_val[j]);
      pk_pos[j]=a; // also save the peak's position
-   ////  pc.printf("peak position is = %d\n",pk_pos[j]);
+     pc.printf("peak position is = %d\n",pk_pos[j]);
      i = a+120;  // once confirmed that this is the necessary peak, skip the next 120 input values
      n = j; // where n is the number of peaks detected
      j = j+1;
@@ -224,28 +232,32 @@
  printf("Denominator = %d\n",n);
  printf("Heart Rate = %f\n",HR_avg);
  */
- 
+ //HR1 = 72; // Added Nidhin 10June17;
  //ADDED Nidhin 1/6/2017
  
-BLEMsg_info_ecg.cal_data.cal_sbp_dummy = 0;
-BLEMsg_info_ecg.cal_data.cal_dbp_OTtyp = (uint16_t)HR1;  //To be modified after HR code is added. 
+ uint16_t HR1;
 
-structure_file(ptr_BLEMsg_info_ecg, pid); //copy the ECG structure to Main file
-ecgfile_mainfile(pid);                        // copy raw data to the main file and ECG file is cleared.
+//HR1 = 72;
+ HR1 = heart_cal(pid);
+  
+BLEMsg_info_ecg.cal_data.cal_sbp_dummy = 0;
+BLEMsg_info_ecg.cal_data.cal_dbp_OTtyp = HR1;  //To be modified after HR code is added. 
 
- pc.printf("Closed the main file\n");
+structure_file(ptr_BLEMsg_info_ecg, pid); //copy the ECG structure to Main file  //COMMENTED Nidhin 10/6/2017 
+ecgfile_mainfile(pid);                        // copy raw data to the main file and ECG file is cleared. //COMMENTED Nidhin 10/6/2017
+
+pc.printf("Closed the main file\n");
  
  
  
   return HR1;
   // return HR_avg; 
-  }
   
+}          
  else 
      {
         pc.printf("no - leadoff");
         return 0;
     } 
   
-}          // End of main function
-
+}   // End of main function
--- a/eeprom.lib	Wed Jun 21 08:39:10 2017 +0000
+++ b/eeprom.lib	Fri Jun 23 07:25:24 2017 +0000
@@ -1,1 +1,1 @@
-https://developer.mbed.org/teams/SenseSemi/code/program/#4707b0a9acaa
+https://developer.mbed.org/teams/SenseSemi/code/program/#469d9e4091b0
--- a/eeprom_pgm.cpp	Wed Jun 21 08:39:10 2017 +0000
+++ b/eeprom_pgm.cpp	Fri Jun 23 07:25:24 2017 +0000
@@ -9,7 +9,7 @@
  
  
 
-void eprom_write(uint32_t address, int8_t data) 
+void eprom_write(uint32_t address, int32_t data) 
 {
     rom.write(address ,data);           // write data into the address
  
@@ -20,8 +20,8 @@
 
 int eprom_read(uint32_t address)       // readng the data from the specified address
 {
-    int8_t rdata;
+    int32_t rdata;                     // changed to int32_t
     rom.read(address, rdata);        
-    ec.printf("Read back value=%u\n", rdata);  
+    ec.printf("Read back value=%d\n", rdata);  
     return rdata;
 }
\ No newline at end of file
--- a/eeprom_pgm.h	Wed Jun 21 08:39:10 2017 +0000
+++ b/eeprom_pgm.h	Fri Jun 23 07:25:24 2017 +0000
@@ -2,7 +2,7 @@
 #define EEPROM_PGM_H_
 
 
-void eprom_write(uint32_t address, int8_t data);
+void eprom_write(uint32_t address, int32_t data);
 int eprom_read(uint32_t address);
 
 #endif
--- a/glc.cpp	Wed Jun 21 08:39:10 2017 +0000
+++ b/glc.cpp	Fri Jun 23 07:25:24 2017 +0000
@@ -251,7 +251,7 @@
 void Compute()
 {
    
-   
+   flipper.detach();     // ticker for sampling is disabled
    datafinal1 = (uint16_t ) NUMB1*(data2*VREF/(TOTAL))-NUMB2;
    datafinal2 = (uint16_t ) NUMB1*(daata1*VREF/(TOTAL))-NUMB2;
    mgdl = (uint16_t) NUMB1*(((daata1+data2)/2)*VREF/(TOTAL))-NUMB2;  // Nidhin 5/6/17 mgdl = (uint16_t) NUMB1*(((daata1+data2)/2)*VREF/(TOTAL))-NUMB2;
@@ -262,35 +262,25 @@
     {
         
         glc_4(mgdl);
-        mgdll = mgdl;      // Nidhin 31/5/2017
-        //gtc.printf("The Result is - %d", mgdll);
-        //sd_write(mgdl);        // storing value into the sd card
-        //sd_close();
-       // ble.printf("blood glucose is %d mg/dl", mgdl);
-        flipper.detach();
-        flipper1.detach();
-      //  return mgdl;
-      
-   
+         
     }
     
     else
-{
+   {
        // gtc.printf("err=1\n"); Commented Nidhin 8/6/2017
-        
-        // lcd1.Print( " ERROR RESULT", 5,220,COLOR_RED,COLOR_BLACK, 0);    
-         //  ble.printf("E:");
-    }
+
+     }
    GLC_START = 0;
+   flipper1.detach();// ticker for computation is disabled
 }
 
 
-void  glc(int pid){
+void  glc(int32_t pid){
    
     char t_s_d = 0;
-     i2c_spec();        
+     i2c_spec();         // programming the i2c expander 
      
-sd_open_GLCfilee(pid);                     // Opens GLC file in APPEND mode
+     sd_open_GLCfilee(pid);                     // Opens GLC file in APPEND mode
    
     // Bluetooth Structure created globally
     BLEMsg_info *ptr_BLEMsg_info_glc, BLEMsg_info_glc; // A copy of master strcuture [ "BLEMsg_info" ] by name "BLEMsg_info_bp" is created
@@ -422,6 +412,9 @@
                       //gtc.printf("mgdl is - %d\n", mgdll);
             } 
         wait(2.25);
+        
+   if (err==0)
+   {     
         gtc.printf("DATA1 is - %d\n", data1n); 
         gtc.printf("DATA2 is - %d\n", data2n);  
         gtc.printf("mgdl is - %d\n", mgdll);   
@@ -438,11 +431,14 @@
             
         structure_file(ptr_BLEMsg_info_glc, pid);           // Copy the structure into the GLC file 
         glcfile_mainfile(pid); 
-        
+       }
+     else 
+    {  
+         gtc.printf("replace teststip"); 
     }  
 
 
-    
+ }   
 
 
 
--- a/glc.h	Wed Jun 21 08:39:10 2017 +0000
+++ b/glc.h	Fri Jun 23 07:25:24 2017 +0000
@@ -13,6 +13,6 @@
 #define alpha 0.99
 #define beta 0.01
 
-void glc(int pid);
+void glc(int32_t pid);
 
 #endif 
\ No newline at end of file
--- a/main.cpp	Wed Jun 21 08:39:10 2017 +0000
+++ b/main.cpp	Fri Jun 23 07:25:24 2017 +0000
@@ -11,7 +11,7 @@
 #include "sdcard.h"
 #include "eeprom_pgm.h"
 #include "struct.h"
- 
+#include "battery.h" 
 Serial gc(USBTX, USBRX);
 DigitalIn q1(PTB11);
 DigitalIn q(PTB8);
@@ -19,7 +19,7 @@
 
 uint8_t touch(uint8_t state);           // touch state
 uint8_t display(uint8_t state1);       //  display state
-int pid=0;                // patient ID
+int32_t pid=0;                // patient ID
 static uint8_t nstate=0;
 static uint8_t nstate1=0;
 static uint8_t flag=0;             //flag to indicate not to update pid as it is already done
@@ -27,7 +27,7 @@
 static uint8_t glc_flag=0;         // glc_flag to indicate the glc test is already done for the same pid
 uint8_t state_r=0;                 // state to receive from bluetooth
 uint8_t state_t=1;                 // state to transmit to bluetooth
-float heart_rate=0;          
+uint16_t heart_rate=0;          
 
 
 int main(){
@@ -49,24 +49,44 @@
 static uint8_t state_touch=1;
 static uint8_t state_t=1;
 static uint8_t state_r=0;
-
+ float b_voltage=0;
+ /*      b_voltage=battery_voltage();
+       //gc.printf("%f \n",  b_voltage );
+       if (b_voltage<3.5)
+       {
+          bat_screen_nocharge();
+          DisableTouch();
+        }
+      
+      
+else
+    {  
+     // enable touch function
+       bat_screen_charge();
+       EnableTouch();*/
    while(1)
    {
      switch(main_state)                          // main state machine
      {
-         case 1:  state_display=touch(state_touch);           // touch state
-                
-                  
-                if(state_display==12)
-                    main_state=3;
+         case 1:  state_display=touch(state_touch);           // touch state 
+                   if(state_display==12)
+                      main_state=3;
                     
-                else 
-                    main_state=2;
+                  else 
+                       main_state=2;
                   break;
     
          case 2:  state_touch=display(state_display);          //display state
-                  main_state=1;
-                 break;
+           /*        if (b_voltage<3.5)
+                     {
+                         bat_screen_nocharge();
+                         DisableTouch();
+                     }
+              else    {                        */
+                    //    bat_screen_charge();
+                       main_state=1;
+                    //   }
+                    break;
                 
                 
          case 3: DisableTouch();                             // bluetooth send 
@@ -92,11 +112,11 @@
                 
                 
     }
-
+}
 
 }
 
-}
+//}
 
 
 uint8_t touch(uint8_t state)
--- a/sdcard.cpp	Wed Jun 21 08:39:10 2017 +0000
+++ b/sdcard.cpp	Fri Jun 23 07:25:24 2017 +0000
@@ -34,7 +34,7 @@
   int pbp = 0;
   
   //Function Added on 16/5/2017 by Nidhin
- void sd_open_mainfile(int pid)
+ void sd_open_mainfile(int32_t pid)
  {
  char buffer1[32];
  sprintf(buffer1,"/sd/%d.csv",pid);
@@ -60,7 +60,7 @@
  
  }   */
  
-  void sd_open_GLCfilee(int pid)    // only opening the glc file after entering TEST screen
+  void sd_open_GLCfilee(int32_t pid)    // only opening the glc file after entering TEST screen
  {
   char buffer1[32];
   sprintf(buffer1,"/sd/%d_GLC.csv",pid);
@@ -85,7 +85,7 @@
  
  } 
  */
-  void sd_open_ECGfilee(int pid)          // only opening the ECG file after entering TEST screen
+  void sd_open_ECGfilee(int32_t pid)          // only opening the ECG file after entering TEST screen
  {
      
   char buffer1[32];
@@ -110,7 +110,7 @@
   fprintf(fp,"%s\n",ctime(&epoch_time1) );
  } */
 
-void sd_open_BPfilee(int pid)          // only opening the BP file after entering TEST screen
+void sd_open_BPfilee(int32_t pid)          // only opening the BP file after entering TEST screen
  {
      
  
@@ -218,7 +218,7 @@
   }
   //-----------------------------------------------SD Read
   
-  void sd_reopen_BPfilee(int pid)          // only opening the BP file after entering TEST screen
+  void sd_reopen_BPfilee(int32_t pid)          // only opening the BP file after entering TEST screen
  {
      
  
@@ -235,7 +235,7 @@
 //---------------------------------------
 
 
-   void sd_open_BP_ECGfile(int pid)         
+   void sd_open_BP_ECGfile(int32_t pid)         
  {
      
   //char buffer[32];
@@ -255,7 +255,7 @@
  } 
  
   
-   void sd_open_BP_PPGfile(int pid)         
+   void sd_open_BP_PPGfile(int32_t pid)         
  {
      
   char buffer1[32];
@@ -307,7 +307,7 @@
  //------------------------------------------  
  //REOPEN FUNCTIONs for reading data Nidhin
  
- int sd_BP_ECGMAX(int pid)         
+ int sd_BP_ECGMAX(int32_t pid)         
  {
      
   char buffer1[32];
@@ -346,7 +346,7 @@
  
  // Function to calculate PPG MAx and location
  
-   int sd_BP_PPGMAX(int pid)         
+   int sd_BP_PPGMAX(int32_t pid)         
  {
      
   char buffer1[32];
@@ -394,7 +394,7 @@
  
  
   //------- FUNCTION TO CREATE SINGLE BP FILE --------2/5/2017 (NIDHIN)---------------//
- void create_single_BPfile(int pid)
+ void create_single_BPfile(int32_t pid)
 {
     
     //int ecgbuf[64] = {0};
@@ -455,7 +455,7 @@
 }
 
 //Creating strcuture file Common to All Functions
-void structure_file(BLEMsg_info *ptr, int pid)
+void structure_file(BLEMsg_info *ptr, int32_t pid)
 {
 char buffer3[32];
 sprintf(buffer3, "/sd/%d.csv", pid);        //Printing file path to the buffer
@@ -466,7 +466,7 @@
 
 //Copy Data into file.
 
-void bpfile_mainfile(int pid)                       //Copy data of bp file to main
+void bpfile_mainfile(int32_t pid)                       //Copy data of bp file to main
 {
     char buffer3[32];
     sprintf(buffer3, "/sd/%d.csv", pid);
@@ -498,7 +498,7 @@
 
 
 //------------ Function to write ECG data to main file
-void ecgfile_mainfile(int pid)
+void ecgfile_mainfile(int32_t pid)
 {
     char buffer3[32];
     sprintf(buffer3, "/sd/%d.csv", pid);       
@@ -528,7 +528,7 @@
 
 
 //---------------------- Function to write GLC data to GLC file
-void sd_glcwrite(uint32_t *glc_ptr, int pid)
+void sd_glcwrite(uint32_t *glc_ptr, int32_t pid)
 {
     char buffer3[32];
     sprintf(buffer3, "/sd/%d_GLC.csv", pid);       
@@ -541,7 +541,7 @@
 
 // --------------------------Fuinction to write GLC data to Main file
 
-void glcfile_mainfile(int pid)
+void glcfile_mainfile(int32_t pid)
 {
     char buffer3[32];
     sprintf(buffer3, "/sd/%d.csv", pid);       
@@ -565,7 +565,7 @@
 }
 
 
-void delete_subfiles(int pid)
+void delete_subfiles(int32_t pid)
 {
     char buffer3[32];  
     int success1 , success2, success3;
@@ -596,4 +596,149 @@
     else 
         snd.printf("\nECG file delete : UnSuccessful");
 
+}
+
+
+uint16_t heart_cal(int pid)   // Function created on 10/6/17, Function modified on 13/6/17 Nidhin
+// Objective ti understand how file pointers work 
+{
+    //FILE *fpeecg2;       // Secondary File
+    
+    char buffer3[32];
+    
+    
+    int32_t buff1[1] ; int32_t buff2[2]; int32_t buff3[1];  // Buff 12 &3 Stores sample 1, 2 & 10th sample
+    int32_t fppos;  // Variable to hold pointer position
+    
+    int32_t val1_val10;  // VARIABLE holding difference of sample 1 and 10 
+    
+    int32_t peakpos[10]; //array taht holds the peak position //Changed to 10 (ORG 4) Nidhin 20/6/17
+     int32_t peakval[10]; // Array holding peak values  ////Changed to 10 (ORG 4) Nidhin 20/6/17
+     int32_t pk = 0;     // Variable to access array
+    
+    int32_t no_of_peaks =0;  // Variable holding toatl number of peaks ; increments on each peak
+    
+    float hrt_rate;         // Heart rate in float varoable
+    uint16_t heart_rate;    // Heart rate to be type casted to int
+    
+    sprintf(buffer3, "/sd/%d_ECG.csv", pid);    // For opening a specific file    
+    fpeecg = fopen(buffer3, "r");
+    
+    /*
+    fppos = ftell(fpeecg);
+    snd.printf("Main pointer is at  Position : %d \n", fppos);
+    */
+    int32_t m =0;       // Variable to move the file pointer in fseek function Org Static Nidhin 22/6/17
+    int32_t i =0;      // Variable to count sample number, ORG Static Nidhin 22/6/17
+    
+    for ( i = 0; i < 1000;  i++)   // for 1024 samples in file
+    {
+    rewind(fpeecg);               // Go to satrt of file each time
+    fseek(fpeecg, m, SEEK_CUR);   // Update the count value according to move pointer
+        
+    fread(buff1, sizeof(int32_t), 1, fpeecg);  // Read sample 1
+    //fpeecg2 = fpeecg;
+    fread(buff2, sizeof(int32_t), 1, fpeecg);  // Read Sample 2
+    
+    int32_t n = 28;       // Variable to move to 10th sampple from current
+    
+       
+    fseek(fpeecg, n, SEEK_CUR);   // Moving to tenth sample  
+    fread(buff3, sizeof(int32_t), 1, fpeecg);  // Read 3rd sample
+    
+     snd.printf("buff1 = %d , buff2 = %d, buff3 = %d\n",buff1[0],buff2[0],buff3[0] ); //Test  Value heald by buffer each round
+    
+    val1_val10 = buff1[0] - buff3[0];   // Difference of sample 1 and 10
+    
+    if(buff1[0] > buff2[0] && val1_val10 > 10000)   // Condition for -ve gradient and difference of 10000
+    {
+        peakpos[pk] = i;              // Peak detected sample number updated in array
+        peakval[pk] = buff1[0];       // Peak value updated in array
+        pk++;                         // Array pointer incremented
+        i = i+120;                     // i incremented to 120 for next 120 samples 
+        no_of_peaks = no_of_peaks + 1;  // no. of peaks incremented
+        //fseek(fpeecg, 480, SEEK_CUR);   // push the pointer to 
+        //fppos = ftell(fpeecg);     Commenetd Nidhin 20/6/17
+        //snd.printf("in if : The Pointer is moved to positon %d\n", fppos);
+        m = m + 480;
+        
+     }
+     
+     else   
+    { 
+       m = m+4;
+       //fpeecg = fpeecg2;
+       //i = i+1;
+       //fppos = ftell(fpeecg); COMMENTED NIDHIN 20/6/17
+       //snd.printf("in Else : The Pointer is moved to positon %d\n", fppos);
+    }
+    }
+    
+    snd.printf("The No: of Peaks is  %d\n", no_of_peaks);
+    
+    for(int j=0; j<no_of_peaks; j++)
+    {
+        snd.printf("The Peak %d, is at %d & value is %d\n",j, peakpos[j], peakval[j]);
+    }
+    
+    // Heart rate calculation
+    //Testing
+    int32_t t_diff;
+    t_diff =  peakpos[1] - peakpos[0];
+    snd.printf("t_diff is %d\n", t_diff);
+    
+    hrt_rate = 60.0/(t_diff*2);
+    //snd.printf("Hear rate in float is %f\n", hrt_rate); COMMENETD NIDHIN 20/6/17
+    
+    hrt_rate = float (hrt_rate*1000);
+    snd.printf("Hear rate in float is %f\n", hrt_rate);
+    
+    heart_rate = uint16_t(hrt_rate + 0.5);
+    snd.printf("The heart rate after type casting is %d\n", heart_rate); 
+
+    /*
+    fppos = ftell(fpeecg);
+    snd.printf("The first data is = %d\n", buff1);
+    snd.printf("The Position after reading first data is = %d\n\n", fppos);
+    
+    fread(buff2, sizeof(int32_t), 1, fpeecg);
+    fppos = ftell(fpeecg);
+    snd.printf("The  Second data is = %d\n", buff2);
+    snd.printf("The Position after reading Second data is = %d\n\n", fppos);
+    
+    fseek(fpeecg, 32,SEEK_CUR); 
+    
+    fread(buff3, sizeof(int32_t), 1, fpeecg);
+    fppos = ftell(fpeecg);
+    snd.printf("The third data is = %d\n", buff3);
+    snd.printf("The Position after reading Second data is = %d\n\n", fppos);
+    
+    fseek(fpeecg, -24,SEEK_CUR); 
+    
+    fread(buff3, sizeof(int32_t), 1, fpeecg);
+    fppos = ftell(fpeecg);
+    snd.printf("The fourth data is = %d\n", buff3);
+    snd.printf("The Position after reading fourth data is = %d\n", fppos);
+        
+    }
+    /*
+    snd.printf("Printing from  500-999\n\n");
+    fread(ecgbufnew, sizeof(int), 500, fpeecg);
+    for(int i=0; i<500; i++)
+    {
+        //snd.printf("%d\n", ecgbufnew[i]);
+    }
+    
+    rewind(fpeecg);
+    fseek(fpeecg, 996, SEEK_SET); // Try modifying with 996 = 249*4 if not working
+    
+    snd.printf("Printing from  250-749\n\n");
+    fread(ecgbufnew, sizeof(int), 500, fpeecg);
+    for(int i=0; i<500; i++)
+    {
+        //snd.printf("%d\n", ecgbufnew[i]);
+    }
+    */
+    fclose(fpeecg);
+    return heart_rate;
 }
\ No newline at end of file
--- a/sdcard.h	Wed Jun 21 08:39:10 2017 +0000
+++ b/sdcard.h	Fri Jun 23 07:25:24 2017 +0000
@@ -7,10 +7,10 @@
 #include "struct.h"
 
 //void sd_open_ECGfile(int pid);
-void sd_open_ECGfilee(int pid);
+void sd_open_ECGfilee(int32_t pid);
 //void sd_open_GLCfile(int pid);
-void sd_open_GLCfilee(int pid);
-void sd_open_BPfilee(int pid);
+void sd_open_GLCfilee(int32_t pid);
+void sd_open_BPfilee(int32_t pid);
 //void sd_open_BPfile(int pid);
 void sd_write (int value);
 int file_size() ;
@@ -22,36 +22,36 @@
 void setpos1(uint32_t position);
 //void sd_BP_ppgwrite (uint32_t value);
 //void sd_BP_ecgwrite (int value);
-
+uint16_t heart_cal(int pid);
 FILE *sd_BP_ecgwrite (int32_t *fpdrum11);
 FILE *sd_BP_ppgwrite (uint32_t *fpdrum21);
  
-void sd_open_BP_ECGfile(int pid); //added to write ECG to seperate file BP
-void sd_open_BP_PPGfile(int pid); //added to write PPG to seperate file BP
+void sd_open_BP_ECGfile(int32_t pid); //added to write ECG to seperate file BP
+void sd_open_BP_PPGfile(int32_t pid); //added to write PPG to seperate file BP
 
 void sd_close();               // Close files opend with FILE pointer fp
  void sd_close_ecg() ;            // Close files opend with FILE pointer fpeecg
  void sd_close_glc() ;            //Close files opend with FILE pointer fpbg
 
-int sd_BP_ECGMAX(int pid);
-int sd_BP_PPGMAX(int pid);
+int sd_BP_ECGMAX(int32_t pid);
+int sd_BP_PPGMAX(int32_t pid);
 //void sd_close1();
 
-void create_single_BPfile(int pid); //Function to create 1 file from 2 temp BP file (ECG &PPG)
+void create_single_BPfile(int32_t pid); //Function to create 1 file from 2 temp BP file (ECG &PPG)
 
-void sd_open_mainfile(int pid); // Function for oprning main file
+void sd_open_mainfile(int32_t pid); // Function for oprning main file
 //----------------- For testing
-void structure_file(BLEMsg_info *ptr, int pid); // Writes structure to the File.
+void structure_file(BLEMsg_info *ptr, int32_t pid); // Writes structure to the File.
 
-void bpfile_mainfile(int pid);
+void bpfile_mainfile(int32_t pid);
 
 void sd_ecgwrite(int *ecg_ptr);  //to print ECG data in ECG func as binary using fwrite.   NIDHIN 25 May 2017
-void ecgfile_mainfile(int pid); 
+void ecgfile_mainfile(int32_t pid); 
 
-void sd_glcwrite(uint32_t *glc_ptr, int pid);  // GLC raw data to GLC file
-void glcfile_mainfile(int pid);  // GLC file to main file
+void sd_glcwrite(uint32_t *glc_ptr, int32_t pid);  // GLC raw data to GLC file
+void glcfile_mainfile(int32_t pid);  // GLC file to main file
 
-void delete_subfiles(int pid);  //deletes all subfiles under a pid
+void delete_subfiles(int32_t pid);  //deletes all subfiles under a pid
 
 
 #endif 
\ No newline at end of file
--- a/struct.h	Wed Jun 21 08:39:10 2017 +0000
+++ b/struct.h	Fri Jun 23 07:25:24 2017 +0000
@@ -30,14 +30,14 @@
     uint16_t             cal_dbp_OTtyp;
 }CalData_info;
 
-typedef enum 
+/*typedef enum 
 { 
      ACK = (uint8_t)0x00,
     start_of_file,
     end_of_file,
     data_trfr,
     NACK=0xff,
-}bt_st;
+}bt_st;*/
 
 typedef enum
 { 
@@ -59,21 +59,22 @@
   CalData_info          cal_data;
 }BLEMsg_info;
 
-typedef enum 
+/*typedef enum 
 { 
      sos= (uint8_t)0xc0,
      eos= (uint8_t)0xc0,
-}file_st;
+}file_st;*/
 
 
 
 typedef struct __attribute__((__packed__))
 {
 
- file_st start_end_of_string;
+uint8_t start_of_string;
  uint32_t length_file;
- bt_st proto;
+  uint8_t proto;
  BLEMsg_info  bt_msg;
+ uint8_t end_of_string;
  uint8_t chk_sum;
 
 }Point;
@@ -83,7 +84,11 @@
 uint8_t bt_receive(uint8_t send_rx);
 void send();
 void sendinit();
+void sendlast();
 void send_data_str1();
-void send_data_str2();
-
+void send_data_str2(uint8_t check_sum);
+uint8_t checksum(uint32_t  *v1);
+uint8_t checksum_init();;
+uint8_t checksum_last();
+uint8_t checksum_struct();
 #endif
\ No newline at end of file