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
- foo = Toggle(joystick, 3)
11
+ foo = Toggle(joystick, 3)
11
12
12
- if foo:
13
- toggleFunction()
13
+ if foo:
14
+ toggleFunction()
14
15
15
- if foo.on:
16
- onToggle()
16
+ if foo.on:
17
+ onToggle()
17
18
18
- if foo.off:
19
- offToggle()
19
+ if foo.off:
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
32
def __init__ (self , joystick , button , period = 0.5 ):
32
- '''
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
49
def set_debounce_period (self , period ):
50
- ''' Set number of seconds to hold return value'''
50
+ """ Set number of seconds to hold return value"""
51
51
self .debounce_period = float (period )
52
52
53
53
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
- '''
54
+ """
55
+ :returns: The value of the joystick button. Once the button is pressed,
56
+ the return value will be `True` until the time expires
57
+ """
57
58
58
- now = self . timer .getFPGATimestamp ()
59
+ now = wpilib . Timer .getFPGATimestamp ()
59
60
if now - self .latest < self .debounce_period :
60
61
return True
61
62
@@ -65,14 +66,15 @@ def get(self):
65
66
else :
66
67
return False
67
68
68
- def __init__ (self , joystick , button , debouncePeriod = None ):
69
+ def __init__ (self , joystick : wpilib . Joystick , button : int , debounce_period = None ):
69
70
"""
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()
71
+ :param joystick: :class:`wpilib.Joystick` that contains the button to toggle
72
+ :param button: Number of button that will act as toggle. Same value used in `getRawButton()`
73
+ :param debounce_period: Period to wait before registering a new button press.
72
74
"""
73
75
74
- if debouncePeriod is not None :
75
- self .joystick = Toggle ._SteadyDebounce (joystick , button , debouncePeriod )
76
+ if debounce_period is not None :
77
+ self .joystick = Toggle ._SteadyDebounce (joystick , button , debounce_period )
76
78
else :
77
79
self .joystick = joystick
78
80
self .joystick .get = partial (self .joystick .getRawButton , button )
@@ -100,11 +102,17 @@ def get(self):
100
102
101
103
@property
102
104
def on (self ):
105
+ """
106
+ Equates to true if toggle is in the 'on' state
107
+ """
103
108
self .get ()
104
109
return self .state
105
110
106
111
@property
107
112
def off (self ):
113
+ """
114
+ Equates to true if toggle is in the 'off' state
115
+ """
108
116
self .get ()
109
117
return not self .state
110
118
0 commit comments