created separate function for hex to char

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of SS_SensePOC2P0_11Dec2017_USERPID by rashmi v

Revision:
57:e82d4bd6b2c0
Parent:
54:f2a413d5dffd
Child:
58:b75255e6e4c3
--- a/main.cpp	Fri Nov 03 07:16:44 2017 +0000
+++ b/main.cpp	Fri Nov 10 11:58:38 2017 +0000
@@ -9,6 +9,7 @@
 #include "eeprom_pgm.h"
 #include "struct.h"
 #include "battery.h" 
+#include "rtc.h"
 #include "main.h"
 
 #define TIMER_RXACK_BTCONNECTION                10000
@@ -16,8 +17,55 @@
 #define SERIAL_BAUD_RATE                        115200
 #define BATTERY_SCREEN_NO_CHARGE_CONDITION      3
 #define TIMER_ENTER_DEBUG_MODE                  10000
+#define HEARTBEAT_DATA_LEN                      0x0Du
+#define DEVICE_RESULT_DATA_LEN                  0x04u
+#define ZERO_DATA_LEN                           0x00u
 
+typedef enum
+{
+    GET_TOUCH_DISP_STATE = 0x00U,
+    BT_TX_STATE,
+    BT_RX_STATE
 
+}DEVICE_MAIN_STATE;
+
+// this helps to respond various status during test cycle, error condition and simple ack or nack
+typedef struct __attribute__((__packed__))
+{
+    uint8_t     sos;
+    uint8_t     cmd;                    // this contains error response
+    uint8_t     length;
+    uint8_t     eos;
+    uint8_t     chksum;
+
+}poc2p0_device_msg_response;
+
+typedef struct __attribute__((__packed__))
+{
+    uint8_t     sos;
+    uint8_t     cmd;
+    uint8_t     length;
+    uint32_t    test_result;
+    uint8_t     eos;
+    uint8_t     chksum;
+
+}poc2p0_device_tst_result_response;
+
+// this will be used for responding heart beat message
+typedef struct __attribute__((__packed__))
+{
+    uint8_t                 sos;
+    uint8_t                 cmd;                    // this contains error response
+    uint8_t                 length;
+    DateTime_info           date_time;
+    uint32_t                pid;
+    uint8_t                 did;
+    uint8_t                 device_state;          //changed POC2P0_DEVICE_STATE to uint8_t
+    uint8_t                 battery_status;
+    uint8_t                 eos;
+    uint8_t                 chksum;
+
+}poc2p0_device_heartbeat_response;
 
 Serial gc(USBTX, USBRX);
 DigitalIn q1(PTB11);
@@ -35,7 +83,7 @@
 static bool file_created_status = false;                                  //initialise file created to 0
 static uint32_t filepid_btpid[2] = {};
 Timer timer_bt;                                                         // timer for bluetooth 
-Timer timer_battery;                                                     // timer for battery
+Timer timer_battery;                                                    // timer for battery
 Timer timer_debug;                                                       // timer for entering the debug mode   
 static bool debug_mode=false;                                               // holds the status of the debug mode "1" indicates the device is in debug mode 
 char debug_message_rx[6];                                               // buffer to receive the debug message data
@@ -45,13 +93,11 @@
 
 int main()
 {
-    static uint8_t main_state=1;
+    static DEVICE_MAIN_STATE main_state = GET_TOUCH_DISP_STATE;
     static uint8_t state_display = 0;
     static uint8_t state_touch = 1;
     uint8_t state_t = 1;                                     // state to transmit to bluetooth
     uint8_t state_r = 0;                                     // state to receive from bluetooth
-    
-  
   
     DisableTouch();
     gc.baud(115200);
@@ -76,7 +122,6 @@
             }
         }   
       
-        
         battery_status_monitoring();                                            // to  monitior battery status after every 10 min. 
 
         if (get_battery_status() == BATTERY_SCREEN_NO_CHARGE_CONDITION)   // to check battery status and take action 
@@ -85,30 +130,24 @@
         }
         else
         {
-
             EnableTouch();
-             gc.printf("c");  
-            switch(main_state)                                             // main state machine
+            gc.printf("c");
+            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 GET_TOUCH_DISP_STATE:
+
+                    state_display = poc2p0_bt_receive(state_touch);
+                    //state_display = touch(state_touch);                   // touch state
+                    display(state_display);                                 //display state
+
+                    if(state_display == 12)
+                    {
+                        main_state = BT_TX_STATE;
+                    }
+
+                    break;
     
-                case 2:    
-                         state_touch = display(state_display);           //display state
-                         main_state = 1;
-                        break;
-                
-                
-                case 3: 
+                case BT_TX_STATE:
                         DisableTouch();                                             // bluetooth send 
                         timer_bt.start();
                          if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)                           // bluetooth connection timer 15 sec
@@ -131,14 +170,14 @@
                                         BT_finished();                         // screen to display communication finished
                                         screen_BT_return();                    //screen to return back
                                         EnableTouch();
