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:
avp2417
Date:
Sat Jul 29 11:23:19 2017 +0000
Parent:
35:43133f017ed4
Child:
37:92fcbf22bf91
Commit message:
Added Suhasini's changes related to BP code and increase Bluetooth buffer from 150 samples to 600 Samples into 27th July 2017 code

Changed in this revision

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
ec_bp.cpp Show annotated file Show diff for this revision Revisions of this file
ec_bp.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
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
--- a/bp.cpp	Sat Jul 29 10:38:05 2017 +0000
+++ b/bp.cpp	Sat Jul 29 11:23:19 2017 +0000
@@ -8,7 +8,6 @@
 #include <stdio.h>
 
 #include <time.h>
-//#include <iostream>
 #include "ds3231.h"
 #include "SDFileSystem.h"
 #include "rtc.h"
@@ -17,10 +16,10 @@
 #include "bp.h"
 #include "display_modules.h"
 
-
+FILE *fpecg_bp; // file pointer in place of fpecg- Suhasini- 21/7/17
+FILE *fpppg1;
 
 #include "struct.h"
-//#include "test_type.h"
 
 using namespace std;
 
@@ -30,22 +29,19 @@
 Timer t; 
 
 
-//FILE *ecg;
-//FILE *ppg;
-    
- void bp(int32_t pid) {
+uint16_t ecgloc = 0;  // PPG and eCG maximum locations //int to uint16_t-Suhasini-21stJuly2017
+uint16_t ppgloc = 0;  //int to uint16_t-Suhasini-21stJuly2017
+uint16_t ebp = 0;      // variable for LOOP operation to read value from File i == ECG , j == PPG
+uint16_t pbp = 0;  //int to uint16_t-Suhasini-21stJuly2017
+
+void bp(int32_t pid) {
     
     uint8_t uch_dummy;
     uint32_t ppgdata; 
     uint8_t lead_reg=0;
-    //int dummyconcat = 700000;
-    int concatenate_value2 = 0;
-    unsigned char chk = 1;
-
-    uint32_t drum11[64];
-    uint32_t drum21[64];
-    
-           
+    uint32_t concatenate_value2 = 0;    //int to uint32_t-Suhasini-21stJuly2017
+    uint8_t chk = 1;    //unsigned char to uint8_t-Suhasini-21stJuly2017
+  
     //Declaration of  Master structure
     
     BLEMsg_info *ptr_BLEMsg_info_bp, BLEMsg_info_bp; // A copy of master strcuture [ "BLEMsg_info" ] by name "BLEMsg_info_bp" is created
@@ -106,30 +102,9 @@
     BLEMsg_info_bp.sampling_freq = 500;              // sampling frrquency
     BLEMsg_info_bp.length = 10774;                   //Total length of data in bytes  22 B+10752 B
     
-    /*
-    //Loading number of samples
-    NumSamples_info NumSamples_info_bp;              //Copy of structure NumSamples_info
-    NumSamples_info_bp.num_ppg_sample = 1664;        // PPG & ECG Sample number loaded in structure copy
-    NumSamples_info_bp.num_ecg_sample = 1024;
-    */
-    
     BLEMsg_info_bp.num_samples.num_sample_ppg_dummy =  1664 ;// PPG  number of samples copied to master struct 
     BLEMsg_info_bp.num_samples.num_sample_ecg_OTtyp =  1024 ; // ECG  number of samples copied to master struct
     
-    
-      
-    
-
-    // Variables for file operations
-   // uint32_t amax=0;
-    //uint32_t bmax = 0;
-    //uint32_t a;
-    //uint32_t b;
-    
-   //int aloc = 0;
-   //int bloc = 0;
-   //int samples = 0;
-
     bpr.baud(baud_rate);
 
     freqset();               // setting the frequency
@@ -141,13 +116,10 @@
     maxim_max30102_init();
         
     chk = 1;
-        
     wait(2);
-    //bc.printf("BP start\n");  
-
-    for(int i=0; i<500; i++)
+    
+    for(uint16_t i=0; i<500; i++)
     {
-        
         concatenate_value2= readvalue();
         
         maxim_max30102_read_reg(0,&uch_dummy);
@@ -161,7 +133,7 @@
  un_max = 0;
 un_min = 0x3FFF;
        
-for(int i=0; i<500; i++)    // READS 500 SAMPLES TO SEE MAX AND MIN VAL DIFFERENCE
+for(uint16_t i=0; i<500; i++)    // READS 500 SAMPLES TO SEE MAX AND MIN VAL DIFFERENCE
 {        
 maxim_max30102_read_reg(0,&uch_dummy);
 while(INTR.read()==1); 
@@ -171,7 +143,6 @@
 un_min=ppgdata;    //update signal min
 if(un_max<ppgdata)
 un_max=ppgdata; 
-
 }  
 
 bpr.printf("The max is %d\n", un_max); //
@@ -186,71 +157,42 @@
 
     FILE *ecg = NULL;
     FILE *ppg = NULL;
-    uint32_t *drum_ecg = NULL; // pointers to ecg buffer  changed to uint32_t from int
-    uint32_t *drum_ppg = NULL; // pointer to ppg buffer
     
-    drum_ecg = drum11;
-    drum_ppg = drum21;
-    
-    for(int j =0 ; j<1; j++) // changed to 16 from 8 // Change by Nidhin 30/5/2017 Direct writing to SD card. no loop required
-    {
-        for(int i=0; i<1024; i++)
+        for(uint16_t i=0; i<1024; i++)  //int to uint16_t-Suhasini-21stJuly2017
         {
             concatenate_value2 = readvalue();
-            //drum11[i]=(uint32_t)concatenate_value2;   //drum11[i]=concatenate_value2;      drum11[i]=dummyconcat; typecasted to uint32_t
-            //Change by Nidhin 30/5/2017 Direct writing to SD card.
             ecg = sd_BP_ecgwrite(&concatenate_value2);
             
             maxim_max30102_read_reg(0,&uch_dummy);
             while(INTR.read()==1); 
             maxim_max30102_read_fifo(&ppgdata); 
             ppg = sd_BP_ppgwrite(&ppgdata); //Change by Nidhin 30/5/2017 Direct writing to SD card.
-            //drum21[i]=ppgdata;            //Change by Nidhin 30/5/2017 Direct writing to SD card.
-        }
-                           
-            //ecg = sd_BP_ecgwrite(drum_ecg); // //Change by Nidhin 30/5/2017 Direct writing to SD card.
-            //ppg = sd_BP_ppgwrite(drum_ppg); // //Change by Nidhin 30/5/2017 Direct writing to SD card.
-        
-        
-    } 
-    
-    for(int j =0 ; j<1; j++) // changed to 16 from 8
-    {
-        for(int i=0; i<640; i++)
+         }
+                
+        for(uint16_t i=0; i<640; i++)   //int to uint16_t-Suhasini-21stJuly2017
         {
             maxim_max30102_read_reg(0,&uch_dummy);
             while(INTR.read()==1); 
             maxim_max30102_read_fifo(&ppgdata); 
-            //drum21[i]=ppgdata; 
             ppg = sd_BP_ppgwrite (&ppgdata);         //Change by Nidhin 30/5/2017 Direct writing to SD card.
         }
         
-         
-        
-     }  
     fclose(ecg);
     fclose(ppg);
      
     maxim_max30102_reset();
     
-    //-----Reading from file starts //
-    
-    //std::ifstream ec1("/sd/123_BP_ECG.csv");
-    //std::ifstream pp1("/sd/123_BP_PPG.csv");
-    
-    int eloc = 0;
-    int ploc = 0;
+    uint16_t eloc = 0;  //int to uint16_t-Suhasini-21stJuly2017
+    uint16_t ploc = 0;  //int to uint16_t-Suhasini-21stJuly2017
    
    
    eloc = sd_BP_ECGMAX(pid); // eloc = sd_BP_ECGMAX(123);
    ploc = sd_BP_PPGMAX(pid); // ploc = sd_BP_PPGMAX(123);
-    
-   //bc.printf("The ECG max is at : %d", eloc);
-   //bc.printf("The PPG Max is at : %d", ploc);
+   bpr.printf("eloc: %d ploc: %d\n",eloc,ploc);
    
    float PWV = 0;
-   int d = 210000;
-   int delta_t = 0;
+   uint32_t d = 210000;     //int to uint32_t-Suhasini-21stJuly2017
+   uint16_t delta_t = 0;    //int to uint16_t-Suhasini-21stJuly2017
    float SBP = 0;
    float DBP = 0;
    
@@ -259,31 +201,23 @@
    
     SBP = 0.0508955*PWV+62.559;
     DBP = (0.0494*PWV + 17.480)+10;
-    
-    /*
-    //Copying to Structure starts
-    CalData_info CalData_info_bp;
-    CalData_info_bp.cal_sbp = (uint16_t) SBP;
-    CalData_info_bp.cal_dbp = (uint16_t) DBP; NOT REQUIRED
-    */
+    bpr.printf("sbp: %.0f dbp: %.0f\n",SBP,DBP);
     
     BLEMsg_info_bp.cal_data.cal_sbp_dummy = (uint16_t) SBP;
     BLEMsg_info_bp.cal_data.cal_dbp_OTtyp = (uint16_t) DBP;
     
      if((SBP>190 || SBP<70)  && (DBP>100 || DBP<40))              // checking for range below and above and then displaying error
-   {
+     {
         screen_bp_error();             // checking for error out of range- nikita 10-7-17
-     
-       } 
+        del_ppg_ecg_BPfile(pid);                          // Copy ECG and PPG data to BP file
+     } 
     
     else 
     { 
-    
         create_single_BPfile(pid);                          // Copy ECG and PPG data to BP file
         structure_file(ptr_BLEMsg_info_bp, pid);            // Copy BP structure to main file
-         bpfile_mainfile(pid);
-    
-         screen_bp1(SBP,DBP);
+        bpfile_mainfile(pid);
+        screen_bp1(SBP,DBP);
       }
   }  //Added Nidhin 16/6/17  
   
@@ -295,6 +229,82 @@
       screen_ecg_lead_bp_fingerdetect();
   }  //Added Nidhin 16/6/17 
      screen_ecg_bp() ; 
