We would like to detect a "Double click" on a tactile button using TIRTOS. Preferably using static clocks. Does someone have an example of how to do this with GPIO interrupt, debounce and clocks?
Thanks.
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
Hi,
I don't have access to my original code, but below is some pseudo-code that I used for double-click.
// Init two timers (debounce and click delay) // Declare global variable state and initialize to IDLE buttonIsrFxn() { switch (state) { case IDLE: //first press timer_start(debounce timer); break; case PRESSED: // first press is released timer_start(debounce timer); break; case UNPRESSED: // second press timer_start(debounce timer); break; default: break; } } TimeoutFxn() { switch (state) { case IDLE: if (io == pressed state) { state = PRESSED; // optionally, post an event or semaphore to notify about the first click here timer_start(delay timer); } break; case PRESSED: if (io == pressed state) { // we have a long press (up to you to decide what behaviour your want) // I considered it as single click) state = IDLE; } else { state = UNPRESSED;
// start counting double click delay timer_start(delay_timer); }
case UNPRESSED:
if (io == pressed state) {
// double click event here (event or semaphore post)
}
state = IDLE;
break; } }
I wrote that pseudo-code from memory so I am not sure whether this code handles all cases, but this should cover the basics. I was able to run everything with a single timer, however, this requires you to stop and change the delay of the timers.
I haven't added data protection (i.e. disable and restore interrupts when I change state). It's up to you to see if this causes problems for you.
As for delays:
I used 20ms for debounce and 500ms for double-click delay
Hope this helps,
Michel
EDIT: For some odd reason, the case UNPRESSED didn't format properly. Several statements have been put on a single line
case UNPRESSED: if (io == pressed state) { // double click event here (event or semaphore post) } state = IDLE; break;
Hi pixbroker,
Unfortunately we do not have examples for your request above. However, here is some pseudo code using Clock objects to debounce a button:
Clock_Handle clkHandle; bool buttonIgnore = false; main() { … clkHandle = Clock_create((Clock_FuncPtr)myClkFxn, 250, NULL, &eb); … void buttonFxn(unsigned int index) { if(buttonIgnore == false) { buttonIgnore = true; Clock_start(clk0Handle); // real button push…do necessary actions } } void myClkFxn(UArg arg0) { buttonIgnore = false; }
You may be able to join the above with Michel's pseudo code for a solution. We are working on a buttons & button actions, but there we don't have a release date yet.
Hope this helps,
-- Emmanuel