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:
41:1141a75eacc4
Parent:
39:69b09bd87502
Child:
42:c81673b04b6a
--- 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