diff --git a/adafruit_nunchuk.py b/adafruit_nunchuk.py index d5aa77f..661126f 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,50 @@ 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() + + 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..7731e90 100644 --- a/examples/nunchuk_simpletest.py +++ b/examples/nunchuk_simpletest.py @@ -12,8 +12,9 @@ ax, ay, az = nc.acceleration print("joystick = {},{}".format(x, y)) print("accceleration ax={}, ay={}, az={}".format(ax, ay, az)) - if nc.button_C: + + if nc.buttons.C: print("button C") - if nc.button_Z: + if nc.buttons.Z: print("button Z") time.sleep(0.5)