Changes done in ECG and BT

Dependencies:   SDFileSystem ds3231 eeprom_Nikita mbed testUniGraphic_150217

Fork of merged_code2_20sept_2017_4th_oct_2017 by nikita teggi

Files at this revision

API Documentation at this revision

Comitter:
nikitateggi
Date:
Tue Sep 12 10:08:04 2017 +0000
Parent:
44:05dbea558c9a
Child:
46:162ed3f051b2
Commit message:
added bluetooth ack timeout, batterymonitoring and pid tracking in sd card

Changed in this revision

battery.cpp Show annotated file Show diff for this revision Revisions of this file
battery.h Show annotated file Show diff for this revision Revisions of this file
bt.cpp Show annotated file Show diff for this revision Revisions of this file
display_modules.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/battery.cpp	Tue Sep 12 04:16:41 2017 +0000
+++ b/battery.cpp	Tue Sep 12 10:08:04 2017 +0000
@@ -1,34 +1,87 @@
 #include "mbed.h"
 #include "battery.h"
 #include "display_modules.h"
-AnalogIn A(PTB1);
+
+#define BATTERY_PIN                                      PTB1                                                   // Battery Pin
+#define BATTERY_MAX_OPERATING_VOLTAGE                   (float)3.3                                              // Battery Max Operating Voltage
+#define BATTERY_INTERMEDIATE_HIGH_VOLTAGE               (float)3.64                                             // Battery Full Voltage
+#define BATTERY_MEDIUM_VOLTAGE                          (float)3.57                                             // battery Medium Voltage
+#define BATTERY_LOW_VOLTAGE                             (float)3.53                                             // Battery LOW Voltage 
+
+#define  BATTERY_SCREEN_CHARGE_CONDITION                                            0
+#define  BATTERY_LOW_CHARGE_CONDITION                                               1
+#define  BATTERY_CRITICALLY_LOW_CHARGE_CONDITION                                    2
+#define  BATTERY_SCREEN_NO_CHARGE_CONDITION                                         3
+
+static uint8_t battery_voltage_condition = 0;
+
+
+AnalogIn A(BATTERY_PIN);
+Serial battery(USBTX, USBRX);
+
+/*  Function    : To Monitor Battery Voltage
+    returns     : void
 
-uint8_t battery_voltage() {
-float b_voltage;
-uint8_t battery_condition=0;
-        b_voltage = A.read_u16()*(3.3/65535)*2;
-        wait(0.001f);
-      if (b_voltage>3.64)
-             {
-                   battery_screen_charge();
-                   battery_condition=0;
-             }
-      else if (3.605>b_voltage>3.57)           ///15% to 10%
-            {
-                   battery_low_charge();
-                   battery_condition=1;
-             }
-       else if (3.57>b_voltage>3.535)          /// 10% to 5%
-            {
-                   battery_critically_low_charge();
-                   battery_condition=2;
-             } 
-       else if (3.535>b_voltage)
-            {
-                   battery_screen_nocharge();
-                   battery_condition=3;
-             }      
-             
+*/
+ 
+ //uint8_t battery_condition=0;
+void battery_monitor() 
+{
+    
+    float b_voltage;
+    b_voltage = A.read_u16()*(BATTERY_MAX_OPERATING_VOLTAGE/65535)*2;
+    wait(0.001f);   
+    battery.printf("voltage=%f",b_voltage);
+    
+    if (b_voltage >= BATTERY_INTERMEDIATE_HIGH_VOLTAGE )
+         battery_voltage_condition = BATTERY_SCREEN_CHARGE_CONDITION;
+    
+    else if ((b_voltage <  BATTERY_INTERMEDIATE_HIGH_VOLTAGE ) && (b_voltage > BATTERY_MEDIUM_VOLTAGE))             // 15% to 10% Battery voltage
+         battery_voltage_condition =  BATTERY_LOW_CHARGE_CONDITION ;
+              
+    else if ((b_voltage < BATTERY_MEDIUM_VOLTAGE) && (b_voltage > BATTERY_LOW_VOLTAGE))                             // 10% to 5% battery volage
+        battery_voltage_condition =  BATTERY_CRITICALLY_LOW_CHARGE_CONDITION;
+            
+    else if (b_voltage < BATTERY_LOW_VOLTAGE)
+         battery_voltage_condition = BATTERY_SCREEN_NO_CHARGE_CONDITION ;
+                   
+  
+    
+ 
+}
+
+/*  Function: To Display Battery Status
+    returns : void
+*/
+
+void battery_status_display()
+{
+    if (battery_voltage_condition == BATTERY_SCREEN_CHARGE_CONDITION)
+        battery_screen_charge();
+        
+    else if (battery_voltage_condition == BATTERY_LOW_CHARGE_CONDITION)      
+         battery_low_charge();
          
-return  battery_condition;
+    else if (battery_voltage_condition == BATTERY_CRITICALLY_LOW_CHARGE_CONDITION)      
+         battery_critically_low_charge();
+  
+    else if (battery_voltage_condition == BATTERY_SCREEN_NO_CHARGE_CONDITION) {
+         do
+            {     
+                battery_screen_nocharge();
+                wait_ms(500);
+               } while(battery_voltage_condition == BATTERY_SCREEN_NO_CHARGE_CONDITION );
+            
+        }
+}
+
+/*  Function    : To get battery status depending battery voltage 
+    returns     : battery_voltage_condition
+
+*/
+
+uint8_t get_battery_status()
+{
+    
+    return battery_voltage_condition;    
 } 
