PGO6

Dependencies:   MQTT

Revision:
4:15e7cac255da
Parent:
3:14b20a4f36d0
Child:
6:754d3e8f9ae9
--- a/main.cpp	Mon Oct 28 16:35:27 2019 +0000
+++ b/main.cpp	Thu Oct 31 15:19:45 2019 +0100
@@ -9,9 +9,131 @@
 #include "MQTTmbed.h"
 #include "MQTTClient.h"
 #include "mbed.h"
+#include "easy-connect/easy-connect.h"
 
 char* topic;
 Serial pc(USBTX, USBRX);
+
+/** hier copy */
+volatile bool button1_pressed = false;   // Used in the main loop
+volatile bool button1_enabled = true;   // Used for debouncing
+volatile int multiclick_state =0 ;   // Counts how many clicks occured in the time slot, used in main loop
+volatile bool button1_busy = false;      // Informs the mainloop that the user is clicking the button
+volatile int button_count =0; // counter for number of clicks within 1 second
+volatile bool disp = false; // show eindtotaal
+
+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
+
+DigitalOut led1(LED1);
+DigitalOut led2(LED2);
+DigitalOut led3(LED3);
+Timeout someTimeout;
+Timeout someTimeout2;
+
+int arrivedcount = 0;
+
+
+/** end copy */
+
+#include "debounce_button.h"
+
+/**
+    Some tips and tricks:
+    -   To use the built-in LED:
+            DigitalOut led1(LED1);
+            ...
+            led1 = 1;
+    -   To delay the call of a function:
+            Timeout someTimeout;
+            ...
+            someTimeout.attach(callback(&someFunction), 0.5) with 0.5 as 500 milliseconds
+    -   The variables that are used in interrupt callbacks have to be volatile, 
+        because these variables can change at any time. Therefore, the compiler is not 
+        going to make optimisations.
+*/
+
+/**
+    TODO
+    ----
+    This function:
+        -   stores the amount of clicks in a variable which is read by the main loop.
+        -   resets the click counter which is used inside this file.
+        -   lowers a flag which tells the main loop that the user stopped pressing the button
+            such that it can proceed its program.
+        -   turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks.
+*/
+void button1_multiclick_reset_cb(void) {
+    led3=1;
+    //pc.printf("Time to reset alles");
+    button_count = multiclick_state;
+    multiclick_state =0;
+    disp=true;
+    
+    button1_busy = false;
+    button1_pressed = false; 
+    
+    DigitalOut led1(LED1);
+    led1 = 0;
+    
+}
+
+/**
+    TODO
+    ----
+    This function enables the button again, such that unwanted clicks of the bouncing button get ignored.
+*/
+void button1_enabled_cb(void)
+{
+    button1_enabled = true;
+    led2=0;
+
+}
+
+/**
+    TODO
+    ----
+    This function:
+        -   turns the built-in LED on, so the user gets informed that the program has started with counting clicks
+        -   disables the button such that the debouncer is active
+        -   enables the button again after a certain amount of time 
+            (use interrupts with "button1_enabled_cb()" as callback.
+        -   counts the amount of clicks within a period of 1 second
+        -   informs the main loop that the button has been pressed
+        -   informs the main loop that the user is clicking the button.
+            Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted.
+*/
+void button1_onpressed_cb(void)
+{
+    // Turn led on
+    led1 = 1;
+    led2= 0;
+    // Set button disabled
+    if(button1_enabled){
+        multiclick_state++;
+        button1_enabled = false;
+        led2=1;
+        //pc.printf("Nu is de button1_enabled false");
+        //pc.printf("Multiclickstate is nu: %d", multiclick_state);
+        someTimeout.attach(callback(button1_enabled_cb), 0.2);
+    }
+    
+    
+    // Inform main loop that user is clicking the button
+    button1_busy = true;
+
+    // Do a count after 1 second
+    if(!button1_pressed){
+        // Inform main loop that button has been pressed
+        //pc.printf("Registering callback for within 1 second");
+        button1_pressed = true; 
+        led3=0;
+        someTimeout2.attach(callback(button1_multiclick_reset_cb), 1);
+    }
+   
+}
+
 /**
     TODO
     ----
@@ -44,17 +166,54 @@
 
 int main(int argc, char* argv[])
 {
-    
+    led3=1;
     InterruptIn button(USER_BUTTON);
-    button.fall(callback(button1_onpressed_cb));
-    multiclick_state = 0;
+    button.fall(callback(&button1_onpressed_cb));
+    //multiclick_state = 0;
     pc.printf("Print print \n");
+    topic = "IGNISVOTESPOSTING";
+    NetworkInterface* network = easy_connect(true);
+    if (!network) {
+        pc.printf("Je netwerk is kapot");
+        return -1;
+    }
+
+    MQTTNetwork mqttNetwork(network);
+    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
+
+    const char* hostname = BROKER_NAME;
+    int port = BROKER_PORT;
+
+    pc.printf("Connecting to %s:%d\r\n", hostname, port);
+    int rc = mqttNetwork.connect(hostname, port);
+    if (rc != 0)
+        pc.printf("rc from TCP connect is %d\r\n", rc);
+
+    // QoS 2
+    MQTT::Message message;
+    char buf[100];
+    sprintf(buf, "Hello World!  QoS 2 message from app version %f\r\n", 1.0);
+    message.qos = MQTT::QOS2;
+    message.payload = (void*)buf;
+    message.payloadlen = strlen(buf)+1;
+    rc = client.publish(topic, message);
+    while (arrivedcount < 1)
+        client.yield(100);
+ 
+    if ((rc = client.unsubscribe(topic)) != 0)
+        pc.printf("rc from unsubscribe was %d\r\n", rc);
+ 
+    if ((rc = client.disconnect()) != 0)
+        pc.printf("rc from disconnect was %d\r\n", rc);
+ 
+    mqttNetwork.disconnect();
+    pc.printf("Version %.2f: finish %d msgs\r\n", 1111, arrivedcount);
+
     while(true){
-        
-        if(!button1_busy && !button1_pressed){
+        if(disp){
             // Toon eindtotaal
-            pc.printf("Er is zoveel keer geklikt: %d",button_count);
-            pc.printf("\n");
+            pc.printf("Er is zoveel keer geklikt: %d \n",button_count);
+            disp=false;
             
         }
     }