Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5847

SDK • Re: Multi function button

$
0
0
The simple way to do it is measure out a fixed period. Then use software to pattern match. This means there is only so many button presses allowed per second. The problem with this is the period is fixed and the user cannot violate the period. (Fat chance of that never happening.)

Instead what you would want to do is create a threshold system for a single click. (This is a single period of time.) Then create a threshold for a long press. Measuring these is pretty straight forward. What a lot of people are tempted to do is create a max period for a double press. The problem with this is it never really works right. (Silly async.) The reason a lot of people do this is cause they are lazy.

Again we can do better with a state machine, which looks for a back off section. Adaptive periods/thresholds are possible. But we must enforce the back off! Using a debouncer:
  • Look for a rising edge, count the time high, and compare the time to idle low. (Report idle.)
  • Look for the falling edge, count the time low, and compare the time to single versus long press high. (Report long press.)
  • Look for the rising edge, count the time high, and compare the time to double versus single press low. (Report single press.)
  • Look for the falling edge, count the time low, and compare the time to single versus long press high. (Report double press.)
  • Repeat.

Now if we wanted to get fancy we could create queue for our button pushes. We keep the sequence and number to allow a menu task to do batch processing. (We can skip menu renders.) This however is usually a bad idea! We would like the ability to lock out our debouncer instead. This prevents more silly async coding.

Statistics: Posted by dthacher — Tue Jun 11, 2024 10:14 am



Viewing all articles
Browse latest Browse all 5847

Trending Articles