\ No newline at end of file
--- a/battery.h	Tue Sep 12 04:16:41 2017 +0000
+++ b/battery.h	Tue Sep 12 10:08:04 2017 +0000
@@ -1,9 +1,20 @@
 #ifndef BATTERY_H_
 #define BATTERY_H_
 
+/*  battery_monitor()   : To Monitor Battery Voltage
+    returns             : battery_condition
 
+*/
+ //uint8_t battery_voltage_condition=0;
+
+void battery_monitor();
 
-uint8_t battery_voltage();
+/*  battery_status_display() : To Display Battery Status
+    parameter : battery_voltage_condition
+    returns   : void
+*/
+void battery_status_display();
 
+uint8_t get_battery_status();
 
 #endif
\ No newline at end of file
--- a/bt.cpp	Tue Sep 12 04:16:41 2017 +0000
+++ b/bt.cpp	Tue Sep 12 10:08:04 2017 +0000
@@ -1,19 +1,19 @@
-
 #include "mbed.h"
 #include "sdcard.h"
 #include "struct.h"
 #include "eeprom_pgm.h"
 #include "main.h"
 
-#define ACK 0x00                 /// changed //16/06 #define all the values removing structure 
-#define START_OF_FILE 0x01
-#define END_OF_FILE 0x02
-#define DATA_TRFR 0x03
-#define NACK 0xFF
-#define SOS 0xc0
-#define EOS 0xc0
-#define NO_OF_SAMPLES 600   //changed the buffer size from 150- 22/7/17- suhasini
-
+#define ACK 0x00                                /// changed //16/06 #define all the values removing structure 
+#define START_OF_FILE       0x01
+#define END_OF_FILE         0x02
+#define DATA_TRFR           0x03
+#define NACK                0xFF
+#define SOS                 0xc0
+#define EOS                 0xc0
+#define NO_OF_SAMPLES       600                   //changed the buffer size from 150- 22/7/17- suhasini
+#define CHKSUM              0x80 
+#define STRUCTURE_LENGTH    26
 
 BLEMsg_info *bt_file;
 Point *myBleMsg;
@@ -44,23 +44,18 @@
     int8_t check_sum=0;
     uint8_t state_rx =0;
  
