Skip to content

Commit b653d73

Browse files
authored
Merge pull request #20 from caternuson/i2c_speedup
I2C Speedup
2 parents eb5f6fd + 9a02cde commit b653d73

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

adafruit_nunchuk.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,27 @@
3131
__version__ = "0.0.0-auto.0"
3232
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Nunchuk.git"
3333

34-
_DEFAULT_ADDRESS = 0x52
3534
_I2C_INIT_DELAY = 0.1
36-
_I2C_READ_DELAY = 0.01
3735

3836

3937
class Nunchuk:
40-
"""Class which provides interface to Nintendo Nunchuk controller."""
41-
42-
def __init__(self, i2c, address=_DEFAULT_ADDRESS):
43-
self.buffer = bytearray(6)
38+
"""
39+
Class which provides interface to Nintendo Nunchuk controller.
40+
41+
:param i2c: The `busio.I2C` object to use.
42+
:param address: The I2C address of the device. Default is 0x52.
43+
:type address: int, optional
44+
:param i2c_read_delay: The time in seconds to pause between the
45+
I2C write and read. This needs to be at least 200us. A
46+
conservative default of 2000us is used since some hosts may
47+
not be able to achieve such timing.
48+
:type i2c_read_delay: float, optional
49+
"""
50+
51+
def __init__(self, i2c, address=0x52, i2c_read_delay=0.002):
52+
self.buffer = bytearray(8)
4453
self.i2c_device = I2CDevice(i2c, address)
54+
self._i2c_read_delay = i2c_read_delay
4555
time.sleep(_I2C_INIT_DELAY)
4656
with self.i2c_device as i2c_dev:
4757
# turn off encrypted data
@@ -83,9 +93,7 @@ def _read_data(self):
8393

8494
def _read_register(self, address):
8595
with self.i2c_device as i2c:
86-
time.sleep(_I2C_READ_DELAY)
8796
i2c.write(address)
88-
time.sleep(_I2C_READ_DELAY)
97+
time.sleep(self._i2c_read_delay) # at least 200us
8998
i2c.readinto(self.buffer)
90-
time.sleep(_I2C_READ_DELAY)
9199
return self.buffer

0 commit comments

Comments
 (0)