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:
rashmivenkataramaiah
Date:
Fri Sep 08 10:35:21 2017 +0000
Parent:
40:f7f351699288
Child:
42:c81673b04b6a
Commit message:
Modified PID tracking; 1 First open the Pid in write mode and then in append mode; 2 PID to increment only after a test is completed; 3 PID stored in SD card;

Changed in this revision

bp.cpp 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
glc.cpp 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
main.h 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
touch_modules.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/bp.cpp	Sun Aug 20 16:21:54 2017 +0000
+++ b/bp.cpp	Fri Sep 08 10:35:21 2017 +0000
@@ -16,6 +16,7 @@
 #include "bp.h"
 #include "display_modules.h"
 
+
 #define MAX_SBP_THRESHOLD 300
 #define MAX_DBP_THRESHOLD 300
 #define MIN_SBP_THRESHOLD 40
@@ -224,6 +225,7 @@
         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);
+        increment_pid ();                                   //increment pid if test is pass 
         screen_bp1(SBP,DBP);
       }
   }  //Added Nidhin 16/6/17  
--- a/ecgg.cpp	Sun Aug 20 16:21:54 2017 +0000
+++ b/ecgg.cpp	Fri Sep 08 10:35:21 2017 +0000
@@ -14,6 +14,7 @@
 #include "ec_bp.h"
 #include "eeprom_pgm.h"
 #include "struct.h"  // Added on 31/5/17_Nidhin
+
 //#include "test_type.h" //Added on  31/5/17_Nidhin
 
 #define MAX_HR_THRESHOLD 205
@@ -243,11 +244,11 @@
      {
  
         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. 
+        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
-
+        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
+        increment_pid ();                                   //increment pid if test is pass 
         pc.printf("Closed the main file\n");
         return HR_avg; 
   }
--- a/glc.cpp	Sun Aug 20 16:21:54 2017 +0000
+++ b/glc.cpp	Fri Sep 08 10:35:21 2017 +0000
@@ -263,7 +263,8 @@
         BLEMsg_info_glc.cal_data.cal_dbp_OTtyp = (uint16_t) mgdl;  //To be checked for reliable results           
             
         structure_file(ptr_BLEMsg_info_glc, pid);           // Copy the structure into the GLC file 
