Skip to content

Commit 526bbed

Browse files
committed
Solve mutation of joystick issue
1 parent 1785221 commit 526bbed

File tree

3 files changed

+36
-36
lines changed

3 files changed

+36
-36
lines changed

docs/robotpy_ext.control.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ robotpy_ext.control.toggle module
1515
:members:
1616
:undoc-members:
1717

18-
1918
robotpy_ext.control.xbox_controller module
2019
------------------------------------------
2120

robotpy_ext/control/toggle.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
class Toggle:
77
"""Utility class for joystick button toggle
88
9-
Usage::
9+
Usage::
1010
11-
foo = Toggle(joystick, 3)
11+
foo = Toggle(joystick, 3)
1212
13-
if foo:
14-
toggleFunction()
13+
if foo:
14+
toggleFunction()
1515
16-
if foo.on:
17-
onToggle()
16+
if foo.on:
17+
onToggle()
1818
19-
if foo.off:
20-
offToggle()
19+
if foo.off:
20+
offToggle()
2121
"""
2222
class _SteadyDebounce:
2323
"""
@@ -29,7 +29,7 @@ class _SteadyDebounce:
2929
used with Toggle
3030
"""
3131

32-
def __init__(self, joystick, button, period=0.5):
32+
def __init__(self, joystick: wpilib.Joystick, button: int, period: float):
3333
"""
3434
:param joystick: Joystick object
3535
:type joystick: :class:`wpilib.Joystick`
@@ -46,10 +46,6 @@ def __init__(self, joystick, button, period=0.5):
4646
self.latest = - self.debounce_period # Negative latest prevents get from returning true until joystick is presed for the first time
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):
5450
"""
5551
:returns: The value of the joystick button. Once the button is pressed,
@@ -66,18 +62,18 @@ def get(self):
6662
else:
6763
return False
6864

69-
def __init__(self, joystick: wpilib.Joystick, button: int, debounce_period=None):
65+
def __init__(self, joystick: wpilib.Joystick, button: int, debounce_period: float=None):
7066
"""
7167
:param joystick: :class:`wpilib.Joystick` that contains the button to toggle
7268
: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.
69+
:param debounce_period: Period in seconds to wait before registering a new button press.
7470
"""
7571

7672
if debounce_period is not None:
77-
self.joystick = Toggle._SteadyDebounce(joystick, button, debounce_period)
73+
self.joystickget = Toggle._SteadyDebounce(joystick, button, debounce_period).get
7874
else:
7975
self.joystick = joystick
80-
self.joystick.get = partial(self.joystick.getRawButton, button)
76+
self.joystickget = partial(self.joystick.getRawButton, button)
8177

8278
self.released = False
8379
self.toggle = False
@@ -88,7 +84,7 @@ def get(self):
8884
:return: State of toggle
8985
:rtype: bool
9086
"""
91-
current_state = self.joystick.get()
87+
current_state = self.joystickget()
9288

9389
if current_state and not self.released:
9490
self.released = True

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)