Skip to content

Commit 8a3444d

Browse files
authored
Merge pull request #101 from twinters007/toggle
Fixup and add toggle to docs
2 parents df9fe94 + 526bbed commit 8a3444d

File tree

4 files changed

+57
-42
lines changed

4 files changed

+57
-42
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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ 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:
1117

1218
robotpy_ext.control.xbox_controller module
1319
------------------------------------------

robotpy_ext/control/toggle.py

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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
1011
foo = Toggle(joystick, 3)
1112
@@ -19,43 +20,39 @@ class Toggle:
1920
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

31-
def __init__(self, joystick, button, period=0.5):
32-
'''
32+
def __init__(self, joystick: wpilib.Joystick, button: int, period: float):
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

49-
def set_debounce_period(self, period):
50-
'''Set number of seconds to hold return value'''
51-
self.debounce_period = float(period)
52-
5349
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+
"""
5754

58-
now = self.timer.getFPGATimestamp()
55+
now = wpilib.Timer.getFPGATimestamp()
5956
if now - self.latest < self.debounce_period:
6057
return True
6158

@@ -65,17 +62,18 @@ def get(self):
6562
else:
6663
return False
6764

68-
def __init__(self, joystick, button, debouncePeriod=None):
65+
def __init__(self, joystick: wpilib.Joystick, button: int, debounce_period: float=None):
6966
"""
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.
7270
"""
7371

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
7674
else:
7775
self.joystick = joystick
78-
self.joystick.get = partial(self.joystick.getRawButton, button)
76+
self.joystickget = partial(self.joystick.getRawButton, button)
7977

8078
self.released = False
8179
self.toggle = False
@@ -86,7 +84,7 @@ def get(self):
8684
:return: State of toggle
8785
:rtype: bool
8886
"""
89-
current_state = self.joystick.get()
87+
current_state = self.joystickget()
9088

9189
if current_state and not self.released:
9290
self.released = True
@@ -100,11 +98,17 @@ def get(self):
10098

10199
@property
102100
def on(self):
101+
"""
102+
Equates to true if toggle is in the 'on' state
103+
"""
103104
self.get()
104105
return self.state
105106

106107
@property
107108
def off(self):
109+
"""
110+
Equates to true if toggle is in the 'off' state
111+
"""
108112
self.get()
109113
return not self.state
110114

tests/test_toggle.py

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,48 @@
22
from robotpy_ext.misc.precise_delay import PreciseDelay
33
class FakeJoystick:
44
def __init__(self):
5-
self._pressed = False
6-
5+
self._pressed = [False] * 2
6+
77
def getRawButton(self, num):
8-
return self._pressed
8+
return self._pressed[num]
99

10-
def press(self):
11-
self._pressed = True
10+
def press(self, num):
11+
self._pressed[num] = True
1212

13-
def release(self):
14-
self._pressed = False
13+
def release(self, num):
14+
self._pressed[num] = False
1515

1616
def test_toggle():
1717
joystick = FakeJoystick()
18-
toggleButton = Toggle(joystick, 1)
18+
toggleButton = Toggle(joystick, 0)
19+
toggleButton2 = Toggle(joystick, 1)
1920
assert toggleButton.off
20-
joystick.press()
21+
joystick.press(0)
2122
assert toggleButton.on
22-
joystick.release()
23+
assert toggleButton2.off
24+
joystick.release(0)
2325
assert toggleButton.on
24-
joystick.press()
26+
joystick.press(0)
27+
assert toggleButton.off
28+
joystick.release(0)
2529
assert toggleButton.off
26-
joystick.release()
30+
joystick.press(1)
2731
assert toggleButton.off
32+
assert toggleButton2.on
2833

2934

3035
def test_toggle_debounce():
3136
delay = PreciseDelay(2.1)
3237
joystick = FakeJoystick()
3338
toggleButton = Toggle(joystick, 1, 2)
3439
assert toggleButton.off
35-
joystick.press()
40+
joystick.press(1)
3641
assert toggleButton.on
37-
joystick.release()
38-
joystick.press()
39-
joystick.release()
42+
joystick.release(1)
43+
joystick.press(1)
44+
joystick.release(1)
4045
assert toggleButton.on
4146
delay.wait()
4247
assert toggleButton.on
43-
joystick.press()
48+
joystick.press(1)
4449
assert toggleButton.off

0 commit comments

Comments
 (0)