Pushbutton Event Handler: Part 4

Pushbutton Handler

The handler routine is started by calling PushButton_Init function. This function registers PushButton_Routine at the end as a callback of the software timer.

   35 void PushButton_Init(uint8_t mask)
   36 {
   37     int i ;
   38 
   39     // clear data
   40     pp.old_state = pp.new_state = 0;
   41     pp.mask = mask;
   42     pp.mode = 0;
   43     pp.flag = false;
   44 
   45     // clear log
   46     for(i = 0; i < 8; i++)
   47     {
   48         PushButton_ClearLog(i);
   49     }
   50 
   51     // register pushbutton main routine
   52     UsrTimer_Set(PUSHBTN_TMR_PERIOD, 0, PushButton_Routine);
   53 }

The PushButton_Routine is then called at every 80msec and tracks the button state. If the button state is ON for at least three consecutive polling (80 x 3 = 240msec) then it declares CLICK by posting an event. These timing can be set to fit the bouncing characteristic of the mechanical switch.

   71 /// PushButton_Routine timer period in msec
   72 #define PUSHBTN_TMR_PERIOD      80
   73 /// Criteria for determination of short click and long click
   74 #define PUSHBTN_TO_SHORT        3       // 3 * PUSHBTN_TMR_PERIOD
   75 #define PUSHBTN_TO_LONG         10      // 10 * PUSHBTN_TMR_PERIOD
   76 #define PUSHBTN_TO_MAX          255     // maximum duration count

The PushButton_Routine, distinguishes single, double, triple click and long click (push and hold) and generates event accordingly, which in turn collected by the event handler in the main loop.

You can also change the mode from click detection to up/down detection, in which event is constantly generated when you hold the button down.

(source code)