-  
-
-  
-
-    switch(state_tx)
+  switch(state_tx)
           {
  
        case 1:    
                   state_tx =0; 
                   counter=0;
                   total_file_size_sent=0; 
-                  file_start=0;          
-                 // j=0; 
-                 count_send=0;  total_samples=0;  
+                  file_start=0;           
+                  count_send=0;  total_samples=0;  
                   file_pointer_position=0;
-                 // mc.printf("%d %d\n",current_file,  bt_file_tosend);
-                if(get_filepid()>get_btpid())
+    
+                if(get_filepid()>get_btpid())                           //check for pid and bluetooth pid by reading from file
                 {
                     send_initial_string(); 
                     state_rx = 1;
@@ -69,7 +64,7 @@
                          
                 else 
                 {
-                    state_rx =0;
+                    state_rx =0;                                        //no files to send
                     mc.printf("no file to send\n");  
                 }       
                 break;
@@ -77,11 +72,11 @@
                    
        case 2:   total_samples=0; 
                  file=sd_open_read(get_btpid());  
-                 total_file_size=file_size() ;             // determined the file size 
-                 sd_read1(bt_file1);               // read the sd card file
+                 total_file_size=file_size() ;                           // determined the file size 
+                 sd_read1(bt_file1);                                    // read the sd card file
                  mc.printf("sd file opened to read data in it\n");
-                 myBleMsg->bt_msg=bt_file1;         //copy the contents to the structure
-                 send_structure();                                // send the initial string
+                 myBleMsg->bt_msg=bt_file1;                              //copy the contents to the structure
+                 send_structure();                                       // send the structure string
                  blue.printf("\n");   
                  mc.printf("sent initial string\n");                    
                  total_samples= myBleMsg->bt_msg.num_samples.num_sample_ppg_dummy+myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp;
@@ -135,7 +130,7 @@
                           send_data_str2(check_sum);
                     }   
                      
-                       total_file_size_sent=file_size1();
+                        total_file_size_sent=file_size1();
                         blue.printf("\n");  
                   break;        
                
@@ -182,14 +177,14 @@
 uint8_t bt_receive (uint8_t state_rx)
 {
        
- unsigned char state_tx =1;      //state for transmitting 
+unsigned char state_tx =1;      //state for transmitting 
 uint8_t ack_rx=0;                        // varibale to receive ack from the bluetooth 
 uint8_t sos_rx=0;              // sos received from app
 uint8_t eos_rx=0;              // eos received from app
 uint32_t length_rx=0;
 uint8_t chksum_rx=0;  
  
- while (blue.readable())
+ while (blue.readable())                                            // reading from bluetooth app
     {
         blue.scanf("%02x", &sos_rx);
         blue.scanf("%02x", &ack_rx);
@@ -206,7 +201,7 @@
           case 1:                                       // wait for the ack
                       state_rx=0;
                       myBleMsg->proto=ACK;
-                 if (ack_rx == ACK)
+                 if ((ack_rx == ACK) && (chksum_rx == CHKSUM))                    // check for ack and checksum
                
                      {
                        
@@ -224,7 +219,7 @@
  
          case 2:      state_rx =0;                        //wait for the ack
                   
-                    if (ack_rx == ACK)
+                    if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
                         {
                 
                           state_tx=3;
@@ -245,12 +240,12 @@
                 
      case 3:       
                    state_rx =0;
-                 if (ack_rx == ACK)
+                 if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
                       {
                      
-                        state_tx=3;
+                         state_tx=3;
                          count_send=count_send+1;
-                       file_pointer_position=total_file_size_sent;
+                         file_pointer_position=total_file_size_sent;
                       }
                     
                   else 
@@ -266,11 +261,11 @@
                                             
      case 4:       
                    state_rx =0;
-                 if (ack_rx == ACK)
+                 if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
                       {
-                        state_tx=4;
+                         state_tx=4;
                          count_send=count_send+1;
-                        file_pointer_position=total_file_size_sent;
+                         file_pointer_position=total_file_size_sent;
                       }
                     
                   else 
@@ -285,10 +280,10 @@
                  
 
       case 5:      state_rx = 0;
-                if (ack_rx == ACK)
+                if ((ack_rx == ACK) && (chksum_rx == CHKSUM)) 
                 {
                     sd_close();
-                    increment_btpid();
+                    increment_btpid();                // increment the bluetooth pid and write in the file
                     mc.printf("next file");       
                     state_tx=1;
                           
@@ -314,31 +309,31 @@
              
 void send_structure()                                 // function to send the structure to bluetooth
 {
-                myBleMsg->start_of_string=SOS;
-                myBleMsg->length_file=26;
-                myBleMsg->proto=DATA_TRFR;                      // data transfer mode
-                blue.printf("%02x",myBleMsg->start_of_string); 
-                blue.printf("%02x",myBleMsg->proto);
-                blue.printf("%08x",myBleMsg->length_file);      //changed the sequence 
-                blue.printf("%08x",myBleMsg->bt_msg.length); 
-                blue.printf("%02x",myBleMsg->bt_msg.device_id); 
-                blue.printf("%08x",myBleMsg->bt_msg.patient_id);    
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.date);     
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.month);  
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.year);  
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.hour);      
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.mins);  
-                blue.printf("%02x",myBleMsg->bt_msg.date_time.sec);  
-                blue.printf("%02x",myBleMsg->bt_msg.test_type);   
-                blue.printf("%04x",myBleMsg->bt_msg.sampling_freq); 
-                blue.printf("%04x",myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy); 
-                blue.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp); 
-                blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy);   
-                blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp); \
-                myBleMsg->end_of_string=EOS;  
-                myBleMsg->chk_sum = checksum_struct((uint8_t *)myBleMsg, sizeof(Point));
-                blue.printf("%02x",myBleMsg->end_of_string);
-                blue.printf("%02x",myBleMsg->chk_sum);
+    myBleMsg->start_of_string=SOS;
+    myBleMsg->length_file = STRUCTURE_LENGTH;
+    myBleMsg->proto=DATA_TRFR;                      // data transfer mode
+    blue.printf("%02x",myBleMsg->start_of_string); 
+    blue.printf("%02x",myBleMsg->proto);
+    blue.printf("%08x",myBleMsg->length_file);      //changed the sequence 
+    blue.printf("%08x",myBleMsg->bt_msg.length); 
+    blue.printf("%02x",myBleMsg->bt_msg.device_id); 
+    blue.printf("%08x",myBleMsg->bt_msg.patient_id);    
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.date);     
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.month);  
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.year);  
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.hour);      
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.mins);  
+    blue.printf("%02x",myBleMsg->bt_msg.date_time.sec);  
+    blue.printf("%02x",myBleMsg->bt_msg.test_type);   
+    blue.printf("%04x",myBleMsg->bt_msg.sampling_freq); 
+    blue.printf("%04x",myBleMsg->bt_msg.num_samples. num_sample_ppg_dummy); 
+    blue.printf("%04x",myBleMsg->bt_msg.num_samples.num_sample_ecg_OTtyp); 
+    blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_sbp_dummy);   
+    blue.printf("%04x",myBleMsg->bt_msg.cal_data.cal_dbp_OTtyp); 
+    myBleMsg->end_of_string=EOS;  
+    myBleMsg->chk_sum = checksum_struct((uint8_t *)myBleMsg, sizeof(Point));
+    blue.printf("%02x",myBleMsg->end_of_string);
+    blue.printf("%02x",myBleMsg->chk_sum);
                
     
     }
