Skip to content

Commit

Permalink
[BREAKING CHANGE] server/controller/sticks: Actually match the JavaSc…
Browse files Browse the repository at this point in the history
…ript Gamepad API (#6)

* server/controller/sticks: Actually match the JavaScript Gamepad API.
UP: s l v -1
DOWN: s l v 1
  • Loading branch information
juharris authored May 22, 2020
1 parent 083ac08 commit 612640a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
11 changes: 10 additions & 1 deletion server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ Example: move the right stick to the left with `'s r left'` and then set it back
For moving by a specific amount: `s <stick> <direction> <amount>`
* stick: left: `l` or right: `r`
* direction: horizontal: `h` or vertical: `v`
* amount: `max`, `min`, `center`, or a decimal number between `-1.0` and `1.0` (down to up or left to right - set up to match the [Gamepad API](https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/))
* amount:
* For your convenience with writing easy to read macros/code:
* UP/RIGHT: `max`
* DOWN/LEFT: `min`
* back to the calibrated center: `center`
* For convenience with matching the [JavaScript Gamepad API](https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/)
a decimal number between `-1.0` and `1.0`:
* UP/LEFT: `-1.0` (yes `-1.0` is UP, this is how the Gamepad API works)
* DOWN/RIGHT: `1.0`
* back to the calibrated center: `0`

Examples:
* Push the left stick up all the way: `'s l v max'`, then let go of it and set it back to it's center position with: `'s l v center'`.
Expand Down
14 changes: 8 additions & 6 deletions server/switchremoteplay/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ def _map_h_val(self, stick, value):
val = stick.get_calibration().h_center
else:
try:
min_val = stick.get_calibration().h_center - stick.get_calibration().h_max_below_center
max_val = stick.get_calibration().h_center + stick.get_calibration().h_max_above_center
val = SwitchController._map_val(float(value), min_val, max_val)
# LEFT: -1, RIGHT: +1
left_val = stick.get_calibration().h_center - stick.get_calibration().h_max_below_center
right_val = stick.get_calibration().h_center + stick.get_calibration().h_max_above_center
val = SwitchController._map_val(float(value), left_val, right_val)
except ValueError:
raise ValueError(f'Unexpected stick value "{value}"')
return val
Expand All @@ -74,9 +75,10 @@ def _map_v_val(self, stick, value):
val = stick.get_calibration().v_center
else:
try:
min_val = stick.get_calibration().v_center - stick.get_calibration().v_max_below_center
max_val = stick.get_calibration().v_center + stick.get_calibration().v_max_above_center
val = SwitchController._map_val(float(value), min_val, max_val)
# Match the JavaScript Gamepad API: UP: -1, DOWN: +1
down_val = stick.get_calibration().v_center - stick.get_calibration().v_max_below_center
up_val = stick.get_calibration().v_center + stick.get_calibration().v_max_above_center
val = SwitchController._map_val(float(value), up_val, down_val)
except ValueError:
raise ValueError(f'Unexpected stick value "{value}"')
return val
Expand Down
17 changes: 13 additions & 4 deletions server/switchremoteplay/tests/test_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
class TestSwitchController(unittest.TestCase):
def test_map_val(self):
min_val = 0
max_val = 0x1000
max_val = 0x1000 - 1
# Mapping the horizontal axis: left (-1) to right (+1)
assert SwitchController._map_val(-1, min_val, max_val) == min_val
assert SwitchController._map_val(+1, min_val, max_val) == max_val
assert SwitchController._map_val(0, min_val, max_val) == (min_val + max_val) / 2
assert SwitchController._map_val(-0.5, min_val, max_val) == (min_val + max_val) / 4
assert SwitchController._map_val(+0.5, min_val, max_val) == (min_val + max_val) * 3 / 4
self.assertEqual(SwitchController._map_val(0, min_val, max_val), int((min_val + max_val) / 2))
assert SwitchController._map_val(-0.5, min_val, max_val) == int((min_val + max_val) / 4)
assert SwitchController._map_val(+0.5, min_val, max_val) == int((min_val + max_val) * 3 / 4)

# Mapping the vertical axis: up (-1) to down (+1)
assert SwitchController._map_val(-1, max_val, min_val) == max_val
assert SwitchController._map_val(+1, max_val, min_val) == min_val
assert SwitchController._map_val(0, max_val, min_val) == int((min_val + max_val) / 2)
assert SwitchController._map_val(-0.5, max_val, min_val) == int((min_val + max_val) * 3 / 4)
assert SwitchController._map_val(+0.5, max_val, min_val) == int((min_val + max_val) / 4)

0 comments on commit 612640a

Please sign in to comment.