rashmi v / Mbed 2 deprecated SS_SensePOC2P0_12Dec2017_USERPID

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of SS_SensePOC2P0_11Dec2017_USERPID by rashmi v

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 "rtc.h"
00013 #include "main.h"
00014 #include "SDFileSystem.h"
00015 
00016 #define TIMER_RXACK_BTCONNECTION                10000
00017 #define BATTERY_TIMER_VALUE                     600000
00018 #define SERIAL_BAUD_RATE                        115200
00019 #define BATTERY_SCREEN_NO_CHARGE_CONDITION      3
00020 #define TIMER_ENTER_DEBUG_MODE                  10000
00021 #define HEARTBEAT_DATA_LEN                      0x13u                           //changed from 0D to 13
00022 #define DEVICE_RESULT_DATA_LEN                  0x04u
00023 #define ZERO_DATA_LEN                           0x00u
00024 
00025 
00026 typedef enum
00027 {
00028     HOME_SCREEN = 0x01U,
00029     TEST_SCREEN
00030 }DEVICE_STATE;
00031 typedef enum
00032 {
00033     GET_TOUCH_DISP_STATE = 0x00U,
00034     BT_TX_STATE,
00035     BT_RX_STATE
00036 
00037 }DEVICE_MAIN_STATE;
00038 
00039 // this helps to respond various status during test cycle, error condition and simple ack or nack
00040 typedef struct __attribute__((__packed__))
00041 {
00042     uint8_t     sos;
00043     uint8_t     cmd;                    // this contains error response
00044     uint8_t     length;
00045     uint8_t     eos;
00046     uint8_t     chksum;
00047 
00048 }poc2p0_device_msg_response;
00049 
00050 typedef struct __attribute__((__packed__))
00051 {
00052     uint8_t     sos;
00053     uint8_t     cmd;
00054     uint8_t     length;
00055     uint32_t    test_result;
00056     uint8_t     eos;
00057     uint8_t     chksum;
00058 
00059 }poc2p0_device_tst_result_response;
00060 
00061 // this will be used for responding heart beat message
00062 typedef struct __attribute__((__packed__))
00063 {
00064     uint8_t                 sos;
00065     uint8_t                 cmd;                    // this contains error response
00066     uint8_t                 length;
00067     uint8_t                 did;
00068     char                    pid[10];
00069     //uint32_t              pid;
00070     DateTime_info           date_time;
00071     uint8_t                 device_state;          //changed POC2P0_DEVICE_STATE to uint8_t
00072     uint8_t                 battery_status;
00073     uint8_t                 eos;
00074     uint8_t                 chksum;
00075 
00076 }poc2p0_device_heartbeat_response;
00077 
00078 SDFileSystem sd(PTE1, PTE3, PTE2, PTE4, "sd");
00079 Serial gc(USBTX, USBRX);
00080 DigitalIn q1(PTB11);
00081 DigitalIn q(PTC5);
00082 DigitalIn q3(PTC12);
00083 DigitalIn BT(PTA5);   
00084 Serial blue1(PTC4,PTC3); 
00085  
00086 uint32_t get_timer_bluetooth();
00087 static void battery_status_monitoring();                                     //bluetooth status pin
00088 uint8_t touch(uint8_t state);                                               // touch state
00089 uint8_t display(uint8_t state1);                                            //  display state
00090 static void ecg_bp_countdown(void);
00091 static void read_device_id(void);
00092 void sd_card_status(void);
00093 
00094 static uint8_t nstate=0;
00095 static uint8_t nstate1=0;                                                  
00096 static bool file_created_status = false;                                  //initialise file created to 0
00097 static uint32_t filepid_btpid[2] = {};
00098 Timer timer_bt;                                                         // timer for bluetooth 
00099 Timer timer_battery;                                                    // timer for battery
00100 Timer timer_debug;                                                       // timer for entering the debug mode   
00101 static bool debug_mode=false;                                               // holds the status of the debug mode "1" indicates the device is in debug mode 
00102 char debug_message_rx[6];                                               // buffer to receive the debug message data
00103 char debug_message[6]={"debug"};                                        //debug message to be received
00104 void debug_status_monitoring();                                         // debug status monitoring
00105 static uint32_t pid = 0;                                                    // patient ID    
00106 static uint8_t state_touch = 1;                                                 //added on 16-NOV rashmi 
00107 uint8_t buffer_pointer = 0x00u;  
00108 static uint8_t device_id = 0;
00109                                 
00110 int main()
00111 {
00112     static DEVICE_MAIN_STATE main_state = GET_TOUCH_DISP_STATE;
00113     static uint8_t state_display = 0;
00114     //static uint8_t state_touch = 1;                                           //commented on 16-NOV rashmi 
00115     uint8_t state_t = 1;                                     // state to transmit to bluetooth
00116     uint8_t state_r = 0;                                     // state to receive from bluetooth
00117   
00118     //DisableTouch();
00119     gc.baud(115200);
00120     blue1.baud(115200);
00121     sd.disk_initialize();
00122     sd_card_status();                                                         
00123     pid = get_filepid() + 1;                                                    //added on 22/09/2017 nikita
00124     //screen_main();                                                              // display of main screen 
00125     battery_monitor();                                                          // monitor battery at the start only
00126     //battery_status_display();                                                   // display of main screen
00127     timer_battery.start();
00128     timer_debug.start();
00129     read_device_id();
00130     gc.printf("enter message to go into debug mode\n");
00131    
00132    while(1)
00133    {
00134     
00135         if (read_debug_status() == false)                                            // checking the debug_mode status
00136         {   
00137             if(timer_debug.read_ms()<TIMER_ENTER_DEBUG_MODE)
00138             {
00139                 debug_status_monitoring();
00140             }
00141         }   
00142       
00143         battery_status_monitoring();                                            // to  monitior battery status after every 10 min. 
00144 
00145         if(false)// (get_battery_status() == BATTERY_SCREEN_NO_CHARGE_CONDITION)   // to check battery status and take action 
00146         {        
00147                  //DisableTouch();
00148         }
00149         else
00150         {
00151             //EnableTouch();
00152             //gc.printf("c");
00153             //gc.printf("main state: %d\n",main_state);
00154             switch(main_state)                                              // main state machine
00155              {
00156                 case GET_TOUCH_DISP_STATE:
00157 
00158                     state_display = poc2p0_bt_receive(state_touch);
00159                     //state_display = touch(state_touch);                   // touch state
00160                     //gc.printf(" state_display = %d\n", state_display);
00161                     display(state_display);                                 //display state
00162                     
00163                     if(state_display == 15)
00164                     {
00165                         main_state = GET_TOUCH_DISP_STATE;
00166                     }
00167                     if(state_display == 12)
00168                     {
00169                         main_state = BT_TX_STATE;
00170                     }
00171                    // else
00172                    //   display(state_display);  
00173 
00174                     break;
00175     
00176                 case BT_TX_STATE:
00177                         //DisableTouch();                                             // bluetooth send 
00178                         timer_bt.start();
00179                          if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)                           // bluetooth connection timer 15 sec
00180                          {
00181                         
00182                             if(BT==1)                                                    // checking for bluetooth connection
00183                             {
00184                     
00185                                timer_bt.stop();                                        // stop the timer once connected to app
00186                                timer_bt.reset(); 
00187                                //BT_connection() ;                                // displaying connection on bluetooth 
00188                                gc.printf(" BT Connected\n");
00189                                state_r=bt_send(state_t);
00190                                timer_bt.start();
00191                                 //gc.printf(" state_t = %d\n", state_t);
00192                                 // gc.printf(" state_r = %d\n", state_r);   
00193                                 if (state_r==0)                                  // if the state received is zero, then there is no new file to send
00194                                     {
00195                                         timer_bt.stop();                           // stop the timer once connected to app
00196                                         timer_bt.reset();
00197                                         //BT_finished();                         // screen to display communication finished
00198                                         //screen_BT_return();                    //screen to return back
00199                                         gc.printf("BT Transfer Complete\n");
00200                                         //EnableTouch();
00201                                         main_state=GET_TOUCH_DISP_STATE;
00202                                         state_touch=12;
00203                                         break;
00204                                      }
00205                                    
00206                                 else 
00207                                     {
00208                                         main_state=BT_RX_STATE;                           // move to receving stste of bluetooth
00209                                     }
00210                           
00211                             }  
00212                   
00213                             else   
00214                             {
00215                              //BT_no_connection();     // display screen to show that there is no connection
00216                              gc.printf("BT No Connection\n");
00217                             }
00218                          }
00219 
00220                         else
00221                         {
00222                             poc2p0_send_device_msg_res(MSG_REJECTED_RES);       //added on 14-Nov by rashmi                                   
00223                             //BT_no_connection();                                 // display screen to show that there is no connection
00224                             gc.printf("stopped");
00225                             //screen_BT_return();                                 // button to return
00226                             //EnableTouch();
00227                             main_state=GET_TOUCH_DISP_STATE;
00228                             state_touch=12;
00229                             //sd_close();  
00230                             timer_bt.stop();
00231                             timer_bt.reset();
00232                         }
00233                         break;
00234                  
00235                 case BT_RX_STATE:
00236                         //DisableTouch();  
00237             
00238                         if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)
00239                         {
00240                             if (blue1.readable())         // if there is data to receive enter the loop
00241                             {
00242                                
00243                                 state_t=bt_receive(state_r);
00244                                //  gc.printf(" state_t = %d\n", state_t);
00245                                 // gc.printf(" state_r = %d\n", state_r);
00246                                 timer_bt.stop();
00247                                 timer_bt.reset();
00248                                 if(state_t ==0)           // if state_t is zero, there is no other file to send
00249                                  {
00250                                   
00251                                     //BT_finished();
00252                                     //screen_BT_return();  
00253                                     gc.printf("BT Transfer Complete\n");
00254                                     //EnableTouch();
00255                                     main_state=GET_TOUCH_DISP_STATE;
00256                                     state_touch=12;  // jumping to touch case 12
00257                                     break;
00258                                   } 
00259                                 else                                 // move to bluetooth transmit if the states are 1,2,3,4
00260                                  {
00261                                     main_state=BT_TX_STATE;
00262                                  }  
00263                             }
00264                          }                 
00265                    
00266                         else 
00267                         {    
00268                             //DisableTouch();
00269                             //BT_no_connection();                     // display screen to show that there is no connection
00270                             gc.printf("stopped");
00271                             //screen_BT_return();                     // button to return
00272                             gc.printf("BT No Connection\n");
00273                             //EnableTouch();
00274                             main_state=GET_TOUCH_DISP_STATE;
00275                             state_touch=12;
00276                             state_t = 1;                                     // state to transmit to bluetooth
00277                             state_r = 0;                                     // state to receive from bluetooth
00278                             sd_close();                             ///close the opened file which was used for bluetooth
00279                             timer_bt.stop();
00280                             timer_bt.reset();
00281                         }
00282                         break;
00283                 default:
00284                     break;
00285              }         
00286         }        
00287    } 
00288 
00289 }
00290 /*uint8_t touch(uint8_t state)
00291 {
00292    static  uint8_t state1=0;
00293    detect_touch(state);                                             // determine the touch points
00294     
00295     switch(state)                                                   // state for touch
00296   {
00297       case 1:   
00298                 state1=touch_main();                                // determining the touch for main screen 
00299                 state=0;
00300                 nstate1=state1;
00301                 break;
00302                
00303       case 2: 
00304                 state1=touch_main_1();                              // determining the touch for SYSTEM CONFIGURATION screen 
00305                 state=0;
00306                 nstate1=state1;
00307                 break;
00308                 
00309       case 3:  
00310                 state1=touch_main_2();                              // determining the touch for TEST screen                 
00311                 state=0;
00312                 nstate1=state1;
00313                 break;
00314         
00315       case 4: 
00316                 state1=touch_glc();                                 // determining the touch for GLC screen 
00317                 state=0;
00318                 nstate1=state1;
00319                 break;       
00320         
00321       case 5: 
00322                 state1=touch_ecg();                                 // determining the touch for ECG screen 
00323                 state=0;
00324                 nstate1=state1;
00325                  break;   
00326      case 6: 
00327                 state1=touch_ret();                                 // determining the touch for returning back to the main screen      
00328                 state=0;
00329                 nstate1=state1;
00330                 break;
00331                 
00332      case 7: 
00333                 state1=touch_again_ecg();                          // determining the touch when readings are taken more than once screen 
00334                 state=0;
00335                 nstate1=state1;
00336                 break;
00337                 
00338      case 8:    
00339                 state1=touch_bp();                                  // determining the touch for BP screen 
00340                 state=0;
00341                 nstate1=state1;
00342                 break;           
00343                 
00344      case 9:    
00345                 state1=touch_again_glc();  
00346                 state=0;
00347                 nstate1=state1;
00348                 break;            
00349                 
00350      case 10 :  
00351                 DisableTouch();
00352                 wait_ms(300);
00353                 EnableTouch();
00354                 state=nstate;
00355                 break;
00356                   
00357 
00358       case 11:   
00359                 state1=touch_again_bp();            
00360                 state=0;
00361                 nstate1=state1;
00362                 break;
00363       
00364       case 12:   
00365                 state1=BT_return();            
00366                 state=0;
00367                 nstate1=state1;
00368                 break;
00369          
00370       case 13:  
00371                 state1 = touch_debug();                                         //touch state for debug
00372                 //state=0;
00373                 nstate1=state1;
00374                 break;  
00375      default :  
00376                 DisableTouch();
00377                 wait_ms(300);
00378                 EnableTouch();
00379                 state=nstate;
00380                 break;          
00381                           
00382     }         
00383      
00384     return state1;
00385 }*/
00386   
00387   
00388 uint8_t display(uint8_t state1)
00389 {
00390     static uint8_t state=0;
00391     static uint8_t ecg_flag=0;                                  // ecg_flag to indicate the ecg test is already done for the same pid
00392     static uint8_t glc_flag=0;                                  // glc_flag to indicate the glc test is already done for the same pid
00393     
00394     switch(state1)                                                              // state for display screens
00395     {
00396 
00397       
00398       case 1: 
00399                 //screen_main();                                                  //  main screen 
00400                 state1=0;
00401                 state=1;
00402                 nstate=state;
00403                 ecg_flag=0;                                                     // ecg, glc flag to determine whether the readings are taken more than once
00404                 glc_flag=0;
00405                 clear_filecreated_status();
00406                 //battery_status_display();                                       //Display battery status on LCD
00407                 pid = get_filepid() + 1;
00408                 state_touch = TOUCH_STATE_HOME_SCREEN;                          //added on 16-Nov rashmi
00409                 break;
00410       
00411       case 2: 
00412                 //screen_main_1();                                // System configration screen 
00413                 state1=0;
00414                 state=2;
00415                 nstate=state;
00416                 //battery_status_display();                                           //Display battery status on LCD
00417                 // timer_debug.stop();
00418                 break;
00419                
00420       case 3: 
00421                 //DisableTouch();                                                     
00422                 gc.printf("pidinit=%d",pid);
00423                 //screen_main_2(pid);     
00424                 state1=0;
00425                 state=3;
00426                 nstate=state;
00427                 //battery_status_display(); 
00428                 state_touch = TOUCH_STATE_TEST_SCREEN;                          //added on 16-Nov rashmi
00429                 break;
00430                 
00431       case 4: 
00432                 if (glc_flag==1)                                // glc_flag=1 when already the reading has been taken 
00433                 {
00434                     //screen_again();    
00435                     state1=0;
00436                     state=9;
00437                     nstate=state;
00438                 }
00439                 else
00440                 { 
00441                     //screen_glc();                                       // glc screen
00442                     state1=0;
00443                     state=4;
00444                     nstate=state;
00445                 }                
00446                 break;
00447      
00448      
00449      case 5:     
00450                 if (ecg_flag==1)
00451                 {
00452                     //screen_again();
00453                     state1=0;
00454                     state=7;
00455                     nstate=state;
00456                 }
00457                 
00458                 else
00459                 {   
00460                     //screen_ecg();                                           //ecg main screen 
00461                     state1=0;
00462                     state=5;
00463                     nstate=state;
00464                 }
00465                 break;
00466      
00467      case 6:   
00468                 //screen_bp();                                                 // bp main screen
00469                 state1=0;
00470                 state=8;
00471                 nstate=state;                
00472                 break;
00473      
00474      case 7:      
00475                 glc(pid);                                                       // glc measurement
00476                 glc_flag=1;
00477                 state1=0;
00478                 state=6;
00479                 eprom_write_8(30,0);
00480                 nstate=state;                                   
00481                 state_touch = TOUCH_STATE_TEST_SCREEN;                     
00482                 
00483                 break;
00484     
00485     case 8:    
00486                 // send ECG test in progress messages to mobile application
00487                 poc2p0_send_device_msg_res(ECG_TEST_IN_PROGRESS_RES);
00488                 ecg_bp_countdown();
00489                 ecg(pid);                                            // capturing ecg
00490                 state1=0;
00491                 ecg_flag=1;
00492                 state=6;
00493                 eprom_write_8(30,1);
00494                 nstate=state;
00495                 state_touch = TOUCH_STATE_TEST_SCREEN;                          
00496                 break;
00497              
00498       case 9:                
00499                 // send BP test in progress response to mobile application
00500                 poc2p0_send_device_msg_res(BP_TEST_IN_PROGRESS_RES);
00501                                               
00502                 ecg_bp_countdown();                                             // countdown for bp 
00503                 bp(pid);                                                        // measuring bp   earlier bp() changed to bp(pid) 28/4/2017
00504                 state1=0;
00505                 state=6;
00506                 eprom_write_8(30,2);
00507                 nstate=state;                                      
00508                 state_touch = TOUCH_STATE_TEST_SCREEN;                          
00509                  break;
00510  
00511                           
00512       case 10 :  //DisableTouch();
00513                  wait_ms(300);
00514                  //EnableTouch();
00515                  state=nstate;
00516                   break;        
00517                   
00518       case 11 :    
00519                 //screen_patient_info(get_filepid(), get_btpid());
00520                 state1=0;
00521                 state=12;
00522                 break;  
00523                          
00524       case 13 :                                                                 //display screen for debug
00525                 state1=0;
00526                 state=13;
00527                 nstate=state;
00528                 break; 
00529                    
00530       case 14 :                                         
00531                 delete_sdcard();                                                // delete SD card
00532                 state1=0;
00533                 state= 12;
00534                 nstate=state;
00535                 break;   
00536                 
00537         case 15 :    
00538                 gc.printf("Total Patients' data available : %d\n",get_filepid());                                        //state to read SD card
00539                 gc.printf("Enter the PID number");
00540                 read_sdcard_file();
00541                 state1=0;
00542                 state= 12;
00543                 nstate=state;
00544                 break;                            
00545        
00546         default :  
00547 
00548                   state1=nstate1;
00549                    break;
00550 
00551 
00552     }            
00553 
00554     return state;       
00555 
00556 }
00557 
00558 
00559 bool get_filecreated_status(void)                                               //function to get file created status
00560 {
00561     return file_created_status;
00562 }
00563 
00564 void set_filecreated_status(void)                                               //function to set file created status
00565 {
00566     file_created_status = true;
00567 }
00568 
00569 
00570 void clear_filecreated_status(void)                                             //function to clear file created status
00571 {
00572     file_created_status = false;
00573 }
00574 
00575 void increment_filepid (void)                                                       //increment pid if a test is completed
00576 {
00577     sd_read(filepid_btpid);                                                     //read pid from sd card
00578     filepid_btpid[0] = filepid_btpid[0] + 1;                                    //increment it by 1
00579     sd_write(filepid_btpid);                                                    //write it back to sd card  
00580 } 
00581 
00582 void increment_btpid (void)                                                      //increment pid if a test is completed
00583 {
00584     sd_read(filepid_btpid);                                                     //read pid from sd card
00585     filepid_btpid[1] = filepid_btpid[1] + 1;                                    //increment it by 1
00586     sd_write(filepid_btpid);                                                    //write it back to sd card
00587     
00588 } 
00589 
00590 uint32_t get_filepid(void)
00591 { 
00592     uint32_t filepid = 0;    
00593     
00594     sd_read(filepid_btpid);                                                           //read pid from sd card
00595     filepid = filepid_btpid[0];                                  
00596     return filepid; 
00597     
00598 }   
00599 
00600 void store_filepid(uint32_t pid)
00601 {
00602     sd_read(filepid_btpid);                                                       //read pid from sd card
00603     filepid_btpid[0] = pid;                                              
00604     sd_write(filepid_btpid);                                                      //write it back to sd card
00605                                                         
00606 } 
00607 
00608 uint32_t get_btpid(void)
00609 {
00610     uint32_t btpid = 0;    
00611     
00612     sd_read(filepid_btpid);                                                           //read pid from sd card
00613     btpid = filepid_btpid[1];                                  
00614     return btpid;  
00615 }   
00616 
00617 void store_btpid(uint32_t bt_pid)
00618 {     
00619     sd_read(filepid_btpid);                                                       //read pid from sd card
00620     filepid_btpid[1] = bt_pid;                                              
00621     sd_write(filepid_btpid);                                                      //write it back to sd card
00622 } 
00623 
00624 
00625 uint32_t get_timer_bluetooth()
00626 {
00627   return  timer_bt.read_ms(); 
00628 } 
00629 
00630 void battery_status_monitoring()
00631 {
00632     if(timer_battery.read_ms() > BATTERY_TIMER_VALUE ) 
00633     {
00634         battery_monitor();
00635         timer_battery.reset();
00636     }
00637 }
00638 
00639 bool read_debug_status()
00640 {
00641    // gc.printf("debug_mode=%d\n",debug_mode); 
00642     return debug_mode;
00643 }
00644 
00645 void debug_status_monitoring()
00646 {
00647  if (gc.readable())                                                             // check for the user input
00648         {
00649             
00650            while (gc.readable()!= '\n')                                         // read till the newline
00651             { 
00652                
00653                 gc.scanf("%s", debug_message_rx);                               // store the message typed in buffer
00654                 gc.printf("%s", debug_message_rx);
00655                 break;
00656             }
00657          
00658          
00659             if (strcmp(debug_message_rx, debug_message) == 0)                   // compare the message with the required one
00660             {
00661                  debug_mode = true;                                             // enable the debug mode status
00662                  gc.printf("entered into debug mode\n");
00663                  timer_debug.reset();
00664                  timer_debug.start();                                           // timer is started in orede
00665             } 
00666         }        
00667     
00668 }  
00669     
00670 uint32_t get_timer_debug()                                                      // timer for enabling debug option
00671 {
00672   if(timer_debug.read_ms()>TIMER_ENABLE_DEBUG_MODE)
00673     {
00674         timer_debug.stop();
00675     }
00676   return  timer_debug.read_ms(); 
00677 }
00678 
00679 // this command response to all the remaining command which doesn't have any data in response
00680 void poc2p0_send_device_msg_res(uint8_t response_cmd)
00681 {
00682         
00683     poc2p0_device_msg_response  poc2p0_device_msg_res = {0x00u};
00684 
00685     poc2p0_device_msg_res.sos = SOS_EOS;
00686     poc2p0_device_msg_res.cmd = response_cmd;
00687     poc2p0_device_msg_res.length = ZERO_DATA_LEN;
00688 
00689     poc2p0_device_msg_res.eos = SOS_EOS;
00690 
00691     poc2p0_device_msg_res.chksum = checksum_struct((uint8_t *)&poc2p0_device_msg_res.cmd, (sizeof(poc2p0_device_msg_res)-1));
00692 
00693     blue1.printf("%02x",poc2p0_device_msg_res.sos);
00694     blue1.printf("%02x",poc2p0_device_msg_res.cmd);
00695     blue1.printf("%02x",poc2p0_device_msg_res.length);
00696     blue1.printf("%02x",poc2p0_device_msg_res.eos);
00697     blue1.printf("%02x",poc2p0_device_msg_res.chksum);
00698     blue1.printf("\n");
00699     
00700     gc.printf("%02x",poc2p0_device_msg_res.sos);
00701     gc.printf("%02x",poc2p0_device_msg_res.cmd);
00702     gc.printf("%02x",poc2p0_device_msg_res.length);
00703     gc.printf("%02x",poc2p0_device_msg_res.eos);
00704     gc.printf("%02x",poc2p0_device_msg_res.chksum);
00705     gc.printf("\n");
00706 }
00707 
00708 // this command response to heartbeat message request send by bluetooth device
00709 void poc2p0_send_device_heartbeat_msg_res(uint8_t curr_touch_state)
00710 {    
00711     poc2p0_device_heartbeat_response    poc2p0_device_heartbeat_res = {0x00u};
00712        
00713     // RTC operations
00714     time_t epoch_time_glc;                              //A copy of time_t by name  epoch_time_bp is created
00715     struct tm * ptr_time_info_glc;                      // Sturucture copy of tm is created
00716 
00717     epoch_time_glc = rtc_read();                        // time is got from get epoch function.
00718     ptr_time_info_glc = localtime(&epoch_time_glc);     // Structure accepts the time in local format from "time_t" type.
00719 
00720     poc2p0_device_heartbeat_res.sos = SOS_EOS;
00721     poc2p0_device_heartbeat_res.cmd = HEARTBEAT_SYNC_MSG_ACCEPTED_RES;
00722     poc2p0_device_heartbeat_res.length = HEARTBEAT_DATA_LEN;
00723 
00724     // device ID
00725     poc2p0_device_heartbeat_res.did = eprom_read_8(12);
00726 
00727     // patient ID
00728     if (curr_touch_state == TOUCH_STATE_TEST_SCREEN)
00729     {
00730         memcpy(poc2p0_device_heartbeat_res.pid,get_userpid(),sizeof(poc2p0_device_heartbeat_res.pid));               
00731     }
00732     else 
00733     {
00734         memset(poc2p0_device_heartbeat_res.pid,'0',sizeof(poc2p0_device_heartbeat_res.pid));
00735         gc.printf("poc2p0_device_heartbeat_res.pid = %s\n",poc2p0_device_heartbeat_res.pid);
00736         
00737     }
00738 
00739     // device current date and time
00740     poc2p0_device_heartbeat_res.date_time.date = (uint8_t)(*ptr_time_info_glc).tm_mday;
00741     poc2p0_device_heartbeat_res.date_time.month = (uint8_t)(*ptr_time_info_glc).tm_mon+1;
00742     poc2p0_device_heartbeat_res.date_time.year = (uint8_t)(*ptr_time_info_glc).tm_year-100;
00743     poc2p0_device_heartbeat_res.date_time.hour = (uint8_t)(*ptr_time_info_glc).tm_hour;
00744     poc2p0_device_heartbeat_res.date_time.mins = (uint8_t)(*ptr_time_info_glc).tm_min;
00745     poc2p0_device_heartbeat_res.date_time.sec = (uint8_t)(*ptr_time_info_glc).tm_sec;
00746 
00747     // device state
00748     if (curr_touch_state == TOUCH_STATE_HOME_SCREEN)
00749     {
00750         poc2p0_device_heartbeat_res.device_state = HOME_SCREEN;
00751     }
00752     else
00753     {
00754         poc2p0_device_heartbeat_res.device_state = TEST_SCREEN;
00755     }
00756     
00757     //poc2p0_device_heartbeat_res.device_state = device_state;
00758 
00759     // device battery status
00760     poc2p0_device_heartbeat_res.battery_status = get_battery_status();
00761 
00762     poc2p0_device_heartbeat_res.eos = SOS_EOS;
00763 
00764     // calculate chksum
00765     poc2p0_device_heartbeat_res.chksum = checksum_struct((uint8_t *)&poc2p0_device_heartbeat_res.cmd, (sizeof(poc2p0_device_heartbeat_res)-1));
00766    
00767     blue1.printf("%02x",poc2p0_device_heartbeat_res.sos);
00768     blue1.printf("%02x",poc2p0_device_heartbeat_res.cmd);
00769     blue1.printf("%02x",poc2p0_device_heartbeat_res.length);
00770     blue1.printf("%02x",poc2p0_device_heartbeat_res.did);
00771     //blue1.printf("%010x",poc2p0_device_heartbeat_res.pid);
00772     for (uint8_t i = 0; i < sizeof(poc2p0_device_heartbeat_res.pid); i++)
00773     {
00774         blue1.printf("%02x",poc2p0_device_heartbeat_res.pid[i]);
00775     }
00776     blue1.printf("%02x",poc2p0_device_heartbeat_res.date_time.date);
00777     blue1.printf("%02x",poc2p0_device_heartbeat_res.date_time.month);
00778     blue1.printf("%02x",poc2p0_device_heartbeat_res.date_time.year);
00779     blue1.printf("%02x",poc2p0_device_heartbeat_res.date_time.hour);
00780     blue1.printf("%02x",poc2p0_device_heartbeat_res.date_time.mins);
00781     blue1.printf("%02x",poc2p0_device_heartbeat_res.date_time.sec);
00782     blue1.printf("%02x",poc2p0_device_heartbeat_res.device_state);
00783     blue1.printf("%02x",poc2p0_device_heartbeat_res.battery_status);
00784     blue1.printf("%02x",poc2p0_device_heartbeat_res.eos);
00785     blue1.printf("%02x",poc2p0_device_heartbeat_res.chksum);
00786     blue1.printf("\n");
00787     
00788     gc.printf("%02x",poc2p0_device_heartbeat_res.sos);
00789     gc.printf("%02x",poc2p0_device_heartbeat_res.cmd);
00790     gc.printf("%02x",poc2p0_device_heartbeat_res.length);
00791     gc.printf("%02x",poc2p0_device_heartbeat_res.did);
00792     //gc.printf("%010x",poc2p0_device_heartbeat_res.pid);
00793     for (uint8_t i = 0; i < sizeof(poc2p0_device_heartbeat_res.pid); i++)
00794     {
00795         gc.printf("%02x",poc2p0_device_heartbeat_res.pid[i]);
00796     }
00797     gc.printf("%02x",poc2p0_device_heartbeat_res.date_time.date);
00798     gc.printf("%02x",poc2p0_device_heartbeat_res.date_time.month);
00799     gc.printf("%02x",poc2p0_device_heartbeat_res.date_time.year);
00800     gc.printf("%02x",poc2p0_device_heartbeat_res.date_time.hour);
00801     gc.printf("%02x",poc2p0_device_heartbeat_res.date_time.mins);
00802     gc.printf("%02x",poc2p0_device_heartbeat_res.date_time.sec);
00803     gc.printf("%02x",poc2p0_device_heartbeat_res.device_state);
00804     gc.printf("%02x",poc2p0_device_heartbeat_res.battery_status);
00805     gc.printf("%02x",poc2p0_device_heartbeat_res.eos);
00806     gc.printf("%02x",poc2p0_device_heartbeat_res.chksum);
00807     gc.printf("\n"); 
00808 }
00809 
00810 // this command send final test result response to bluetooth device
00811 void poc2p0_send_device_test_result_res(uint8_t response_cmd, uint32_t result)
00812 {    
00813     poc2p0_device_tst_result_response   poc2p0_device_tst_result_res = {0x00u};
00814 
00815     poc2p0_device_tst_result_res.sos = SOS_EOS;
00816     poc2p0_device_tst_result_res.cmd = response_cmd;
00817     poc2p0_device_tst_result_res.length = DEVICE_RESULT_DATA_LEN;
00818 
00819     // device result provided by device
00820     poc2p0_device_tst_result_res.test_result = result;
00821 
00822     poc2p0_device_tst_result_res.eos = SOS_EOS;
00823 
00824     poc2p0_device_tst_result_res.chksum = checksum_struct((uint8_t *)&poc2p0_device_tst_result_res.cmd, (sizeof(poc2p0_device_tst_result_res)-1));
00825 
00826     blue1.printf("%02x",poc2p0_device_tst_result_res.sos);
00827     blue1.printf("%02x",poc2p0_device_tst_result_res.cmd);
00828     blue1.printf("%02x",poc2p0_device_tst_result_res.length);
00829     blue1.printf("%08x",poc2p0_device_tst_result_res.test_result);
00830     blue1.printf("%02x",poc2p0_device_tst_result_res.eos);
00831     blue1.printf("%02x",poc2p0_device_tst_result_res.chksum);
00832     blue1.printf("\n");  
00833     
00834     gc.printf("%02x",poc2p0_device_tst_result_res.sos);
00835     gc.printf("%02x",poc2p0_device_tst_result_res.cmd);
00836     gc.printf("%02x",poc2p0_device_tst_result_res.length);
00837     gc.printf("%08x",poc2p0_device_tst_result_res.test_result);
00838     gc.printf("%02x",poc2p0_device_tst_result_res.eos);
00839     gc.printf("%02x",poc2p0_device_tst_result_res.chksum);
00840     gc.printf("\n");  
00841 }
00842 
00843 static void ecg_bp_countdown(void)
00844 {
00845     for(uint8_t i = 5; i <= 0; i--)
00846     {
00847         wait(1);
00848     }
00849 }
00850 
00851 static void read_device_id(void)                                                // reading device id from eeprom
00852 {
00853     
00854     device_id =  eprom_read_8(12);                                              // reading did from eeprom location 
00855     gc.printf("device_id = %d\n", device_id);                                                     
00856     if((get_Error()!= 0) || (device_id == 0xFF))                                // checking for error                               
00857     {
00858             device_id = eprom_read_8(12) ; 
00859          if(get_Error()!= 0  || (device_id == 0xFF))  
00860             {
00861                 gc.printf("database error!\n");                                 // database error
00862                 //return did; 
00863             }
00864     }
00865      
00866 }
00867 
00868 uint8_t get_did()
00869 {
00870     return  device_id;
00871 }
00872 
00873 void sd_card_status(void)                                                       // checking the status of the sd card initialization
00874 {
00875  uint8_t error_sd= 0;
00876  error_sd = dummy_file_create_delete();                                         // creating the dummy file
00877 
00878  if( error_sd == 1)                                                             // if error = 1 // error in opening file 
00879  {
00880        error_sd = dummy_file_create_delete(); 
00881         if( error_sd == 1)
00882             {
00883                 gc.printf("database error!\n");                                 // dispaly data base error
00884                 //database_error();
00885               
00886             }
00887  }
00888  
00889 } 
00890