@@ -346,19 +341,18 @@
 void send_initial_string()                // function to send the initial string at the start f communication to bluetooth
 {              
 
-                uint8_t sum_init=0  ;               
-                myBleMsg->start_of_string=SOS;
-                myBleMsg->length_file=0;
-                myBleMsg->proto=START_OF_FILE;
-               //  myBleMsg->bt_msg=NULL;
-                sum_init=checksum_init();
-                myBleMsg->chk_sum=sum_init;
-                blue.printf("%02x",myBleMsg->start_of_string);
-                blue.printf("%02x",myBleMsg->proto); 
-                blue.printf("%08x",myBleMsg->length_file);     // chnged
-                myBleMsg->end_of_string=EOS;  
-                blue.printf("%02x",myBleMsg->end_of_string);
-                blue.printf("%02x",myBleMsg->chk_sum);
+    uint8_t sum_init=0  ;               
+    myBleMsg->start_of_string=SOS;
+    myBleMsg->length_file=0;
+    myBleMsg->proto=START_OF_FILE;
+    sum_init=checksum_init();
+    myBleMsg->chk_sum=sum_init;
+    blue.printf("%02x",myBleMsg->start_of_string);
+    blue.printf("%02x",myBleMsg->proto); 
+    blue.printf("%08x",myBleMsg->length_file);     // chnged
+    myBleMsg->end_of_string=EOS;  
+    blue.printf("%02x",myBleMsg->end_of_string);
+    blue.printf("%02x",myBleMsg->chk_sum);
     
     }
   
@@ -366,70 +360,61 @@
 uint8_t checksum_init()          // function to calculate the checksum for the initial string
  {
   
-  uint32_t sum=0;
-  uint8_t chksum_init=0;
+  uint32_t sum = 0;
+  uint8_t chksum_init = 0;
   sum=SOS+EOS+START_OF_FILE; 
-  chksum_init=sum & 0x000000FF;
+  chksum_init = sum & 0x000000FF;
   return chksum_init;
   }
 
 uint8_t checksum_last()        // function to calculate the checksum for the last string
  {
   
-  uint32_t sum=0;
-  uint8_t chksum_last=0;
+  uint32_t sum = 0;
+  uint8_t chksum_last = 0;
   sum=SOS+EOS+END_OF_FILE; // changed //16/06 #define all the values removing structure 
   chksum_last=sum & 0x000000FF;
   return chksum_last;
   }    
     
- void send_last_string()                 // function to send the last string to the bluetooth // which determines the end of file
+void send_last_string()                 // function to send the last string to the bluetooth // which determines the end of file
 {
-                uint8_t sum_last=0;
-                myBleMsg->start_of_string=SOS;
-                myBleMsg->length_file=0;
-                myBleMsg->proto=END_OF_FILE;
-               //  myBleMsg->bt_msg=NULL;
-                sum_last=checksum_last();
-                myBleMsg->chk_sum=sum_last;
-                blue.printf("%02x",myBleMsg->start_of_string);
-                blue.printf("%02x",myBleMsg->proto); 
-                blue.printf("%08x",myBleMsg->length_file);     // chnged
-                 mc.printf("%02x",myBleMsg->start_of_string);
-                mc.printf("%02x",myBleMsg->proto); 
-               mc.printf("%08x",myBleMsg->length_file); 
-                myBleMsg->end_of_string=EOS;  
-                blue.printf("%02x",myBleMsg->end_of_string);
-                blue.printf("%02x",myBleMsg->chk_sum);
-                 mc.printf("%02x",myBleMsg->end_of_string);
-               mc.printf("%02x",myBleMsg->chk_sum);
-    
-    }
+    uint8_t sum_last=0;
+    myBleMsg->start_of_string = SOS;
+    myBleMsg->length_file = 0;
+    myBleMsg->proto=END_OF_FILE;
+    sum_last=checksum_last();
+    myBleMsg->chk_sum=sum_last;
+    blue.printf("%02x",myBleMsg->start_of_string);
+    blue.printf("%02x",myBleMsg->proto); 
+    blue.printf("%08x",myBleMsg->length_file);     // chnged
+    myBleMsg->end_of_string=EOS;  
+    blue.printf("%02x",myBleMsg->end_of_string);
+    blue.printf("%02x",myBleMsg->chk_sum);
+                
+}
        
- void send_data_str1()       // function to send a part of the structure // after this raw data is sent//
-   {
-                 myBleMsg->start_of_string=SOS;
-                 myBleMsg->length_file=(counter*4);
-                 myBleMsg->proto=DATA_TRFR;
-                 blue.printf("%02x",myBleMsg->start_of_string); 
-                 blue.printf("%02x",myBleMsg->proto);                   // chnaged
-                 blue.printf("%08x",myBleMsg->length_file);
-                 mc.printf("%02x",myBleMsg->start_of_string); 
-                 mc.printf("%02x",myBleMsg->proto);                   // chnaged
-                 mc.printf("%08x",myBleMsg->length_file);
-    }
+void send_data_str1()       // function to send a part of the structure // after this raw data is sent//
+{
+    myBleMsg->start_of_string=SOS;
+    myBleMsg->length_file=(counter*4);
+    myBleMsg->proto=DATA_TRFR;
+    blue.printf("%02x",myBleMsg->start_of_string); 
+    blue.printf("%02x",myBleMsg->proto);                   // chnaged
+    blue.printf("%08x",myBleMsg->length_file);
+
+}
     
     
     
