nikita teggi / Mbed 2 deprecated merged_code2_3rd_nov_2017

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of merged_code2_20sept_2017_4th_oct_2017 by nikita teggi

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "display_modules.h"
00003 #include "touch_modules.h"
00004 #include "ecg_dec.h"
00005 #include "touch_modules.h"
00006 #include "glc.h"
00007 #include "bp.h"
00008 #include "sdcard.h"
00009 #include "eeprom_pgm.h"
00010 #include "struct.h"
00011 #include "battery.h" 
00012 #include "main.h"
00013 
00014 #define TIMER_RXACK_BTCONNECTION                10000
00015 #define BATTERY_TIMER_VALUE                     600000
00016 #define SERIAL_BAUD_RATE                        115200
00017 #define BATTERY_SCREEN_NO_CHARGE_CONDITION      3
00018 #define TIMER_ENTER_DEBUG_MODE                  10000
00019 
00020 
00021 
00022 Serial gc(USBTX, USBRX);
00023 DigitalIn q1(PTB11);
00024 DigitalIn q(PTC5);
00025 DigitalIn q3(PTC12);
00026 DigitalIn BT(PTA5);   
00027 Serial blue1(PTC4,PTC3);  
00028 uint32_t get_timer_bluetooth();
00029 static void battery_status_monitoring();                                     //bluetooth status pin
00030 uint8_t touch(uint8_t state);                                               // touch state
00031 uint8_t display(uint8_t state1);                                            //  display state
00032 
00033 static uint8_t nstate=0;
00034 static uint8_t nstate1=0;                                                  
00035 static bool file_created_status = false;                                  //initialise file created to 0
00036 static uint32_t filepid_btpid[2] = {};
00037 Timer timer_bt;                                                         // timer for bluetooth 
00038 Timer timer_battery;                                                     // timer for battery
00039 Timer timer_debug;                                                       // timer for entering the debug mode   
00040 static bool debug_mode=false;                                               // holds the status of the debug mode "1" indicates the device is in debug mode 
00041 char debug_message_rx[6];                                               // buffer to receive the debug message data
00042 char debug_message[6]={"debug"};                                        //debug message to be received
00043 void debug_status_monitoring();                                         // debug status monitoring
00044 static uint32_t pid = 0;                                                    // patient ID    
00045 
00046 int main()
00047 {
00048     static uint8_t main_state=1;
00049     static uint8_t state_display = 0;
00050     static uint8_t state_touch = 1;
00051     uint8_t state_t = 1;                                     // state to transmit to bluetooth
00052     uint8_t state_r = 0;                                     // state to receive from bluetooth
00053     
00054   
00055   
00056     DisableTouch();
00057     gc.baud(115200);
00058     blue1.baud(115200);
00059     init_screen();                                                              //initialize lcd
00060     pid = get_filepid() + 1;                                                    //added on 22/09/2017 nikita
00061     screen_main();                                                              // display of main screen 
00062     battery_monitor();                                                          // monitor battery at the start only
00063     battery_status_display();                                                   // display of main screen
00064     timer_battery.start();
00065     timer_debug.start();
00066     gc.printf("enter message to go into debug mode\n");
00067 
00068    while(1)
00069    {
00070     
00071         if (read_debug_status() == false)                                            // checking the debug_mode status
00072         {   
00073             if(timer_debug.read_ms()<TIMER_ENTER_DEBUG_MODE)
00074             {
00075                 debug_status_monitoring();
00076             }
00077         }   
00078       
00079         
00080         battery_status_monitoring();                                            // to  monitior battery status after every 10 min. 
00081 
00082         if (get_battery_status() == BATTERY_SCREEN_NO_CHARGE_CONDITION)   // to check battery status and take action 
00083         {        
00084                  DisableTouch();
00085         }
00086         else
00087         {
00088 
00089             EnableTouch();
00090              gc.printf("c");  
00091             switch(main_state)                                             // main state machine
00092              {
00093                 case 1:  
00094                          state_display = touch(state_touch);             // touch state 
00095                          if(state_display == 12)
00096                          {
00097                             main_state = 3;
00098                          }
00099                         else 
00100                          {
00101                             main_state = 2;
00102                          }
00103                         break;
00104     
00105                 case 2:    
00106                          state_touch = display(state_display);           //display state
00107                          main_state = 1;
00108                         break;
00109                 
00110                 
00111                 case 3: 
00112                         DisableTouch();                                             // bluetooth send 
00113                         timer_bt.start();
00114                          if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)                           // bluetooth connection timer 15 sec
00115                          {
00116                         
00117                             if(BT==1)                                                    // checking for bluetooth connection
00118                             {
00119                     
00120                                timer_bt.stop();                                        // stop the timer once connected to app
00121                                timer_bt.reset(); 
00122                                BT_connection() ;                                // displaying connection on bluetooth 
00123                                state_r=bt_send(state_t);
00124                                timer_bt.start();
00125                                 //gc.printf(" state_t = %d\n", state_t);
00126                                 // gc.printf(" state_r = %d\n", state_r);   
00127                                 if (state_r==0)                                  // if the state received is zero, then there is no new file to send
00128                                     {
00129                                         timer_bt.stop();                           // stop the timer once connected to app
00130                                         timer_bt.reset();
00131                                         BT_finished();                         // screen to display communication finished
00132                                         screen_BT_return();                    //screen to return back
00133                                         EnableTouch();
00134                                         main_state=1;
00135                                         state_touch=12;
00136                                         break;
00137                                      }
00138                                    
00139                                 else 
00140                                     {
00141                                         main_state=4;                           // move to receving stste of bluetooth
00142                                     }
00143                           
00144                             }  
00145                   
00146                             else   
00147                             {
00148                              BT_no_connection();     // display screen to show that there is no connection
00149                             }
00150                          }
00151 
00152                         else
00153                         {                                   
00154                              BT_no_connection();     // display screen to show that there is no connection
00155                             gc.printf("stopped");
00156                             screen_BT_return();     // button to return
00157                             EnableTouch();
00158                             main_state=1;
00159                             state_touch=12;
00160                             //sd_close();  
00161                             timer_bt.stop();
00162                             timer_bt.reset();
00163                         }
00164                         break;
00165                  
00166                 case 4: 
00167                         DisableTouch();  
00168             
00169                         if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)
00170                         {
00171                             if (blue1.readable())         // if there is data to receive enter the loop
00172                             {
00173                                
00174                                 state_t=bt_receive(state_r);
00175                                //  gc.printf(" state_t = %d\n", state_t);
00176                                 // gc.printf(" state_r = %d\n", state_r);
00177                                 timer_bt.stop();
00178                                 timer_bt.reset();
00179                                 if(state_t ==0)           // if state_t is zero, there is no other file to send
00180                                  {
00181                                   
00182                                     BT_finished();
00183                                     screen_BT_return();  
00184                                     EnableTouch();
00185                                     main_state=1;
00186                                     state_touch=12;  // jumping to touch case 12
00187                                     break;
00188                                   } 
00189                                 else                                 // move to bluetooth transmit if the states are 1,2,3,4
00190                                  {
00191                                     main_state=3;
00192                                  }  
00193                             }
00194                          }                 
00195                    
00196                         else 
00197                         {    
00198                             DisableTouch();
00199                             BT_no_connection();                     // display screen to show that there is no connection
00200                             gc.printf("stopped");
00201                             screen_BT_return();                     // button to return
00202                             EnableTouch();
00203                             main_state=1;
00204                             state_touch=12;
00205                             state_t = 1;                                     // state to transmit to bluetooth
00206                             state_r = 0;                                     // state to receive from bluetooth
00207                             sd_close();                             ///close the opened file which was used for bluetooth
00208                             timer_bt.stop();
00209                             timer_bt.reset();
00210                         }
00211                         break;      
00212              }         
00213         }        
00214    } 
00215 
00216 }
00217 uint8_t touch(uint8_t state)
00218 {
00219    static  uint8_t state1=0;
00220    detect_touch(state);                                             // determine the touch points
00221     
00222     switch(state)                                                   // state for touch
00223   {
00224       case 1:   
00225                 state1=touch_main();                                // determining the touch for main screen 
00226                 state=0;
00227                 nstate1=state1;
00228                 break;
00229                
00230       case 2: 
00231                 state1=touch_main_1();                              // determining the touch for SYSTEM CONFIGURATION screen 
00232                 state=0;
00233                 nstate1=state1;
00234                 break;
00235                 
00236       case 3:  
00237                 state1=touch_main_2();                              // determining the touch for TEST screen                 
00238                 state=0;
00239                 nstate1=state1;
00240                 break;
00241         
00242       case 4: 
00243                 state1=touch_glc();                                 // determining the touch for GLC screen 
00244                 state=0;
00245                 nstate1=state1;
00246                 break;       
00247         
00248       case 5: 
00249                 state1=touch_ecg();                                 // determining the touch for ECG screen 
00250                 state=0;
00251                 nstate1=state1;
00252                  break;   
00253      case 6: 
00254                 state1=touch_ret();                                 // determining the touch for returning back to the main screen      
00255                 state=0;
00256                 nstate1=state1;
00257                 break;
00258                 
00259      case 7: 
00260                 state1=touch_again_ecg();                          // determining the touch when readings are taken more than once screen 
00261                 state=0;
00262                 nstate1=state1;
00263                 break;
00264                 
00265      case 8:    
00266                 state1=touch_bp();                                  // determining the touch for BP screen 
00267                 state=0;
00268                 nstate1=state1;
00269                 break;           
00270                 
00271      case 9:    
00272                 state1=touch_again_glc();  
00273                 state=0;
00274                 nstate1=state1;
00275                 break;            
00276                 
00277      case 10 :  
00278                 DisableTouch();
00279                 wait_ms(300);
00280                 EnableTouch();
00281                 state=nstate;
00282                 break;
00283                   
00284                   
00285       case 11:   
00286                 state1=touch_again_bp();            
00287                 state=0;
00288                 nstate1=state1;
00289                 break;
00290       
00291       case 12:   
00292                 state1=BT_return();            
00293                 state=0;
00294                 nstate1=state1;
00295                 break;
00296          
00297       case 13:  
00298                 state1 = touch_debug();                                         //touch state for debug
00299                 //state=0;
00300                 nstate1=state1;
00301                 break;  
00302      default :  
00303                 DisableTouch();
00304                 wait_ms(300);
00305                 EnableTouch();
00306                 state=nstate;
00307                 break;          
00308                           
00309     }         
00310      
00311     return state1;
00312 }
00313   
00314   
00315 uint8_t display(uint8_t state1)
00316 {
00317     static uint8_t state=0;
00318     static uint8_t ecg_flag=0;                                  // ecg_flag to indicate the ecg test is already done for the same pid
00319     static uint8_t glc_flag=0;                                  // glc_flag to indicate the glc test is already done for the same pid
00320     uint16_t heart_rate=0; 
00321 
00322     
00323     switch(state1)                                              // state for display screens
00324     {
00325 
00326       
00327       case 1: 
00328                 DisableTouch();
00329                 screen_main();                                  //  main screen 
00330                 state1=0;
00331                 state=1;
00332                 nstate=state;
00333                 ecg_flag=0;                                     // ecg, glc flag to determine whether the readings are taken more than once
00334                 glc_flag=0;
00335                 clear_filecreated_status();
00336                 battery_status_display();                          //Display battery status on LCD
00337                 pid = get_filepid() + 1;
00338                 EnableTouch();
00339                 break;
00340       
00341       case 2: 
00342                 DisableTouch();
00343                 screen_main_1();                                // System configration screen 
00344                 state1=0;
00345                 state=2;
00346                 nstate=state;
00347                 battery_status_display();                                           //Display battery status on LCD
00348                 EnableTouch();
00349                 // timer_debug.stop();
00350                 break;
00351                
00352       case 3: 
00353                 DisableTouch();
00354                 //sd_read(pid_btpid);                             //read pid from sd card
00355                 //pid=pid_btpid[0];                               //copy pid to a variable
00356                 gc.printf("pidinit=%d",pid);
00357                 screen_main_2(pid);     
00358                 state1=0;
00359                 state=3;
00360                 nstate=state;
00361                 battery_status_display(); 
00362                 EnableTouch();
00363                 break;
00364                 
00365       case 4: 
00366                 DisableTouch();
00367                 if (glc_flag==1)                                // glc_flag=1 when already the reading has been taken 
00368                 {
00369                     screen_again();    
00370                     state1=0;
00371                     state=9;
00372                     nstate=state;
00373                 }
00374                 else
00375                 { 
00376                     screen_glc();                                       // glc screen
00377                     state1=0;
00378                     state=4;
00379                     nstate=state;
00380                 }
00381                 EnableTouch();
00382                 
00383                 break;
00384      
00385      
00386      case 5:     
00387                 DisableTouch();
00388                 if (ecg_flag==1)
00389                 {
00390                     screen_again();
00391                     state1=0;
00392                     state=7;
00393                     nstate=state;
00394                 }
00395                 
00396                 else
00397                 {   
00398                     screen_ecg();                                           //ecg main screen 
00399                     state1=0;
00400                     state=5;
00401                     nstate=state;
00402                 }
00403                 EnableTouch();
00404                 break;
00405      
00406      case 6:   
00407                 DisableTouch();
00408                 screen_bp();                                                 // bp main screen
00409                 state1=0;
00410                 state=8;
00411                 nstate=state;         
00412                 EnableTouch();        
00413                 break;
00414      
00415      case 7:    
00416                 DisableTouch();     
00417                 glc(pid);                                                       // glc measurement
00418                 glc_flag=1;
00419                 wait(1); 
00420                 state1=0;
00421                 screen_glc_2();
00422                 state=6;
00423                 eprom_write_8(30,0);
00424                 nstate=state;        
00425                 EnableTouch();
00426                 break;
00427     
00428     case 8:     DisableTouch();
00429                 ecg_countdown();                                                // countdown before ecg capture
00430                 heart_rate=ecg(pid);                                            // capturing ecg
00431                 state1=0;
00432                 if (heart_rate==0)
00433                     {
00434                        
00435                        screen_ecg_lead();  
00436                      }
00437                 else if(heart_rate==1)                                          // if heart rate goes above or below range display error // 10/7/17 nikita
00438                      {
00439                           screen_ecg_error();
00440                          
00441                          }      
00442                   else 
00443                        {
00444                        
00445                          screen_ecg_2(heart_rate);
00446                          ecg_flag=1;
00447                          }                            
00448                 state=6;
00449                 eprom_write_8(30,1);
00450                 nstate=state;
00451                 screen_ecg_bp();
00452                 EnableTouch();
00453                 break;
00454              
00455       case 9: 
00456                 DisableTouch();
00457                 screen_bp2();                                                   // bp main screen
00458                 bp_countdown();                                                 // countdown for bp 
00459                 bp(pid);                                                        // measuring bp   earlier bp() changed to bp(pid) 28/4/2017
00460                 state1=0;
00461                 state=6;
00462                 eprom_write_8(30,2);
00463                 nstate=state;
00464                 EnableTouch();
00465                  break;
00466  
00467                           
00468       case 10 :  DisableTouch();
00469                  wait_ms(300);
00470                  EnableTouch();
00471                  state=nstate;
00472                   break;        
00473                   
00474       case 11 :    
00475                 DisableTouch();
00476                 screen_patient_info(get_filepid(), get_btpid());
00477                 state1=0;
00478                 state=12;
00479                 EnableTouch();
00480                 break;  
00481                          
00482       case 13 :                                                                 //display screen for debug
00483                 DisableTouch();
00484                 screen_debug();
00485                 state1=0;
00486                 state=13;
00487                 nstate=state;
00488                 EnableTouch();
00489                 break; 
00490                    
00491       case 14 :    
00492                 DisableTouch();                                                 //state to delete SD card
00493                 delete_sdcard();
00494                 screen_sdcard_delete();
00495                 state1=0;
00496                 state= 12;
00497                 nstate=state;
00498                 EnableTouch();
00499                 break;   
00500                 
00501         case 15 :    
00502                 DisableTouch();
00503                 gc.printf("Total Patients' data available : %d\n",get_filepid());                                        //state to read SD card
00504                 gc.printf("Enter the PID number");
00505                 read_sdcard_file();
00506                 screen_sdcard_read();
00507                 state1=0;
00508                 state= 12;
00509                 nstate=state;
00510                 EnableTouch();
00511                 break;                            
00512        
00513         default :  
00514                   
00515                   state1=nstate1;
00516                    break;          
00517                             
00518             
00519     }            
00520     
00521            
00522                 
00523     return state;       
00524                 
00525 }
00526 
00527 
00528 bool get_filecreated_status(void)                                               //function to get file created status
00529 {
00530     return file_created_status;
00531 }
00532 
00533 void set_filecreated_status(void)                                               //function to set file created status
00534 {
00535     file_created_status = true;
00536 }
00537 
00538 
00539 void clear_filecreated_status(void)                                             //function to clear file created status
00540 {
00541     file_created_status = false;
00542 }
00543 
00544 void increment_filepid (void)                                                       //increment pid if a test is completed
00545 {
00546     //uint32_t filepid_btpid[2] = {};
00547     
00548     //set_filecreated_status();                                                   //set it to make it append mode
00549     sd_read(filepid_btpid);                                                     //read pid from sd card
00550     filepid_btpid[0] = filepid_btpid[0] + 1;                                    //increment it by 1
00551     sd_write(filepid_btpid);                                                    //write it back to sd card
00552     
00553 } 
00554 
00555 void increment_btpid (void)                                                       //increment pid if a test is completed
00556 {
00557     //uint32_t filepid_btpid[2] = {};
00558     
00559     //set_filecreated_status();                                                 //set it to make it append mode
00560     sd_read(filepid_btpid);                                                     //read pid from sd card
00561     filepid_btpid[1] = filepid_btpid[1] + 1;                                    //increment it by 1
00562     sd_write(filepid_btpid);                                                    //write it back to sd card
00563     
00564 } 
00565 
00566 uint32_t get_filepid(void)
00567 {
00568     //uint32_t filepid_btpid[2] = {};  
00569     uint32_t filepid = 0;    
00570     
00571     sd_read(filepid_btpid);                                                           //read pid from sd card
00572     filepid = filepid_btpid[0];                                  
00573     return filepid; 
00574     
00575 }   
00576 
00577 void store_filepid(uint32_t pid)
00578 {
00579        //uint32_t filepid_btpid[2] = {};
00580         sd_read(filepid_btpid);                                                       //read pid from sd card
00581         filepid_btpid[0] = pid;                                              
00582         sd_write(filepid_btpid);                                                      //write it back to sd card
00583                                                         
00584 } 
00585 
00586 uint32_t get_btpid(void)
00587 {
00588     //uint32_t filepid_btpid[2] = {}; 
00589     uint32_t btpid = 0;    
00590     
00591     sd_read(filepid_btpid);                                                           //read pid from sd card
00592     btpid = filepid_btpid[1];                                  
00593     return btpid; 
00594     
00595 }   
00596 
00597 void store_btpid(uint32_t bt_pid)
00598 {
00599        //uint32_t filepid_btpid[2] = {};
00600     
00601         sd_read(filepid_btpid);                                                       //read pid from sd card
00602         filepid_btpid[1] = bt_pid;                                              
00603         sd_write(filepid_btpid);                                                      //write it back to sd card
00604                                                         
00605 } 
00606 
00607 
00608 uint32_t get_timer_bluetooth()
00609 {
00610   return  timer_bt.read_ms(); 
00611 } 
00612 
00613 void battery_status_monitoring()
00614 {
00615     if(timer_battery.read_ms() > BATTERY_TIMER_VALUE ) 
00616     {
00617         battery_monitor();
00618         timer_battery.reset();
00619     }
00620 }
00621 
00622 bool read_debug_status()
00623 {
00624    // gc.printf("debug_mode=%d\n",debug_mode); 
00625     return debug_mode;
00626 }
00627 
00628 void debug_status_monitoring()
00629 {
00630 
00631 
00632 //if(get_timer_debug()<TIMER_ENTER_DEBUG_MODE)
00633 //{
00634  if (gc.readable())                                                             // check for the user input
00635         {
00636             
00637            while (gc.readable()!= '\n')                                         // read till the newline
00638             { 
00639                
00640                 gc.scanf("%s", debug_message_rx);                               // store the message typed in buffer
00641                 gc.printf("%s", debug_message_rx);
00642                 break;
00643             }
00644          
00645          
00646             if (strcmp(debug_message_rx, debug_message) == 0)                   // compare the message with the required one
00647             {
00648                  debug_mode = true;                                                // enable the debug mode status
00649                  gc.printf("entered into debug mode\n");
00650                  timer_debug.reset();
00651                  timer_debug.start();                                           // timer is started in orede
00652             } 
00653         }        
00654 //}
00655     
00656 }  
00657     
00658 uint32_t get_timer_debug()                                                         // timer for enabling debug option
00659 {
00660   if(timer_debug.read_ms()>TIMER_ENABLE_DEBUG_MODE)
00661     {
00662         timer_debug.stop();
00663     }
00664   return  timer_debug.read_ms(); 
00665 }
00666 
00667