-    // Only for testing
- //} //Commented Nidhin 16/6/17 
-} 
\ No newline at end of file
+} 
+
+
+ //------------------------------------------  
+ //REOPEN FUNCTIONs for reading data Nidhin
+ 
+ uint16_t sd_BP_ECGMAX(int32_t pid)         
+ {
+  uint32_t ecgmax = 0;      // changed from int to uint32_t - suhasini - 21/7/17
+  uint32_t a[1] = {0};      // changed from int to uint32_t - suhasini - 21/7/17
+  uint16_t samplesecg =0;   // changed from int to uint32_t - suhasini - 21/7/17  
+  char buffer1[32];
+   
+  sprintf(buffer1,"/sd/%d_BP_ECG.csv",pid);
+  fpecg_bp = fopen(buffer1,"r"); 
+  if(fpecg_bp == NULL)
+  {
+            exit(1);
+  }
+  
+  else
+  {
+    for(ebp=0; ebp<1024; ebp++)
+    {
+
+    
+    fread(a,sizeof(uint32_t), 1,fpecg_bp);
+        if(a[0] > ecgmax)
+            {
+                ecgmax=a[0];
+                ecgloc= ebp +1;
+            }
+        else
+        samplesecg =ebp+1;
+    }
+    }
+   fclose (fpecg_bp);
+   return ecgloc; 
+ } 
+ 
+ // Function to calculate PPG MAx and location
+ 
+   uint16_t sd_BP_PPGMAX(int32_t pid)      //int to uint16_t-Suhasini-21stJuly2017
+ {
+  uint32_t ppgmax = 0;          //int to uint32_t-Suhasini-21stJuly2017
+  uint32_t b[1] ={0};           //int to uint32_t-Suhasini-21stJuly2017
+  uint16_t samplesppg = 0;      //int to uint16_t-Suhasini-21stJuly2017
+  char buffer1[32];
+   
+  sprintf(buffer1,"/sd/%d_BP_PPG.csv",pid);
+  fpppg1 = fopen(buffer1,"r"); 
+  if(fpppg1 == NULL)
+  {
+            exit(1);
+  }
+  
+  else
+  {
+    for(pbp=0; pbp<1664; pbp++)
+            {
+                fread(b, sizeof(uint32_t),1, fpppg1);
+                             
+                if((pbp>ecgloc) && (pbp< (ecgloc+350)))
+                    {
+                        if(b[0] > ppgmax)
+                            {
+                                ppgmax=b[0];
+                                ppgloc= pbp+1;
+                            }
+                        else
+                            samplesppg =pbp+1;
+                    }
+                else 
+                    samplesppg = pbp+1;
+            }
+    }        
+    fclose (fpppg1);
+    return ppgloc;
+ } 
\ No newline at end of file
--- a/bp.h	Sat Jul 29 10:38:05 2017 +0000
+++ b/bp.h	Sat Jul 29 11:23:19 2017 +0000
@@ -13,9 +13,9 @@
 #define ECG_SPLS BUFFER*ECG_LOOP
 #define PPG_SPLS BUFFER*PPG_LOOP
 #define BPLEN_BYT 10778 // TOTAL length in BYTEs for BP inclu Should change if BUffer is changed
