Skip to content

update read speeds when using the properties - fix #2 #18

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

Closed
wants to merge 1 commit into from
Closed
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
12 changes: 9 additions & 3 deletions adafruit_nunchuk.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
_DEFAULT_ADDRESS = 0x52
_I2C_INIT_DELAY = 0.1
_I2C_READ_DELAY = 0.01
_I2C_BUFFER_UPDATE_DELAY = 0.03


class Nunchuk:
Expand All @@ -67,6 +68,9 @@ def __init__(self, i2c, address=_DEFAULT_ADDRESS):
time.sleep(_I2C_INIT_DELAY)
i2c_dev.write(b"\xFB\x00")

# set to 0 to force read for intial
self.last_updated = 0

@property
def joystick(self):
"""Return tuple of current joystick position."""
Expand Down Expand Up @@ -96,13 +100,15 @@ def acceleration(self):
return x, y, z

def _read_data(self):
return self._read_register(b"\x00")
if (time.monotonic() - self.last_updated) > _I2C_BUFFER_UPDATE_DELAY:
self.last_updated = time.monotonic()
self._read_register(b"\x00")

return self.buffer

def _read_register(self, address):
with self.i2c_device as i2c:
time.sleep(_I2C_READ_DELAY)
i2c.write(address)
time.sleep(_I2C_READ_DELAY)
i2c.readinto(self.buffer)
time.sleep(_I2C_READ_DELAY)
return self.buffer