- void send_data_str2(uint8_t check_sum)          // after raw data// this last part of structure is sent
-    {          
-                myBleMsg->chk_sum=check_sum;
-                myBleMsg->end_of_string=EOS;  
-                blue.printf("%02x",myBleMsg->end_of_string);
-                blue.printf("%02x",myBleMsg->chk_sum);
-                mc.printf("%02x",myBleMsg->end_of_string);
-                mc.printf("%02x",myBleMsg->chk_sum);
-    }
+void send_data_str2(uint8_t check_sum)          // after raw data// this last part of structure is sent
+{          
+    myBleMsg->chk_sum=check_sum;
+    myBleMsg->end_of_string=EOS;  
+    blue.printf("%02x",myBleMsg->end_of_string);
+    blue.printf("%02x",myBleMsg->chk_sum);
+    
+}
     
 
 uint8_t checksum_struct(const uint8_t data[], uint16_t numBytes)         // calculating checksum for 
@@ -451,27 +436,26 @@
 uint8_t checksum_rawdata(const uint8_t data[], uint32_t numBytes)          // this function to calculate the checksum for the raw data
 {
 
-uint32_t sum=0;
-uint8_t  chksum_datatrfr=0;
-uint32_t index = 0;
-uint32_t index1 = 0;
+    uint32_t sum=0;
+    uint8_t  chksum_datatrfr=0;
+    uint32_t index = 0;
+    uint32_t index1 = 0;
     sum += SOS;
     sum += DATA_TRFR;  
-   index1=(numBytes*4);
-   sum +=(index1>>(8*0))&0xFF;
+    index1=(numBytes*4);
+    sum +=(index1>>(8*0))&0xFF;
     sum +=(index1>>(8*1))&0xFF; 
 
-for (index=0U; index<(numBytes*4); index++)
-{ 
+    for (index=0U; index<(numBytes*4); index++)
+    { 
     sum += data[index];
-
-}
+    }
 
-  sum+=EOS;
+    sum+=EOS;
 
-  chksum_datatrfr = sum & 0x000000FF;
+    chksum_datatrfr = sum & 0x000000FF;
  //mc.printf("chksum_datatrfr=%02x\n",chksum_datatrfr);
-  return chksum_datatrfr;
+    return chksum_datatrfr;
 }
 
 
--- a/display_modules.cpp	Tue Sep 12 04:16:41 2017 +0000
+++ b/display_modules.cpp	Tue Sep 12 10:08:04 2017 +0000
@@ -49,7 +49,7 @@
     TFT.printf(buff1);
     TFT.set_font((unsigned char*) Arial24x23);  
     TFT.foreground(Black);
-    TFT.locate(35,20) ;
+    TFT.locate(35,30) ;
     TFT.printf("SENSESEMI") ;
     TFT.locate(45,60) ;
     TFT.printf("Sense POC") ;
@@ -143,6 +143,7 @@
     TFT.fillrect(130,215,220,285,White);
     TFT.fillrect(10,215,100,285,White);
     TFT.fillrect(60,130,180,200,White);
+     TFT.fillrect(5,5,115,20,White);
     TFT.background(Green) ;
     TFT.foreground(Black) ;  
     TFT.fillrect( 60,130,180,200,Green);
@@ -252,7 +253,8 @@
     TFT.fillrect(10,130,100,200,White);   
     TFT.fillrect(130,130,220,200,White);  
     TFT.fillrect(10,215,100,285,White);  
-    TFT.fillrect(130,215,220,285,White);  
+    TFT.fillrect(130,215,220,285,White);
+     TFT.fillrect(5,5,115,20,White);  
     TFT.fillrect( 60,130,180,200,Green);
     TFT.locate(80,150) ;
     TFT.printf("START") ;    
@@ -323,6 +325,7 @@
     TFT.fillrect(130,130,220,200,White);
     TFT.fillrect(10,215,100,285,White);
     TFT.fillrect(130,215,220,285,White);
+     TFT.fillrect(5,5,115,20,White);
    // TFT.fillrect( 60,130,180,200,Green);
    // TFT.locate(80,150) ;
    // TFT.printf("START") ;   
@@ -367,7 +370,7 @@
     TFT.fillrect(3,80,240,300,White);   //changed on 11/8/17 nikita
     TFT.fillrect(60,230,180,300,White);
     TFT.fillrect(5,215,230,290,White);
-    TFT.fillrect(5,230,240,320,White ); 
+    TFT.fillrect(5,230,240,320,White );  
     TFT.locate(25,100) ;        
     TFT.printf("ECG starts in") ; 
     for(int i=5;i>=0;i--)