-                                        main_state=1;
+                                        main_state=GET_TOUCH_DISP_STATE;
                                         state_touch=12;
                                         break;
                                      }
                                    
                                 else 
                                     {
-                                        main_state=4;                           // move to receving stste of bluetooth
+                                        main_state=BT_RX_STATE;                           // move to receving stste of bluetooth
                                     }
                           
                             }  
@@ -151,11 +190,11 @@
 
                         else
                         {                                   
-                             BT_no_connection();     // display screen to show that there is no connection
+                            BT_no_connection();     // display screen to show that there is no connection
                             gc.printf("stopped");
                             screen_BT_return();     // button to return
                             EnableTouch();
-                            main_state=1;
+                            main_state=GET_TOUCH_DISP_STATE;
                             state_touch=12;
                             //sd_close();  
                             timer_bt.stop();
@@ -163,7 +202,7 @@
                         }
                         break;
                  
-                case 4: 
+                case BT_RX_STATE:
                         DisableTouch();  
             
                         if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)
@@ -182,13 +221,13 @@
                                     BT_finished();
                                     screen_BT_return();  
                                     EnableTouch();
-                                    main_state=1;
+                                    main_state=GET_TOUCH_DISP_STATE;
                                     state_touch=12;  // jumping to touch case 12
                                     break;
                                   } 
                                 else                                 // move to bluetooth transmit if the states are 1,2,3,4
                                  {
-                                    main_state=3;
+                                    main_state=BT_TX_STATE;
                                  }  
                             }
                          }                 
@@ -200,7 +239,7 @@
                             gc.printf("stopped");
                             screen_BT_return();                     // button to return
                             EnableTouch();
-                            main_state=1;
+                            main_state=GET_TOUCH_DISP_STATE;
                             state_touch=12;
                             state_t = 1;                                     // state to transmit to bluetooth
                             state_r = 0;                                     // state to receive from bluetooth
@@ -208,7 +247,9 @@
                             timer_bt.stop();
                             timer_bt.reset();
                         }
-                        break;      
+                        break;
+                default:
+                    break;
              }         
         }        
    } 
@@ -281,7 +322,7 @@
                 state=nstate;
                 break;
                   
-                  
+
       case 11:   
                 state1=touch_again_bp();            
                 state=0;
@@ -427,24 +468,31 @@
     
     case 8:     DisableTouch();
                 ecg_countdown();                                                // countdown before ecg capture
+
+                // send ECG test in progress messages to mobile application
+                poc2p0_send_device_msg_res(ECG_TEST_IN_PROGRESS_RES);
+
                 heart_rate=ecg(pid);                                            // capturing ecg
                 state1=0;
                 if (heart_rate==0)
-                    {
-                       
-                       screen_ecg_lead();  
-                     }
+                {
+                    // send ECG lead off messages to mobile application
+                    poc2p0_send_device_msg_res(ECG_LEADOFF_RES);
+                    screen_ecg_lead();
+                }
                 else if(heart_rate==1)                                          // if heart rate goes above or below range display error // 10/7/17 nikita
-                     {
-                          screen_ecg_error();
-                         
-                         }      
-                  else 
-                       {
-                       
-                         screen_ecg_2(heart_rate);
-                         ecg_flag=1;
-                         }                            
+                {
+                    // send ECG result improper messages to mobile application
+                    poc2p0_send_device_msg_res(ECG_RESULT_IMPROPER_RES);
+                    screen_ecg_error();
+                }
+                else
+                {
+                    // send ECG final result messages to mobile application
+                    poc2p0_send_device_test_result_res (ECG_TEST_SUCCSS_RES, (uint32_t)heart_rate);
+                    screen_ecg_2(heart_rate);
+                    ecg_flag=1;
+                }
                 state=6;
                 eprom_write_8(30,1);
                 nstate=state;
@@ -452,10 +500,14 @@
                 EnableTouch();
                 break;
              
-      case 9: 
+      case 9:
                 DisableTouch();
                 screen_bp2();                                                   // bp main screen
                 bp_countdown();                                                 // countdown for bp 
+
+                // send BP test in progress response to mobile application
+                poc2p0_send_device_msg_res(BP_TEST_IN_PROGRESS_RES);
+
                 bp(pid);                                                        // measuring bp   earlier bp() changed to bp(pid) 28/4/2017
                 state1=0;
                 state=6;
@@ -511,17 +563,17 @@
                 break;                            
        
         default :  
-                  
+
                   state1=nstate1;
-                   break;          
-                            
-            
+                   break;
+
+
     }            
-    
-           
-                
+
+
+
     return state;       
-                
+
 }
 
 
@@ -664,4 +716,101 @@
   return  timer_debug.read_ms(); 
 }
 
