From f88f6534c940dd3e0ec6d780dd16a132603f59eb Mon Sep 17 00:00:00 2001 From: John Furcean Date: Tue, 26 Jan 2021 14:11:26 -0500 Subject: [PATCH 1/3] refactor and add values property --- adafruit_nunchuk.py | 76 ++++++++++++++++++++++++-------- docs/conf.py | 2 +- examples/nunchuk_accel_mouse.py | 6 ++- examples/nunchuk_analog_mouse.py | 6 ++- examples/nunchuk_mouse.py | 25 ----------- examples/nunchuk_simpletest.py | 15 ++++--- 6 files changed, 76 insertions(+), 54 deletions(-) delete mode 100644 examples/nunchuk_mouse.py diff --git a/adafruit_nunchuk.py b/adafruit_nunchuk.py index d5aa77f..4cf2e41 100644 --- a/adafruit_nunchuk.py +++ b/adafruit_nunchuk.py @@ -26,6 +26,7 @@ * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice """ import time +from collections import namedtuple from adafruit_bus_device.i2c_device import I2CDevice __version__ = "0.0.0-auto.0" @@ -48,6 +49,11 @@ class Nunchuk: :type i2c_read_delay: float, optional """ + _Values = namedtuple("Values", ("joystick", "buttons", "acceleration")) + _Joystick = namedtuple("Joystick", ("x", "y")) + _Buttons = namedtuple("Buttons", ("C", "Z")) + _Acceleration = namedtuple("Acceleration", ("x", "y", "z")) + def __init__(self, i2c, address=0x52, i2c_read_delay=0.002): self.buffer = bytearray(8) self.i2c_device = I2CDevice(i2c, address) @@ -61,32 +67,66 @@ def __init__(self, i2c, address=0x52, i2c_read_delay=0.002): i2c_dev.write(b"\xFB\x00") @property - def joystick(self): - """Return tuple of current joystick position.""" + def values(self): + """The current state of all values.""" self._read_data() - return self.buffer[0], self.buffer[1] + return self._Values( + self._joystick(do_read=False), + self._buttons(do_read=False), + self._acceleration(do_read=False), + ) @property - def button_C(self): # pylint: disable=invalid-name - """Return current pressed state of button C.""" - return not bool(self._read_data()[5] & 0x02) + def joystick(self): + """The current joystick position.""" + return self._joystick() @property - def button_Z(self): # pylint: disable=invalid-name - """Return current pressed state of button Z.""" - return not bool(self._read_data()[5] & 0x01) + def buttons(self): # pylint: disable=invalid-name + """The current pressed state of button Z.""" + return self._buttons() @property def acceleration(self): - """Return 3 tuple of accelerometer reading.""" - self._read_data() - x = (self.buffer[5] & 0xC0) >> 6 - x |= self.buffer[2] << 2 - y = (self.buffer[5] & 0x30) >> 4 - y |= self.buffer[3] << 2 - z = (self.buffer[5] & 0x0C) >> 2 - z |= self.buffer[4] << 2 - return x, y, z + """The current accelerometer reading.""" + return self._acceleration() + + @property + def button_C(self): # pylint: disable=invalid-name + """ + The current pressed state of button C. + """ + print("`button_C` is deprecated. Please use the `buttons` property instead") + return self._buttons().C + + @property + def button_Z(self): # pylint: disable=invalid-name + """ + The current pressed state of button Z. + """ + print("`button_Z` is deprecated. Please use the `buttons` property instead") + return self._buttons().Z + + def _joystick(self, do_read=True): + if do_read: + self._read_data() + return self._Joystick(self.buffer[0], self.buffer[1]) # x, y + + def _buttons(self, do_read=True): + if do_read: + self._read_data() + return self._Buttons( + not bool(self.buffer[5] & 0x02), not bool(self.buffer[5] & 0x01) # C # Z + ) + + def _acceleration(self, do_read=True): + if do_read: + self._read_data() + return self._Acceleration( + ((self.buffer[5] & 0xC0) >> 6) | (self.buffer[2] << 2), # ax + ((self.buffer[5] & 0x30) >> 4) | (self.buffer[3] << 2), # ay + ((self.buffer[5] & 0x0C) >> 2) | (self.buffer[4] << 2), # az + ) def _read_data(self): return self._read_register(b"\x00") diff --git a/docs/conf.py b/docs/conf.py index d9f48ad..d3eb015 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -25,7 +25,7 @@ # Uncomment the below if you use native CircuitPython modules such as # digitalio, micropython and busio. List the modules you use. Without it, the # autodoc module docs will fail to generate with a warning. -# autodoc_mock_imports = ["digitalio", "busio"] +autodoc_mock_imports = ["adafruit_bus_device"] intersphinx_mapping = { diff --git a/examples/nunchuk_accel_mouse.py b/examples/nunchuk_accel_mouse.py index de5691a..c386590 100644 --- a/examples/nunchuk_accel_mouse.py +++ b/examples/nunchuk_accel_mouse.py @@ -27,6 +27,7 @@ # print((0 if nc.button_C else 1, 0 if nc.button_Z else 1)) while True: + accel = nc.acceleration # print(accel) # x, y = nc.joystick @@ -41,9 +42,10 @@ relY = y - centerY m.move(int(scaleX * relX), int(scaleY * relY), 0) + buttons = nc.buttons - c = nc.button_C - z = nc.button_Z + c = buttons.C + z = buttons.Z if z and not zDown: stillDown = True diff --git a/examples/nunchuk_analog_mouse.py b/examples/nunchuk_analog_mouse.py index 7f4f751..f26295c 100644 --- a/examples/nunchuk_analog_mouse.py +++ b/examples/nunchuk_analog_mouse.py @@ -27,6 +27,7 @@ # print((0 if nc.button_C else 1, 0 if nc.button_Z else 1)) while True: + x, y = nc.joystick # Eliminate spurious reads if x == 255 or y == 255: @@ -36,8 +37,9 @@ m.move(int(scaleX * relX), int(scaleY * relY), 0) - c = nc.button_C - z = nc.button_Z + buttons = nc.buttons + c = buttons.C + z = buttons.Z if z and not zDown: stillDown = True diff --git a/examples/nunchuk_mouse.py b/examples/nunchuk_mouse.py deleted file mode 100644 index a054f6c..0000000 --- a/examples/nunchuk_mouse.py +++ /dev/null @@ -1,25 +0,0 @@ -# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries -# SPDX-License-Identifier: MIT - -import board -import usb_hid -from adafruit_hid.mouse import Mouse -import adafruit_nunchuk - -THRESHOLD = 10 - -m = Mouse(usb_hid.devices) -nc = adafruit_nunchuk.Nunchuk(board.I2C()) - -while True: - x, y = nc.joystick - x = (x - 128) // 2 - y = (128 - y) // 2 - if abs(x) > THRESHOLD: - m.move(x, 0, 0) - if abs(y) > THRESHOLD: - m.move(0, y, 0) - if nc.button_Z: - m.click(Mouse.LEFT_BUTTON) - if nc.button_C: - m.click(Mouse.RIGHT_BUTTON) diff --git a/examples/nunchuk_simpletest.py b/examples/nunchuk_simpletest.py index db643fe..5ecfb9b 100644 --- a/examples/nunchuk_simpletest.py +++ b/examples/nunchuk_simpletest.py @@ -8,12 +8,15 @@ nc = adafruit_nunchuk.Nunchuk(board.I2C()) while True: - x, y = nc.joystick - ax, ay, az = nc.acceleration - print("joystick = {},{}".format(x, y)) - print("accceleration ax={}, ay={}, az={}".format(ax, ay, az)) - if nc.button_C: + joystick, buttons, acceleration = nc.values + print("joystick = {},{}".format(joystick.x, joystick.y)) + print( + "accceleration ax={}, ay={}, az={}".format( + acceleration.x, acceleration.y, acceleration.z + ) + ) + if buttons.C: print("button C") - if nc.button_Z: + if buttons.Z: print("button Z") time.sleep(0.5) From e548db1dbffe5951502ed3152a577a83a5ac1992 Mon Sep 17 00:00:00 2001 From: John Furcean Date: Fri, 29 Jan 2021 16:52:23 -0500 Subject: [PATCH 2/3] remove button_Z button_C properties and fix nunchuk_simpletest.py --- adafruit_nunchuk.py | 16 ---------------- examples/nunchuk_simpletest.py | 13 ++++++------- 2 files changed, 6 insertions(+), 23 deletions(-) diff --git a/adafruit_nunchuk.py b/adafruit_nunchuk.py index 4cf2e41..661126f 100644 --- a/adafruit_nunchuk.py +++ b/adafruit_nunchuk.py @@ -91,22 +91,6 @@ def acceleration(self): """The current accelerometer reading.""" return self._acceleration() - @property - def button_C(self): # pylint: disable=invalid-name - """ - The current pressed state of button C. - """ - print("`button_C` is deprecated. Please use the `buttons` property instead") - return self._buttons().C - - @property - def button_Z(self): # pylint: disable=invalid-name - """ - The current pressed state of button Z. - """ - print("`button_Z` is deprecated. Please use the `buttons` property instead") - return self._buttons().Z - def _joystick(self, do_read=True): if do_read: self._read_data() diff --git a/examples/nunchuk_simpletest.py b/examples/nunchuk_simpletest.py index 5ecfb9b..8ebe6fb 100644 --- a/examples/nunchuk_simpletest.py +++ b/examples/nunchuk_simpletest.py @@ -8,13 +8,12 @@ nc = adafruit_nunchuk.Nunchuk(board.I2C()) while True: - joystick, buttons, acceleration = nc.values - print("joystick = {},{}".format(joystick.x, joystick.y)) - print( - "accceleration ax={}, ay={}, az={}".format( - acceleration.x, acceleration.y, acceleration.z - ) - ) + x, y = nc.joystick + ax, ay, az = nc.acceleration + print("joystick = {},{}".format(x, y)) + print("accceleration ax={}, ay={}, az={}".format(ax, ay, az)) + + buttons = nc.buttons if buttons.C: print("button C") if buttons.Z: From faba1247d3604816d5bbbff9cb35cfce733fff18 Mon Sep 17 00:00:00 2001 From: John Furcean Date: Fri, 29 Jan 2021 17:36:05 -0500 Subject: [PATCH 3/3] clean up nunchuk_simpletest.py --- examples/nunchuk_simpletest.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/examples/nunchuk_simpletest.py b/examples/nunchuk_simpletest.py index 8ebe6fb..7731e90 100644 --- a/examples/nunchuk_simpletest.py +++ b/examples/nunchuk_simpletest.py @@ -13,9 +13,8 @@ print("joystick = {},{}".format(x, y)) print("accceleration ax={}, ay={}, az={}".format(ax, ay, az)) - buttons = nc.buttons - if buttons.C: + if nc.buttons.C: print("button C") - if buttons.Z: + if nc.buttons.Z: print("button Z") time.sleep(0.5)