Skip to content

Commit 7b49cff

Browse files
committed
Joy FeatherWing library w/ examples, docs, images
1 parent bd7705b commit 7b49cff

File tree

10 files changed

+313
-1
lines changed

10 files changed

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

api.rst

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
44
.. automodule:: adafruit_featherwing.motor_featherwing
55
.. automodule:: adafruit_featherwing.ina219_featherwing
6+
.. automodule:: adafruit_featherwing.joy_featherwing
67
:members:

conf.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
# Uncomment the below if you use native CircuitPython modules such as
1919
# digitalio, micropython and busio. List the modules you use. Without it, the
2020
# autodoc module docs will fail to generate with a warning.
21-
autodoc_mock_imports = ["adafruit_motor", "adafruit_pca9685", "board", "busio", "adafruit_ina219"]
21+
autodoc_mock_imports = ["adafruit_motor", "adafruit_pca9685", "board", "busio", "adafruit_ina219",
22+
"adafruit_seesaw", "micropython"]
2223

2324
intersphinx_mapping = {
2425
'python': ('https://docs.python.org/3.4', None),

examples/joy_featherwing/joy.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""This example zeros the joystick, and prints when the joystick moves
2+
or the buttons are pressed."""
3+
import time
4+
from adafruit_featherwing import joy_featherwing
5+
6+
wing = joy_featherwing.JoyFeatherWing()
7+
last_x = 0
8+
last_y = 0
9+
10+
while True:
11+
x, y = wing.joystick
12+
if (abs(x - last_x) > 3) or (abs(y - last_y) > 3):
13+
last_x = x
14+
last_y = y
15+
print(x, y)
16+
if wing.button_a:
17+
print("Button A!")
18+
if wing.button_b:
19+
print("Button B!")
20+
if wing.button_x:
21+
print("Button X!")
22+
if wing.button_y:
23+
print("Button Y!")
24+
if wing.button_select:
25+
print("Button SELECT!")
26+
time.sleep(.01)

0 commit comments

Comments
 (0)