HAL Library 13- Buttons for STM32Fxxx
New port. Buttons library for HAL based libraries. It can handle buttons, connected to your STM32Fxxx devices.
It supports basically unlimited number of buttons with different triggering. Currently, callback functions can be called on these events:
- Button on pressed: Called directly when button is pressed
- Button normal press: Called when button is released after specific amount of time
- Button long press: Called when button is pressed for specific “long” amount of time
It uses malloc() to allocate memory, so you have to make sure that your heap is big enough. It also allows you to select state when button is pressed, high or low.
You just have to make sure, that your button is properly connected to STM device with capacitor and pull resistor!
Library
Features
- Supports buttons connected to STM32F4xx
- Support different button trigger events
Dependencies
- HAL
- TM
- STM32Fxxx HAL
- defines.h
- TM GPIO
- TM DELAY
Parameters
Library allows user to change some of parameters in library for button handling. In case you need to change it, I recommend you use defines.h file and edit your defines.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/* Number of maximal supported buttons */ #ifndef BUTTON_MAX_BUTTONS #define BUTTON_MAX_BUTTONS 10 #endif /* Number of milliseconds for normal press detection */ #ifndef BUTTON_NORMAL_PRESS_TIME #define BUTTON_NORMAL_PRESS_TIME 100 #endif /* Number of milliseconds for long press detection */ #ifndef BUTTON_LONG_PRESS_TIME #define BUTTON_LONG_PRESS_TIME 1500 #endif |
Functions and enumerations
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
/** * @defgroup TM_BUTTON_Typedefs * @brief Library Typedefs * @{ */ /** * @brief Button possible press types */ typedef enum { TM_BUTTON_PressType_OnPressed = 0x00, /*!< Button pressed */ TM_BUTTON_PressType_Normal, /*!< Normal press type, released */ TM_BUTTON_PressType_Long /*!< Long press type */ } TM_BUTTON_PressType_t; /** * @brief Button private structure */ typedef struct _TM_BUTTON_t { GPIO_TypeDef* GPIOx; /*!< GPIOx PORT for button */ uint16_t GPIO_Pin; /*!< GPIO pin for button */ uint8_t GPIO_State; /*!< GPIO state for pin when pressed */ void (*ButtonHandler)(struct _TM_BUTTON_t*, TM_BUTTON_PressType_t); /*!< Button function handler */ uint32_t StartTime; /*!< Time when button was pressed */ uint8_t LastStatus; /*!< Button status on last check */ uint8_t State; /*!< Current button state */ uint16_t PressNormalTime; /*!< Time in ms for normal press for button */ uint16_t PressLongTime; /*!< Time in ms for long press for button */ } TM_BUTTON_t; /** * @} */ /** * @defgroup TM_BUTTON_Functions * @brief Library Functions * @{ */ /** * @brief Initializes a new button to library * @note This library uses @ref malloc() to allocate memory, so make sure you have enough heap memory * @param *GPIOx: Pointer to GPIOx where button is located * @param GPIO_Pin: GPIO pin where button is located * @param ButtonState: Button state when it is pressed. * - 0: Button is low when pressed * - > 0: Button is high when pressed * @param *ButtonHandler: * @retval Button creation status: * - 0: Button was not created * - > 0: Button created and saved to library, button pointer is returned */ TM_BUTTON_t* TM_BUTTON_Init(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin, uint8_t ButtonState, void (*ButtonHandler)(TM_BUTTON_t*, TM_BUTTON_PressType_t)); /** * @brief Sets press timing values * @param *ButtonStruct: Pointer to @ref TM_BUTTON_t structure for button used * @param Normal: Time that button must be pressed to indicate normal press. Value is in milliseconds * @param Normal: Time that button must be pressed to indicate long press. Value is in milliseconds * @retval Pointer to @ref TM_BUTTON_t */ TM_BUTTON_t* TM_BUTTON_SetPressTime(TM_BUTTON_t* ButtonStruct, uint16_t Normal, uint16_t Long); /** * @brief Updates buttons. This function have to be called periodically * @note Function will automatically call callback functions for buttons if needed * @param None * @retval None */ void TM_BUTTON_Update(void); /** * @} */ |
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
/** * Keil project example for button handling * * Before you start, select your target, on the right of the "Load" button * * @author Tilen Majerle * @email tilen@majerle.eu * @website http://stm32f4-discovery.net * @ide Keil uVision 5 * @conf PLL parameters are set in "Options for Target" -> "C/C++" -> "Defines" * @packs STM32F4xx/STM32F7xx Keil packs are requred with HAL driver support * @stdperiph STM32F4xx/STM32F7xx HAL drivers required */ /* Include core modules */ #include "stm32fxxx_hal.h" /* Include my libraries here */ #include "defines.h" #include "tm_stm32_disco.h" #include "tm_stm32_delay.h" #include "tm_stm32_button.h" /* Button pointer */ TM_BUTTON_t* MyButton; /* Button callback function */ static void BUTTON_Callback(TM_BUTTON_t* ButtonPtr, TM_BUTTON_PressType_t PressType); int main(void) { /* Init system clock for maximum system speed */ TM_RCC_InitSystem(); /* Init HAL layer */ HAL_Init(); /* Init leds */ TM_DISCO_LedInit(); /* Init button, PORT, PIN, STATE when PRESSED */ MyButton = TM_BUTTON_Init(TM_DISCO_BUTTON_PORT, TM_DISCO_BUTTON_PIN, TM_DISCO_BUTTON_PRESSED, BUTTON_Callback); /* Init all your buttons you have in project if needed */ //MyButton2 = TM_BUTTON_Init(TM_DISCO_BUTTON_PORT, TM_DISCO_BUTTON_PIN, TM_DISCO_BUTTON_PRESSED, BUTTON_Callback); //MyButton3 = TM_BUTTON_Init(TM_DISCO_BUTTON_PORT, TM_DISCO_BUTTON_PIN, TM_DISCO_BUTTON_PRESSED, BUTTON_Callback); /* Set time how button is detected, 30 ms for normal press, 2000 ms for long press */ TM_BUTTON_SetPressTime(MyButton, 30, 2000); while (1) { /* Update all buttons */ TM_BUTTON_Update(); } } /* Implement handler function */ static void BUTTON_Callback(TM_BUTTON_t* ButtonPtr, TM_BUTTON_PressType_t PressType) { /* Normal press detected */ if (PressType == TM_BUTTON_PressType_Normal) { /* Set LEDS ON */ TM_DISCO_LedOn(LED_ALL); } else if (PressType == TM_BUTTON_PressType_Long) { /* Set LEDS OFF */ TM_DISCO_LedOff(LED_ALL); } } |
Project is available on Github, download all libraries below.
Recent comments