-//#define 
 
 void bp(int32_t pid);
-
+uint16_t sd_BP_ECGMAX(int32_t pid); // updated- Suhasini-21/7/17
+uint16_t sd_BP_PPGMAX(int32_t pid); // updated- Suhasini-21/7/17
 
 #endif
\ No newline at end of file
--- a/bt.cpp	Sat Jul 29 10:38:05 2017 +0000
+++ b/bt.cpp	Sat Jul 29 11:23:19 2017 +0000
@@ -9,7 +9,7 @@
 #define NACK 0xFF
 #define sos 0xc0
 #define eos 0xc0
-#define no_of_samples 150
+#define no_of_samples 600   //changed the buffer size from 150- 22/7/17- suhasini
 #include "eeprom_pgm.h"
 //#define len 6000
 
@@ -91,9 +91,12 @@
                    
        case 2:   total_samples=0;   
                  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();                                // send the initial string
-                 blue.printf("\n");                       
+                 
+                 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;
                  
                  state_tx =0;
@@ -109,14 +112,14 @@
                      sd_read(v1,no_of_samples);      
                      counter=j+no_of_samples;               //read 1500 samples
                      send_data_str1(); 
+                     
                      for(file_start=j;file_start<(j+no_of_samples);file_start++)
                          {     
                        
                              blue.printf("%08x", v1[file_start]);
                            
                          }
