6th September 2017

Click, double click and long click button timer interrupt library

This is simple timer interrupt driven button library for the STM32 microControllers. Main features:

  1. Any number of buttons support
  2. “Normal” click, double click and “long” click supported
  3. L/H signal level configurable
  4. Any regular invoked  interrupt supported
  5. Callback functions for any of the keys (if compiled with KBDCALLBACKENABLED == 1)
  6. Callbacks on key events (key up/ key down / key) if compiled with KEYEVENTSCALLBACKS == 1

Interface:

int KBD_addKey(GPIO_TypeDef *gpio, int pin, int type);
Adds button to the button pool.
Input parameters :
gpio – pointer to the CMSIS port definitions structure (GPIOA, GPIOB …)
pin – number in the port (0 – 15)
type – signal level when key pressed (LO or HIGH)
Returns : key handle to be used in another functions in this library

/* when compiled with KBDCALLBACKENABLED == 1 */
int KBD_addKey(GPIO_TypeDef *gpio, int pin, int type, void (*callback)(int key);

Adds button to the button pool.
Input parameters :
gpio – pointer to the CMSIS port definitions structure (GPIOA, GPIOB …)
pin – number in the port (0 – 15)
type – signal level when key pressed (LO or HIGH)
callback – callback function called when button action is detected.
Returns : key handle to be used in another functions in this library

int KBD_GetKey(int key);
Returns last key
Input parameters :
  key – the key handle returned by KBD_addKey
Returns : last key or NOKEY. Return values are defined in the KEYSTROKES_T enum type

void KBD_SetKey(int key, int st);
Manually set key to be returned by KBD_GetKey
Input parameters :
  key – the key handle returned by KBD_addKey
  stKEYSTROKES_T key status
Returns : nothing

void KBD_ISR_Callback(void);
Function to be called from the timer interrupt

Example code:

In this example code one button is used to control behaviour of the six LEDs. LED are driven by my Timer Interrupt LED Library. 3 diodes are set in the button callback function

 

The main function to illustrate how to initialize and use the button library

 

Whole demo and library can downloaded from my github repository : https://github.com/diymat/ARM-blog-libraries-and-examples

 

Leave a Reply

Your email address will not be published. Required fields are marked *