In this post, actual pushbutton checking algorithm is explained. As discussed previously, it utilizes the software timer (UsrTimer) to address the chattering issue and to implement a multi-function input button. The main timer routine is registered by calling:
Source file
This period can be tuned to serve particular hardware better. However you have to remember that other parameters such as the duration values that determine short click and long click have reference to this value. For example PUSHBTN_TO_SHORT value is set to 3, which means that the algorithm wait for 3 * 80 = 240 msec after a click is detected then posts a single click event. If subsequent click is detected during this period, it does not post any event but keeps track the state of the button.
Actual algorithm is implemented in the main routine, PushButton_Routine(). However this routine needs to read the current state of the button(s) by calling PushButton_Read() function:
Source file
Source file
Source file
As the PushButton_Routine() automatically posts events according to the state of the button, you need to implement event handling function inside the main() loop as shown in the previous post. In the sample code, it will print out the result via UART port.
It is rather easy to modify the code to have two modes. In addition to the method described above, i.e., algorithm detects the change of the buttons state, it can detects just up or down state of the buttons, so that it will continuously generate event when the button is pressed, while silent when it is released.
<<source code>>
103 // initialize the pushbutton handler with mask byte. 104 PushButton_Init(0x01);where the mask byte (0x01) indicates the location of the state bits of active buttons in the 8bit button state byte. Here we have only one button that is located at the first bit location. This call starts a timer routine that runs indefinitely at a predefined interval, PUSHBTN_TMR_PERIOD whose value is set to 80 msec at the moment.
This period can be tuned to serve particular hardware better. However you have to remember that other parameters such as the duration values that determine short click and long click have reference to this value. For example PUSHBTN_TO_SHORT value is set to 3, which means that the algorithm wait for 3 * 80 = 240 msec after a click is detected then posts a single click event. If subsequent click is detected during this period, it does not post any event but keeps track the state of the button.
Actual algorithm is implemented in the main routine, PushButton_Routine(). However this routine needs to read the current state of the button(s) by calling PushButton_Read() function:
63 /** Main routine 64 */ 65 void PushButton_Routine() 66 { 67 int i; 68 uint8_t diff_state; 69 uint8_t event[EVT_QWIDTH]; 70 static bool flag = false; 71 72 pp.new_state = PushButton_Read(); 73 74 // difference in the button state 75 diff_state = pp.old_state ^ pp.new_state;where the function is defined as weak:
81 /// Read all pushbutton state and return them in a uint8_t form 82 uint8_t PushButton_Read(void) __attribute__((weak));Thus you have to write your own version of the function somewhere in your code:
298 /** This function returns the state (click/release) of the pushbuttons 299 * in a uint8_t variable, each bit of which corresponds to a button. 300 * Note that 1 implies the button is clicked (pressed) and 0 implies the 301 * button is released. 302 * 303 * \return pushbutton state packed in a uint8_t. 304 */ 305 uint8_t PushButton_Read() 306 { 307 // button released 308 if(HAL_GPIO_ReadPin(TEST_BTN_GPIO_Port, TEST_BTN_Pin)) 309 { 310 return 0x00; 311 } 312 // button pressed 313 else 314 { 315 return 0x01; 316 } 317 }Note that the return value of the function should match with the mask bit, in this case the first bit of the 8bit data.
As the PushButton_Routine() automatically posts events according to the state of the button, you need to implement event handling function inside the main() loop as shown in the previous post. In the sample code, it will print out the result via UART port.
It is rather easy to modify the code to have two modes. In addition to the method described above, i.e., algorithm detects the change of the buttons state, it can detects just up or down state of the buttons, so that it will continuously generate event when the button is pressed, while silent when it is released.
<<source code>>
Comments
Post a Comment