From 2c7e43fa94585750333a0020565af7124e19808f Mon Sep 17 00:00:00 2001 From: Liz Date: Fri, 5 May 2023 09:37:20 -0400 Subject: [PATCH 1/3] Adding delay for third-party nunchuks --- adafruit_nunchuk.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/adafruit_nunchuk.py b/adafruit_nunchuk.py index f6b7845..380231e 100644 --- a/adafruit_nunchuk.py +++ b/adafruit_nunchuk.py @@ -60,8 +60,17 @@ class Nunchuk: def __init__( self, i2c: I2C, address: int = 0x52, i2c_read_delay: float = 0.002 ) -> None: - self.buffer = bytearray(8) - self.i2c_device = I2CDevice(i2c, address) + for i in range(10): + try: + self.buffer = bytearray(8) + self.i2c_device = I2CDevice(i2c, address) + except ValueError: + # boot up delay for third-party controllers + time.sleep(1) + if i < 10: + continue + raise + break self._i2c_read_delay = i2c_read_delay time.sleep(_I2C_INIT_DELAY) with self.i2c_device as i2c_dev: From cd34338b4e9ca61ad6cc5047731769df1c4358b1 Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 9 May 2023 12:29:58 -0400 Subject: [PATCH 2/3] updating fix to use i2c.scan --- adafruit_nunchuk.py | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/adafruit_nunchuk.py b/adafruit_nunchuk.py index 380231e..e330135 100644 --- a/adafruit_nunchuk.py +++ b/adafruit_nunchuk.py @@ -60,17 +60,15 @@ class Nunchuk: def __init__( self, i2c: I2C, address: int = 0x52, i2c_read_delay: float = 0.002 ) -> None: - for i in range(10): - try: - self.buffer = bytearray(8) - self.i2c_device = I2CDevice(i2c, address) - except ValueError: - # boot up delay for third-party controllers - time.sleep(1) - if i < 10: - continue - raise - break + self.buffer = bytearray(8) + #-| HACK |--------------------------------------------------- + # fixes quirk with RP2040 + 3rd party controllers + while not i2c.try_lock(): + pass + _ = i2c.scan() + i2c.unlock() + #------------------------------------------------------------ + self.i2c_device = I2CDevice(i2c, address) self._i2c_read_delay = i2c_read_delay time.sleep(_I2C_INIT_DELAY) with self.i2c_device as i2c_dev: From e91d4841273bcc59bd7cdfb94374a4c8fb8b850f Mon Sep 17 00:00:00 2001 From: Liz Date: Tue, 9 May 2023 12:35:08 -0400 Subject: [PATCH 3/3] black --- adafruit_nunchuk.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_nunchuk.py b/adafruit_nunchuk.py index e330135..1e3ec33 100644 --- a/adafruit_nunchuk.py +++ b/adafruit_nunchuk.py @@ -61,13 +61,13 @@ def __init__( self, i2c: I2C, address: int = 0x52, i2c_read_delay: float = 0.002 ) -> None: self.buffer = bytearray(8) - #-| HACK |--------------------------------------------------- + # -| HACK |--------------------------------------------------- # fixes quirk with RP2040 + 3rd party controllers while not i2c.try_lock(): pass _ = i2c.scan() i2c.unlock() - #------------------------------------------------------------ + # ------------------------------------------------------------ self.i2c_device = I2CDevice(i2c, address) self._i2c_read_delay = i2c_read_delay time.sleep(_I2C_INIT_DELAY)