|
31 | 31 | __version__ = "0.0.0-auto.0"
|
32 | 32 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Nunchuk.git"
|
33 | 33 |
|
34 |
| -_DEFAULT_ADDRESS = 0x52 |
35 | 34 | _I2C_INIT_DELAY = 0.1
|
36 |
| -_I2C_READ_DELAY = 0.01 |
37 | 35 |
|
38 | 36 |
|
39 | 37 | 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) |
44 | 53 | self.i2c_device = I2CDevice(i2c, address)
|
| 54 | + self._i2c_read_delay = i2c_read_delay |
45 | 55 | time.sleep(_I2C_INIT_DELAY)
|
46 | 56 | with self.i2c_device as i2c_dev:
|
47 | 57 | # turn off encrypted data
|
@@ -83,9 +93,7 @@ def _read_data(self):
|
83 | 93 |
|
84 | 94 | def _read_register(self, address):
|
85 | 95 | with self.i2c_device as i2c:
|
86 |
| - time.sleep(_I2C_READ_DELAY) |
87 | 96 | i2c.write(address)
|
88 |
| - time.sleep(_I2C_READ_DELAY) |
| 97 | + time.sleep(self._i2c_read_delay) # at least 200us |
89 | 98 | i2c.readinto(self.buffer)
|
90 |
| - time.sleep(_I2C_READ_DELAY) |
91 | 99 | return self.buffer
|
0 commit comments