1
1
from _functools import partial
2
- from wpilib .timer import Timer
2
+
3
+ import wpilib
3
4
4
5
5
6
class Toggle :
6
- """Utility class for button toggle
7
+ """Utility class for joystick button toggle
7
8
8
- Usage:
9
+ Usage: :
9
10
10
11
foo = Toggle(joystick, 3)
11
12
@@ -19,43 +20,39 @@ class Toggle:
19
20
offToggle()
20
21
"""
21
22
class _SteadyDebounce :
22
- '''
23
+ """
23
24
Similar to ButtonDebouncer, but the output stays steady for
24
25
the given periodic_filter. E.g, if you set the period to 2
25
26
and press the button, the value will return true for 2 seconds.
26
27
27
28
Steady debounce will return true for the given period, allowing it to be
28
29
used with Toggle
29
- '''
30
+ """
30
31
31
- def __init__ (self , joystick , button , period = 0.5 ):
32
- '''
32
+ def __init__ (self , joystick : wpilib . Joystick , button : int , period : float ):
33
+ """
33
34
:param joystick: Joystick object
34
35
:type joystick: :class:`wpilib.Joystick`
35
36
:param button: Number of button to retrieve
36
37
:type button: int
37
38
:param period: Period of time (in seconds) to wait before allowing new button
38
39
presses. Defaults to 0.5 seconds.
39
40
:type period: float
40
- '''
41
+ """
41
42
self .joystick = joystick
42
43
self .button = button
43
44
44
45
self .debounce_period = float (period )
45
46
self .latest = - self .debounce_period # Negative latest prevents get from returning true until joystick is presed for the first time
46
- self .timer = Timer
47
47
self .enabled = False
48
48
49
- def set_debounce_period (self , period ):
50
- '''Set number of seconds to hold return value'''
51
- self .debounce_period = float (period )
52
-
53
49
def get (self ):
54
- '''Returns the value of the joystick button. Once the button is pressed,
55
- the return value will be True until the time expires
56
- '''
50
+ """
51
+ :returns: The value of the joystick button. Once the button is pressed,
52
+ the return value will be `True` until the time expires
53
+ """
57
54
58
- now = self . timer .getFPGATimestamp ()
55
+ now = wpilib . Timer .getFPGATimestamp ()
59
56
if now - self .latest < self .debounce_period :
60
57
return True
61
58
@@ -65,17 +62,18 @@ def get(self):
65
62
else :
66
63
return False
67
64
68
- def __init__ (self , joystick , button , debouncePeriod = None ):
65
+ def __init__ (self , joystick : wpilib . Joystick , button : int , debounce_period : float = None ):
69
66
"""
70
- :param joystick: wpilib.Joystick that contains the button to toggle
71
- :param button: Value of button that will act as toggle. Same value used in getRawButton()
67
+ :param joystick: :class:`wpilib.Joystick` that contains the button to toggle
68
+ :param button: Number of button that will act as toggle. Same value used in `getRawButton()`
69
+ :param debounce_period: Period in seconds to wait before registering a new button press.
72
70
"""
73
71
74
- if debouncePeriod is not None :
75
- self .joystick = Toggle ._SteadyDebounce (joystick , button , debouncePeriod )
72
+ if debounce_period is not None :
73
+ self .joystickget = Toggle ._SteadyDebounce (joystick , button , debounce_period ). get
76
74
else :
77
75
self .joystick = joystick
78
- self .joystick . get = partial (self .joystick .getRawButton , button )
76
+ self .joystickget = partial (self .joystick .getRawButton , button )
79
77
80
78
self .released = False
81
79
self .toggle = False
@@ -86,7 +84,7 @@ def get(self):
86
84
:return: State of toggle
87
85
:rtype: bool
88
86
"""
89
- current_state = self .joystick . get ()
87
+ current_state = self .joystickget ()
90
88
91
89
if current_state and not self .released :
92
90
self .released = True
@@ -100,11 +98,17 @@ def get(self):
100
98
101
99
@property
102
100
def on (self ):
101
+ """
102
+ Equates to true if toggle is in the 'on' state
103
+ """
103
104
self .get ()
104
105
return self .state
105
106
106
107
@property
107
108
def off (self ):
109
+ """
110
+ Equates to true if toggle is in the 'off' state
111
+ """
108
112
self .get ()
109
113
return not self .state
110
114
0 commit comments