-                         
-                         
+                          
                           check_sum=checksum((uint8_t *)v1,counter);               // calculating checksum            // calculating checksum
                           state_tx =0;
                           state_rx =3;
@@ -516,5 +519,4 @@
   chksum_datatrfr = sum & 0x000000FF;
  mc.printf("chksum_datatrfr=%02x\n",chksum_datatrfr);
   return chksum_datatrfr;
-}
-
+}
\ No newline at end of file
--- a/ec_bp.cpp	Sat Jul 29 10:38:05 2017 +0000
+++ b/ec_bp.cpp	Sat Jul 29 11:23:19 2017 +0000
@@ -329,9 +329,9 @@
    ADS_START = 1; 
   
   } 
-int readvalue()
+uint32_t readvalue()
   {
-      int concatenate_value;
+      uint32_t concatenate_value;
       while(DRDY_BAR);
     doWrite() ;
     concatenate_value = ((value4<< 16)|(value5 <<8) |(value6));
--- a/ec_bp.h	Sat Jul 29 10:38:05 2017 +0000
+++ b/ec_bp.h	Sat Jul 29 11:23:19 2017 +0000
@@ -93,7 +93,7 @@
 #define MAX_BRIGHTNESS 255 // BP ADD
  void setupfunc();
 uint8_t ecgsetupfunc();
- int readvalue();
+ uint32_t readvalue();
 #endif
 
 
--- a/ecgg.cpp	Sat Jul 29 10:38:05 2017 +0000
+++ b/ecgg.cpp	Sat Jul 29 11:23:19 2017 +0000
@@ -108,12 +108,13 @@
     Timer t;
     
     //------------------ Declaration for Peak value detection ------------------------------------
-  uint32_t ecg_samp1[1] ; uint32_t ecg_samp2[1]; uint32_t ecg_samp3[1];  // Buff 12 &3 Stores sample 1, 2 & 10th sample
+  uint32_t ecg_samp1[1] ; 
+  uint32_t ecg_samp2[1]; 
+  uint32_t ecg_samp3[1];  // to Stores sample 1, 2 & 10th sample
   uint32_t fppos;  // Variable to hold pointer position
   uint32_t hi_val;
   uint32_t pk_val[20];
   uint16_t pk_pos[20]={0};
- // unsigned int a; //uint32_t pk=0;
   int32_t hi_dif = 0; //diff between high value and it's consecutive value
   uint16_t j=0; // int count1 = N_ECG/fs, a_dif=0, fs1 = fs ,h=0;
   int32_t m =0;       // Variable to move the file pointer in fseek fun
@@ -126,15 +127,12 @@
  uint8_t t_sec = 60; 
  float HR_sum = 0,HR_avg;
     // -------------------------------------------------------------------------------------------
-  //  unsigned char chk = 1;
     pc.baud(baud_rate);
     freqset();               // setting the frequency
     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
  {  
@@ -151,23 +149,9 @@
      
    } 
   
-   //sd_close();          // closing the file COMMENTED Nidhin 1/6/2017
-    sd_close_ecg();          // closing the ECG file REPLACED Nidhin 1/6/2017 Nidhin
+ sd_close_ecg();          // closing the ECG file REPLACED Nidhin 1/6/2017 Nidhin
     
     
-/*   
- // ---------- reading back SD data for processing --------------------
- sd_read_file(15);
- printf("Reading back SD data\n");
- for(int i=10;i<N_ECG;i++)
- {
- ecg_buf= sd_read();
- //printf("%d\n",ecg_buf[i]);
- }
- 
-  sd_close();    */
- 
-
  //----------------------------- PEAK DETECTION AND HEART RATE CALCULATION ---------------------------------------------------
  
  // -------------------------------------- PEAK DETECTION -------------------------------------------------------------
@@ -184,24 +168,16 @@
 fread(ecg_samp2, sizeof(uint32_t), 1, fpeecg1);  // Read Sample 2
 fseek(fpeecg1, samp_10, SEEK_CUR);   // Moving to tenth sample
 fread(ecg_samp3, sizeof(uint32_t), 1, fpeecg1);  // Read 3rd sample
-//pc.printf("ecg_samp1 = %d , ecg_samp2 = %d, ecg_samp3 = %d\n",ecg_samp1[0],ecg_samp2[0],ecg_samp3[0] ); //Test  Value held by buffer each round
-
 
 if(ecg_samp1[0]>ecg_samp2[0])
 {
 hi_val = ecg_samp1[0]; //To find the high value
-//printf("high value= %d\n",hi_val);
-//a = i;
-//printf("a= %d\n",a);
 hi_dif = hi_val-ecg_samp3[0];
 
   // ---------------------------- If hi_val is greater than next ten input values, then compare the hi_val with the tenth input value. 
   //     If the diff is greater than 10000, then it is a valid peak (pls chk the below condition)-------------------------------------
-  //   if((hi_dif > 10000) && ((a+10) < N_ECG))
     if(hi_dif > 10000)
     {
-   //  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]);
      pk_pos[j]=i; // also save the peak's position
@@ -211,16 +187,13 @@
      j = j+1;
      m = m + 480; //similar reason to considering 28, but to skip 120 samples. this cond. is satisfied only when we hit a peak - suhasini_26thjune17
      
-  //   printf("j after peak detection is= %d\n",j);
-  //   }
-     }
+    }
      
     else
     {
      m = m+4; // this is when we do not hit a peak and have to continue searching thru, hence move to the next sample and not skip 120 samples- - suhasini_26thjune17
     }
-     // store the peak value position in "pk_pos" 
-}
+ }
 else
 {
 m = m+4;
@@ -236,12 +209,10 @@
  {
   pos_dif = pk_pos[i+1] - pk_pos[i];                 // difference between two consequtive peaks
   pc.printf("peak position diff is = %f\n",pos_dif);
-  //printf("peak position i value is = %d\n",i);
   t_pos_dif = pos_dif/fs;                           // sample difference between peak positions divided by sampling frequency gives the difference value in terms of actual time
   pc.printf("time in seconds is = %f\n",t_pos_dif);
   HR[i] = t_sec/t_pos_dif;                          //HR calculation
   pc.printf("Heart Rate is = %f\n",HR[i]);
- // n = i;
   HR1 = HR[0];
  }
  
@@ -253,7 +224,6 @@
  }
  HR_avg = HR_sum/(n-1); // To find average of all the individual HRs calculated
  printf("Heart Rate sum is = %f\n",HR_sum);