-        glcfile_mainfile(pid); 
+        glcfile_mainfile(pid);
+        increment_pid ();                                   //increment pid if test is pass
        }
   else if(err == 1 || mgdl > MAX_GLC_THRESHOLD || mgdl < MIN_GLC_THRESHOLD)      // added threshold to check value out of range   
     {  
--- a/main.cpp	Sun Aug 20 16:21:54 2017 +0000
+++ b/main.cpp	Fri Sep 08 10:35:21 2017 +0000
@@ -12,106 +12,78 @@
 #include "eeprom_pgm.h"
 #include "struct.h"
 #include "battery.h" 
+#include "main.h"
 
 Serial gc(USBTX, USBRX);
 DigitalIn q1(PTB11);
 DigitalIn q(PTC5);
 DigitalIn q3(PTC12);
-DigitalIn BT(PTA5);                     //bluetooth status pin
-static uint32_t current_test_pid=0;
-static uint32_t  bt_file_sent_pid=0; 
-uint8_t touch(uint8_t state);           // touch state
-uint8_t display(uint8_t state1);       //  display state
-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
-static uint8_t ecg_flag=0;         // ecg_flag to indicate the ecg test is already done for the same pid
-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
-uint16_t heart_rate=0;          
+DigitalIn BT(PTA5);                                                  //bluetooth status pin
+
+int32_t pid = 0;                                                    // patient ID
+bool file_created_status = 0x0000;                                  //initialise file created to 0
 
-
-int main(){
-static uint8_t main_state=1;
-static uint8_t state_display=0;
-static uint8_t state_touch=1;
-static uint8_t state_t=1;
-static uint8_t state_r=0;
-uint8_t battery_voltage_condition=0;
+int main()
+{
+    static uint8_t main_state=1;
+    static uint8_t state_display = 0;
+    static uint8_t state_touch = 1;
+    static uint8_t state_t = 1;                                     // state to transmit to bluetooth
+    static uint8_t state_r = 0;                                     // state to receive from bluetooth
+    uint8_t battery_voltage_condition = 0;
   
   
- DisableTouch();
- gc.baud(115200);
- init_screen();    //initialize lcd
- screen_main(); // display of main screen 
+    DisableTouch();
+    gc.baud(115200);
+    init_screen();                                                  //initialize lcd
+    sd_read(pid_btpid);                                             //read test pid and bt pid from sd card
+    pid = pid_btpid[0];                                             //copy test pid to pid variable
+    screen_main();                                                  // display of main screen 
 
 
    while(1)
    {
-           /*DisableTouch();
-           battery_voltage_condition=battery_voltage();
-    
-   if (battery_voltage_condition==3)
-         {
-             DisableTouch();
-         }
-              
-                         
-  else
-       {       */      
     
      EnableTouch();
-     switch(main_state)                          // main state machine
+     switch(main_state)                                             // main state machine
      {
-         case 1:  state_display=touch(state_touch);           // touch state 
-                   if(state_display==12)
-                      main_state=3;
-                    
-                  else 
-                       main_state=2;
-                  break;
+         case 1:  
+                    state_display = touch(state_touch);             // touch state 
+                    if(state_display == 12)
+                        main_state = 3;
+                    else 
+                        main_state = 2;
+                    break;
     
-         case 2:  state_touch=display(state_display);          //display state
-           /*        if (b_voltage<3.5)
-                     {
-                         bat_screen_nocharge();
-                         DisableTouch();
-                     }
-              else    {                        */
-                    //    bat_screen_charge();
-                       main_state=1;
-                    //   }
+         case 2:    
+                    state_touch = display(state_display);           //display state
+                    main_state = 1;
                     break;
                 
                 
-         case 3: DisableTouch();                             // bluetooth send 
-                 if(BT==1)
-                 {
-                    
-                    BT_connection() ;    // displaying connection on bluetooth 
-                    state_r=bt_send(state_t);
-                    main_state=4;
-                 }
-               else   
-               {
-                 BT_no_connection();  
-                 gc.printf("timeout");
-                 }
-                 break;
+         case 3: 
+                    DisableTouch();                                 // bluetooth send 
+                    if(BT==1)
+                    {
+                        BT_connection() ;                           // displaying connection on bluetooth 
+                        state_r=bt_send(state_t);
+                        main_state=4;
+                    }
+                    else   
+                        BT_no_connection();  
+                            
+                    break;
                  
          case 4:  DisableTouch();  
-                 state_t=bt_receive(state_r);               // bluetooth receive
+                 state_t=bt_receive(state_r);                       // bluetooth receive
                  main_state=3;
-                // gc.printf("statet=%d",state_t);
                  if(state_t ==0)
                  {
                     BT_finished();
                     screen_BT_return();  
                     EnableTouch();
                     main_state=1;
-                    state_touch=12;  // jumping to touch case 12
+                    state_touch=12;                                 // jumping to touch case 12
                    } 
                  break;    
         }             
@@ -119,236 +91,217 @@
     }
 }
 
-//}
-
-//}
-
-
 uint8_t touch(uint8_t state)
 {
    static  uint8_t state1=0;
-   detect_touch(state);     // determine the touch points
+   detect_touch(state);                                             // determine the touch points
     
-    switch(state)    // state for touch
+    switch(state)                                                   // state for touch
   {
-      case 1:   state1=touch_main();   // determining the touch for main screen 
+      case 1:   
+                state1=touch_main();                                // determining the touch for main screen 
                 state=0;
                 nstate1=state1;
-                 break;
+                break;
                
       case 2: 
-                state1=touch_main_1();    // determining the touch for SYSTEM CONFIGURATION screen 
+                state1=touch_main_1();                              // determining the touch for SYSTEM CONFIGURATION screen 
                 state=0;
                 nstate1=state1;
                 break;
                 
       case 3:  
-               state1=touch_main_2();     //// determining the touch for TEST screen 
-                                 
+                state1=touch_main_2();                              // determining the touch for TEST screen                 
                 state=0;
                 nstate1=state1;
                 break;
         
       case 4: 
-                state1=touch_glc();     // determining the touch for GLC screen 
+                state1=touch_glc();                                 // determining the touch for GLC screen 
                 state=0;
                 nstate1=state1;
                 break;       
         
       case 5: 
-                state1=touch_ecg();     // determining the touch for ECG screen 
+                state1=touch_ecg();                                 // determining the touch for ECG screen 
                 state=0;
                 nstate1=state1;
                  break;   
      case 6: 
-                state1=touch_ret();     // determining the touch for returning back to the main screen 
-               /* if (ecg_flag==1)
-                     state1=8;
-                else if (glc_flag==1)
-                    state1=7; */     
+                state1=touch_ret();                                 // determining the touch for returning back to the main screen      
                 state=0;
                 nstate1=state1;
                 break;
                 
      case 7: 
-                state1=touch_again_ecg();     // determining the touch when readings are taken more than once screen 
+                state1=touch_again_ecg();                          // determining the touch when readings are taken more than once screen 
                 state=0;
                 nstate1=state1;
                 break;
                 
      case 8:    
-                state1=touch_bp();     // determining the touch for BP screen 
+                state1=touch_bp();                                  // determining the touch for BP screen 
                 state=0;
                 nstate1=state1;
                 break;           
                 
-     case 9:    state1=touch_again_glc();  
+     case 9:    
+                state1=touch_again_glc();  
                 state=0;
                 nstate1=state1;
                 break;            
                 
-     case 10 :  DisableTouch();
-                 wait_ms(300);
-                 EnableTouch();
-                 state=nstate;
-                  break;
+     case 10 :  
+                DisableTouch();
+                wait_ms(300);
+                EnableTouch();
+                state=nstate;
+                break;
                   
                   
-      case 11:   state1=touch_again_bp();            
-                 state=0;
-                 nstate1=state1;
-                 break;
+      case 11:   
+                state1=touch_again_bp();            
+                state=0;
+                nstate1=state1;
+                break;
       
-      case 12:   state1=BT_return();            
-                 state=0;
-                 nstate1=state1;
-                 break;
+      case 12:   
+                state1=BT_return();            
+                state=0;
+                nstate1=state1;
+                break;
       
-     default :  DisableTouch();
-                 wait_ms(300);
-                 EnableTouch();
-                 state=nstate;
-                  break;          
+     default :  
+                DisableTouch();
+                wait_ms(300);
+                EnableTouch();
+                state=nstate;
+                break;          
                           
     }         
      
-return state1;
-  }
+    return state1;
+}
   
   
 uint8_t display(uint8_t state1)
 {
-  static uint8_t state=0;
-  
- switch(state1)    // state for display screens
-  {
+    static uint8_t state=0;
+    static uint8_t ecg_flag=0;                                  // ecg_flag to indicate the ecg test is already done for the same pid
+    static uint8_t glc_flag=0;                                  // glc_flag to indicate the glc test is already done for the same pid
+    uint16_t heart_rate=0; 
+    
+    
+    switch(state1)                                              // state for display screens
+    {
 
       
-      case 1: DisableTouch();
-                screen_main();        //  main screen 
+      case 1: 
+                DisableTouch();
+                screen_main();                                  //  main screen 
                 state1=0;
                 state=1;
                 nstate=state;
-                flag=0;
-                ecg_flag=0;             // ecg, glc flag to determine whether the readings are taken more than once
+                ecg_flag=0;                                     // ecg, glc flag to determine whether the readings are taken more than once
                 glc_flag=0;
+                clear_filecreated_status();
+                sd_read(pid_btpid);                             //read pid from sd card
+                pid=pid_btpid[0];                               //copy pid to a variable
                 EnableTouch();
                 break;
       
-      case 2: DisableTouch();
-                 // bt();
-                screen_main_1();     // System configration screen 
+      case 2: 
+                DisableTouch();
+                screen_main_1();                                // System configration screen 
                 state1=0;
                 state=2;
                 nstate=state;
                 EnableTouch();
                 break;
                
-      case 3: DisableTouch();
-              if (flag==0)
-              {
-                //pid=pid+1;
-                pid=eprom_read(1);         // reading the patient id from eeprom
+      case 3: 
+                DisableTouch();
                 gc.printf("pidinit=%d",pid);
-                pid=pid+1;
-                gc.printf("pid+1=%d",pid);
-               eprom_write(1,pid);         // writing the patient id to eeprom
-                screen_main_2(pid);
-              /*  sd_open_GLCfilee(pid);     // only opening the glc,bp and ecg file
-                 sd_close();
-                 sd_open_ECGfilee(pid);
-                 sd_close();
-                 sd_open_BPfilee(pid);
-                 sd_close();*/
-                 flag=1;
-                 }
-               else
-               {
-               screen_main_2(pid);          // TEST screen 
-               }
-                 
-                 state1=0;
-                 state=3;
-                 nstate=state;
-                 EnableTouch();
-                  break;
+                screen_main_2(pid);     
+                state1=0;
+                state=3;
+                nstate=state;
+                EnableTouch();
+                break;
                 
-      case 4: DisableTouch();
-               if (glc_flag==1)         // glc_flag=1 when already the reading has been taken 
-               {
-                  screen_again();    
-                 state1=0;
-                 state=9;
-                 nstate=state;
-                 }
-            else
+      case 4: 
+                DisableTouch();
+                if (glc_flag==1)                                // glc_flag=1 when already the reading has been taken 
+                {
+                    screen_again();    
+                    state1=0;
+                    state=9;
+                    nstate=state;
+                }
+                else
                 { 
-                 screen_glc();       // glc screen
-                 state1=0;
-                 state=4;
-                 nstate=state;
-                 }
+                    screen_glc();                                       // glc screen
+                    state1=0;
+                    state=4;
+                    nstate=state;
+                }
                 EnableTouch();
                 
                 break;
      
      
-     case 5:     DisableTouch();
-               if (ecg_flag==1)
-               {
-                  screen_again();
-                  state1=0;
-                   state=7;
+     case 5:     
+                DisableTouch();
+                if (ecg_flag==1)
+                {
+                    screen_again();
+                    state1=0;
+                    state=7;
                     nstate=state;
-                    }
-                    
-                 else
-                 {   
-                  screen_ecg();       //ecg main screen 
-                   state1=0;
-                   state=5;
+                }
+                
+                else
+                {   
+                    screen_ecg();                                           //ecg main screen 
+                    state1=0;
+                    state=5;
                     nstate=state;
-                    }
-                   EnableTouch();
+                }
+                EnableTouch();
                 break;
      
-     case 6:   DisableTouch();
-               screen_bp();          // bp main screen
-               //EnableTouch();
+     case 6:   
+                DisableTouch();
+                screen_bp();                                                 // bp main screen
                 state1=0;
                 state=8;
-                nstate=state;         ///////
-                EnableTouch();        ///////
+                nstate=state;         
+                EnableTouch();        
                 break;
      
-     
-     
-     
-     
-     case 7:    DisableTouch();     
-                glc(pid);        // glc measurement
-             //  bt();
+     case 7:    
+                DisableTouch();     
+                glc(pid);                                                       // glc measurement
                 glc_flag=1;
                 wait(1); 
                 state1=0;
                 screen_glc_2();
-                 state=6;
-                 eprom_write_8(30,0);
-              //  gc.printf("%d", state);
-              nstate=state;         //////////////
-               EnableTouch();
+                state=6;
+                eprom_write_8(30,0);
+                nstate=state;        
+                EnableTouch();
                 break;
     
     case 8:     DisableTouch();
-                ecg_countdown();    // countdown before ecg capture
-                heart_rate=ecg(pid);           // capturing ecg
+                ecg_countdown();                                                // countdown before ecg capture
+                heart_rate=ecg(pid);                                            // capturing ecg
                 state1=0;
                 if (heart_rate==0)
                     {
                        
                        screen_ecg_lead();  
                      }
-                else if(heart_rate==1)   // if heart rate goes above or below range display error // 10/7/17 nikita
+                else if(heart_rate==1)                                          // if heart rate goes above or below range display error // 10/7/17 nikita
                      {
                           screen_ecg_error();
                          
@@ -365,24 +318,13 @@
                 screen_ecg_bp();
                 EnableTouch();
                 break;
-     
-     
-     /*case 9: 
-                DisableTouch();
-                screen_glc_2();
-                state1=0;
-                state=6;
-                nstate=state;
-                EnableTouch();
-                 break;*/
-        
+             
     case 9: 
                 DisableTouch();
-                screen_bp2();      // bp main screen
-                bp_countdown();    // countdown for bp 
-                bp(pid);              // measuring bp   earlier bp() changed to bp(pid) 28/4/2017
+                screen_bp2();                                                   // bp main screen
+                bp_countdown();                                                 // countdown for bp 
+                bp(pid);                                                        // measuring bp   earlier bp() changed to bp(pid) 28/4/2017
                 state1=0;
-               // wait(1);
                 state=6;
                 eprom_write_8(30,2);
                 nstate=state;
@@ -396,14 +338,16 @@
                  state=nstate;
                   break;        
                   
-      case 11 :    DisableTouch();
-                   current_test_pid=eprom_read(1);
-                   bt_file_sent_pid=eprom_read(6);
-                   screen_patient_info(current_test_pid, bt_file_sent_pid);
-                   state1=0;
-                   state=12;
-                   EnableTouch();
-                   break;           
+      case 11 :    
+                DisableTouch();
+                sd_read(pid_btpid);                                         //read pid from sd card 
+                current_test_pid=pid_btpid[0];
+                bt_file_sent_pid=pid_btpid[1];
+                screen_patient_info(current_test_pid, bt_file_sent_pid);
+                state1=0;
+                state=12;
+                EnableTouch();
+                break;           
                      
        
         default :  
@@ -416,6 +360,23 @@
     
            
                 
-         return state;       
+    return state;       
                 
+}
+
+
+bool get_filecreated_status(void)                                               //function to get file created status
+{
+    return file_created_status;
+}
+
+void set_filecreated_status(void)                                               //function to set file created status
+{
+    file_created_status = 0xFFFF;
+}
+
+
+void clear_filecreated_status(void)                                             //function to clear file created status
+{
+    file_created_status = 0x0000;
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.h	Fri Sep 08 10:35:21 2017 +0000
@@ -0,0 +1,19 @@
+#ifndef MAIN_H_
+#define MAIN_H_
+
+uint8_t touch(uint8_t);
+uint8_t display(uint8_t);
+
+bool get_filecreated_status(void);                  //get status of file created
+void set_filecreated_status(void);                  //set file created
+void clear_filecreated_status(void);                //clear file created
+
+static uint32_t current_test_pid = 0;
+static uint32_t  bt_file_sent_pid = 0; 
+static uint8_t nstate = 0;
+static uint8_t nstate1 = 0;         
+static uint32_t pid_btpid[2] = {};               //buffer to store pid and btpid
+
+
+#endif
+
--- a/sdcard.cpp	Sun Aug 20 16:21:54 2017 +0000
+++ b/sdcard.cpp	Fri Sep 08 10:35:21 2017 +0000
@@ -2,12 +2,14 @@
  //Nidhin 1/6/2017
  
  
- #include "mbed.h"
- #include <stdio.h>
- #include "SDFileSystem.h"
- #include "sdcard.h"
-  #include "rtc.h"
-  #include "struct.h"
+#include "mbed.h"
+#include <stdio.h>
+#include "SDFileSystem.h"
+#include "sdcard.h"
+#include "rtc.h"
+#include "struct.h"
+#include "main.h"
+#include "eeprom_pgm.h"
   
   Serial snd(USBTX,USBRX);
   SDFileSystem sd(PTE1, PTE3, PTE2, PTE4, "sd");
@@ -61,22 +63,6 @@
  } 
  
  
- 
-  /*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(int32_t pid)          // only opening the ECG file after entering TEST screen
  {
      
@@ -88,20 +74,6 @@
  } 
  
  
-  /*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(int32_t pid)          // only opening the BP file after entering TEST screen
  {
      
@@ -371,9 +343,19 @@
 //Creating strcuture file Common to All Functions
 void structure_file(BLEMsg_info *ptr, int32_t pid)
 {
-char buffer3[32];
-sprintf(buffer3, "/sd/%d.csv", pid);        //Printing file path to the buffer
-fpmain = fopen(buffer3, "a");               // open the file PID.csv with FILE pointer fpmain
+    char buffer3[32];
+    sprintf(buffer3, "/sd/%d.csv", pid);            //Printing file path to the buffer
+    if(get_filecreated_status())                    //if file created open in append mode
+    {
+        fpmain = fopen(buffer3, "a");               // open the file PID.csv with FILE pointer fpmain
+        snd.printf("\n Append mode\n");
+    }
+    else                                            //if file not created, open in write mode
+    {
+        fpmain = fopen(buffer3, "w");               // open the file PID.csv with FILE pointer fpmain 
+        snd.printf("\n Write mode\n"); 
+    }    
+        
 fwrite(ptr, sizeof(uint8_t),26, fpmain );   // writing the Structure into the file
 fclose(fpmain);                             // Close File
 }
@@ -540,4 +522,45 @@
     else 
         snd.printf("\n temp PPG file delete for error condition: UnSuccessful");
        
-}
\ No newline at end of file
+}
+
+void sd_read(uint32_t  *pid_btpid)          //reading the data from the openend file
+ {
+    FILE *fp; FILE *fp1;
+    uint32_t pid[2]={1,1};
+    fp= fopen("/sd/pid_info.txt","r");
+     if (fp==NULL)
+    {
+       fp1= fopen("/sd/pid_info.txt","w");
+       fwrite(&pid, sizeof(uint32_t),2,fp1);
+       fclose(fp1);
+       fp= fopen("/sd/pid_info.txt","r");
+    }
+     
+     fread(pid_btpid,sizeof(pid_btpid),2,fp);
+     fclose(fp);
+     
+ }
+ 
+ 
+void sd_write(uint32_t  *pid_btpid)          //reading the data from the openend file
+{
+     FILE *fp;
+     fp= fopen("/sd/pid_info.txt","w");
+     fwrite(pid_btpid,sizeof(pid_btpid),2,fp);
+     fclose(fp);
+     
+}
+
+void increment_pid (void)                       //increment pid if a test is completed
+{
+    uint32_t pid[2];
+    if(!get_filecreated_status())               //if file is in write mode                       
+    {
+        set_filecreated_status();               //set it to make it append mode
+        sd_read(pid);                           //read pid from sd card
+        pid[0] = pid[0] + 1;                    //increment it by 1
+        sd_write(pid);                          //write it back to sd card
+    }
+    
+}     
\ No newline at end of file
--- a/sdcard.h	Sun Aug 20 16:21:54 2017 +0000
+++ b/sdcard.h	Fri Sep 08 10:35:21 2017 +0000
@@ -52,6 +52,8 @@
 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
-
+void sd_write(uint32_t  *pid_btpid);
+void sd_read(uint32_t  *pid_btpid);
+void increment_pid (void);
 
 #endif 
\ No newline at end of file
--- a/touch_modules.cpp	Sun Aug 20 16:21:54 2017 +0000
+++ b/touch_modules.cpp	Fri Sep 08 10:35:21 2017 +0000
@@ -203,17 +203,17 @@
                  else if ( ((xt >=RETEST_HOME_X_MIN) && (xt<=RETEST_HOME_X_MAX)) && ( (yt>=RETEST_HOME_Y_MIN) && (yt<=RETEST_HOME_Y_MAX) ) ) // RETEST
                        {
                                  if (type==0)
-                                     state=7;
+                                    state=7;
                                  else if (type==1)
-                                     state=8;
-                                     else if (type==2)
-                                     state=9;
+                                    state=8;
+                                else if (type==2)
+                                    state=9;
                         }
-                       else if (xt==0 && yt==0)
-                  {
+                else if (xt==0 && yt==0)
+                {
                       state=3;
-                       }
-              else 
+                }
+                else 
                      state=10;                      
            return state;
     }