Utkarsh Original Code (Given in India)

Dependencies:   mbed LoRaWAN-lib SX1276Lib

Files at this revision

API Documentation at this revision

Comitter:
uss1994
Date:
Wed Jan 02 02:27:07 2019 +0000
Parent:
10:9a4efdd07a77
Child:
12:c1cf0e717684
Commit message:
Adding code to allow activating tests with 2 or 3 button taps

Changed in this revision

app/main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/app/main.cpp	Fri Dec 07 13:25:30 2018 +0000
+++ b/app/main.cpp	Wed Jan 02 02:27:07 2019 +0000
@@ -148,6 +148,13 @@
 AnalogIn LIGHT_2_PIN(A5);
 AnalogIn VCE_PIN(PB_1);
 
+InterruptIn button1(USER_BUTTON);
+volatile bool button1_pressed = false; // Used in the main loop
+volatile bool button1_enabled = true; // Used for debouncing
+Timeout button1_timeout; // Used for debouncing
+int press_count;
+Timeout buttonOptions_timeout; // Used for determining number of presses
+
 unsigned int time_window = 5; // Default 3600 seconds in an hour
 unsigned long long_interval = 604800; // Default 31557600 seconds in a year
 unsigned int hb_interval = 3600; // Default 86400 seconds in a day
@@ -337,6 +344,22 @@
     wait(1);                       // wait for a second
 }
 
+// Enables button when bouncing is over
+void button1_enabled_cb(void)
+{
+    button1_enabled = true;
+}
+
+// ISR handling button pressed event
+void button1_onpressed_cb(void)
+{
+    if (button1_enabled) { // Disabled while the button is bouncing
+        button1_enabled = false;
+        button1_pressed = true; // To be read by the main loop
+        button1_timeout.attach(callback(button1_enabled_cb), 0.3); // Debounce time 300 ms
+    }
+}
+
 // ****************************    COMMUNICATION PACKET DEFINITION METHODS   ********************************   //
 
 void heartbeat_message(uint8_t *hb_data, uint8_t *current_date) {
@@ -936,6 +959,35 @@
     relayPin = 0;
 }
 
+void button_options_handler(void)
+{
+    if (press_count >= 3) {
+        // Run long test
+        MessageType = MESSAGE_TYPE_LONG_TEST;
+        measurements = l_measurements;
+        
+        startTest(measurements);
+                    
+        running_test = true;
+        test_start = current_time;
+        last_measurement = current_time;
+    }
+    if (press_count == 2) {
+        // Run short test
+        MessageType = MESSAGE_TYPE_SHORT_TEST;
+        measurements = s_measurements;
+        
+        startTest(measurements);
+                    
+        running_test = true;
+        test_start = current_time;
+        last_measurement = current_time;
+    }
+    
+    // Reset count
+    press_count = 0;
+}
+
 
 /**
  * Main application entry point.
@@ -976,6 +1028,9 @@
   
     last_hb = start_time;
     last_measurement = start_time;
+    
+    //button1.mode(PullUp); // Activate pull-up
+    button1.fall(callback(button1_onpressed_cb)); // Attach ISR to handle button press event
 
     while( 1 )
     {
@@ -1195,6 +1250,15 @@
                   //  dl_handler(received_data);
                   //}
                   
+                  //check if button is pressed
+                    if (button1_pressed) { // Set when button is pressed
+                        if (press_count == 0) {
+                            buttonOptions_timeout.attach(callback(button_options_handler), 2); // Debounce time 300 ms
+                        }
+                        button1_pressed = false;
+                        press_count++;
+                    }
+                  
                   //check if it's time to run a test
                   if (current_time >= next_stest || current_time >= next_ltest) {