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:
jensdehoog
Date:
Thu Oct 26 07:33:07 2017 +0000
Child:
1:34e76c0cbe5a
Commit message:
Added the template for the assignment of the Vote Controller

Changed in this revision

MQTT.lib Show annotated file Show diff for this revision Revisions of this file
MQTTNetwork.h Show annotated file Show diff for this revision Revisions of this file
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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTT.lib	Thu Oct 26 07:33:07 2017 +0000
@@ -0,0 +1,1 @@
+https://mbed.org/teams/mqtt/code/MQTT/#c37c8236e84a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MQTTNetwork.h	Thu Oct 26 07:33:07 2017 +0000
@@ -0,0 +1,38 @@
+#ifndef _MQTTNETWORK_H_
+#define _MQTTNETWORK_H_
+
+#include "NetworkInterface.h"
+
+class MQTTNetwork {
+public:
+    MQTTNetwork(NetworkInterface* aNetwork) : network(aNetwork) {
+        socket = new TCPSocket();
+    }
+
+    ~MQTTNetwork() {
+        delete socket;
+    }
+
+    int read(unsigned char* buffer, int len, int timeout) {
+        return socket->recv(buffer, len);
+    }
+
+    int write(unsigned char* buffer, int len, int timeout) {
+        return socket->send(buffer, len);
+    }
+
+    int connect(const char* hostname, int port) {
+        socket->open(network);
+        return socket->connect(hostname, port);
+    }
+
+    int disconnect() {
+        return socket->close();
+    }
+
+private:
+    NetworkInterface* network;
+    TCPSocket* socket;
+};
+
+#endif // _MQTTNETWORK_H_
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/debounce_button.cpp	Thu Oct 26 07:33:07 2017 +0000
@@ -0,0 +1,58 @@
+#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) {
+    
+}
+
+/**
+    TODO
+    ----
+    This function enables the button again, such that unwanted clicks of the bouncing button get ignored.
+*/
+void button1_enabled_cb(void)
+{
+    
+}
+
+/**
+    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)
+{
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/debounce_button.h	Thu Oct 26 07:33:07 2017 +0000
@@ -0,0 +1,19 @@
+#include "mbed.h"
+
+/**
+    Due to the imperfect design of the buttons, a press on the button is registered multple times.
+    The debouncer module makes sure that these false positives of the button are going to be ignored.
+    
+    Also, this module provides a multiclick service which allows the user to press the button multiple times 
+    within a certain time frame. Therefore, multiple actions can be mapped to a single button.
+    
+*/
+
+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
+
+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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Oct 26 07:33:07 2017 +0000
@@ -0,0 +1,48 @@
+#define APP_VERSION     0.6f
+#define MQTT_VERSION    3
+#define BROKER_NAME     "broker.hivemq.com"
+#define BROKER_PORT     1883
+
+#include "debounce_button.h"
+#include "EthernetInterface.h"
+#include "MQTTNetwork.h"
+#include "MQTTmbed.h"
+#include "MQTTClient.h"
+
+char* topic;
+
+/**
+    TODO
+    ----
+    -   Check if the button has been pressed. If so, print the amount of clicks to a serial terminal.
+    -   Make an MQTT-service which:
+        -   starts up a network using EthernetInterface. Make sure the development board requests its address via DHCP.
+        -   makes a client and connects it to the broker using a client ID and credentials (username & password).
+        -   sends messages at the same topic as the smartphone app from PGO 2. Feel free to choose which Quality of Service
+            you are going to use. Make a separate function which handles the sending procedure. Therefore, this function
+            can be called each time we want to send a certain message.
+    -   When the button is pressed once, we send an upvote. When pressed twice, a downvote is sent. By pressing 4 times,
+        the program disconnects from the broker and terminates.
+        
+    Extra
+    -----
+    -   Subscribe to the topic on which the song data is published. Display this received message on the serial terminal.
+    -   Test this controller in the complete system of PGO 2. Use these controllers instead of the smartphones.
+    
+    Tips & tricks
+    -------------
+    -   To generate an interrupt on the press of a button, use:
+            InterruptIn button(USER_BUTTON);
+            ...
+            button.fall(callback(someFunction));
+    -   Before implementing MQTT, test the multiclick feature first.
+    -   Have a look at the MQTT-library for Mbed and the HelloMQTT-example.
+    -   To have a uniform message sending procedure, use the following function usage:
+            sendMessage(&client, topic, buf, qos, retained, duplicate)
+*/
+
+int main(int argc, char* argv[])
+{
+
+    return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Thu Oct 26 07:33:07 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#2885c1b41e63158cb6faf5f107cd821ae06ef26c