PGO6_VoteController (Astrid Vanneste)

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Files at this revision

API Documentation at this revision

Comitter:
AstridVanneste
Date:
Mon Oct 08 15:39:22 2018 +0000
Parent:
4:08da93eb6014
Child:
6:60b968c8f35c
Commit message:
mqtt

Changed in this revision

debounce_button.cpp Show annotated file Show diff for this revision Revisions of this file
debounce_button.h 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/debounce_button.cpp	Mon Oct 08 13:37:19 2018 +0000
+++ b/debounce_button.cpp	Mon Oct 08 15:39:22 2018 +0000
@@ -6,6 +6,9 @@
 volatile bool button1_busy;              // Informs the mainloop that the user is clicking the button => busy multiclick
 volatile int internal_click_count;       // Counts how many clicks occured in the time slot
 
+Timeout debounce_time;
+Timeout multiclick_time;
+
 /**
     Some tips and tricks:
     -   To use the built-in LED:
@@ -33,13 +36,14 @@
 */
 void button1_multiclick_reset_cb(void) 
 {
-    printf("multiclick disable");
     button1_busy = false;
     multiclick_state = internal_click_count;
+    //printf("Clicks: %d\n", multiclick_state);
     internal_click_count = 0;
     
     DigitalOut led1(LED1);
     led1 = 0;
+    button1_pressed = true;
 }
 
 /**
@@ -66,26 +70,22 @@
             Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted.
 */
 void button1_onpressed_cb(void)
-{
-    DigitalOut led1(LED1);
-    led1 = 1;
-    
+{   
     if(button1_enabled)
     {
+        DigitalOut led1(LED1);
+        led1 = 1;
         button1_enabled = false;        // button is not enabled => debouncing
         
-        Timeout debounce_time;
-        debounce_time.attach(callback(button1_enabled_cb), 0.5);
+        debounce_time.attach(callback(&button1_enabled_cb), 0.1);
         
         internal_click_count++;
         
         if(!button1_busy)
         {
-            button1_pressed = true;
             button1_busy = true;
             
-            Timeout multiclick_time;
-            multiclick_time.attach(callback(button1_multiclick_reset_cb), 1);
+            multiclick_time.attach(callback(&button1_multiclick_reset_cb), 1);
         }
     }
 }
@@ -97,5 +97,4 @@
     button1_busy = false;
     multiclick_state = 0;
     internal_click_count = 0;
-    
 }
\ No newline at end of file
--- a/debounce_button.h	Mon Oct 08 13:37:19 2018 +0000
+++ b/debounce_button.h	Mon Oct 08 15:39:22 2018 +0000
@@ -15,6 +15,9 @@
 extern volatile bool button1_busy;              // Informs the mainloop that the user is clicking the button => busy multiclick
 extern volatile int internal_click_count;       // Counts how many clicks occured in the time slot
 
+extern Timeout debounce_time;
+extern Timeout multiclick_time;
+
 void button1_multiclick_reset_cb(void);         // Resets the amount of clicks, but stores this value for the usage in the main loop
 void button1_enabled_cb(void);                  // Enables the button again after a timeout, used for debouncing the button 
 void button1_onpressed_cb(void);                // Callback which is called when the user presses the button
--- a/main.cpp	Mon Oct 08 13:37:19 2018 +0000
+++ b/main.cpp	Mon Oct 08 15:39:22 2018 +0000
@@ -43,15 +43,41 @@
 
 int main(int argc, char* argv[])
 {
+    printf("STARTED!\n");
     InterruptIn button(USER_BUTTON);
-    button.fall(callback(button1_onpressed_cb));
+    button.fall(callback(&button1_onpressed_cb));
     init_debouncer();
     
-    printf("started!");
+    EthernetInterface network;
+    int error = network.connect();
+    if(error != 0)
+    {
+        printf("ERROR: ethernet.connect() = %d\n", error);
+    }
+    const char* ip_add = network.get_ip_address();
+    
+    MQTTNetwork mqttNetwork(&network);
+    
+    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
+    
+    error = mqttNetwork.connect(BROKER_NAME, BROKER_PORT);
+    if(error != 0)
+    {
+        printf("ERROR: mqtt.connect() = %d\n", error);
+    }
+    
+    MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+    data.MQTTVersion = MQTT_VERSION;
+    
+    printf("INITIALIZED\n");
     
     while(true)
     {
-        
+        if(button1_pressed)
+        {
+            printf("Clicks: %d\n", multiclick_state);
+            button1_pressed = false;
+        }
     }
 
     return 0;