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:
72:1f990fa8c516
Parent:
71:8f1851c556d6
Child:
73:22e4f398a2a6
--- a/main.cpp	Fri Dec 01 11:18:34 2017 +0000
+++ b/main.cpp	Wed Dec 06 12:47:30 2017 +0000
@@ -1,8 +1,8 @@
 #include "mbed.h"
-#include "display_modules.h"
-#include "touch_modules.h"
+//#include "display_modules.h"
+//#include "touch_modules.h"
 #include "ecg_dec.h"
-#include "touch_modules.h"
+//#include "touch_modules.h"
 #include "glc.h"
 #include "bp.h"
 #include "sdcard.h"
@@ -11,6 +11,7 @@
 #include "battery.h" 
 #include "rtc.h"
 #include "main.h"
+#include "SDFileSystem.h"
 
 #define TIMER_RXACK_BTCONNECTION                10000
 #define BATTERY_TIMER_VALUE                     600000
@@ -20,9 +21,7 @@
 #define HEARTBEAT_DATA_LEN                      0x0Du
 #define DEVICE_RESULT_DATA_LEN                  0x04u
 #define ZERO_DATA_LEN                           0x00u
-#define DEVICE_RES_LEN                          0x10u
-#define HEARTBEAT_RES_LEN                       0X36u
-#define TEST_RES_LEN                            0X18u
+
 
 typedef enum
 {
@@ -67,10 +66,8 @@
     uint8_t                 length;
     uint8_t                 did;
     char                    pid[10];
-    //uint32_t                pid;
+    //uint32_t              pid;
     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;
@@ -78,19 +75,22 @@
 
 }poc2p0_device_heartbeat_response;
 
+SDFileSystem sd(PTE1, PTE3, PTE2, PTE4, "sd");
 Serial gc(USBTX, USBRX);
 DigitalIn q1(PTB11);
 DigitalIn q(PTC5);
 DigitalIn q3(PTC12);
 DigitalIn BT(PTA5);   
 Serial blue1(PTC4,PTC3); 
-static void send_thru_terminal(uint8_t input_buf[], uint8_t len_buff,uint8_t struct_data[],uint8_t len_struct);
  
 uint32_t get_timer_bluetooth();
 static void battery_status_monitoring();                                     //bluetooth status pin
 uint8_t touch(uint8_t state);                                               // touch state
 uint8_t display(uint8_t state1);                                            //  display state
 static void set_userpid(char *user_pid);
+static void ecg_bp_countdown(void);
+static void read_device_id(void);
+void sd_card_status(void);
 
 static uint8_t nstate=0;
 static uint8_t nstate1=0;                                                  
@@ -107,6 +107,7 @@
 static uint8_t state_touch = 1;                                                 //added on 16-NOV rashmi 
 uint8_t buffer_pointer = 0x00u;  
 static char user_pid[10]; 
