-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathbutton_debouncer.py
39 lines (32 loc) · 1.3 KB
/
button_debouncer.py
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
import wpilib
class ButtonDebouncer:
"""Useful utility class for debouncing buttons"""
def __init__(self, joystick, buttonnum, period=0.5):
"""
:param joystick: Joystick object
:type joystick: :class:`wpilib.Joystick`
:param buttonnum: Number of button to retrieve
:type buttonnum: int
:param period: Period of time (in seconds) to wait before allowing new button
presses. Defaults to 0.5 seconds.
:type period: float
"""
self.joystick = joystick
self.buttonnum = buttonnum
self.latest = 0
self.debounce_period = float(period)
self.timer = wpilib.Timer
def set_debounce_period(self, period):
"""Set number of seconds to wait before returning True for the
button again"""
self.debounce_period = float(period)
def get(self):
"""Returns the value of the joystick button. If the button is held down, then
True will only be returned once every ``debounce_period`` seconds"""
now = self.timer.getFPGATimestamp()
if self.joystick.getRawButton(self.buttonnum):
if (now - self.latest) > self.debounce_period:
self.latest = now
return True
return False
__bool__ = get