@@ -507,7 +510,7 @@
   void glc_3()     // GLC screen 
   {
     backlight = 0 ;
-    TFT.BusEnable(true) ;    
+    TFT.BusEnable(true) ; 
     TFT.locate(5,190) ;  
     TFT.printf("Blood Detected") ; 
     TFT.locate( 5,220) ;  
@@ -546,11 +549,13 @@
    {
      
      
-    backlight = 0 ;
+   backlight = 0 ;
     TFT.BusEnable(true) ;
-    TFT.background(White) ;
+    TFT.background(Red) ;
     TFT.set_font((unsigned char*) Arial12x12);
-    TFT.foreground(Red);
+    TFT.foreground(White);
+     TFT.fillrect(5,5,115,20,White);
+    TFT.fillrect(5,5,95,20,Red);   //added rect
     TFT.locate(10,8) ;
    TFT.printf("No Charge") ;
     TFT.set_font((unsigned char*) Arial24x23);  
@@ -562,16 +567,19 @@
     
  void battery_screen_charge()
    {
+        
         backlight = 0 ;
     TFT.BusEnable(true) ;
-    TFT.background(White) ;
+    TFT.background(Green) ;
     TFT.set_font((unsigned char*) Arial12x12);
-    TFT.foreground(Green);
+    TFT.foreground(Black);
+    TFT.fillrect(5,5,115,20,White);
+    TFT.fillrect(5,5,115,20,Green);   //added rect
     TFT.locate(10,8) ;
    TFT.printf("Battery Charged") ;
     TFT.set_font((unsigned char*) Arial24x23);  
    TFT.BusEnable(false) ;
-    backlight = 1 ;  
+    backlight = 1 ;   
         
     }     
  
@@ -581,9 +589,11 @@
    {
         backlight = 0 ;
     TFT.BusEnable(true) ;
-    TFT.background(White) ;
+    TFT.background(Red) ;
     TFT.set_font((unsigned char*) Arial12x12);
-    TFT.foreground(Green);
+    TFT.foreground(White);
+     TFT.fillrect(5,5,115,20,White);
+    TFT.fillrect(5,5,95,20,Red);   //added rect
     TFT.locate(10,8) ;
    TFT.printf("Critically Low") ;
     TFT.set_font((unsigned char*) Arial24x23);  
@@ -594,11 +604,13 @@
    
    void battery_low_charge()
    {
-        backlight = 0 ;
+         backlight = 0 ;
     TFT.BusEnable(true) ;
-    TFT.background(White) ;
+    TFT.background(Green) ;
     TFT.set_font((unsigned char*) Arial12x12);
-    TFT.foreground(Green);
+    TFT.foreground(Black);
+     TFT.fillrect(5,5,115,20,White);
+    TFT.fillrect(5,5,95,20,Green);   //added rect
     TFT.locate(10,8) ;
    TFT.printf("Low Battery") ;
     TFT.set_font((unsigned char*) Arial24x23);  
@@ -613,7 +625,8 @@
     TFT.BusEnable(true) ;
     TFT.background(White) ;
     TFT.foreground(Red) ;
-   TFT.fillrect(5,90,230,200,White); // added on 11/8/17 nikita 
+   TFT.fillrect(5,90,230,200,White); // added on 11/8/17 nikita
+     
     TFT.locate(50,100) ;  
     TFT.printf("ERROR!") ;   
     TFT.locate(20,130) ;  
@@ -630,7 +643,8 @@
     TFT.BusEnable(true) ;
     TFT.background(White) ;
     TFT.foreground(Red) ;
-    TFT.fillrect(5,90,230,150,White);  
+    TFT.fillrect(5,90,230,150,White);
+       
     TFT.locate(50,100) ;  
     TFT.printf("ERROR!") ;   
     TFT.locate(20,130) ;  
@@ -648,7 +662,8 @@
     TFT.BusEnable(true) ;
     TFT.background(White) ;
     TFT.foreground(Red) ;
-    TFT.fillrect(5,90,230,300,White);  
+    TFT.fillrect(5,90,230,300,White);
+       
     TFT.locate(60,100) ;                //changed from 50 to 60 //rashmi 22/7/17
     TFT.printf("Replace") ;   
     TFT.locate(40,130) ;                //changed from 20 to 40 //rashmi 22/7/17
@@ -666,7 +681,8 @@
     TFT.BusEnable(true) ;
     TFT.background(White) ;
     TFT.foreground(Blue) ;
-    TFT.fillrect(5,90,230,300,White);  
+    TFT.fillrect(5,90,230,300,White); 
+     TFT.fillrect(5,5,115,20,White); 
     TFT.locate(40,140) ;  
     TFT.printf("Connected") ;   
     TFT.locate(20,170) ;  
@@ -683,7 +699,8 @@
     TFT.BusEnable(true) ;
     TFT.background(White) ;
     TFT.foreground(Blue) ;
-    TFT.fillrect(5,90,230,300,White);  
+    TFT.fillrect(5,90,230,300,White); 
+    TFT.fillrect(5,5,115,20,White); 
     TFT.locate(80,140) ;  
     TFT.printf("Not") ;   
     TFT.locate(40,160) ;  
@@ -701,6 +718,7 @@
     TFT.background(White) ;
     TFT.foreground(Blue) ;
     TFT.fillrect(5,90,230,300,White);  
+    
     TFT.locate(20,140) ;  
     TFT.printf("Files Tranfer") ;   
     TFT.locate(40,170) ;  
@@ -719,6 +737,7 @@
     TFT.background(Green) ;
     TFT.foreground(Black) ; 
    // TFT.fillrect(5,90,230,300,White);    
+    
     TFT.fillrect( 60,230,180,300,Green);
     TFT.locate(80,250) ;        
     TFT.printf("MAIN") ;          
@@ -738,6 +757,7 @@
     TFT.background(White);
     TFT.foreground(Blue);   
     TFT.fillrect(5,90,230,300,White);
+    TFT.fillrect(5,5,115,20,White);
     TFT.locate(15,140) ;                               //changed from 20 to 15 //rashmi 24/7/17      
     TFT.printf(buf) ; 
     sprintf (buf,"BT pid :%d",bt_file_sent_pid);
@@ -759,6 +779,7 @@
     TFT.background(White) ;
     TFT.foreground(Red) ;
     TFT.fillrect(5,90,230,300,White);  
+    
     TFT.locate(50,130);            //changed from 50 to 60 //rashmi 22/7/17// changed from 100 to 130added on 10/8/17 nikita
     TFT.printf("Test Strip") ;   
     TFT.locate(50,160) ;                //changed from 20 to 40 //rashmi 22/7/17// changed from 130 to 160added on 10/8/17 nikita
--- a/main.cpp	Tue Sep 12 04:16:41 2017 +0000
+++ b/main.cpp	Tue Sep 12 10:08:04 2017 +0000
@@ -1,6 +1,3 @@
-// changed the pid in all to int32_t;
-//added bt
-
 #include "mbed.h"
 #include "display_modules.h"
 #include "touch_modules.h"
@@ -13,7 +10,11 @@
 #include "struct.h"
 #include "battery.h" 
 #include "main.h"
-#define TIMER_RXACK_BTCONNECTION 10000
+
+#define TIMER_RXACK_BTCONNECTION                10000
+#define BATTERY_TIMER_VALUE                     600000
+#define SERIAL_BAUD_RATE                        115200
+#define BATTERY_SCREEN_NO_CHARGE_CONDITION      3
 
 Serial gc(USBTX, USBRX);
 DigitalIn q1(PTB11);
@@ -22,17 +23,17 @@
 DigitalIn BT(PTA5);   
 Serial blue1(PTC4,PTC3);  
 uint32_t get_timer_bluetooth();
-                                             //bluetooth status pin
-uint8_t touch(uint8_t state);           // touch state
-uint8_t display(uint8_t state1);       //  display state
+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 uint8_t nstate=0;
 static uint8_t nstate1=0;
-static uint32_t current_test_pid=0;
-static uint32_t  bt_file_sent_pid=0; 
 static uint32_t pid = 0;                                                    // patient ID
 static bool file_created_status = false;                                  //initialise file created to 0
 static uint32_t filepid_btpid[2] = {};
-Timer bt_t;                        // timer 
+Timer timer_bt;                                                         // timer for bluetooth 
+Timer timer_battery;                                                     // timer for battery
 
 
 
@@ -43,63 +44,75 @@
     static uint8_t state_touch = 1;
     static uint8_t state_t = 1;                                     // state to transmit to bluetooth
     static uint8_t state_r = 0;                                     // state to receive from bluetooth
-//    uint8_t battery_voltage_condition = 0;
+
   
   
     DisableTouch();
     gc.baud(115200);
     init_screen();                                                  //initialize lcd
-    //sd_read(pid_btpid);                                             //read test pid and bt pid from sd card
-    //pid = pid_btpid[0];                                             //copy test pid to pid variable
     pid = get_filepid();
     screen_main();                                                  // display of main screen 
-
+    battery_monitor();                                                          // monitor battery at the start only
+    battery_status_display();                                                   // display of main screen
+    timer_battery.start();
 
    while(1)
    {
+        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 
+        {        
+                 DisableTouch();
+        }
+        else
+        {
+
+            EnableTouch();
+            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;
     
-     EnableTouch();
-     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 2:    
-                    state_touch = display(state_display);           //display state
-                    main_state = 1;
-                    break;
+                case 2:    
+                         state_touch = display(state_display);           //display state
+                         main_state = 1;
+                        break;
                 
                 
-         case 3: 
-                    DisableTouch();                                             // bluetooth send 
-                    bt_t.start();
-                 if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)                           // bluetooth connection timer 15 sec
-                    {
+                case 3: 
+                        DisableTouch();                                             // bluetooth send 
+                        timer_bt.start();
+                         if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)                           // bluetooth connection timer 15 sec
+                         {
                         
-                         if(BT==1)                                                    // checking for bluetooth connection
-                        {
+                            if(BT==1)                                                    // checking for bluetooth connection
+                            {
                     
-                               bt_t.stop();                                        // stop the timer once connected to app
-                               bt_t.reset(); 
+                               timer_bt.stop();                                        // stop the timer once connected to app
+                               timer_bt.reset(); 
                                BT_connection() ;                                // displaying connection on bluetooth 
                                state_r=bt_send(state_t);
-                               bt_t.start();
+                               timer_bt.start();
                                     
                                 if (state_r==0)                                  // if the state received is zero, then there is no new file to send
-                                     {
-                                         bt_t.stop();                           // stop the timer once connected to app
-                                         bt_t.reset();
-                                         BT_finished();                         // screen to display communication finished
-                                         screen_BT_return();                    //screen to return back
-                                         EnableTouch();
-                                         main_state=1;
-                                         state_touch=12;
-                                         break;
+                                    {
+                                        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();
+                                        main_state=1;
+                                        state_touch=12;
+                                        break;
                                      }
                                    
                                 else 
@@ -107,76 +120,72 @@
                                         main_state=4;                           // move to receving stste of bluetooth
                                     }
                           
-                       }      
+                            }  
                   
-                 else   
-                      {
-                      BT_no_connection();     // display screen to show that there is no connection
-                      }
-                 }
-                  
+                            else   
+                            {
+                             BT_no_connection();     // display screen to show that there is no connection
+                            }
+                         }
 
-           
-                else
-                {                                   
-                      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;
-                      state_touch=12;
-                      bt_t.stop();
-                      bt_t.reset();
-                      }
-                 break;
+                        else
+                        {                                   
+                             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;
+                            state_touch=12;
+                            timer_bt.stop();
+                            timer_bt.reset();
+                        }
+                        break;
                  
-         case 4: DisableTouch();  
+                case 4: 
+                        DisableTouch();  
             
-                if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)
-                {
-                   if (blue1.readable())         // if there is data to receive enter the loop
-                  {
-                      bt_t.stop();
-                      bt_t.reset();
-                      state_t=bt_receive(state_r);
-                         if(state_t ==0)           // if state_t is zero, there is no other file to send
-                               {
-                                  //bt_t.stop();
-                                  //bt_t.reset();
-                                  BT_finished();
-                                  screen_BT_return();  
-                                  EnableTouch();
-                                  main_state=1;
-                                  state_touch=12;  // jumping to touch case 12
-                                  break;
-                               } 
-                          else    // move to bluetooth transmit if the states are 1,2,3,4
-                              {
+                        if(get_timer_bluetooth()<TIMER_RXACK_BTCONNECTION)
+                        {
+                            if (blue1.readable())         // if there is data to receive enter the loop
+                            {
+                                timer_bt.stop();
+                                timer_bt.reset();
+                                state_t=bt_receive(state_r);
+                                if(state_t ==0)           // if state_t is zero, there is no other file to send
+                                 {
+                                  
+                                    BT_finished();
+                                    screen_BT_return();  
+                                    EnableTouch();
+                                    main_state=1;
+                                    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;
-                               
-                     }        }  
-                  }
-                                          
+                                 }  
+                            }
+                         }                 
                    
-             else 
-                  {    
-                      DisableTouch();
-                      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;
-                      state_touch=12;
-                      bt_t.stop();
-                      bt_t.reset();
-                }
-            
-                 break;      
-        }             
-                
-    }
+                        else 
+                        {    
+                            DisableTouch();
+                            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;
+                            state_touch=12;
+                            timer_bt.stop();
+                            timer_bt.reset();
+                        }
+                        break;      
+             }         
+        }        
+   } 
+
 }
