Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added comment to simpletest #11

Merged
merged 2 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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