Fix all the following issues which we faced during 01st April internal demo 1) Display hang and jump issue 2) Sequencing issue 3) Issue with BP code

Dependencies:   SDFileSystem ds3231 program mbed testUniGraphic_150217

Fork of Int_Demo_01Apr2017_newdisplaycode_bp_nikita by nikita teggi

sdcard.cpp

Committer:
nikitateggi
Date:
2017-05-08
Revision:
12:ab85607bb10a
Parent:
11:9a6545c2eb5d

File content as of revision 12:ab85607bb10a:

 /*
 #include "mbed.h"
 #include <stdio.h>
 #include "SDFileSystem.h"
 #include "sdcard.h"
  #include "rtc.h"
  

  SDFileSystem sd(PTE1, PTE3, PTE2, PTE4, "sd");
  FILE *fp=NULL;

 void sd_open_GLCfile(int pid)     // opening the glc file
 {
     
  char buffer[32];
  char buffer1[32];
  time_t  epoch_time1;
  epoch_time1=rtc_read();
 // strftime(buffer, 32, "GLC_%s", pid);
  
 sprintf(buffer1,"/sd/%d_GLC.csv",pid);   
  fp = fopen(buffer1,"a"); 
  fprintf(fp,"%s\n",ctime(&epoch_time1) );  //writing the date, time into the file
  
 
 }   
 
  void sd_open_GLCfilee(int pid)    // only opening the glc file after entering TEST screen
 {
     

  char buffer1[32];
  sprintf(buffer1,"/sd/%d_GLC.csv",pid);
  fp = fopen(buffer1,"a"); 
   fprintf(fp,"PATIENT ID %d\n",pid);
  
 
 } 
 
  void sd_open_ECGfile(int pid)         
 {
     
 // char buffer[32];
  char buffer1[32];
  time_t  epoch_time1;
  epoch_time1=rtc_read();
  // strftime(buffer, 32, "ECG_%d", pid);
  
  sprintf(buffer1,"/sd/%d_ECG.csv",pid);
  fp = fopen(buffer1,"a"); 
  fprintf(fp,"%s\n",ctime(&epoch_time1) );
 
 } 
 
  void sd_open_ECGfilee(int pid)          // only opening the ECG file after entering TEST screen
 {
     
  char buffer1[32];
   sprintf(buffer1,"/sd/%d_ECG.csv",pid);
  fp = fopen(buffer1,"a"); 
  fprintf(fp,"PATIENT ID %d\n",pid );
 
 } 
 
 
  void sd_open_BPfile(int pid)         
 {
     
  char buffer[32];
  char buffer1[32];
  time_t  epoch_time1;
  epoch_time1=rtc_read();
  // strftime(buffer, 32, "ECG_%d", pid);
  
  sprintf(buffer1,"/sd/%d_BP.csv",pid);
  fp = fopen(buffer1,"a"); 
  fprintf(fp,"%s\n",ctime(&epoch_time1) );
 } 

void sd_open_BPfilee(int pid)          // only opening the BP file after entering TEST screen
 {
     
 
  char buffer1[32];
  sprintf(buffer1,"/sd/%d_BP.csv",pid);
  fp = fopen(buffer1,"a"); 
  fprintf(fp,"PATIENT ID %d\n",pid );
 
 } 
  
 void sd_write (int value)      // writing data into the file
 {
  
  fprintf(fp,"%d\n", value); 
  }
  
  void sd_close()            //closing the file
  {
   fclose(fp);
   }*/
 
 //----------------------------------------NEW ADD NIDHIN 28/4/2017-------------------------------//
 
 #include "mbed.h"
 #include <stdio.h>
 #include "SDFileSystem.h"
 #include "sdcard.h"
  #include "rtc.h"
  
  Serial snd(USBTX,USBRX);
  

  SDFileSystem sd(PTE1, PTE3, PTE2, PTE4, "sd");
  FILE *fp;
  FILE *fpecg;  // PPG and ECG File pointers for BP
  FILE *fpppg;
  
  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;
  
  
 void sd_open_GLCfile(int pid)     // opening the glc file
 {
     
  char buffer[32];
  char buffer1[32];
  time_t  epoch_time1;
  epoch_time1=rtc_read();
 // strftime(buffer, 32, "GLC_%s", pid);
  
 sprintf(buffer1,"/sd/%d_GLC.csv",pid);   
  fp = fopen(buffer1,"a"); 
  fprintf(fp,"%s\n",ctime(&epoch_time1) );  //writing the date, time into the file
  
 
 }   
 
  void sd_open_GLCfilee(int pid)    // only opening the glc file after entering TEST screen
 {
     

  char buffer1[32];
  sprintf(buffer1,"/sd/%d_GLC.csv",pid);
  fp = fopen(buffer1,"a"); 
   fprintf(fp,"PATIENT ID %d\n",pid);
  
 
 } 
 
  void sd_open_ECGfile(int pid)         
 {
     
 // char buffer[32];
  char buffer1[32];
  time_t  epoch_time1;
  epoch_time1=rtc_read();
  // strftime(buffer, 32, "ECG_%d", pid);
  
  sprintf(buffer1,"/sd/%d_ECG.csv",pid);
  fp = fopen(buffer1,"a"); 
  fprintf(fp,"%s\n",ctime(&epoch_time1) );
 
 } 
 
  void sd_open_ECGfilee(int pid)          // only opening the ECG file after entering TEST screen
 {
     
  char buffer1[32];
   sprintf(buffer1,"/sd/%d_ECG.csv",pid);
  fp = fopen(buffer1,"a"); 
  fprintf(fp,"PATIENT ID %d\n",pid );
 
 } 
 
 
  void sd_open_BPfile(int pid)         
 {
     
  char buffer[32];
  char buffer1[32];
  time_t  epoch_time1;
  epoch_time1=rtc_read();
  // strftime(buffer, 32, "ECG_%d", pid);
  
  sprintf(buffer1,"/sd/%d_BP.csv",pid);
  fp = fopen(buffer1,"a"); 
  fprintf(fp,"%s\n",ctime(&epoch_time1) );
 } 

