Skip to content

Commit 1785221

Browse files
committed
Fixup and add toggle to docs
1 parent 3c5547c commit 1785221

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

docs/magicbot.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Tunable
2626
:show-inheritance:
2727

2828
Resettable
29-
~~~~~
29+
~~~~~~~~~~
3030

3131
.. automodule:: magicbot.magic_reset
3232
:members:

docs/robotpy_ext.control.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ robotpy_ext.control.button_debouncer module
88
:members:
99
:undoc-members:
1010

11+
robotpy_ext.control.toggle module
12+
---------------------------------
13+
14+
.. automodule:: robotpy_ext.control.toggle
15+
:members:
16+
:undoc-members:
17+
1118

1219
robotpy_ext.control.xbox_controller module
1320
------------------------------------------

robotpy_ext/control/toggle.py

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,62 @@
11
from _functools import partial
2-
from wpilib.timer import Timer
2+
3+
import wpilib
34

45

56
class Toggle:
6-
"""Utility class for button toggle
7+
"""Utility class for joystick button toggle
78
8-
Usage:
9+
Usage::
910
10-
foo = Toggle(joystick, 3)
11+
foo = Toggle(joystick, 3)
1112
12-
if foo:
13-
toggleFunction()
13+
if foo:
14+
toggleFunction()
1415
15-
if foo.on:
16-
onToggle()
16+
if foo.on:
17+
onToggle()
1718
18-
if foo.off:
19-
offToggle()
19+
if foo.off:
20+
offToggle()
2021
"""
2122
class _SteadyDebounce:
22-
'''
23+
"""
2324
Similar to ButtonDebouncer, but the output stays steady for
2425
the given periodic_filter. E.g, if you set the period to 2
2526
and press the button, the value will return true for 2 seconds.
2627
2728
Steady debounce will return true for the given period, allowing it to be
2829
used with Toggle
29-
'''
30+
"""
3031

3132
def __init__(self, joystick, button, period=0.5):
32-
'''
33+
"""
3334
:param joystick: Joystick object
3435
:type joystick: :class:`wpilib.Joystick`
3536
:param button: Number of button to retrieve
3637
:type button: int
3738
:param period: Period of time (in seconds) to wait before allowing new button
3839
presses. Defaults to 0.5 seconds.
3940
:type period: float
40-
'''
41+
"""
4142
self.joystick = joystick
4243
self.button = button
4344

4445
self.debounce_period = float(period)
4546
self.latest = - self.debounce_period # Negative latest prevents get from returning true until joystick is presed for the first time
46-
self.timer = Timer
4747
self.enabled = False
4848

4949
def set_debounce_period(self, period):
50-
'''Set number of seconds to hold return value'''
50+
"""Set number of seconds to hold return value"""
5151
self.debounce_period = float(period)
5252

5353
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+
"""
5758

58-
now = self.timer.getFPGATimestamp()
59+
now = wpilib.Timer.getFPGATimestamp()
5960
if now - self.latest < self.debounce_period:
6061
return True
6162

@@ -65,14 +66,15 @@ def get(self):
6566
else:
6667
return False
6768

68-
def __init__(self, joystick, button, debouncePeriod=None):
69+
def __init__(self, joystick: wpilib.Joystick, button: int, debounce_period=None):
6970
"""
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.
7274
"""
7375

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)
7678
else:
7779
self.joystick = joystick
7880
self.joystick.get = partial(self.joystick.getRawButton, button)
@@ -100,11 +102,17 @@ def get(self):
100102

101103
@property
102104
def on(self):
105+
"""
106+
Equates to true if toggle is in the 'on' state
107+
"""
103108
self.get()
104109
return self.state
105110

106111
@property
107112
def off(self):
113+
"""
114+
Equates to true if toggle is in the 'off' state
115+
"""
108116
self.get()
109117
return not self.state
110118

0 commit comments

Comments
 (0)