+static uint8_t device_id = 0;
                                 
 int main()
 {
@@ -116,18 +117,19 @@
     uint8_t state_t = 1;                                     // state to transmit to bluetooth
     uint8_t state_r = 0;                                     // state to receive from bluetooth
   
-    DisableTouch();
+    //DisableTouch();
     gc.baud(115200);
     blue1.baud(115200);
-    init_screen();                                                              //initialize lcd
+    sd.disk_initialize();
+    sd_card_status();                                                         
     pid = get_filepid() + 1;                                                    //added on 22/09/2017 nikita
-    screen_main();                                                              // display of main screen 
+    //screen_main();                                                              // display of main screen 
     battery_monitor();                                                          // monitor battery at the start only
-    battery_status_display();                                                   // display of main screen
+    //battery_status_display();                                                   // display of main screen
     timer_battery.start();
     timer_debug.start();
     gc.printf("enter message to go into debug mode\n");
-    
+    read_device_id();
    while(1)
    {
     
@@ -143,11 +145,11 @@
 
         if (get_battery_status() == BATTERY_SCREEN_NO_CHARGE_CONDITION)   // to check battery status and take action 
         {        
-                 DisableTouch();
+                 //DisableTouch();
         }
         else
         {
-            EnableTouch();
+            //EnableTouch();
             //gc.printf("c");
             switch(main_state)                                              // main state machine
              {
@@ -155,17 +157,25 @@
 
                     state_display = poc2p0_bt_receive(state_touch);
                     //state_display = touch(state_touch);                   // touch state
+                    //gc.printf(" state_display = %d\n", state_display);
                     display(state_display);                                 //display state
-
+                    
+                    // if(state_display == 3)
+                    //{
+                    //   display(15);
+                        //main_state = GET_TOUCH_DISP_STATE;
+                   // }
                     if(state_display == 12)
                     {
                         main_state = BT_TX_STATE;
                     }
+                   // else
+                   //   display(state_display);  
 
                     break;
     
                 case BT_TX_STATE:
-                        DisableTouch();                                             // bluetooth send 
+                        //DisableTouch();                                             // bluetooth send 
                         timer_bt.start();
                          if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)                           // bluetooth connection timer 15 sec
                          {
@@ -175,7 +185,8 @@
                     
                                timer_bt.stop();                                        // stop the timer once connected to app
                                timer_bt.reset(); 
-                               BT_connection() ;                                // displaying connection on bluetooth 
+                               //BT_connection() ;                                // displaying connection on bluetooth 
+                               gc.printf(" BT Connected\n");
                                state_r=bt_send(state_t);
                                timer_bt.start();
                                 //gc.printf(" state_t = %d\n", state_t);
@@ -184,9 +195,10 @@
                                     {
                                         timer_bt.stop();                           // stop the timer once connected to app
                                         timer_bt.reset();
-                                        BT_finished();                         // screen to display communication finished
-                                        screen_BT_return();                    //screen to return back
-                                        EnableTouch();
+                                        //BT_finished();                         // screen to display communication finished
+                                        //screen_BT_return();                    //screen to return back
+                                        gc.printf("BT Transfer Complete\n");
+                                        //EnableTouch();
                                         main_state=GET_TOUCH_DISP_STATE;
                                         state_touch=12;
                                         break;
@@ -201,17 +213,18 @@
                   
                             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("BT No Connection\n");
                             }
                          }
 
                         else
                         {
                             poc2p0_send_device_msg_res(MSG_REJECTED_RES);       //added on 14-Nov by rashmi                                   
-                            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();
+                            //screen_BT_return();                                 // button to return
+                            //EnableTouch();
                             main_state=GET_TOUCH_DISP_STATE;
                             state_touch=12;
                             //sd_close();  
@@ -221,7 +234,7 @@
                         break;
                  
                 case BT_RX_STATE:
-                        DisableTouch();  
+                        //DisableTouch();  
             
                         if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)
                         {
@@ -236,9 +249,10 @@
                                 if(state_t ==0)           // if state_t is zero, there is no other file to send
                                  {
                                   
-                                    BT_finished();
-                                    screen_BT_return();  
-                                    EnableTouch();
+                                    //BT_finished();
+                                    //screen_BT_return();  
+                                    gc.printf("BT Transfer Complete\n");
+                                    //EnableTouch();
                                     main_state=GET_TOUCH_DISP_STATE;
                                     state_touch=12;  // jumping to touch case 12
                                     break;
@@ -252,11 +266,12 @@
                    
                         else 
                         {    
-                            DisableTouch();
-                            BT_no_connection();                     // display screen to show that there is no connection
+                            //DisableTouch();
+                            //BT_no_connection();                     // display screen to show that there is no connection
                             gc.printf("stopped");
-                            screen_BT_return();                     // button to return
-                            EnableTouch();
+                            //screen_BT_return();                     // button to return
+                            gc.printf("BT No Connection\n");
+                            //EnableTouch();
                             main_state=GET_TOUCH_DISP_STATE;
                             state_touch=12;
                             state_t = 1;                                     // state to transmit to bluetooth
@@ -273,7 +288,7 @@
    } 
 
 }
-uint8_t touch(uint8_t state)
+/*uint8_t touch(uint8_t state)
 {
    static  uint8_t state1=0;
    detect_touch(state);                                             // determine the touch points
@@ -368,7 +383,7 @@
     }         
      
     return state1;
-}
+}*/
   
   
 uint8_t display(uint8_t state1)
@@ -377,81 +392,75 @@
     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 
+                //DisableTouch();
+                //screen_main();                                                  //  main screen 
                 state1=0;
                 state=1;
                 nstate=state;
                 ecg_flag=0;                                                     // ecg, glc flag to determine whether the readings are taken more than once
                 glc_flag=0;
                 clear_filecreated_status();
-                battery_status_display();                                       //Display battery status on LCD
+                //battery_status_display();                                       //Display battery status on LCD
                 pid = get_filepid() + 1;
                 state_touch = TOUCH_STATE_HOME_SCREEN;                          //added on 16-Nov rashmi
-                EnableTouch();
+                //EnableTouch();
                 break;
       
       case 2: 
-                DisableTouch();
-                screen_main_1();                                // System configration screen 
+                //DisableTouch();
+                //screen_main_1();                                // System configration screen 
                 state1=0;
                 state=2;
                 nstate=state;
-                battery_status_display();                                           //Display battery status on LCD
-                EnableTouch();
+                //battery_status_display();                                           //Display battery status on LCD
+                //EnableTouch();
                 // timer_debug.stop();
                 break;
                
       case 3: 
-                DisableTouch();
-                gc.printf("\n\r Enter User PID\n"); 
-                                           
-                set_userpid(user_pid);
-                gc.printf("User pid = %s\n",user_pid);
-                                             
+                //DisableTouch();                                                     
                 gc.printf("pidinit=%d",pid);
-                screen_main_2(user_pid);     
+                //screen_main_2(pid);     
                 state1=0;
                 state=3;
                 nstate=state;
-                battery_status_display(); 
+                //battery_status_display(); 
                 state_touch = TOUCH_STATE_TEST_SCREEN;                          //added on 16-Nov rashmi
-                EnableTouch();
+                //EnableTouch();
                 break;
                 
       case 4: 
-                DisableTouch();
+                //DisableTouch();
                 if (glc_flag==1)                                // glc_flag=1 when already the reading has been taken 
                 {
-                    screen_again();    
+                    //screen_again();    
                     state1=0;
                     state=9;
                     nstate=state;
                 }
                 else
                 { 
-                    screen_glc();                                       // glc screen
+                    //screen_glc();                                       // glc screen
                     state1=0;
                     state=4;
                     nstate=state;
                 }
-                EnableTouch();
+                //EnableTouch();
                 
                 break;
      
      
      case 5:     
-                DisableTouch();
+               // DisableTouch();
                 if (ecg_flag==1)
                 {
-                    screen_again();
+                    //screen_again();
                     state1=0;
                     state=7;
                     nstate=state;
@@ -459,120 +468,120 @@
                 
                 else
                 {   
-                    screen_ecg();                                           //ecg main screen 
+                    //screen_ecg();                                           //ecg main screen 
                     state1=0;
                     state=5;
                     nstate=state;
                 }
-                EnableTouch();
+                //EnableTouch();
                 break;
      
      case 6:   
-                DisableTouch();
-                screen_bp();                                                 // bp main screen
+                //DisableTouch();
+                //screen_bp();                                                 // bp main screen
                 state1=0;
                 state=8;
                 nstate=state;         
-                EnableTouch();        
+                //EnableTouch();        
                 break;
      
      case 7:    
-                DisableTouch();     
+                //DisableTouch();     
                 glc(pid);                                                       // glc measurement
                 glc_flag=1;
                 state1=0;
-                screen_glc_2();
+                //screen_glc_2();
                 state=6;
                 eprom_write_8(30,0);
                 nstate=state;  
                 //wait(2);                                                        //added on 16-NOV rashmi
-                screen_main_2(user_pid);                                       //added on 20-NOV rashmi   
+                //screen_main_2(pid);                                       //added on 20-NOV rashmi   
                 state_touch = TOUCH_STATE_TEST_SCREEN;                          //added on 16-Nov rashmi    
-                EnableTouch();
+                //EnableTouch();
                 break;
     
-    case 8:     DisableTouch();
+    case 8:    // DisableTouch();
                 // send ECG test in progress messages to mobile application
                 poc2p0_send_device_msg_res(ECG_TEST_IN_PROGRESS_RES);
-                ecg_countdown();                                                // countdown before ecg capture
-
+                //ecg_countdown();                                                // countdown before ecg capture
+                ecg_bp_countdown();
                 ecg(pid);                                            // capturing ecg
                 state1=0;
                 ecg_flag=1;
                 state=6;
                 eprom_write_8(30,1);
                 nstate=state;
-                screen_ecg_bp();
+                //screen_ecg_bp();
                 //wait(2);                                                        //added on 16-NOV rashmi
-                screen_main_2(user_pid);                                        //added on 20-NOV rashmi
+                //screen_main_2(pid);                                        //added on 20-NOV rashmi
                 state_touch = TOUCH_STATE_TEST_SCREEN;                          //added on 16-Nov rashmi
-                EnableTouch();
+                //EnableTouch();
                 break;
              
       case 9:
-                DisableTouch();
-                screen_bp2();                                                   // bp main screen
+                //DisableTouch();
+                //screen_bp2();                                                   // bp main screen
                 
                 // send BP test in progress response to mobile application
                 poc2p0_send_device_msg_res(BP_TEST_IN_PROGRESS_RES);
 
-                bp_countdown();                                                 // countdown for bp 
-
+                //bp_countdown();                                                 // countdown for bp 
+                ecg_bp_countdown();
                 bp(pid);                                                        // measuring bp   earlier bp() changed to bp(pid) 28/4/2017
                 state1=0;
                 state=6;
                 eprom_write_8(30,2);
                 nstate=state;
                 //wait(2);                                                        //added on 16-NOV rashmi
-                screen_main_2(user_pid);                                       //added on 20-NOV rashmi 
+                //screen_main_2(pid);                                       //added on 20-NOV rashmi 
                 state_touch = TOUCH_STATE_TEST_SCREEN;                          //added on 16-Nov rashmi
-                EnableTouch();
+                //EnableTouch();
                  break;
  
                           
-      case 10 :  DisableTouch();
+      case 10 :  //DisableTouch();
                  wait_ms(300);
-                 EnableTouch();
+                 //EnableTouch();
                  state=nstate;
                   break;        
                   
       case 11 :    
-                DisableTouch();
-                screen_patient_info(get_filepid(), get_btpid());
+                //DisableTouch();
+                //screen_patient_info(get_filepid(), get_btpid());
                 state1=0;
                 state=12;
-                EnableTouch();
+                //EnableTouch();
                 break;  
                          
       case 13 :                                                                 //display screen for debug
-                DisableTouch();
-                screen_debug();
+               // DisableTouch();
+                //screen_debug();
                 state1=0;
                 state=13;
                 nstate=state;
-                EnableTouch();
+               // EnableTouch();
                 break; 
                    
       case 14 :    
-                DisableTouch();                                                 //state to delete SD card
+                //DisableTouch();                                                 //state to delete SD card
                 delete_sdcard();
-                screen_sdcard_delete();
+                //screen_sdcard_delete();
                 state1=0;
                 state= 12;
                 nstate=state;
-                EnableTouch();
+                //EnableTouch();
                 break;   
                 
         case 15 :    
-                DisableTouch();
+               // DisableTouch();
                 gc.printf("Total Patients' data available : %d\n",get_filepid());                                        //state to read SD card
                 gc.printf("Enter the PID number");
                 read_sdcard_file();
-                screen_sdcard_read();
+                //screen_sdcard_read();
                 state1=0;
                 state= 12;
                 nstate=state;
-                EnableTouch();
+               // EnableTouch();
                 break;                            
        
         default :  
@@ -727,8 +736,7 @@
 // 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)
 {
-    uint8_t device_res_buffer[DEVICE_RES_LEN] = {0x00u};
-    
+        
     poc2p0_device_msg_response  poc2p0_device_msg_res = {0x00u};
 
     poc2p0_device_msg_res.sos = SOS_EOS;
@@ -738,8 +746,7 @@
     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)-1));
-    
-    //send_thru_terminal(device_res_buffer,DEVICE_RES_LEN,(uint8_t *)&poc2p0_device_msg_res,sizeof(poc2p0_device_msg_res));
+
     blue1.printf("%02x",poc2p0_device_msg_res.sos);
     blue1.printf("%02x",poc2p0_device_msg_res.cmd);
     blue1.printf("%02x",poc2p0_device_msg_res.length);
@@ -757,9 +764,7 @@
 
 // this command response to heartbeat message request send by bluetooth device
 void poc2p0_send_device_heartbeat_msg_res(uint8_t curr_touch_state)
-{
-    uint8_t heartbeat_res_buffer[HEARTBEAT_RES_LEN] = {0x00u};
-    
+{    
     poc2p0_device_heartbeat_response    poc2p0_device_heartbeat_res = {0x00u};
        
     // RTC operations
@@ -827,7 +832,6 @@
     // calculate chksum
     poc2p0_device_heartbeat_res.chksum = checksum_struct((uint8_t *)&poc2p0_device_heartbeat_res.cmd, (sizeof(poc2p0_device_heartbeat_res)-1));
    
-    send_thru_terminal(heartbeat_res_buffer,HEARTBEAT_RES_LEN,(uint8_t *)&poc2p0_device_heartbeat_res,sizeof(poc2p0_device_heartbeat_res));
     blue1.printf("%02x",poc2p0_device_heartbeat_res.sos);
     blue1.printf("%02x",poc2p0_device_heartbeat_res.cmd);
     blue1.printf("%02x",poc2p0_device_heartbeat_res.length);
@@ -873,9 +877,7 @@
 
 // this command send final test result response to bluetooth device
 void poc2p0_send_device_test_result_res(uint8_t response_cmd, uint32_t result)
-{
-    uint8_t test_res_buffer[TEST_RES_LEN] = {0x00u};
-    
+{    
     poc2p0_device_tst_result_response   poc2p0_device_tst_result_res = {0x00u};
 
     poc2p0_device_tst_result_res.sos = SOS_EOS;
@@ -888,8 +890,6 @@
     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)-1));
-   
-    //send_thru_terminal(test_res_buffer,TEST_RES_LEN,(uint8_t *)&poc2p0_device_tst_result_res,sizeof(poc2p0_device_tst_result_res));
 
     blue1.printf("%02x",poc2p0_device_tst_result_res.sos);
     blue1.printf("%02x",poc2p0_device_tst_result_res.cmd);
@@ -908,46 +908,28 @@
     gc.printf("\n");  
 }
 
-static void send_thru_terminal(uint8_t input_buf[],uint8_t len_buff,uint8_t struct_data[],uint8_t len_struct)
-{
-    memcpy(input_buf,struct_data, len_buff);
-    
-    gc.printf("RESPONSE: \n");
-    for (buffer_pointer = 0; buffer_pointer < len_struct; buffer_pointer++)
-    {
-        blue1.printf("%02x",input_buf[buffer_pointer]);
-        gc.printf("%02x",input_buf[buffer_pointer]);
-    }
-    blue1.printf("\n");
-    gc.printf("\n"); 
-    
-    /*for (buffer_pointer = 0; buffer_pointer < len_struct; buffer_pointer++)
-    {
-        gc.printf("%2x",input_buf[buffer_pointer]);
-    }
-    gc.printf("\n"); */
-    
-    
-}
 
 void set_userpid(char *user_pid)
 {
+    char pid_buffer[20];
+    char test_screen_chk[3];
+    
     //char user_pid_buffer[10] = {0};
     if (gc.readable()!= '\n')                                                   // reading string from putty
     //if (blue.readable()!= NULL)                                               // reading string from bluetooth
     {                                                         
         //blue.scanf("%10s",user_pid);
-        gc.scanf("%10s",user_pid);
-              
-    }  
+        gc.scanf("%20s",pid_buffer);
+    } 
+    memcpy(test_screen_chk,pid_buffer+1,2);
+    //if(test_screen_chk == "73")
+        memcpy(user_pid,pid_buffer+6,10); 
+    //else
+    //    gc.printf("Not a valid PID\n");
 }
 
 
-char* get_userpid(void)
-{
-    return user_pid;
 
-}
 
 
 
@@ -979,4 +961,51 @@
     return;
 }
 
+static void ecg_bp_countdown(void)
+{
+    for(uint8_t i = 5; i <= 0; i--)
+    {
+        wait(1);
+    }
+}
 
+static void read_device_id(void)                                                // reading device id from eeprom
+{
+    
+    device_id =  eprom_read_8(12);                                              // reading from eeprom location 
+    gc.printf("device_id = %d\n", device_id);                                                      //read did from eeprom
+    if((get_Error()!= 0) || (device_id == 0xFF))                                // checking for error                               
+    {
+            device_id = eprom_read_8(12) ; 
+         if(get_Error()!= 0  || (device_id == 0xFF))  
+            {
+                gc.printf("database error!\n");                                 // database error
+                //return did; 
+            }
+    }
+     
+}
+
+uint8_t get_did()
+{
+    return  device_id;
+}
+
+void sd_card_status(void)                                                           // checking the status of the sd card initialization
+{
+ uint8_t error_sd= 0;
+ error_sd = dummy_file_create_delete();                                         // creating the dummy file
+
+ if( error_sd == 1)                                                             // if error = 1 // error in opening file 
+ {
+       error_sd = dummy_file_create_delete(); 
+        if( error_sd == 1)
+            {
+                gc.printf("database error!\n");                                 // dispaly data base error
+                //database_error();
+              
+            }
+ }
+ 
+} 
+