Skip to content

Commit 388ad15

Browse files
authored
Merge pull request #11 from fourstix/b_fix_connection
Added comment to simpletest
2 parents 2ea01f8 + 89b6be2 commit 388ad15

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

README.rst

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,24 @@ Usage Example
157157
# use QwiicJoystick(i2c, address) for a different address
158158
# joystick = QwiicJoystick(i2c, 0x21)"""
159159
160+
Upgrading
161+
=========
162+
On supported GNU/Linux systems like the Raspberry Pi, you can upgrade the driver locally `from
163+
PyPI <https://pypi.org/project/Sparkfun-circuitpython-qwiicjoystick/>`_.
164+
165+
To upgrade for current user:
166+
167+
.. code-block:: shell
168+
169+
pip3 install --upgrade sparkfun-circuitpython-qwiicjoystick
170+
171+
To upgrade system-wide (this may be required in some cases):
172+
173+
.. code-block:: shell
174+
175+
sudo pip3 install --upgrade sparkfun-circuitpython-qwiicjoystick
176+
177+
160178
Contributing
161179
============
162180

examples/qwiicjoystick_simpletest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636

3737
print("Press Joystick button to exit program.")
3838

39+
# joystick.button goes to 0 when pressed
3940
while joystick.button == 1:
4041
print("X = " + str(joystick.horizontal) + " Y = " + str(joystick.vertical))
4142
sleep(0.200) # sleep a bit to slow down messages

sparkfun_qwiicjoystick.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,12 @@ def __init__(self, i2c, address=QWIIC_JOYSTICK_ADDR, debug=False):
110110
@property
111111
def connected(self):
112112
"""True if the Joystick is connected and a valid id is successful read."""
113-
if self._read_register(_JOYSTICK_ID) != QWIIC_JOYSTICK_ADDR:
113+
try:
114+
# Attempt to read the id and see if we get an error
115+
self._read_register(_JOYSTICK_ID)
116+
except ValueError:
114117
return False
118+
115119
return True
116120

117121
@property
@@ -195,8 +199,11 @@ def set_i2c_address(self, new_address):
195199
def _read_register(self, addr):
196200
# Read and return a byte from the specified 8-bit register address.
197201
with self._device as device:
202+
device.write(bytes([addr & 0xFF]))
198203
result = bytearray(1)
199-
device.write_then_readinto(bytes([addr & 0xFF]), result)
204+
device.readinto(result)
205+
# For some reason, write_then_readinto returns invalid data
206+
# device.write_then_readinto(bytes([addr & 0xFF]), result)
200207
if self._debug:
201208
print("$%02X => %s" % (addr, [hex(i) for i in result]))
202209
return result[0]

0 commit comments

Comments
 (0)