Skip to content

Commit

Permalink
Merge pull request #11 from fourstix/b_fix_connection
Browse files Browse the repository at this point in the history
Added comment to simpletest
  • Loading branch information
fourstix authored Nov 14, 2021
2 parents 2ea01f8 + 89b6be2 commit 388ad15
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
18 changes: 18 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ Usage Example
# use QwiicJoystick(i2c, address) for a different address
# joystick = QwiicJoystick(i2c, 0x21)"""
Upgrading
=========
On supported GNU/Linux systems like the Raspberry Pi, you can upgrade the driver locally `from
PyPI <https://pypi.org/project/Sparkfun-circuitpython-qwiicjoystick/>`_.

To upgrade for current user:

.. code-block:: shell
pip3 install --upgrade sparkfun-circuitpython-qwiicjoystick
To upgrade system-wide (this may be required in some cases):

.. code-block:: shell
sudo pip3 install --upgrade sparkfun-circuitpython-qwiicjoystick
Contributing
============

Expand Down
1 change: 1 addition & 0 deletions examples/qwiicjoystick_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

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

# joystick.button goes to 0 when pressed
while joystick.button == 1:
print("X = " + str(joystick.horizontal) + " Y = " + str(joystick.vertical))
sleep(0.200) # sleep a bit to slow down messages
Expand Down
11 changes: 9 additions & 2 deletions sparkfun_qwiicjoystick.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,12 @@ def __init__(self, i2c, address=QWIIC_JOYSTICK_ADDR, debug=False):
@property
def connected(self):
"""True if the Joystick is connected and a valid id is successful read."""
if self._read_register(_JOYSTICK_ID) != QWIIC_JOYSTICK_ADDR:
try:
# Attempt to read the id and see if we get an error
self._read_register(_JOYSTICK_ID)
except ValueError:
return False

return True

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

0 comments on commit 388ad15

Please sign in to comment.