|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2018 Kattni Rembor 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 | +`adafruit_featherwing.joy_featherwing` |
| 24 | +==================================================== |
| 25 | +
|
| 26 | +Helper for using the `Joy FeatherWing <https://www.adafruit.com/product/3632>`_. |
| 27 | +
|
| 28 | +* Author(s): Kattni Rembor |
| 29 | +""" |
| 30 | + |
| 31 | +__version__ = "0.0.0-auto.0" |
| 32 | +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_FeatherWing.git" |
| 33 | + |
| 34 | +import adafruit_seesaw |
| 35 | +from adafruit_featherwing import shared |
| 36 | +from micropython import const |
| 37 | + |
| 38 | +BUTTON_A = const(1 << 6) |
| 39 | +BUTTON_B = const(1 << 7) |
| 40 | +BUTTON_Y = const(1 << 9) |
| 41 | +BUTTON_X = const(1 << 10) |
| 42 | +BUTTON_SELECT = const(1 << 14) |
| 43 | + |
| 44 | + |
| 45 | +class JoyFeatherWing: |
| 46 | + """Class representing an `Adafruit Joy FeatherWing <https://www.adafruit.com/product/3632>`_. |
| 47 | +
|
| 48 | + Automatically uses the feather's I2C bus.""" |
| 49 | + def __init__(self): |
| 50 | + self._seesaw = adafruit_seesaw.Seesaw(shared.I2C_BUS) |
| 51 | + self._seesaw.pin_mode_bulk(BUTTON_A | BUTTON_B | BUTTON_Y | BUTTON_X | BUTTON_SELECT, |
| 52 | + self._seesaw.INPUT_PULLUP) |
| 53 | + |
| 54 | + # Initialise joystick_offset |
| 55 | + self._joystick_offset = (0, 0) |
| 56 | + |
| 57 | + @property |
| 58 | + def button_a(self): |
| 59 | + """Joy featherwing button A. |
| 60 | +
|
| 61 | + .. image :: /_static/joy_featherwing/joy_featherwing_a.jpg |
| 62 | + :alt: Joy FeatherWing Button A |
| 63 | +
|
| 64 | + This example prints when button A is pressed. |
| 65 | +
|
| 66 | + .. code-block:: python |
| 67 | +
|
| 68 | + from adafruit_featherwing import joy_featherwing |
| 69 | + import time |
| 70 | +
|
| 71 | + wing = joy_featherwing.JoyFeatherWing() |
| 72 | +
|
| 73 | + while True: |
| 74 | + if wing.button_a: |
| 75 | + print("Button A pressed!") |
| 76 | +
|
| 77 | + """ |
| 78 | + return self._check_button(BUTTON_A) |
| 79 | + |
| 80 | + @property |
| 81 | + def button_b(self): |
| 82 | + """Joy featherwing button B. |
| 83 | +
|
| 84 | + .. image :: /_static/joy_featherwing/joy_featherwing_b.jpg |
| 85 | + :alt: Joy FeatherWing Button B |
| 86 | +
|
| 87 | + This example prints when button B is pressed. |
| 88 | +
|
| 89 | + .. code-block:: python |
| 90 | +
|
| 91 | + from adafruit_featherwing import joy_featherwing |
| 92 | + import time |
| 93 | +
|
| 94 | + wing = joy_featherwing.JoyFeatherWing() |
| 95 | +
|
| 96 | + while True: |
| 97 | + if wing.button_b: |
| 98 | + print("Button B pressed!") |
| 99 | +
|
| 100 | + """ |
| 101 | + return self._check_button(BUTTON_B) |
| 102 | + |
| 103 | + @property |
| 104 | + def button_x(self): |
| 105 | + """Joy featherwing button X. |
| 106 | +
|
| 107 | + .. image :: /_static/joy_featherwing/joy_featherwing_x.jpg |
| 108 | + :alt: Joy FeatherWing Button X |
| 109 | +
|
| 110 | + This example prints when button X is pressed. |
| 111 | +
|
| 112 | + .. code-block:: python |
| 113 | +
|
| 114 | + from adafruit_featherwing import joy_featherwing |
| 115 | + import time |
| 116 | +
|
| 117 | + wing = joy_featherwing.JoyFeatherWing() |
| 118 | +
|
| 119 | + while True: |
| 120 | + if wing.button_x: |
| 121 | + print("Button X pressed!") |
| 122 | +
|
| 123 | + """ |
| 124 | + return self._check_button(BUTTON_X) |
| 125 | + |
| 126 | + @property |
| 127 | + def button_y(self): |
| 128 | + """Joy featherwing button Y. |
| 129 | +
|
| 130 | + .. image :: /_static/joy_featherwing/joy_featherwing_y.jpg |
| 131 | + :alt: Joy FeatherWing Button Y |
| 132 | +
|
| 133 | + This example prints when button Y is pressed. |
| 134 | +
|
| 135 | + .. code-block:: python |
| 136 | +
|
| 137 | + from adafruit_featherwing import joy_featherwing |
| 138 | + import time |
| 139 | +
|
| 140 | + wing = joy_featherwing.JoyFeatherWing() |
| 141 | +
|
| 142 | + while True: |
| 143 | + if wing.button_y: |
| 144 | + print("Button Y pressed!") |
| 145 | +
|
| 146 | + """ |
| 147 | + return self._check_button(BUTTON_Y) |
| 148 | + |
| 149 | + @property |
| 150 | + def button_select(self): |
| 151 | + """Joy featherwing button SELECT. |
| 152 | +
|
| 153 | + .. image :: /_static/joy_featherwing/joy_featherwing_select.jpg |
| 154 | + :alt: Joy FeatherWing Button SELECT |
| 155 | +
|
| 156 | + This example prints when button SELECT is pressed. |
| 157 | +
|
| 158 | + .. code-block:: python |
| 159 | +
|
| 160 | + from adafruit_featherwing import joy_featherwing |
| 161 | + import time |
| 162 | +
|
| 163 | + wing = joy_featherwing.JoyFeatherWing() |
| 164 | +
|
| 165 | + while True: |
| 166 | + if wing.button_select: |
| 167 | + print("Button SELECT pressed!") |
| 168 | +
|
| 169 | + """ |
| 170 | + return self._check_button(BUTTON_SELECT) |
| 171 | + |
| 172 | + def _check_button(self, button): |
| 173 | + """Utilises the seesaw to determine which button is being pressed.""" |
| 174 | + buttons = self._seesaw.digital_read_bulk(button) |
| 175 | + return not buttons != 0 |
| 176 | + |
| 177 | + @property |
| 178 | + def joystick_offset(self): |
| 179 | + """Offset used to correctly report (0, 0) when the joystick is centered. |
| 180 | +
|
| 181 | + .. image :: /_static/joy_featherwing/joy_featherwing_joystick.jpg |
| 182 | + :alt: Joy FeatherWing Joystick |
| 183 | +
|
| 184 | + Provide a tuple of (x, y) to set your joystick center to (0, 0). |
| 185 | + The offset you provide is subtracted from the current reading. |
| 186 | + For example, if your joystick reads as (-4, 0), you would enter |
| 187 | + (-4, 0) as the offset. The code will subtract -4 from -4, and 0 |
| 188 | + from 0, returning (0, 0). |
| 189 | +
|
| 190 | + This example supplies an offset for zeroing, and prints the |
| 191 | + coordinates of the joystick when it is moved. |
| 192 | +
|
| 193 | + .. code-block:: python |
| 194 | +
|
| 195 | + from adafruit_featherwing import joy_featherwing |
| 196 | + import time |
| 197 | +
|
| 198 | + wing = joy_featherwing.JoyFeatherWing() |
| 199 | + last_x = 0 |
| 200 | + last_y = 0 |
| 201 | +
|
| 202 | + while True: |
| 203 | + wing.joystick_offset = (-4, 0) |
| 204 | + x, y = wing.joystick |
| 205 | + if (abs(x - last_x) > 3) or (abs(y - last_y) > 3): |
| 206 | + last_x = x |
| 207 | + last_y = y |
| 208 | + print(x, y) |
| 209 | + time.sleep(0.01) |
| 210 | +
|
| 211 | + """ |
| 212 | + return self._joystick_offset |
| 213 | + |
| 214 | + @joystick_offset.setter |
| 215 | + def joystick_offset(self, offset): |
| 216 | + self._joystick_offset = offset |
| 217 | + |
| 218 | + def zero_joystick(self): |
| 219 | + """Zeros the joystick by using current reading as (0, 0). |
| 220 | + Note: You must not be touching the joystick at the time of zeroing |
| 221 | + for it to be accurate. |
| 222 | +
|
| 223 | + .. image :: /_static/joy_featherwing/joy_featherwing_joystick.jpg |
| 224 | + :alt: Joy FeatherWing Joystick |
| 225 | +
|
| 226 | + This example zeros the joystick, and prints the coordinates of |
| 227 | + joystick when it is moved. |
| 228 | +
|
| 229 | + .. code-block:: python |
| 230 | +
|
| 231 | + from adafruit_featherwing import joy_featherwing |
| 232 | + import time |
| 233 | +
|
| 234 | + wing = joy_featherwing.JoyFeatherWing() |
| 235 | + last_x = 0 |
| 236 | + last_y = 0 |
| 237 | + wing.zero_joystick() |
| 238 | +
|
| 239 | + while True: |
| 240 | + x, y = wing.joystick |
| 241 | + if (abs(x - last_x) > 3) or (abs(y - last_y) > 3): |
| 242 | + last_x = x |
| 243 | + last_y = y |
| 244 | + print(x, y) |
| 245 | + time.sleep(0.01) |
| 246 | +
|
| 247 | + """ |
| 248 | + self._joystick_offset = (0, 0) |
| 249 | + self._joystick_offset = self.joystick |
| 250 | + |
| 251 | + @property |
| 252 | + def joystick(self): |
| 253 | + """Joy FeatherWing joystick. |
| 254 | +
|
| 255 | + .. image :: /_static/joy_featherwing/joy_featherwing_joystick.jpg |
| 256 | + :alt: Joy FeatherWing Joystick |
| 257 | +
|
| 258 | + This example zeros the joystick, and prints the coordinates of |
| 259 | + joystick when it is moved. |
| 260 | +
|
| 261 | + .. code-block:: python |
| 262 | +
|
| 263 | + from adafruit_featherwing import joy_featherwing |
| 264 | + import time |
| 265 | +
|
| 266 | + wing = joy_featherwing.JoyFeatherWing() |
| 267 | + last_x = 0 |
| 268 | + last_y = 0 |
| 269 | + wing.zero_joystick() |
| 270 | +
|
| 271 | + while True: |
| 272 | + x, y = wing.joystick |
| 273 | + if (abs(x - last_x) > 3) or (abs(y - last_y) > 3): |
| 274 | + last_x = x |
| 275 | + last_y = y |
| 276 | + print(x, y) |
| 277 | + time.sleep(0.01) |
| 278 | + """ |
| 279 | + x = int(127 - self._seesaw.analog_read(2) / 4) - self._joystick_offset[0] |
| 280 | + y = int(self._seesaw.analog_read(3) / 4 - 127) - self._joystick_offset[1] |
| 281 | + return x, y |
0 commit comments