fatih kilic / Mbed OS PGO6_VoteController_template

Dependencies:   MQTT

Files at this revision

API Documentation at this revision

Comitter:
fatihkilic
Date:
Fri Nov 22 17:55:41 2019 +0000
Parent:
2:5b7d055dbc91
Commit message:
last version;

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
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
--- a/debounce_button.cpp	Tue Oct 31 09:01:56 2017 +0000
+++ b/debounce_button.cpp	Fri Nov 22 17:55:41 2019 +0000
@@ -25,7 +25,24 @@
             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.
 */
+
+InterruptIn button(USER_BUTTON);
+DigitalOut led1(LED1);
+DigitalIn enable(USER_BUTTON);
+volatile bool button1_pressed = false;
+volatile bool button1_enabled = false;
+volatile int multiclick_state= 0;
+volatile bool button1_busy = false;
+volatile int total = 0;
+Timeout timeout;
+
 void button1_multiclick_reset_cb(void) {
+    total = multiclick_state;
+    multiclick_state = 0;
+    led1 = 0;
+    button1_pressed = false;
+     button1_busy = false;
+     button1_enabled_cb();
     
 }
 
@@ -34,9 +51,15 @@
     ----
     This function enables the button again, such that unwanted clicks of the bouncing button get ignored.
 */
+
+
+
+           
 void button1_enabled_cb(void)
 {
-    
+    // button.rise(callback(button1_onpressed_cb));
+     
+     button1_enabled = true;
 }
 
 /**
@@ -54,5 +77,24 @@
 */
 void button1_onpressed_cb(void)
 {
+    if(button1_enabled && enable){
+        led1 = 1;
+        button1_pressed = true;
+        button1_enabled = false;
+        button1_busy = true;
+       timeout.attach(callback(&button1_multiclick_reset_cb),1);
+    }
+    if(enable && button1_pressed){
+        button1_pressed = false;
+        multiclick_state++;
+    }
+    if(!enable){
+        button1_pressed = true;
+        }
     
+    
+}
+
+void button1_onrelease_cb(void){
+    button1_pressed = false;   
 }
\ No newline at end of file
--- a/debounce_button.h	Tue Oct 31 09:01:56 2017 +0000
+++ b/debounce_button.h	Fri Nov 22 17:55:41 2019 +0000
@@ -9,11 +9,15 @@
     
 */
 
+
 extern volatile bool button1_pressed;   // Used in the main loop
 extern volatile bool button1_enabled;   // Used for debouncing
 extern volatile int multiclick_state;   // Counts how many clicks occured in the time slot, used in main loop
 extern volatile bool button1_busy;      // Informs the mainloop that the user is clicking the button
+extern volatile int total;
+
 
 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
\ No newline at end of file
+void button1_onpressed_cb(void);        // Callback which is called when the user presses the button
+void button1_onrelease_cb(void);
\ No newline at end of file
--- a/main.cpp	Tue Oct 31 09:01:56 2017 +0000
+++ b/main.cpp	Fri Nov 22 17:55:41 2019 +0000
@@ -8,6 +8,7 @@
 #include "MQTTNetwork.h"
 #include "MQTTmbed.h"
 #include "MQTTClient.h"
+#include "mbed.h"
 
 char* topic;
 
@@ -41,8 +42,72 @@
             sendMessage(&client, topic, buf, qos, retained, duplicate)
 */
 
+
+EthInterface *ethernet;
+ Serial pc(USBTX,USBRX);
+ 
+void messageArrived(MQTT::MessageData& md) {
+   MQTT::Message &message = md.message;
+   char msg[300];
+   sprintf(msg, "Message arrived: QoS%d, retained %d, dup %d, packetID %d\r\n", message.qos, message.retained, message.dup, message.id);
+   printf(msg);
+   wait_ms(1000);
+   char payload[300];
+   sprintf(payload, "Payload %.*s\r\n", message.payloadlen, (char*)message.payload);
+   printf(payload);
+}
+
 int main(int argc, char* argv[])
 {
+    pc.baud(9600);
+    ethernet = EthInterface::get_default_instance();
+    if (!ethernet) {
+      printf("ERROR: No ethernet found.\r\n");
+      return -1;
+    }    
 
+    NetworkInterface* net = ethernet;
+    MQTTNetwork mqttNetwork(net);
+    MQTT::Client<MQTTNetwork, Countdown> client(mqttNetwork);
+    
+    const char* host = "broker.hivemq.com";
+   const char* topic = "fat";
+   pc.printf("Connecting to TCP network...\r\n");
+   int rc = mqttNetwork.connect(host, 1883);
+   if (rc != 0) {
+      pc.printf("Connection error.");
+      return -1;
+   }
+   pc.printf("Successfully connected!\r\n");
+
+   MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
+   data.MQTTVersion = 3;
+   data.clientID.cstring = "fat";
+
+   if ((rc = client.connect(data)) != 0){
+      pc.printf("Fail to connect MQTT\r\n");
+   }
+   if (client.subscribe(topic, MQTT::QOS0, messageArrived) != 0){
+      pc.printf("Fail to subscribe\r\n");
+   }
+
+    button1_enabled_cb();
+      
+    while(true){
+        if(button1_enabled ){
+           // pc.printf("button enabled");
+        }
+        if(button1_busy && !button1_enabled){
+            
+             
+        }
+        else if(!button1_busy && !button1_enabled){
+            pc.printf("total: %d /n", total);
+         }
+         button1_onpressed_cb();
+    }
+    
     return 0;
 }
+
+
--- a/mbed-os.lib	Tue Oct 31 09:01:56 2017 +0000
+++ b/mbed-os.lib	Fri Nov 22 17:55:41 2019 +0000
@@ -1,1 +1,1 @@
-https://github.com/ARMmbed/mbed-os/#2885c1b41e63158cb6faf5f107cd821ae06ef26c
+https://github.com/ARMmbed/mbed-os/#3a57ec7401a77b8b98f6356a1498cb154229483f