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

Revision:
45:067673dae242
Parent:
44:05dbea558c9a
Child:
46:162ed3f051b2
--- 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