cancel
Showing results for 
Search instead for 
Did you mean: 

Are there button libraries available for use on STM32 MCUs?

lightaiyee
Associate II
Posted on January 06, 2015 at 11:01

I am a newbie to STM32. I would like to migrate existing solution to STM32 which uses a button library that can detect double-press, triple-press, long-press, very-long-press etc.

Is there a button library available for the STM32 platform that I can use?

Thank you very much.
1 REPLY 1
Tamas Novak
Associate III
Posted on January 06, 2015 at 13:09

I have never seen a library like that, but it could be written quite easily.

My usual pushbutton filtering code is

//Key is pressed when ''Keyfilter'' goes up to 10, and released if counts down to 0. Counting +/-1  happens at every 1 msec.

#define KEY_FILTER_MAX    10

if (GPIO_ReadInputDataBit (<port, bit>)==0) {

    Keyfilter++;

    }

else {

    Keyfilter--;

    }

if (Keyfilter < 0) {

    Keyfilter= 0;    //stuck at lower limit

    }

if (Keyfilter > KEY_FILTER_MAX) {

    Keyfilter= KEY_FILTER_MAX;    //limit at max value

    }

if ((Keyfilter == 0) && (KeyServiced != 0))    { //if it have been reported, but not held down any more, we do service for release

    KeyServiced= 0;

    KeyReleaseService();

    }

if ((Keyfilter == KEY_FILTER_MAX) && (KeyServiced == 0)) {    //it is pushed but not serviced yet

    KeyPressTime= T_msec;    

    KeyServiced= 1;

    KeyPressService();

    }

}

If you push key-state-change-times into an array of 6 element (oldest item is to be shifted out: last 6 up/down times are stored), you can detect double/triple strokes:

I evaluate buffer if last pushed item is a release event. At evaluate function

- I check the time difference of the actual release and the previous down event. If it is shorter than 10msec, it's a glitch only. If it is 10msec..0.5sec, then normal push, if >0.5sec then a long push.

- I check the time and duration of [last-2] and [last-3] element pair...if they are older than 1sec, then they are part of a different sequence...if newer than 1sec, it's double (or triple) press

..and so on...