Vincent Nys
/
PGO6_VoteController_template
Vincent Nys
Fork of PGO6_VoteController_template by
Revision 3:c53f27bdcfb5, committed 2017-10-31
- Comitter:
- vincentnys
- Date:
- Tue Oct 31 18:31:34 2017 +0000
- Parent:
- 2:5b7d055dbc91
- Commit message:
- PGO6
Changed in this revision
diff -r 5b7d055dbc91 -r c53f27bdcfb5 debounce_button.cpp --- a/debounce_button.cpp Tue Oct 31 09:01:56 2017 +0000 +++ b/debounce_button.cpp Tue Oct 31 18:31:34 2017 +0000 @@ -1,5 +1,10 @@ #include "debounce_button.h" +DigitalOut led1(LED1); +volatile int click_counter = 0; +Timeout multiclick; +Timeout debounce; + /** Some tips and tricks: - To use the built-in LED: @@ -25,8 +30,12 @@ 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) { - +void button1_multiclick_reset_cb(void) +{ + button1_busy = false; + multiclick_state = click_counter; + click_counter = 0; + led1 = 0; } /** @@ -36,7 +45,7 @@ */ void button1_enabled_cb(void) { - + button1_enabled = true; } /** @@ -54,5 +63,16 @@ */ void button1_onpressed_cb(void) { - + if(!button1_busy) + { + multiclick.attach( &button1_multiclick_reset_cb, 1 ); + } + button1_busy = true; + led1 = 1; + if( button1_enabled ) + { + button1_enabled = false; + click_counter++; + debounce.attach( &button1_enabled_cb, 0.08 ); // 80ms + } } \ No newline at end of file
diff -r 5b7d055dbc91 -r c53f27bdcfb5 debounce_button.h --- a/debounce_button.h Tue Oct 31 09:01:56 2017 +0000 +++ b/debounce_button.h Tue Oct 31 18:31:34 2017 +0000 @@ -14,6 +14,7 @@ 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
diff -r 5b7d055dbc91 -r c53f27bdcfb5 main.cpp --- a/main.cpp Tue Oct 31 09:01:56 2017 +0000 +++ b/main.cpp Tue Oct 31 18:31:34 2017 +0000 @@ -9,7 +9,11 @@ #include "MQTTmbed.h" #include "MQTTClient.h" -char* topic; +char* topic = "dansvloerVincent/votes"; +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 /** TODO @@ -43,6 +47,75 @@ int main(int argc, char* argv[]) { + printf("Vote controller booting...\n"); + int rc; + + InterruptIn button(USER_BUTTON); + button.fall(&button1_onpressed_cb); + + EthernetInterface eth; + eth.connect(); + const char *ip = eth.get_ip_address(); + printf("IP address is: %s\n", ip ? ip : "No IP"); + + NetworkInterface *net = ð + MQTTNetwork mqtt( net ); + + MQTT::Client<MQTTNetwork, Countdown> client( mqtt ); + + char *host = "143.129.39.151";//"broker.mqttdashboard.com"; + int port = 1883; + rc = mqtt.connect(host, port); + if (rc != 0) + printf("rc from TCP connect is %d\n", rc); + + + + MQTTPacket_connectData data = MQTTPacket_connectData_initializer; + data.MQTTVersion = MQTT_VERSION; + data.clientID.cstring = "VincentController"; + data.username.cstring = "smartcity"; + data.password.cstring = "smartcity"; + if ((rc = client.connect(data)) != 0) + printf("rc from MQTT connect is %d\n", rc); + + MQTT::Message message; + message.qos = MQTT::QOS1; + printf("Start clicking\n"); + char buf[100]; + + while( multiclick_state < 4 ) + { + // Keep running + + + if(!button1_busy && multiclick_state > 0) + { + //printf("%d\n", multiclick_state); + if(multiclick_state == 1) + sprintf(buf, "like\n"); + else + //buf = "dislike\n"; + sprintf(buf, "dislike\n"); + multiclick_state = 0; + + printf("%s\n", buf); + message.payload = (void *)buf; + message.payloadlen = strlen(buf) + 1; + client.publish(topic, message); + } + + + //printf("%d\n", multiclick_state); + } + + + + if ((rc = client.disconnect()) != 0) + printf("rc from disconnect was %d\n", rc); + mqtt.disconnect(); + + printf("Terminate...\n"); return 0; }