- //printf("Denominator = %d\n",n);
  printf("Heart Rate avg is = %f\n",HR_avg);
  
  fclose(fpeecg1);
@@ -261,33 +231,25 @@
  
 if(HR_avg>100 || HR_avg<40) 
     {
-        
         return 1;               // out of range condition returning 1  //nikita//10/7
-        
         }
  else 
      {
  
         BLEMsg_info_ecg.cal_data.cal_sbp_dummy = 0;
-      BLEMsg_info_ecg.cal_data.cal_dbp_OTtyp = HR_avg;  //To be modified after HR code is added. // changed nikita
+        BLEMsg_info_ecg.cal_data.cal_dbp_OTtyp = HR_avg;  //To be modified after HR code is added. 
 
         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("improper lead connection");
-        return 0;                        // returning 0 for improper lead connection
+        return 0;
     } 
 pc.printf("closing temporary file\n");
- // fclose(fpeecg1);
-//  pc.printf("temporary file closed\n");
 }   // End of main function
\ No newline at end of file
--- a/sdcard.cpp	Sat Jul 29 10:38:05 2017 +0000
+++ b/sdcard.cpp	Sat Jul 29 11:23:19 2017 +0000
@@ -25,14 +25,6 @@
   
   FILE *fp;    // FILE pointer for random use
   
