Project Design
Pushbuttons, or tactile switches are convenient way of interacting with the firmware, whether the final product retains them as its UI components or not. There are a couple of things to consider to implement the pushbutton handling features.Debouncing and Software Timer
First you have to deal with the contact bouncing effect of the switch. Following snapshot of an oscilloscope from Wikipedia page illustrates one situation. However it can be radically different from one case to another.There are numerous ways of addressing the issue, each of which has its own pros and cons. So one method works better in one situation but not necessarily in other cases primarily depending on the chattering characteristics but also depending on other situations. Here we are going to use software timer, in which a timer routine checks (samples) the state of the switch with a regular interval.
Short Click vs. Long Click, Single Click vs. Multiple Click
Another consideration has regard to endowing multiple functions to a single button. So you want to distinguish a short click from a long click, or a single click from a multiple click. This requires an algorithm that keeps track of the state of the button in time. So for example when a single click is detected, it should be able to determine whether it has to wait certain amount of time for the next potential click or it can declare the single click event now. The timer based pushbutton check routine is well suited for this purpose. It can implement this function with minimal complexity.Event Queue Handling
Finally, there is a situation when you want to divide one task into at least two parts, one part that needs to be executed in very short duration of time usually much less than a millisecond, and the other part that has no particular time limit and that can be interrupted at any moment.Typical interrupt service routines including software timer routines fall into this framework. Imagine that you want to send a UART message when certain button is clicked. It is not a good idea to put a UART Tx function inside a GPIO external interrupt routine that detects the rise or the falling edge of the button signal, especially when the UART Tx function is a blocking call.
Instead your interrupt service routine only sets a flag, or posts an event. Then the main loop checks the flag later and processes it asynchronously. An event queue as a form of FIFO ring buffer is used for this purpose.
UsrTimer
UsrTimer is a function that implements a software based timer. The base housekeeping routine, UsrTimer_Routine() should be run at a regular interval, which dictates the resolution of the timer. Thus if you call this function inside the SysTick callback then the resolution, and the basic unit of the timer becomes 1 millisecond.290 /** SysTick callback function override. 291 */ 292 void HAL_SYSTICK_Callback() 293 { 294 // UsrTimer_Routine will have 1msec resolution 295 UsrTimer_Routine(); 296 }A new software timer routine can be registered by calling UsrTimer_Set() function.
105 // start a timer routine: 100msec period, perpetual 106 result = UsrTimer_Set(100, 0, TestCallback);In this example, the function TestCallback() will be executed for every 100msec. Note that the function returns the id of the timer. You can set the timer as perpetual or you can set it to be run for a limited number of times. So for example, if the TestCallback() is toggling a LED, you can see that it will change on/off state at every 100msec.
321 void TestCallback() 322 { 323 HAL_GPIO_TogglePin(TEST_LED_GPIO_Port, TEST_LED_Pin); 324 }You can also pause/resume or stop the timer at any moment.
45 /// Initialize all timers 46 void UsrTimer_Init(); 47 /// Clear the timer 48 void UsrTimer_Clear(uint32_t index); 49 /// Pause the timer 50 void UsrTimer_Pause(uint32_t index); 51 /// Resume the timer 52 void UsrTimer_Resume(uint32_t index); 53 /// Main timer routine 54 void UsrTimer_Routine(void); 55 /// Set a new timer with the callback function 56 int UsrTimer_Set(uint32_t interval, uint32_t duration, usrtimer_callback f);<<source code>>
Comments
Post a Comment