Initial commit

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

debounce_button.cpp

Committer:
Rennert
Date:
2017-10-26
Revision:
2:f283e5274a0f
Parent:
1:e7a311bd038e

File content as of revision 2:f283e5274a0f:

#include "debounce_button.h"
volatile bool button1_pressed;   // Used in the main loop
volatile bool button1_enabled=true;   // Used for debouncing
volatile int multiclick_state;   // Counts how many clicks occured in the time slot, used in main loop
volatile bool button1_busy=false;  
Timeout enableTimeout;
Timeout resetTimeout;
DigitalOut myled(LED1);
int counter=0;

/**
    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) {
    myled=0;
    multiclick_state=counter;
    counter=0;
    button1_busy=false;
}

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

    
/**
    TODO
    ----
    This function:
        X-   turns the built-in LED on, so the user gets informed that the program has started with counting clicks
        X-   disables the button such that the debouncer is active
        X-   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
        X-   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.
*/

//Processes button interrupts
void button1_onpressed_cb(void)
{
    //If not busy, start measuring presses
    if(!button1_busy){
        button1_busy=true;
        button1_enabled=false;
        myled=1;
        resetTimeout.attach(callback(&button1_multiclick_reset_cb), 1.0);
        enableTimeout.attach(callback(&button1_enabled_cb), 0.1); 

    }   
    //If busy, measure presses
    if(button1_busy && button1_enabled){
        button1_enabled=false;
        enableTimeout.attach(callback(&button1_enabled_cb), 0.1); 
        counter++;
    }
}