-  
-  
-  int ecgloc = 0;  // PPG and eCG maximum locations
-  int ppgloc = 0;  
-  
-  int ebp = 0;      // variable for LOOP operation to read value from File i == ECG , j == PPG
-  int pbp = 0;
-  
   //Function Added on 16/5/2017 by Nidhin
  void sd_open_mainfile(int32_t pid)
  {
@@ -207,9 +199,9 @@
  //------------------------------------------------------
   
 //FILE *sd_BP_ecgwrite (int value)      // writing data into the file NIDHIN
-FILE *sd_BP_ecgwrite (int32_t *fpdrum11)      // writing data into the file NIDHIN  
+FILE *sd_BP_ecgwrite (uint32_t *fpdrum11)      // writing data into the file NIDHIN  
   {
-        fwrite(fpdrum11,sizeof(int32_t),1, fpecg); //Change by Nidhin 30/5/2017 Direct writing to SD card. Org Val 64
+        fwrite(fpdrum11,sizeof(uint32_t),1, fpecg); //Change by Nidhin 30/5/2017 Direct writing to SD card. Org Val 64
         return fpecg;
   }
     
@@ -306,88 +298,6 @@
  
  
  
- //------------------------------------------  
- //REOPEN FUNCTIONs for reading data Nidhin
- 
- int sd_BP_ECGMAX(int32_t pid)         
- {
-     
-  char buffer1[32];
-   
-  sprintf(buffer1,"/sd/%d_BP_ECG.csv",pid);
-  fpecg = fopen(buffer1,"r"); 
-  if(fpecg == NULL)
-  {
-            exit(1);
-  }
-  
-  else
-  {
-    int ecgmax = 0;
-    int a[1] = {0};
-    int samplesecg =0;
-for(ebp=0; ebp<1024; ebp++)
-    {
-
-    
-    fread(a,sizeof(uint32_t), 1,fpecg);
-    // snd.printf("\n%d", a[0]); Commented May3_2017 (Nidhin)
-        if(a[0] > ecgmax)
-            {
-                ecgmax=a[0];
-                ecgloc= ebp +1;
-            }
-        else
-        samplesecg =ebp+1;
-    
-    }
-    }
-    fclose (fpecg);
-   return ecgloc; 
- } 
- 
- // Function to calculate PPG MAx and location
- 
-   int sd_BP_PPGMAX(int32_t pid)         
- {
-     
-  char buffer1[32];
-   
-  sprintf(buffer1,"/sd/%d_BP_PPG.csv",pid);
-  fpppg = fopen(buffer1,"r"); 
-  if(fpppg == NULL)
-  {
-            exit(1);
-  }
-  
-  else
-  {
-    uint32_t ppgmax = 0;
-    uint32_t b[1] ={0};
-    int samplesppg = 0;
-       for(pbp=0; pbp<1664; pbp++)
-            {
-                fread(b, sizeof(uint32_t),1, fpppg);
-                // snd.printf("\n%d", b[0]);   Commented May3_2017 (Nidhin)
-                
-                if((pbp>ecgloc) && (pbp< (ecgloc+350)))
-                    {
-                        if(b[0] > ppgmax)
-                            {
-                                ppgmax=b[0];
-                                ppgloc= pbp+1;
-                            }
-                        else
-                            samplesppg =pbp+1;
-                    }
-                else 
-                    samplesppg = pbp+1;
-            }
-    }        
-    fclose (fpppg);
-    return ppgloc;
- } 
- 
 //fread(buffer, 1, size, fp)
 //fwrite(&buffer, ,   
  //----------------------------------------------------28/4/2017---------------------------------
@@ -456,6 +366,8 @@
        
 }
 
