IoT werksessies embedded

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

debounce_button.cpp

Committer:
stijndirickx
Date:
2018-10-15
Revision:
3:6793060aacc6
Parent:
2:5b7d055dbc91

File content as of revision 3:6793060aacc6:

#include "debounce_button.h"

int amount_of_clicks = 0;
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
Timeout someTimeout1;
Timeout someTimeout2;

volatile bool button1_pressed;   // Used in the main loop
volatile bool button1_enabled;   // Used for debouncing
volatile int multiclick_state;   // Counts how many clicks occured in the time slot, used in main loop
volatile bool button1_busy;      // Informs the mainloop that the user is clicking the button

/*
    Delay;
    Timeout someTimeout;
    someTimeout.attach(callback(&someFunction), 0.5)
*/


void button1_multiclick_reset_cb(void) {
    
    //stores the amount of clicks in a variable which is read by the main loop.
    multiclick_state = amount_of_clicks;
    
    //resets the click counter which is used inside this file.
    amount_of_clicks = 0;
    
    //lowers a flag which tells the main loop that the user stopped pressing the button such that it can proceed its program.
    button1_pressed = false;
    
    //turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks.
    led1 = 0;
}

void button1_enabled_cb(void)
{
    //This function enables the button again, such that unwanted clicks of the bouncing button get ignored.
    button1_enabled = true;
}

void button1_onpressed_cb(void)
{
    //turns the built-in LED on, so the user gets informed that the program has started with counting clicks
    led1 = 1;
    
    //disables the button such that the debouncer is active
    button1_enabled = false;
    
    
    //enables the button again after a certain amount of time 
    //(use interrupts with "button1_enabled_cb()" as callback.
    someTimeout1.attach(callback(button1_enabled_cb), 0.1);
    
    
    //TODO revisit debouncing flag
    amount_of_clicks += 1; //clicks too much   
    
    
    //counts the amount of clicks within a period of 1 second
    someTimeout2.attach(callback(button1_multiclick_reset_cb), 1);
    
    
    //informs the main loop that the button has been pressed
    button1_pressed = true;
    
    //informs the main loop that the user is clicking the button.
    button1_busy = true;
    
    
    //Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted.  
}