void sd_open_BPfilee(int pid)          // only opening the BP file after entering TEST screen
 {
     
 
  char buffer1[32];
  sprintf(buffer1,"/sd/%d_BP.csv",pid);
  fp = fopen(buffer1,"a"); 
  fprintf(fp,"PATIENT ID %d\n",pid );
 
 } 
  
 void sd_write (int value)      // writing data into the file
 {
    fprintf(fp,"%d\n", value);
 }
 
 //------------------------------------------------------
  
//FILE *sd_BP_ecgwrite (int value)      // writing data into the file NIDHIN
FILE *sd_BP_ecgwrite (int *fpdrum11)      // writing data into the file NIDHIN
  {
        fwrite(fpdrum11,sizeof(int),64, fpecg); 
        return fpecg;
  }
    
FILE* sd_BP_ppgwrite (uint32_t *fpdrum21)      // writing data into the file NIDHIN
  {
       fwrite(fpdrum21,sizeof(uint32_t),64, fpppg); 
    return fpppg;
  }
  //-----------------------------------------------SD Read
  
  void sd_reopen_BPfilee(int pid)          // only opening the BP file after entering TEST screen
 {
     
 
  char buffer1[32];
  sprintf(buffer1,"/sd/%d_BP.csv",pid);
  fp = fopen(buffer1,"r"); 
  //fprintf(fp,"PATIENT ID %d\n",pid );
 
 
 } 
  //-------------------------------------------------


//---------------------------------------


   void sd_open_BP_ECGfile(int pid)         
 {
     
  //char buffer[32];
  char buffer1[32];
  //time_t  epoch_time1;
  //epoch_time1=rtc_read();
  // strftime(buffer, 32, "ECG_%d", pid);
  
  sprintf(buffer1,"/sd/%d_BP_ECG.csv",pid);
  fpecg = fopen(buffer1,"a"); 
  if (fpecg == NULL)
  {
      exit(1);
  }   
  //fprintf(fpecg,"%s\n",ctime(&epoch_time1) );
  
 } 
 
  
   void sd_open_BP_PPGfile(int pid)         
 {
     
  char buffer1[32];
  //time_t  epoch_time1;
  //epoch_time1=rtc_read();
  // strftime(buffer, 32, "ECG_%d", pid);
  
  sprintf(buffer1,"/sd/%d_BP_PPG.csv",pid);
  fpppg = fopen(buffer1,"a"); 
  if(fpppg == NULL)
  {
            exit(1);
  }
  
  //fprintf(fpppg,"%s\n",ctime(&epoch_time1) );
  
 } 
  
  
  
  //--------------------------------------------------
  
//void sd_bp_read(  
  
void sd_close()            //closing the file
  {
   
      fclose(fp);
   }
 //------------------------------------------  
 //REOPEN FUNCTIONs for reading data Nidhin
 
 int sd_BP_ECGMAX(int 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(int), 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(int 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---------------------------------
 //----------------------------------------------------------------------------------------------
 
 
 
  //------- FUNCTION TO CREATE SINGLE BP FILE --------2/5/2017 (NIDHIN)---------------//
 void create_single_BPfile(int pid)
{
    
    //int ecgbuf[64] = {0};
    //uint32_t ppgbuf[64] = {0};
    
    char buffer3[32];
    sprintf(buffer3, "/sd/%d_BP.csv", pid);
    fp = fopen(buffer3, "a");

    //buffer1[32] = ""; 
    sprintf(buffer3, "/sd/%d_BP_ECG.csv", pid);
    fpecg = fopen(buffer3, "r");

    //char buffer1[32] = "";
    sprintf(buffer3, "/sd/%d_BP_PPG.csv", pid);
    fpppg = fopen(buffer3, "r");

    for(int i=0; i<16; i++)
    {
    int ecgbuf[64] = {0} ;
    fread(ecgbuf, sizeof(int), 64, fpecg);
    fwrite(ecgbuf, sizeof(int), 64, fp);
    }

    for(int i=0; i<26; i++)
    {
    int ppgbuf[64] = {0} ;
    fread(ppgbuf, sizeof(int), 64, fpppg);
    fwrite(ppgbuf, sizeof(int), 64, fp);
    }

    fclose(fpecg);
    fclose(fpppg);
    fclose(fp);

    //char buffer1[32] = "";
    int status = 0 ;
    sprintf(buffer3, "/sd/%d_BP_ECG.csv", pid);
    status = remove(buffer3);
    
    
    if( status == 0 )
        snd.printf("\nECG file delete : Successful");
    else 
        snd.printf("\nECG file delete : UnSuccessful");
   
    
    //char buffer1[32] = "";
    status = 0 ;
    sprintf(buffer3, "/sd/%d_BP_PPG.csv", pid);
    status = remove(buffer3);
    
    if( status == 0 )
        snd.printf("\nPPG file delete : Successful");
    else 
        snd.printf("\nPPG file delete : UnSuccessful");
       
}