+
+
 //Creating strcuture file Common to All Functions
 void structure_file(BLEMsg_info *ptr, int32_t pid)
 {
@@ -605,3 +517,27 @@
 }
 
 
+void del_ppg_ecg_BPfile(int32_t pid)
+{
+    int status = 1 ;
+    char buffer3[32];
+    sprintf(buffer3, "/sd/%d_BP_ECG.csv", pid);
+    status = remove(buffer3);                         //Removes ECG file of BP and updates status with 0 if successful
+        
+    if( status == 0 )
+        snd.printf("\n temp ECG file delete for error condition: Successful");   // Checks for SUCCESS
+    else 
+        snd.printf("\n temp ECG file delete for error condition: UnSuccessful");
+   
+    
+    //char buffer1[32] = "";
+    status = 1 ;
+    sprintf(buffer3, "/sd/%d_BP_PPG.csv", pid);
+    status = remove(buffer3);                           //Removes PPG file of BP and updates status with 0 if successful
+    
+    if( status == 0 )
+        snd.printf("\n temp PPG file delete for error condition: Successful");   // Checks for SUCCESS
+    else 
+        snd.printf("\n temp PPG file delete for error condition: UnSuccessful");
+       
+}
\ No newline at end of file
--- a/sdcard.h	Sat Jul 29 10:38:05 2017 +0000
+++ b/sdcard.h	Sat Jul 29 11:23:19 2017 +0000
@@ -23,7 +23,7 @@
 //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_ecgwrite (uint32_t *fpdrum11);
 FILE *sd_BP_ppgwrite (uint32_t *fpdrum21);
  
 void sd_open_BP_ECGfile(int32_t pid); //added to write ECG to seperate file BP
@@ -33,8 +33,6 @@
  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(int32_t pid);
-int sd_BP_PPGMAX(int32_t pid);
 //void sd_close1();
 
 void create_single_BPfile(int32_t pid); //Function to create 1 file from 2 temp BP file (ECG &PPG)
@@ -53,5 +51,7 @@
 
 void delete_subfiles(int32_t pid);  //deletes all subfiles under a pid
 
+void del_ppg_ecg_BPfile(int32_t pid); // deletes temporary ECG_PPG files in BP when ERROR condition- 22/7/17-Suhasini
+
 
 #endif 
\ No newline at end of file