+// this command response to all the remaining command which doesn't have any data in response
+void poc2p0_send_device_msg_res(uint8_t response_cmd)
+{
+    poc2p0_device_msg_response  poc2p0_device_msg_res = {0x00u};
 
+    poc2p0_device_msg_res.sos = SOS_EOS;
+    poc2p0_device_msg_res.cmd = response_cmd;
+    poc2p0_device_msg_res.length = ZERO_DATA_LEN;
+
+    poc2p0_device_msg_res.eos = SOS_EOS;
+
+    poc2p0_device_msg_res.chksum = checksum_struct((uint8_t *)poc2p0_device_msg_res.cmd, (sizeof(poc2p0_device_msg_res)-2));
+
+    //blue1.puts((const char *)&poc2p0_device_msg_res);
+    gc.puts((const char *)&poc2p0_device_msg_res);
+    
+}
+
+// this command response to heartbeat message request send by bluetooth device
+void poc2p0_send_device_heartbeat_msg_res(uint8_t curr_touch_state)
+{
+    poc2p0_device_heartbeat_response    poc2p0_device_heartbeat_res = {0x00u};
+
+    // RTC operations
+    time_t epoch_time_glc;                              //A copy of time_t by name  epoch_time_bp is created
+    struct tm * ptr_time_info_glc;                      // Sturucture copy of tm is created
+
+    epoch_time_glc = rtc_read();                        // time is got from get epoch function.
+    ptr_time_info_glc = localtime(&epoch_time_glc);     // Structure accepts the time in local format from "time_t" type.
+
+    poc2p0_device_heartbeat_res.sos = SOS_EOS;
+    poc2p0_device_heartbeat_res.cmd = HEARTBEAT_SYNC_MSG_ACCEPTED_RES;
+    poc2p0_device_heartbeat_res.length = HEARTBEAT_DATA_LEN;
+
+    // device ID
+    poc2p0_device_heartbeat_res.did = eprom_read_8(12);
+
+    // patient ID
+    if (curr_touch_state == TOUCH_STATE_TEST_SCREEN)
+    {
+        poc2p0_device_heartbeat_res.pid = pid;
+    }
+    else
+    {
+        poc2p0_device_heartbeat_res.pid = 0x00u;
+    }
+
+    // device current date and time
+    poc2p0_device_heartbeat_res.date_time.date = (uint8_t)(*ptr_time_info_glc).tm_mday;
+    poc2p0_device_heartbeat_res.date_time.month = (uint8_t)(*ptr_time_info_glc).tm_mon+1;
+    poc2p0_device_heartbeat_res.date_time.year = (uint8_t)(*ptr_time_info_glc).tm_year-100;
+    poc2p0_device_heartbeat_res.date_time.hour = (uint8_t)(*ptr_time_info_glc).tm_hour;
+    poc2p0_device_heartbeat_res.date_time.mins = (uint8_t)(*ptr_time_info_glc).tm_min;
+    poc2p0_device_heartbeat_res.date_time.sec = (uint8_t)(*ptr_time_info_glc).tm_sec;
+
+    // device state
+    if (curr_touch_state == TOUCH_STATE_TEST_SCREEN)
+    {
+        poc2p0_device_heartbeat_res.device_state = TOUCH_STATE_TEST_SCREEN;
+    }
+    else
+    {
+        poc2p0_device_heartbeat_res.device_state = TOUCH_STATE_HOME_SCREEN;
+    }
+    
+    //poc2p0_device_heartbeat_res.device_state = device_state;
+
+    // device battery status
+    poc2p0_device_heartbeat_res.battery_status = get_battery_status();
+
+    poc2p0_device_heartbeat_res.eos = SOS_EOS;
+
+    // calculate chksum
+    poc2p0_device_heartbeat_res.chksum = checksum_struct((uint8_t *)poc2p0_device_heartbeat_res.cmd, (sizeof(poc2p0_device_heartbeat_res)-2));
+
+    //blue1.puts((const char *)&poc2p0_device_heartbeat_res);
+    gc.puts((const char *)&poc2p0_device_heartbeat_res);
+}
+
+// this command send final test result response to bluetooth device
+void poc2p0_send_device_test_result_res(uint8_t response_cmd, uint32_t result)
+{
+    poc2p0_device_tst_result_response   poc2p0_device_tst_result_res = {0x00u};
+
+    poc2p0_device_tst_result_res.sos = SOS_EOS;
+    poc2p0_device_tst_result_res.cmd = response_cmd;
+    poc2p0_device_tst_result_res.length = DEVICE_RESULT_DATA_LEN;
+
+    // device result provided by device
+    poc2p0_device_tst_result_res.test_result = result;
+
+    poc2p0_device_tst_result_res.eos = SOS_EOS;
+
+    poc2p0_device_tst_result_res.chksum = checksum_struct((uint8_t *)poc2p0_device_tst_result_res.cmd, (sizeof(poc2p0_device_tst_result_res)-2));
+
+    //blue1.puts((const char *)&poc2p0_device_tst_result_res);
+    gc.puts((const char *)&poc2p0_device_tst_result_res);
+}