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:
36:00d96aa14658
Parent:
31:0f67acfc6236
Child:
39:69b09bd87502
--- 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