Skip to content

Commit 45ce3e4

Browse files
committed
Added comement to simpletest
1 parent 2ea01f8 commit 45ce3e4

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

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)