-
 uint8_t touch(uint8_t state)
 {
    static  uint8_t state1=0;
@@ -292,6 +301,7 @@
                 glc_flag=0;
                 clear_filecreated_status();
                 pid = get_filepid();
+                battery_status_display();                          //Display battery status on LCD
                 EnableTouch();
                 break;
       
@@ -301,6 +311,7 @@
                 state1=0;
                 state=2;
                 nstate=state;
+                battery_status_display();                                           //Display battery status on LCD
                 EnableTouch();
                 break;
                
@@ -313,6 +324,7 @@
                 state1=0;
                 state=3;
                 nstate=state;
+                battery_status_display(); 
                 EnableTouch();
                 break;
                 
@@ -486,7 +498,7 @@
     
     //set_filecreated_status();                                                 //set it to make it append mode
     sd_read(filepid_btpid);                                                     //read pid from sd card
-    filepid_btpid[0] = filepid_btpid[1] + 1;                                    //increment it by 1
+    filepid_btpid[1] = filepid_btpid[1] + 1;                                    //increment it by 1
     sd_write(filepid_btpid);                                                    //write it back to sd card
     
 } 
@@ -497,7 +509,7 @@
     uint32_t filepid = 0;    
     
     sd_read(filepid_btpid);                                                           //read pid from sd card
-    filepid= filepid_btpid[0];                                  
+    filepid = filepid_btpid[0];                                  
     return filepid; 
     
 }   
@@ -517,7 +529,7 @@
     uint32_t btpid = 0;    
     
     sd_read(filepid_btpid);                                                           //read pid from sd card
-    btpid= filepid_btpid[1];                                  
+    btpid = filepid_btpid[1];                                  
     return btpid; 
     
 }   
@@ -532,7 +544,17 @@
                                                         
 } 
 
+
 uint32_t get_timer_bluetooth()
 {
-  return  bt_t.read_ms(); 
+  return  timer_bt.read_ms(); 
 } 
+
+void battery_status_monitoring()
+{
+    if(timer_battery.read_ms() > BATTERY_TIMER_VALUE ) 
+    {
+        battery_monitor();
+        timer_battery.reset();
+    }
+}
\ No newline at end of file