|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2020 Jeff Epler for Adafruit Industries LLC |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +""" |
| 23 | +Convert an analog joystick to digital |
| 24 | +""" |
| 25 | + |
| 26 | +import analogio |
| 27 | +import board |
| 28 | + |
| 29 | +class AnalogJoystick: |
| 30 | + """Convert an analog joystick to digital""" |
| 31 | + def __init__(self, pin_x=None, pin_y=None, x_invert=False, y_invert=True, deadzone=8000): |
| 32 | + self._x = analogio.AnalogIn(pin_x or board.JOYSTICK_X) |
| 33 | + self._y = analogio.AnalogIn(pin_y or board.JOYSTICK_Y) |
| 34 | + self.x_invert = x_invert |
| 35 | + self.y_invert = y_invert |
| 36 | + self.deadzone = deadzone |
| 37 | + self.recenter() |
| 38 | + self.poll() |
| 39 | + |
| 40 | + def poll(self): |
| 41 | + """Read the analog values and update the digital outputs""" |
| 42 | + self.x = (self._x.value - self.x_center) * (-1 if self.x_invert else 1) |
| 43 | + self.y = (self._y.value - self.y_center) * (-1 if self.y_invert else 1) |
| 44 | + return [self.up, self.down, self.left, self.right] |
| 45 | + |
| 46 | + # pylint: disable=invalid-name |
| 47 | + @property |
| 48 | + def up(self): |
| 49 | + """Return true when the stick was pressed up at the last poll""" |
| 50 | + return self.y > self.deadzone |
| 51 | + # pylint: enable=invalid-name |
| 52 | + |
| 53 | + @property |
| 54 | + def down(self): |
| 55 | + """Return true when the stick was pressed down at the last poll""" |
| 56 | + return self.y < -self.deadzone |
| 57 | + |
| 58 | + @property |
| 59 | + def left(self): |
| 60 | + """Return true when the stick was pressed left at the last poll""" |
| 61 | + return self.x < -self.deadzone |
| 62 | + |
| 63 | + @property |
| 64 | + def right(self): |
| 65 | + """Return true when the stick was pressed right at the last poll""" |
| 66 | + return self.x > self.deadzone |
| 67 | + |
| 68 | + def recenter(self): |
| 69 | + """Use the current position of the analog joystick as the center""" |
| 70 | + self.x_center = self._x.value |
| 71 | + self.y_center = self._y.value |
0 commit comments