From 34f7059fb719cc59dfa07a3d1e1914ebc3f014fa Mon Sep 17 00:00:00 2001 From: John Furcean Date: Fri, 15 Jan 2021 23:34:04 -0500 Subject: [PATCH 01/16] covert to module, fix up examples, optimize read_data, add classic_controller --- adafruit_nunchuk/__init__.py | 65 ++++++++ adafruit_nunchuk/classic_controller.py | 145 ++++++++++++++++++ .../nunchuk.py | 37 ++--- examples/classic_controller_simpletest.py | 53 +++++++ examples/nunchuk_accel_mouse.py | 7 +- examples/nunchuk_analog_mouse.py | 7 +- examples/nunchuk_mouse.py | 7 +- examples/nunchuk_simpletest.py | 2 +- 8 files changed, 285 insertions(+), 38 deletions(-) create mode 100644 adafruit_nunchuk/__init__.py create mode 100644 adafruit_nunchuk/classic_controller.py rename adafruit_nunchuk.py => adafruit_nunchuk/nunchuk.py (66%) create mode 100644 examples/classic_controller_simpletest.py diff --git a/adafruit_nunchuk/__init__.py b/adafruit_nunchuk/__init__.py new file mode 100644 index 0000000..4bfe2b0 --- /dev/null +++ b/adafruit_nunchuk/__init__.py @@ -0,0 +1,65 @@ +""" +`adafruit_nunchuk` +================================================================================ +Base Library for the Nunchuck Style libraries. +* Author(s): John Furcean + +Implementation Notes +-------------------- + +**Hardware:** + +* `Wii Remote Nunchuk `_ +* `Wiichuck `_ + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the supported boards: + https://github.com/adafruit/circuitpython/releases +* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice +""" +import time +from adafruit_bus_device.i2c_device import I2CDevice + +__version__ = "0.0.0-auto.0" +__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_PortalBase.git" + +_DEFAULT_ADDRESS = 0x52 +_I2C_INIT_DELAY = 0.1 +_I2C_READ_DELAY = 0.01 +_I2C_BUFFER_UPDATE_DELAY = .05 + + +class NunchukBase: + """Base Class which provides interface to Nintendo Nunchuk style controllers. + :param i2c: An i2c device. + :address: an i2c address. + Defaults to _DEFAULT_ADDRESS (0x52). + """ + + def __init__(self, i2c, address=_DEFAULT_ADDRESS): + self.buffer = bytearray(6) + self.i2c_device = I2CDevice(i2c, address) + time.sleep(_I2C_INIT_DELAY) + with self.i2c_device as i2c_dev: + # turn off encrypted data + # http://wiibrew.org/wiki/Wiimote/Extension_Controllers + i2c_dev.write(b"\xF0\x55") + time.sleep(_I2C_INIT_DELAY) + i2c_dev.write(b"\xFB\x00") + self.last_updated = time.monotonic() + + + def _read_data(self): + if (time.monotonic() - self.last_updated) > _I2C_BUFFER_UPDATE_DELAY: + self.last_updated = time.monotonic() + self._read_register(b"\x00") + + def _read_register(self, address): + with self.i2c_device as i2c: + time.sleep(_I2C_READ_DELAY) + i2c.write(address) + time.sleep(_I2C_READ_DELAY) + i2c.readinto(self.buffer) + time.sleep(_I2C_READ_DELAY) + return self.buffer \ No newline at end of file diff --git a/adafruit_nunchuk/classic_controller.py b/adafruit_nunchuk/classic_controller.py new file mode 100644 index 0000000..c03cd06 --- /dev/null +++ b/adafruit_nunchuk/classic_controller.py @@ -0,0 +1,145 @@ +# SPDX-FileCopyrightText: Copyright (c) 2021 John Furcean +# +# SPDX-License-Identifier: MIT +""" +`adafruit_nunchuk.classic_controller` +================================================================================ +CircuitPython library for the Nintendo Wii Classic Controller + +* Author(s): John Furcean + +Implementation Notes +-------------------- + +**Hardware:** + +* Wii Classic Controller http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Classic_Controller + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the supported boards: + https://github.com/adafruit/circuitpython/releases +* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice +""" +import time +from adafruit_bus_device.i2c_device import I2CDevice +from adafruit_nunchuk import NunchukBase + + +_DEFAULT_ADDRESS = 0x52 +_I2C_INIT_DELAY = 0.1 +_I2C_READ_DELAY = 0.01 + + +class ClassicController(NunchukBase): + """Class which provides interface to the Nintendo Classic controller.""" + + def __init__(self, i2c, address=_DEFAULT_ADDRESS): + super().__init__(i2c, address=address) + + + @property + def joystick_right(self): + """Return tuple of current right joystick position.""" + self._read_data() + x = (self.buffer[0] & 0xC0) >> 3 + x |= (self.buffer[1] & 0xC0) >> 5 + x |= (self.buffer[2] & 0x80) >> 7 + + return (x, self.buffer[2] & 0x1F) + + @property + def joystick_left(self): + """Return tuple of current left joystick position.""" + self._read_data() + return (self.buffer[0] & 0x3F, self.buffer[1] & 0x3F) + + @property + def trigger_right(self): + """Return reading right trigger position""" + self._read_data() + return self.buffer[3] & 0x1F + + @property + def trigger_left(self): + """Return reading left trigger position""" + self._read_data() + return ((self.buffer[2] & 0x60) >> 2) | ((self.buffer[3] & 0xE0) >> 5) + + @property + def dpad_left(self): + """Return current pressed state of D-pad left.""" + self._read_data() + return not bool(self.buffer[5] & 0x2) + + @property + def dpad_right(self): + """Return current pressed state of D-pad right""" + self._read_data() + return not bool(self.buffer[4] & 0x80) + + @property + def dpad_up(self): + """Return current pressed state of D-pad up.""" + self._read_data() + return not bool(self.buffer[5] & 0x1) + + @property + def dpad_down(self): + """Return current pressed state of D-pad down""" + self._read_data() + return not bool(self.buffer[4] & 0x40) + + @property + def button_a(self): + """Return current pressed state of the A button""" + self._read_data() + return not bool(self.buffer[5] & 0x10) + + @property + def button_b(self): + """Return current pressed state of the B button""" + self._read_data() + return not bool(self.buffer[5] & 0x40) + + @property + def button_x(self): + """Return current pressed state of the X button""" + self._read_data() + return not bool(self.buffer[5] & 0x8) + + @property + def button_y(self): + """Return current pressed state of the Y button""" + self._read_data() + return not bool(self.buffer[5] & 0x20) + + @property + def button_zr(self): + """Return current pressed state of the Zr button""" + self._read_data() + return not bool(self.buffer[5] & 0x4) + + @property + def button_zl(self): + """Return current pressed state of the Zl button""" + self._read_data() + return not bool(self.buffer[5] & 0x80) + + @property + def button_home(self): + """Return current pressed state of the Home button""" + self._read_data() + return not bool(self.buffer[4] & 0x8) + + @property + def button_start(self): + """Return current pressed state of the Start/Plus button""" + self._read_data() + return not bool(self.buffer[4] & 0x4) + + @property + def button_select(self): + """Return current pressed state of the Select/Minus button""" + self._read_data() + return not bool(self.buffer[4] & 0x10) diff --git a/adafruit_nunchuk.py b/adafruit_nunchuk/nunchuk.py similarity index 66% rename from adafruit_nunchuk.py rename to adafruit_nunchuk/nunchuk.py index a3b0681..ace35a1 100644 --- a/adafruit_nunchuk.py +++ b/adafruit_nunchuk/nunchuk.py @@ -3,12 +3,11 @@ # SPDX-License-Identifier: MIT """ -`adafruit_nunchuk` +`adafruit_nunchuk.nunchuck` ================================================================================ CircuitPython library for Nintendo Nunchuk controller - * Author(s): Carter Nelson Implementation Notes @@ -27,28 +26,19 @@ """ import time from adafruit_bus_device.i2c_device import I2CDevice +from adafruit_nunchuk import NunchukBase __version__ = "0.0.0-auto.0" __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Nunchuk.git" _DEFAULT_ADDRESS = 0x52 -_I2C_INIT_DELAY = 0.1 -_I2C_READ_DELAY = 0.01 - -class Nunchuk: +class Nunchuk(NunchukBase): """Class which provides interface to Nintendo Nunchuk controller.""" def __init__(self, i2c, address=_DEFAULT_ADDRESS): - self.buffer = bytearray(6) - self.i2c_device = I2CDevice(i2c, address) - time.sleep(_I2C_INIT_DELAY) - with self.i2c_device as i2c_dev: - # turn off encrypted data - # http://wiibrew.org/wiki/Wiimote/Extension_Controllers - i2c_dev.write(b"\xF0\x55") - time.sleep(_I2C_INIT_DELAY) - i2c_dev.write(b"\xFB\x00") + super().__init__(i2c, address=address) + @property def joystick(self): @@ -59,12 +49,14 @@ def joystick(self): @property def button_C(self): # pylint: disable=invalid-name """Return current pressed state of button C.""" - return not bool(self._read_data()[5] & 0x02) + self._read_data() + return not bool(self.buffer[5] & 0x02) @property def button_Z(self): # pylint: disable=invalid-name """Return current pressed state of button Z.""" - return not bool(self._read_data()[5] & 0x01) + self._read_data() + return not bool(self.buffer[5] & 0x01) @property def acceleration(self): @@ -78,14 +70,3 @@ def acceleration(self): z |= self.buffer[4] << 2 return x, y, z - def _read_data(self): - return self._read_register(b"\x00") - - def _read_register(self, address): - with self.i2c_device as i2c: - time.sleep(_I2C_READ_DELAY) - i2c.write(address) - time.sleep(_I2C_READ_DELAY) - i2c.readinto(self.buffer) - time.sleep(_I2C_READ_DELAY) - return self.buffer diff --git a/examples/classic_controller_simpletest.py b/examples/classic_controller_simpletest.py new file mode 100644 index 0000000..e0be1ba --- /dev/null +++ b/examples/classic_controller_simpletest.py @@ -0,0 +1,53 @@ +# SPDX-FileCopyrightText: 2021 John Furcean +# +# SPDX-License-Identifier: MIT +import board +from adafruit_nunchuk.classic_controller import ClassicController + +controller = ClassicController(board.I2C()) + +while True: + + # Right Joystick: (0-31,0-31), middle is (16,16) + if controller.joystick_right != (16, 16): + print(f"Right Joystick (x,y): {controller.joystick_right}") + + # Left Joystick: (0-63,063), middle is (32,32) + if controller.joystick_left != (32, 32): + print(f"Left Joystick (x,y): {controller.joystick_left}") + + # Triggers: 0-31 + if controller.trigger_right > 0: + print(f"Right Trigger (x,y): {controller.trigger_right}") + if controller.trigger_left > 0: + print(f"Left Trigger (x,y): {controller.trigger_left}") + + # DPad: True or False + if controller.dpad_down: + print("D-Pad Down Pressed") + if controller.dpad_up: + print("D-Pad Up Pressed") + if controller.dpad_left: + print("D-Pad Left Pressed") + if controller.dpad_right: + print("D-Pad Right Pressed") + + # Buttons: True of False + if controller.button_zr: + print("Button Pressed: Zr") + if controller.button_zl: + print("Button Pressed: Zl") + if controller.button_a: + print("Button Pressed: A") + if controller.button_b: + print("Button Pressed: B") + if controller.button_x: + print("Button Pressed: X") + if controller.button_y: + print("Button Pressed: Y") + if controller.button_home: + print("Button Pressed: HOME") + if controller.button_start: + print("Button Pressed: START") + if controller.button_select: + print("Button Pressed: SELECT") diff --git a/examples/nunchuk_accel_mouse.py b/examples/nunchuk_accel_mouse.py index 886bef8..7ee8c53 100644 --- a/examples/nunchuk_accel_mouse.py +++ b/examples/nunchuk_accel_mouse.py @@ -2,11 +2,12 @@ # SPDX-License-Identifier: MIT import board +import usb_hid from adafruit_hid.mouse import Mouse -import adafruit_nunchuk +from adafruit_nunchuk.nunchuk import Nunchuk -m = Mouse() -nc = adafruit_nunchuk.Nunchuk(board.I2C()) +m = Mouse(usb_hid.devices) +nc = Nunchuk(board.I2C()) centerX = 120 centerY = 110 diff --git a/examples/nunchuk_analog_mouse.py b/examples/nunchuk_analog_mouse.py index 05917da..3a5f28c 100644 --- a/examples/nunchuk_analog_mouse.py +++ b/examples/nunchuk_analog_mouse.py @@ -2,11 +2,12 @@ # SPDX-License-Identifier: MIT import board +import usb_hid from adafruit_hid.mouse import Mouse -import adafruit_nunchuk +from adafruit_nunchuk.nunchuk import Nunchuk -m = Mouse() -nc = adafruit_nunchuk.Nunchuk(board.I2C()) +m = Mouse(usb_hid.devices) +nc = Nunchuk(board.I2C()) centerX = 128 centerY = 128 diff --git a/examples/nunchuk_mouse.py b/examples/nunchuk_mouse.py index 7df4daa..433e366 100644 --- a/examples/nunchuk_mouse.py +++ b/examples/nunchuk_mouse.py @@ -2,13 +2,14 @@ # SPDX-License-Identifier: MIT import board +import usb_hid from adafruit_hid.mouse import Mouse -import adafruit_nunchuk +from adafruit_nunchuk.nunchuk import Nunchuk THRESHOLD = 10 -m = Mouse() -nc = adafruit_nunchuk.Nunchuk(board.I2C()) +m = Mouse(usb_hid.devices) +nc = Nunchuk(board.I2C()) while True: x, y = nc.joystick diff --git a/examples/nunchuk_simpletest.py b/examples/nunchuk_simpletest.py index db643fe..e647841 100644 --- a/examples/nunchuk_simpletest.py +++ b/examples/nunchuk_simpletest.py @@ -3,7 +3,7 @@ import time import board -import adafruit_nunchuk +from adafruit_nunchuk.nunchuk import Nunchuk nc = adafruit_nunchuk.Nunchuk(board.I2C()) From b289432a25489f511a76c62fae5870c680a79154 Mon Sep 17 00:00:00 2001 From: John Furcean Date: Fri, 15 Jan 2021 23:42:04 -0500 Subject: [PATCH 02/16] wip --- adafruit_nunchuk/__init__.py | 7 +++++-- examples/classic_controller_simpletest.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/adafruit_nunchuk/__init__.py b/adafruit_nunchuk/__init__.py index 4bfe2b0..9132a5b 100644 --- a/adafruit_nunchuk/__init__.py +++ b/adafruit_nunchuk/__init__.py @@ -1,7 +1,11 @@ +# SPDX-FileCopyrightText: Copyright (c) 2021 John Furcean +# +# SPDX-License-Identifier: MIT """ `adafruit_nunchuk` ================================================================================ -Base Library for the Nunchuck Style libraries. +Base Library for the Nintento Nunchuck Extension Controller libraries. + * Author(s): John Furcean Implementation Notes @@ -29,7 +33,6 @@ _I2C_READ_DELAY = 0.01 _I2C_BUFFER_UPDATE_DELAY = .05 - class NunchukBase: """Base Class which provides interface to Nintendo Nunchuk style controllers. :param i2c: An i2c device. diff --git a/examples/classic_controller_simpletest.py b/examples/classic_controller_simpletest.py index e0be1ba..da0c519 100644 --- a/examples/classic_controller_simpletest.py +++ b/examples/classic_controller_simpletest.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: 2021 John Furcean +# SPDX-FileCopyrightText: Copyright (c) 2021 John Furcean # # SPDX-License-Identifier: MIT import board From 9ab30b2d062364ea9046a23262a2b7873e8b2c03 Mon Sep 17 00:00:00 2001 From: John Furcean Date: Fri, 15 Jan 2021 23:50:10 -0500 Subject: [PATCH 03/16] after running pylint and black --- adafruit_nunchuk/__init__.py | 11 ++++---- adafruit_nunchuk/classic_controller.py | 38 ++++++++++++-------------- adafruit_nunchuk/nunchuk.py | 14 ++++------ 3 files changed, 28 insertions(+), 35 deletions(-) diff --git a/adafruit_nunchuk/__init__.py b/adafruit_nunchuk/__init__.py index 9132a5b..0842549 100644 --- a/adafruit_nunchuk/__init__.py +++ b/adafruit_nunchuk/__init__.py @@ -6,7 +6,7 @@ ================================================================================ Base Library for the Nintento Nunchuck Extension Controller libraries. -* Author(s): John Furcean +* Author(s): John Furcean Implementation Notes -------------------- @@ -31,7 +31,8 @@ _DEFAULT_ADDRESS = 0x52 _I2C_INIT_DELAY = 0.1 _I2C_READ_DELAY = 0.01 -_I2C_BUFFER_UPDATE_DELAY = .05 +_I2C_BUFFER_UPDATE_DELAY = 0.05 + class NunchukBase: """Base Class which provides interface to Nintendo Nunchuk style controllers. @@ -52,8 +53,8 @@ def __init__(self, i2c, address=_DEFAULT_ADDRESS): i2c_dev.write(b"\xFB\x00") self.last_updated = time.monotonic() - - def _read_data(self): + def read_data(self): + """Reads data stream from register""" if (time.monotonic() - self.last_updated) > _I2C_BUFFER_UPDATE_DELAY: self.last_updated = time.monotonic() self._read_register(b"\x00") @@ -65,4 +66,4 @@ def _read_register(self, address): time.sleep(_I2C_READ_DELAY) i2c.readinto(self.buffer) time.sleep(_I2C_READ_DELAY) - return self.buffer \ No newline at end of file + return self.buffer diff --git a/adafruit_nunchuk/classic_controller.py b/adafruit_nunchuk/classic_controller.py index c03cd06..708b2a8 100644 --- a/adafruit_nunchuk/classic_controller.py +++ b/adafruit_nunchuk/classic_controller.py @@ -19,10 +19,7 @@ * Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases -* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice """ -import time -from adafruit_bus_device.i2c_device import I2CDevice from adafruit_nunchuk import NunchukBase @@ -37,11 +34,10 @@ class ClassicController(NunchukBase): def __init__(self, i2c, address=_DEFAULT_ADDRESS): super().__init__(i2c, address=address) - @property def joystick_right(self): """Return tuple of current right joystick position.""" - self._read_data() + self.read_data() x = (self.buffer[0] & 0xC0) >> 3 x |= (self.buffer[1] & 0xC0) >> 5 x |= (self.buffer[2] & 0x80) >> 7 @@ -51,95 +47,95 @@ def joystick_right(self): @property def joystick_left(self): """Return tuple of current left joystick position.""" - self._read_data() + self.read_data() return (self.buffer[0] & 0x3F, self.buffer[1] & 0x3F) @property def trigger_right(self): """Return reading right trigger position""" - self._read_data() + self.read_data() return self.buffer[3] & 0x1F @property def trigger_left(self): """Return reading left trigger position""" - self._read_data() + self.read_data() return ((self.buffer[2] & 0x60) >> 2) | ((self.buffer[3] & 0xE0) >> 5) @property def dpad_left(self): """Return current pressed state of D-pad left.""" - self._read_data() + self.read_data() return not bool(self.buffer[5] & 0x2) @property def dpad_right(self): """Return current pressed state of D-pad right""" - self._read_data() + self.read_data() return not bool(self.buffer[4] & 0x80) @property def dpad_up(self): """Return current pressed state of D-pad up.""" - self._read_data() + self.read_data() return not bool(self.buffer[5] & 0x1) @property def dpad_down(self): """Return current pressed state of D-pad down""" - self._read_data() + self.read_data() return not bool(self.buffer[4] & 0x40) @property def button_a(self): """Return current pressed state of the A button""" - self._read_data() + self.read_data() return not bool(self.buffer[5] & 0x10) @property def button_b(self): """Return current pressed state of the B button""" - self._read_data() + self.read_data() return not bool(self.buffer[5] & 0x40) @property def button_x(self): """Return current pressed state of the X button""" - self._read_data() + self.read_data() return not bool(self.buffer[5] & 0x8) @property def button_y(self): """Return current pressed state of the Y button""" - self._read_data() + self.read_data() return not bool(self.buffer[5] & 0x20) @property def button_zr(self): """Return current pressed state of the Zr button""" - self._read_data() + self.read_data() return not bool(self.buffer[5] & 0x4) @property def button_zl(self): """Return current pressed state of the Zl button""" - self._read_data() + self.read_data() return not bool(self.buffer[5] & 0x80) @property def button_home(self): """Return current pressed state of the Home button""" - self._read_data() + self.read_data() return not bool(self.buffer[4] & 0x8) @property def button_start(self): """Return current pressed state of the Start/Plus button""" - self._read_data() + self.read_data() return not bool(self.buffer[4] & 0x4) @property def button_select(self): """Return current pressed state of the Select/Minus button""" - self._read_data() + self.read_data() return not bool(self.buffer[4] & 0x10) diff --git a/adafruit_nunchuk/nunchuk.py b/adafruit_nunchuk/nunchuk.py index ace35a1..78f8892 100644 --- a/adafruit_nunchuk/nunchuk.py +++ b/adafruit_nunchuk/nunchuk.py @@ -22,10 +22,7 @@ * Adafruit CircuitPython firmware for the supported boards: https://github.com/adafruit/circuitpython/releases -* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice """ -import time -from adafruit_bus_device.i2c_device import I2CDevice from adafruit_nunchuk import NunchukBase __version__ = "0.0.0-auto.0" @@ -33,35 +30,35 @@ _DEFAULT_ADDRESS = 0x52 + class Nunchuk(NunchukBase): """Class which provides interface to Nintendo Nunchuk controller.""" def __init__(self, i2c, address=_DEFAULT_ADDRESS): super().__init__(i2c, address=address) - @property def joystick(self): """Return tuple of current joystick position.""" - self._read_data() + self.read_data() return self.buffer[0], self.buffer[1] @property def button_C(self): # pylint: disable=invalid-name """Return current pressed state of button C.""" - self._read_data() + self.read_data() return not bool(self.buffer[5] & 0x02) @property def button_Z(self): # pylint: disable=invalid-name """Return current pressed state of button Z.""" - self._read_data() + self.read_data() return not bool(self.buffer[5] & 0x01) @property def acceleration(self): """Return 3 tuple of accelerometer reading.""" - self._read_data() + self.read_data() x = (self.buffer[5] & 0xC0) >> 6 x |= self.buffer[2] << 2 y = (self.buffer[5] & 0x30) >> 4 @@ -69,4 +66,3 @@ def acceleration(self): z = (self.buffer[5] & 0x0C) >> 2 z |= self.buffer[4] << 2 return x, y, z - From fc648f24cfdebae1c8de89c3037a34641b1937d4 Mon Sep 17 00:00:00 2001 From: John Furcean Date: Fri, 15 Jan 2021 23:53:52 -0500 Subject: [PATCH 04/16] fix nunchuk_simpletest import usage --- examples/nunchuk_simpletest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/nunchuk_simpletest.py b/examples/nunchuk_simpletest.py index e647841..547a106 100644 --- a/examples/nunchuk_simpletest.py +++ b/examples/nunchuk_simpletest.py @@ -5,7 +5,7 @@ import board from adafruit_nunchuk.nunchuk import Nunchuk -nc = adafruit_nunchuk.Nunchuk(board.I2C()) +nc = Nunchuk(board.I2C()) while True: x, y = nc.joystick From d830d1b8ba584280718821bf3da78cc8fc49cb11 Mon Sep 17 00:00:00 2001 From: John Furcean Date: Sat, 16 Jan 2021 00:01:40 -0500 Subject: [PATCH 05/16] fix documentation issues --- adafruit_nunchuk/__init__.py | 5 ++--- docs/conf.py | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/adafruit_nunchuk/__init__.py b/adafruit_nunchuk/__init__.py index 0842549..26d78eb 100644 --- a/adafruit_nunchuk/__init__.py +++ b/adafruit_nunchuk/__init__.py @@ -36,9 +36,8 @@ class NunchukBase: """Base Class which provides interface to Nintendo Nunchuk style controllers. - :param i2c: An i2c device. - :address: an i2c address. - Defaults to _DEFAULT_ADDRESS (0x52). + :param i2c: An i2c device. + :address: an i2c address. Defaults to _DEFAULT_ADDRESS (0x52). """ def __init__(self, i2c, address=_DEFAULT_ADDRESS): 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 = { From fd52ce0520e93bb1ff9448c0cfed40e97a5893b7 Mon Sep 17 00:00:00 2001 From: John Furcean Date: Sat, 16 Jan 2021 00:04:41 -0500 Subject: [PATCH 06/16] fix black --- adafruit_nunchuk/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_nunchuk/__init__.py b/adafruit_nunchuk/__init__.py index 26d78eb..9fe6c1a 100644 --- a/adafruit_nunchuk/__init__.py +++ b/adafruit_nunchuk/__init__.py @@ -36,8 +36,8 @@ class NunchukBase: """Base Class which provides interface to Nintendo Nunchuk style controllers. - :param i2c: An i2c device. - :address: an i2c address. Defaults to _DEFAULT_ADDRESS (0x52). + :param i2c: An i2c device. + :address: an i2c address. Defaults to _DEFAULT_ADDRESS (0x52). """ def __init__(self, i2c, address=_DEFAULT_ADDRESS): From 0d9bb6e0e7abce4a9443bffbd2254c66c4757bdd Mon Sep 17 00:00:00 2001 From: John Furcean Date: Sat, 16 Jan 2021 00:25:22 -0500 Subject: [PATCH 07/16] fix classic_controller_simpletest.py --- examples/classic_controller_simpletest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/classic_controller_simpletest.py b/examples/classic_controller_simpletest.py index da0c519..8e9eeb9 100644 --- a/examples/classic_controller_simpletest.py +++ b/examples/classic_controller_simpletest.py @@ -18,9 +18,9 @@ # Triggers: 0-31 if controller.trigger_right > 0: - print(f"Right Trigger (x,y): {controller.trigger_right}") + print(f"Right Trigger: {controller.trigger_right}") if controller.trigger_left > 0: - print(f"Left Trigger (x,y): {controller.trigger_left}") + print(f"Left Trigger: {controller.trigger_left}") # DPad: True or False if controller.dpad_down: From d39b8d710c5f226c9a3005a34a75aa0409178f61 Mon Sep 17 00:00:00 2001 From: David Glaude Date: Sun, 17 Jan 2021 00:19:12 +0100 Subject: [PATCH 08/16] Add support for uDraw GameTablet as a module --- adafruit_nunchuk/udraw.py | 67 ++++++++++++++++++++++++++++++++++++ examples/udraw_simpletest.py | 24 +++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 adafruit_nunchuk/udraw.py create mode 100644 examples/udraw_simpletest.py diff --git a/adafruit_nunchuk/udraw.py b/adafruit_nunchuk/udraw.py new file mode 100644 index 0000000..7c2732a --- /dev/null +++ b/adafruit_nunchuk/udraw.py @@ -0,0 +1,67 @@ +# SPDX-FileCopyrightText: Copyright (c) 2021 David Glaude +# +# SPDX-License-Identifier: MIT +""" +`udraw` +================================================================================ +CircuitPython library for the Nintendo Wii uDraw GameTablet + +* Author(s): David Glaude + +Implementation Notes +-------------------- + +**Hardware:** + +* Wii uDraw GameTablet http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Classic_Controller + +**Software and Dependencies:** + +* Adafruit CircuitPython firmware for the supported boards: + https://github.com/adafruit/circuitpython/releases +* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice +`adafruit_nunchuk.classic_controller` +""" +from adafruit_nunchuk import NunchukBase + + +_DEFAULT_ADDRESS = 0x52 +_I2C_INIT_DELAY = 0.1 +_I2C_READ_DELAY = 0.01 + + +class uDraw(NunchukBase): + """Class which provides interface to the uDraw GameTablet.""" + + def __init__(self, i2c, address=_DEFAULT_ADDRESS): + super().__init__(i2c, address=address) + + @property + def pressure(self): + """Return current pen pressure.""" + self.read_data() + return (self.buffer[3]) + + @property + def button_pen(self): + """Return current pressed state of the PEN button""" + self.read_data() + return bool((self.buffer[5] & 0x04) >> 2) + + @property + def button_c(self): + """Return current pressed state of the C button""" + self.read_data() + return not bool((self.buffer[5] & 0x02) >> 1) + + @property + def button_z(self): + """Return current pressed state of the Z button""" + self.read_data() + return not bool((self.buffer[5] & 0x01)) + + @property + def position(self): + """Return tuple of current position.""" + self.read_data() + return ((self.buffer[2] & 0x0F) << 8 | self.buffer[0]), ((self.buffer[2] & 0xF0) << 4 | self.buffer[1]) diff --git a/examples/udraw_simpletest.py b/examples/udraw_simpletest.py new file mode 100644 index 0000000..9e6094c --- /dev/null +++ b/examples/udraw_simpletest.py @@ -0,0 +1,24 @@ +# SPDX-FileCopyrightText: 2021 David Glaude +# +# SPDX-License-Identifier: MIT +import board +from adafruit_nunchuk.udraw import uDraw + +controller = uDraw(board.I2C()) + +while True: + + # Pressure: (8-248) + if controller.pressure != 8: + print("Pen pressure: ", controller.pressure) + + if controller.position != (4095, 4095): + print("Pen (x,y): ", controller.position) + + # Buttons: True of False + if controller.button_pen: + print("Button Pressed: PEN") + if controller.button_c: + print("Button Pressed: C") + if controller.button_z: + print("Button Pressed: Z") From 9fc84638c3ae4bd7a0123a25b96b1604e1670342 Mon Sep 17 00:00:00 2001 From: David Glaude Date: Sun, 17 Jan 2021 00:30:53 +0100 Subject: [PATCH 09/16] Black edit --- adafruit_nunchuk/udraw.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/adafruit_nunchuk/udraw.py b/adafruit_nunchuk/udraw.py index 7c2732a..1eddef0 100644 --- a/adafruit_nunchuk/udraw.py +++ b/adafruit_nunchuk/udraw.py @@ -40,7 +40,7 @@ def __init__(self, i2c, address=_DEFAULT_ADDRESS): def pressure(self): """Return current pen pressure.""" self.read_data() - return (self.buffer[3]) + return self.buffer[3] @property def button_pen(self): @@ -64,4 +64,6 @@ def button_z(self): def position(self): """Return tuple of current position.""" self.read_data() - return ((self.buffer[2] & 0x0F) << 8 | self.buffer[0]), ((self.buffer[2] & 0xF0) << 4 | self.buffer[1]) + return ((self.buffer[2] & 0x0F) << 8 | self.buffer[0]), ( + (self.buffer[2] & 0xF0) << 4 | self.buffer[1] + ) From ba0379b5c7f3e1a38a45842b47e88f2d044904d4 Mon Sep 17 00:00:00 2001 From: David Glaude Date: Sun, 17 Jan 2021 00:39:45 +0100 Subject: [PATCH 10/16] Pylint check --- adafruit_nunchuk/udraw.py | 2 +- examples/udraw_simpletest.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/adafruit_nunchuk/udraw.py b/adafruit_nunchuk/udraw.py index 1eddef0..e529e17 100644 --- a/adafruit_nunchuk/udraw.py +++ b/adafruit_nunchuk/udraw.py @@ -30,7 +30,7 @@ _I2C_READ_DELAY = 0.01 -class uDraw(NunchukBase): +class UDraw(NunchukBase): """Class which provides interface to the uDraw GameTablet.""" def __init__(self, i2c, address=_DEFAULT_ADDRESS): diff --git a/examples/udraw_simpletest.py b/examples/udraw_simpletest.py index 9e6094c..cb24505 100644 --- a/examples/udraw_simpletest.py +++ b/examples/udraw_simpletest.py @@ -2,9 +2,9 @@ # # SPDX-License-Identifier: MIT import board -from adafruit_nunchuk.udraw import uDraw +from adafruit_nunchuk.udraw import UDraw -controller = uDraw(board.I2C()) +controller = UDraw(board.I2C()) while True: From 1f7a829d5d4219013973093b68d88d8b86428227 Mon Sep 17 00:00:00 2001 From: John Furcean Date: Sun, 17 Jan 2021 14:06:50 -0500 Subject: [PATCH 11/16] change to using values property --- adafruit_nunchuk/__init__.py | 4 +- adafruit_nunchuk/classic_controller.py | 181 ++++++++++++++-------- adafruit_nunchuk/nunchuk.py | 43 +++-- adafruit_nunchuk/udraw.py | 55 ++++--- examples/classic_controller_simpletest.py | 50 +++--- examples/udraw_simpletest.py | 6 +- 6 files changed, 214 insertions(+), 125 deletions(-) mode change 100644 => 100755 adafruit_nunchuk/__init__.py mode change 100644 => 100755 adafruit_nunchuk/classic_controller.py mode change 100644 => 100755 adafruit_nunchuk/nunchuk.py mode change 100644 => 100755 adafruit_nunchuk/udraw.py diff --git a/adafruit_nunchuk/__init__.py b/adafruit_nunchuk/__init__.py old mode 100644 new mode 100755 index 9fe6c1a..d98349c --- a/adafruit_nunchuk/__init__.py +++ b/adafruit_nunchuk/__init__.py @@ -50,7 +50,9 @@ def __init__(self, i2c, address=_DEFAULT_ADDRESS): i2c_dev.write(b"\xF0\x55") time.sleep(_I2C_INIT_DELAY) i2c_dev.write(b"\xFB\x00") - self.last_updated = time.monotonic() + time.sleep(_I2C_INIT_DELAY) + + self.last_updated = 0 def read_data(self): """Reads data stream from register""" diff --git a/adafruit_nunchuk/classic_controller.py b/adafruit_nunchuk/classic_controller.py old mode 100644 new mode 100755 index 708b2a8..955e0a6 --- a/adafruit_nunchuk/classic_controller.py +++ b/adafruit_nunchuk/classic_controller.py @@ -13,7 +13,7 @@ **Hardware:** -* Wii Classic Controller http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Classic_Controller +* Wii Classic Controller https://en.wikipedia.org/wiki/Classic_Controller **Software and Dependencies:** @@ -24,118 +24,171 @@ _DEFAULT_ADDRESS = 0x52 -_I2C_INIT_DELAY = 0.1 -_I2C_READ_DELAY = 0.01 class ClassicController(NunchukBase): - """Class which provides interface to the Nintendo Classic controller.""" + """Class which provides interface to the Nintendo Classic Controller.""" def __init__(self, i2c, address=_DEFAULT_ADDRESS): super().__init__(i2c, address=address) @property - def joystick_right(self): + def values(self): # pylint: disable=too-many-locals + """returns tuple of values""" + + self.read_data() + + # https://wiibrew.org/wiki/Wiimote/Extension_Controllers/Classic_Controller + + # left joystick + jlx = (self.buffer[0] & 0xC0) >> 3 + jlx |= (self.buffer[1] & 0xC0) >> 5 + jlx |= (self.buffer[2] & 0x80) >> 7 + jly = self.buffer[2] & 0x1F + + # right joystick + jrx = self.buffer[0] & 0x3F + jry = self.buffer[1] & 0x3F + + # left trigger + tl = (self.buffer[2] & 0x60) >> 2 # pylint: disable=invalid-name + tl |= (self.buffer[3] & 0xE0) >> 5 # pylint: disable=invalid-name + + # right trigger + tr = self.buffer[3] & 0x1F # pylint: disable=invalid-name + + # D-Pad + dl = not bool(self.buffer[5] & 0x2) # pylint: disable=invalid-name + dr = not bool(self.buffer[4] & 0x80) # pylint: disable=invalid-name + du = not bool(self.buffer[5] & 0x1) # pylint: disable=invalid-name + dd = not bool(self.buffer[4] & 0x40) # pylint: disable=invalid-name + + # Buttons + A = not bool(self.buffer[5] & 0x10) # pylint: disable=invalid-name + B = not bool(self.buffer[5] & 0x40) # pylint: disable=invalid-name + X = not bool(self.buffer[5] & 0x8) # pylint: disable=invalid-name + Y = not bool(self.buffer[5] & 0x20) # pylint: disable=invalid-name + ZL = not bool(self.buffer[5] & 0x80) # pylint: disable=invalid-name + ZR = not bool(self.buffer[5] & 0x4) # pylint: disable=invalid-name + start = not bool(self.buffer[4] & 0x4) + select = not bool(self.buffer[4] & 0x10) + home = not bool(self.buffer[4] & 0x8) + + return ( + jlx, + jly, + jrx, + jry, + dl, + dr, + du, + dd, + A, + B, + X, + Y, + tr, + tl, + ZR, + ZL, + start, + select, + home, + ) + + @property + def joystick_R(self): # pylint: disable=invalid-name """Return tuple of current right joystick position.""" - self.read_data() - x = (self.buffer[0] & 0xC0) >> 3 - x |= (self.buffer[1] & 0xC0) >> 5 - x |= (self.buffer[2] & 0x80) >> 7 - return (x, self.buffer[2] & 0x1F) + return self.values[0], self.values[1] @property - def joystick_left(self): + def joystick_L(self): # pylint: disable=invalid-name """Return tuple of current left joystick position.""" - self.read_data() - return (self.buffer[0] & 0x3F, self.buffer[1] & 0x3F) - - @property - def trigger_right(self): - """Return reading right trigger position""" - self.read_data() - return self.buffer[3] & 0x1F - - @property - def trigger_left(self): - """Return reading left trigger position""" - self.read_data() - return ((self.buffer[2] & 0x60) >> 2) | ((self.buffer[3] & 0xE0) >> 5) + return self.values[2], self.values[3] @property - def dpad_left(self): + def dpad_L(self): # pylint: disable=invalid-name """Return current pressed state of D-pad left.""" - self.read_data() - return not bool(self.buffer[5] & 0x2) + return self.values[4] @property - def dpad_right(self): + def dpad_R(self): # pylint: disable=invalid-name """Return current pressed state of D-pad right""" - self.read_data() - return not bool(self.buffer[4] & 0x80) + return self.values[5] @property - def dpad_up(self): + def dpad_U(self): # pylint: disable=invalid-name """Return current pressed state of D-pad up.""" - self.read_data() - return not bool(self.buffer[5] & 0x1) + return self.values[6] @property - def dpad_down(self): + def dpad_D(self): # pylint: disable=invalid-name """Return current pressed state of D-pad down""" - self.read_data() - return not bool(self.buffer[4] & 0x40) + return self.values[7] @property - def button_a(self): + def button_A(self): # pylint: disable=invalid-name """Return current pressed state of the A button""" - self.read_data() - return not bool(self.buffer[5] & 0x10) + return self.values[8] @property - def button_b(self): + def button_B(self): # pylint: disable=invalid-name """Return current pressed state of the B button""" - self.read_data() - return not bool(self.buffer[5] & 0x40) + return self.values[9] @property - def button_x(self): + def button_X(self): # pylint: disable=invalid-name """Return current pressed state of the X button""" - self.read_data() - return not bool(self.buffer[5] & 0x8) + return self.values[10] @property - def button_y(self): + def button_Y(self): # pylint: disable=invalid-name """Return current pressed state of the Y button""" - self.read_data() - return not bool(self.buffer[5] & 0x20) + return self.values[11] @property - def button_zr(self): + def trigger_R(self): # pylint: disable=invalid-name + """Return reading right trigger position""" + return self.values[12] + + @property + def trigger_L(self): # pylint: disable=invalid-name + """Return reading left trigger position""" + return self.values[13] + + @property + def button_ZR(self): # pylint: disable=invalid-name """Return current pressed state of the Zr button""" - self.read_data() - return not bool(self.buffer[5] & 0x4) + return self.values[14] @property - def button_zl(self): + def button_ZL(self): # pylint: disable=invalid-name """Return current pressed state of the Zl button""" + return self.values[15] + + @property + def button_start(self): + """Return current pressed state of the Start button""" + return self.values[16] + + @property + def button_select(self): + """Return current pressed state of the Select button""" self.read_data() - return not bool(self.buffer[5] & 0x80) + return self.values[17] @property def button_home(self): """Return current pressed state of the Home button""" - self.read_data() - return not bool(self.buffer[4] & 0x8) + return self.values[18] @property - def button_start(self): - """Return current pressed state of the Start/Plus button""" - self.read_data() - return not bool(self.buffer[4] & 0x4) + def button_plus(self): + """Return current pressed state of the Plus(Start) button""" + return self.button_start @property - def button_select(self): - """Return current pressed state of the Select/Minus button""" - self.read_data() - return not bool(self.buffer[4] & 0x10) + def button_minus(self): + """Return current pressed state of the Minus(Select) button""" + return self.button_select diff --git a/adafruit_nunchuk/nunchuk.py b/adafruit_nunchuk/nunchuk.py old mode 100644 new mode 100755 index 78f8892..a29310a --- a/adafruit_nunchuk/nunchuk.py +++ b/adafruit_nunchuk/nunchuk.py @@ -37,32 +37,47 @@ class Nunchuk(NunchukBase): def __init__(self, i2c, address=_DEFAULT_ADDRESS): super().__init__(i2c, address=address) + @property + def values(self): + """Return tuple of values.""" + self.read_data() + + # https://wiibrew.org/wiki/Wiimote/Extension_Controllers/Nunchuck + + # joystick + jx = self.buffer[0] # pylint: disable=invalid-name + jy = self.buffer[1] # pylint: disable=invalid-name + + # buttons + C = not bool(self.buffer[5] & 0x02) # pylint: disable=invalid-name + Z = not bool(self.buffer[5] & 0x01) # pylint: disable=invalid-name + + # acceleration + ax = (self.buffer[5] & 0xC0) >> 6 # pylint: disable=invalid-name + ax |= self.buffer[2] << 2 # pylint: disable=invalid-name + ay = (self.buffer[5] & 0x30) >> 4 # pylint: disable=invalid-name + ay |= self.buffer[3] << 2 # pylint: disable=invalid-name + az = (self.buffer[5] & 0x0C) >> 2 # pylint: disable=invalid-name + az |= self.buffer[4] << 2 # pylint: disable=invalid-name + + return jx, jy, C, Z, ax, ay, az + @property def joystick(self): """Return tuple of current joystick position.""" - self.read_data() - return self.buffer[0], self.buffer[1] + return self.values[0], self.values[1] @property def button_C(self): # pylint: disable=invalid-name """Return current pressed state of button C.""" - self.read_data() - return not bool(self.buffer[5] & 0x02) + return self.values[2] @property def button_Z(self): # pylint: disable=invalid-name """Return current pressed state of button Z.""" - self.read_data() - return not bool(self.buffer[5] & 0x01) + return self.values[3] @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 + return self.values[4], self.values[5], self.values[6] diff --git a/adafruit_nunchuk/udraw.py b/adafruit_nunchuk/udraw.py old mode 100644 new mode 100755 index e529e17..70f9ea6 --- a/adafruit_nunchuk/udraw.py +++ b/adafruit_nunchuk/udraw.py @@ -13,7 +13,7 @@ **Hardware:** -* Wii uDraw GameTablet http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Classic_Controller +* Wii uDraw GameTablet http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Drawsome_Tablet **Software and Dependencies:** @@ -26,8 +26,6 @@ _DEFAULT_ADDRESS = 0x52 -_I2C_INIT_DELAY = 0.1 -_I2C_READ_DELAY = 0.01 class UDraw(NunchukBase): @@ -37,33 +35,50 @@ def __init__(self, i2c, address=_DEFAULT_ADDRESS): super().__init__(i2c, address=address) @property - def pressure(self): - """Return current pen pressure.""" + def values(self): + """Return tuple of values.""" + self.read_data() - return self.buffer[3] + + # http://wiibrew.org/wiki/Wiimote/Extension_Controllers/Drawsome_Tablet + + # position + px = (self.buffer[2] & 0x0F) << 8 # pylint: disable=invalid-name + px |= self.buffer[0] # pylint: disable=invalid-name + py = (self.buffer[2] & 0xF0) << 4 # pylint: disable=invalid-name + py |= self.buffer[1] # pylint: disable=invalid-name + + # pressure sensor reading + pressure = self.buffer[3] + + # buttons + pen = bool((self.buffer[5] & 0x04) >> 2) + C = not bool((self.buffer[5] & 0x02) >> 1) # pylint: disable=invalid-name + Z = not bool((self.buffer[5] & 0x01)) # pylint: disable=invalid-name + + return px, py, pen, C, Z, pressure + + @property + def position(self): + """Return tuple of current position.""" + return self.values[0], self.values[1] @property def button_pen(self): """Return current pressed state of the PEN button""" - self.read_data() - return bool((self.buffer[5] & 0x04) >> 2) + return self.values[2] @property - def button_c(self): + def button_C(self): # pylint: disable=invalid-name """Return current pressed state of the C button""" - self.read_data() - return not bool((self.buffer[5] & 0x02) >> 1) + return self.values[3] @property - def button_z(self): + def button_Z(self): # pylint: disable=invalid-name """Return current pressed state of the Z button""" - self.read_data() - return not bool((self.buffer[5] & 0x01)) + return self.values[4] @property - def position(self): - """Return tuple of current position.""" - self.read_data() - return ((self.buffer[2] & 0x0F) << 8 | self.buffer[0]), ( - (self.buffer[2] & 0xF0) << 4 | self.buffer[1] - ) + def pressure(self): + """Return current pen pressure.""" + return self.values[5] diff --git a/examples/classic_controller_simpletest.py b/examples/classic_controller_simpletest.py index 8e9eeb9..4510b0a 100644 --- a/examples/classic_controller_simpletest.py +++ b/examples/classic_controller_simpletest.py @@ -9,45 +9,49 @@ while True: # Right Joystick: (0-31,0-31), middle is (16,16) - if controller.joystick_right != (16, 16): - print(f"Right Joystick (x,y): {controller.joystick_right}") + if controller.joystick_R != (16, 16): + print(f"Right Joystick (x,y): {controller.joystick_R}") # Left Joystick: (0-63,063), middle is (32,32) - if controller.joystick_left != (32, 32): - print(f"Left Joystick (x,y): {controller.joystick_left}") + if controller.joystick_L != (32, 32): + print(f"Left Joystick (x,y): {controller.joystick_L}") # Triggers: 0-31 - if controller.trigger_right > 0: - print(f"Right Trigger: {controller.trigger_right}") - if controller.trigger_left > 0: - print(f"Left Trigger: {controller.trigger_left}") + if controller.trigger_R > 0: + print(f"Right Trigger: {controller.trigger_R}") + if controller.trigger_L > 0: + print(f"Left Trigger: {controller.trigger_L}") # DPad: True or False - if controller.dpad_down: + if controller.dpad_D: print("D-Pad Down Pressed") - if controller.dpad_up: + if controller.dpad_U: print("D-Pad Up Pressed") - if controller.dpad_left: + if controller.dpad_L: print("D-Pad Left Pressed") - if controller.dpad_right: + if controller.dpad_R: print("D-Pad Right Pressed") # Buttons: True of False - if controller.button_zr: - print("Button Pressed: Zr") - if controller.button_zl: - print("Button Pressed: Zl") - if controller.button_a: + if controller.button_ZR: + print("Button Pressed: ZR") + if controller.button_ZL: + print("Button Pressed: ZL") + if controller.button_A: print("Button Pressed: A") - if controller.button_b: + if controller.button_B: print("Button Pressed: B") - if controller.button_x: + if controller.button_X: print("Button Pressed: X") - if controller.button_y: + if controller.button_Y: print("Button Pressed: Y") if controller.button_home: - print("Button Pressed: HOME") + print("Button Pressed: Home") if controller.button_start: - print("Button Pressed: START") + print("Button Pressed: Start") if controller.button_select: - print("Button Pressed: SELECT") + print("Button Pressed: Select") + if controller.button_plus: + print("Button Pressed: Plus") + if controller.button_minus: + print("Button Pressed: Minus") diff --git a/examples/udraw_simpletest.py b/examples/udraw_simpletest.py index cb24505..07fa1a2 100644 --- a/examples/udraw_simpletest.py +++ b/examples/udraw_simpletest.py @@ -17,8 +17,8 @@ # Buttons: True of False if controller.button_pen: - print("Button Pressed: PEN") - if controller.button_c: + print("Button Pressed: Pen") + if controller.button_C: print("Button Pressed: C") - if controller.button_z: + if controller.button_Z: print("Button Pressed: Z") From 52907faeeb221ee9d31ba1aa6a66b60ab85da12e Mon Sep 17 00:00:00 2001 From: John Furcean Date: Sun, 17 Jan 2021 14:15:09 -0500 Subject: [PATCH 12/16] fix authors listed --- adafruit_nunchuk/__init__.py | 4 ++-- adafruit_nunchuk/nunchuk.py | 2 +- adafruit_nunchuk/udraw.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_nunchuk/__init__.py b/adafruit_nunchuk/__init__.py index d98349c..30bf78b 100755 --- a/adafruit_nunchuk/__init__.py +++ b/adafruit_nunchuk/__init__.py @@ -1,4 +1,4 @@ -# SPDX-FileCopyrightText: Copyright (c) 2021 John Furcean +# SPDX-FileCopyrightText: 2019 Carter Nelson for Adafruit Industries # # SPDX-License-Identifier: MIT """ @@ -6,7 +6,7 @@ ================================================================================ Base Library for the Nintento Nunchuck Extension Controller libraries. -* Author(s): John Furcean +* Author(s): Carter Nelson, John Furcean Implementation Notes -------------------- diff --git a/adafruit_nunchuk/nunchuk.py b/adafruit_nunchuk/nunchuk.py index a29310a..9bc41e9 100755 --- a/adafruit_nunchuk/nunchuk.py +++ b/adafruit_nunchuk/nunchuk.py @@ -8,7 +8,7 @@ CircuitPython library for Nintendo Nunchuk controller -* Author(s): Carter Nelson +* Author(s): Carter Nelson, John Furcean Implementation Notes -------------------- diff --git a/adafruit_nunchuk/udraw.py b/adafruit_nunchuk/udraw.py index 70f9ea6..ae5e136 100755 --- a/adafruit_nunchuk/udraw.py +++ b/adafruit_nunchuk/udraw.py @@ -6,7 +6,7 @@ ================================================================================ CircuitPython library for the Nintendo Wii uDraw GameTablet -* Author(s): David Glaude +* Author(s): David Glaude, John Furcean Implementation Notes -------------------- From 68377a2dbf022c4fd7a2b7e29f69814ada6ef8df Mon Sep 17 00:00:00 2001 From: John Furcean Date: Sun, 17 Jan 2021 14:31:59 -0500 Subject: [PATCH 13/16] fix mixup between jr and jl in values property --- adafruit_nunchuk/classic_controller.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/adafruit_nunchuk/classic_controller.py b/adafruit_nunchuk/classic_controller.py index 955e0a6..7af1ae1 100755 --- a/adafruit_nunchuk/classic_controller.py +++ b/adafruit_nunchuk/classic_controller.py @@ -40,15 +40,15 @@ def values(self): # pylint: disable=too-many-locals # https://wiibrew.org/wiki/Wiimote/Extension_Controllers/Classic_Controller - # left joystick - jlx = (self.buffer[0] & 0xC0) >> 3 - jlx |= (self.buffer[1] & 0xC0) >> 5 - jlx |= (self.buffer[2] & 0x80) >> 7 - jly = self.buffer[2] & 0x1F + # right joystick + jrx = (self.buffer[0] & 0xC0) >> 3 + jrx |= (self.buffer[1] & 0xC0) >> 5 + jrx |= (self.buffer[2] & 0x80) >> 7 + jry = self.buffer[2] & 0x1F # right joystick - jrx = self.buffer[0] & 0x3F - jry = self.buffer[1] & 0x3F + jlx = self.buffer[0] & 0x3F + jly = self.buffer[1] & 0x3F # left trigger tl = (self.buffer[2] & 0x60) >> 2 # pylint: disable=invalid-name @@ -75,10 +75,10 @@ def values(self): # pylint: disable=too-many-locals home = not bool(self.buffer[4] & 0x8) return ( - jlx, - jly, jrx, jry, + jlx, + jly, dl, dr, du, From a89235e92fff19409b1310dbd35c0ea78ee175ab Mon Sep 17 00:00:00 2001 From: John Furcean Date: Sun, 17 Jan 2021 16:40:24 -0500 Subject: [PATCH 14/16] clean up classic_controller --- adafruit_nunchuk/classic_controller.py | 61 ++++++++--------------- examples/classic_controller_simpletest.py | 12 ++--- 2 files changed, 26 insertions(+), 47 deletions(-) diff --git a/adafruit_nunchuk/classic_controller.py b/adafruit_nunchuk/classic_controller.py index 7af1ae1..57b3952 100755 --- a/adafruit_nunchuk/classic_controller.py +++ b/adafruit_nunchuk/classic_controller.py @@ -40,22 +40,21 @@ def values(self): # pylint: disable=too-many-locals # https://wiibrew.org/wiki/Wiimote/Extension_Controllers/Classic_Controller - # right joystick jrx = (self.buffer[0] & 0xC0) >> 3 jrx |= (self.buffer[1] & 0xC0) >> 5 jrx |= (self.buffer[2] & 0x80) >> 7 jry = self.buffer[2] & 0x1F - # right joystick + # left joystick jlx = self.buffer[0] & 0x3F jly = self.buffer[1] & 0x3F - # left trigger - tl = (self.buffer[2] & 0x60) >> 2 # pylint: disable=invalid-name - tl |= (self.buffer[3] & 0xE0) >> 5 # pylint: disable=invalid-name + # analog trigger - left + atl = (self.buffer[2] & 0x60) >> 2 # pylint: disable=invalid-name + atl |= (self.buffer[3] & 0xE0) >> 5 # pylint: disable=invalid-name - # right trigger - tr = self.buffer[3] & 0x1F # pylint: disable=invalid-name + # analog trigger - right + atr = self.buffer[3] & 0x1F # pylint: disable=invalid-name # D-Pad dl = not bool(self.buffer[5] & 0x2) # pylint: disable=invalid-name @@ -67,6 +66,8 @@ def values(self): # pylint: disable=too-many-locals A = not bool(self.buffer[5] & 0x10) # pylint: disable=invalid-name B = not bool(self.buffer[5] & 0x40) # pylint: disable=invalid-name X = not bool(self.buffer[5] & 0x8) # pylint: disable=invalid-name + btr = not bool(self.buffer[4] & 0x2) # pylint: disable=invalid-name + btl = not bool(self.buffer[4] & 0x20) # pylint: disable=invalid-name Y = not bool(self.buffer[5] & 0x20) # pylint: disable=invalid-name ZL = not bool(self.buffer[5] & 0x80) # pylint: disable=invalid-name ZR = not bool(self.buffer[5] & 0x4) # pylint: disable=invalid-name @@ -74,27 +75,8 @@ def values(self): # pylint: disable=too-many-locals select = not bool(self.buffer[4] & 0x10) home = not bool(self.buffer[4] & 0x8) - return ( - jrx, - jry, - jlx, - jly, - dl, - dr, - du, - dd, - A, - B, - X, - Y, - tr, - tl, - ZR, - ZL, - start, - select, - home, - ) + return jrx, jry, jlx, jly, dl, dr, du, dd, A, B, X, Y, atr,\ + atl, btr, btl, ZR, ZL, start, select, home @property def joystick_R(self): # pylint: disable=invalid-name @@ -148,40 +130,39 @@ def button_Y(self): # pylint: disable=invalid-name return self.values[11] @property - def trigger_R(self): # pylint: disable=invalid-name - """Return reading right trigger position""" - return self.values[12] + def button_RT(self): # pylint: disable=invalid-name + """Return current pressed state of the right trigger button""" + return self.values[14] @property - def trigger_L(self): # pylint: disable=invalid-name - """Return reading left trigger position""" - return self.values[13] + def button_LT(self): # pylint: disable=invalid-name + """Return current pressed state of the left trigger button""" + return self.values[15] @property def button_ZR(self): # pylint: disable=invalid-name """Return current pressed state of the Zr button""" - return self.values[14] + return self.values[16] @property def button_ZL(self): # pylint: disable=invalid-name """Return current pressed state of the Zl button""" - return self.values[15] + return self.values[17] @property def button_start(self): """Return current pressed state of the Start button""" - return self.values[16] + return self.values[18] @property def button_select(self): """Return current pressed state of the Select button""" - self.read_data() - return self.values[17] + return self.values[19] @property def button_home(self): """Return current pressed state of the Home button""" - return self.values[18] + return self.values[20] @property def button_plus(self): diff --git a/examples/classic_controller_simpletest.py b/examples/classic_controller_simpletest.py index 4510b0a..6fa611c 100644 --- a/examples/classic_controller_simpletest.py +++ b/examples/classic_controller_simpletest.py @@ -5,7 +5,6 @@ from adafruit_nunchuk.classic_controller import ClassicController controller = ClassicController(board.I2C()) - while True: # Right Joystick: (0-31,0-31), middle is (16,16) @@ -16,12 +15,6 @@ if controller.joystick_L != (32, 32): print(f"Left Joystick (x,y): {controller.joystick_L}") - # Triggers: 0-31 - if controller.trigger_R > 0: - print(f"Right Trigger: {controller.trigger_R}") - if controller.trigger_L > 0: - print(f"Left Trigger: {controller.trigger_L}") - # DPad: True or False if controller.dpad_D: print("D-Pad Down Pressed") @@ -45,6 +38,10 @@ print("Button Pressed: X") if controller.button_Y: print("Button Pressed: Y") + if controller.button_RT: + print("Button Pressed: RT") + if controller.button_LT: + print("Button Pressed: LT") if controller.button_home: print("Button Pressed: Home") if controller.button_start: @@ -55,3 +52,4 @@ print("Button Pressed: Plus") if controller.button_minus: print("Button Pressed: Minus") + \ No newline at end of file From a2548b48c58328d28446af466d7758bafd87ef30 Mon Sep 17 00:00:00 2001 From: John Furcean Date: Sun, 17 Jan 2021 16:53:00 -0500 Subject: [PATCH 15/16] clean up classic_controlelr --- adafruit_nunchuk/classic_controller.py | 25 +++++++++++++++++++++-- examples/classic_controller_simpletest.py | 1 - 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/adafruit_nunchuk/classic_controller.py b/adafruit_nunchuk/classic_controller.py index 57b3952..55c543f 100755 --- a/adafruit_nunchuk/classic_controller.py +++ b/adafruit_nunchuk/classic_controller.py @@ -75,8 +75,29 @@ def values(self): # pylint: disable=too-many-locals select = not bool(self.buffer[4] & 0x10) home = not bool(self.buffer[4] & 0x8) - return jrx, jry, jlx, jly, dl, dr, du, dd, A, B, X, Y, atr,\ - atl, btr, btl, ZR, ZL, start, select, home + return ( + jrx, + jry, + jlx, + jly, + dl, + dr, + du, + dd, + A, + B, + X, + Y, + atr, + atl, + btr, + btl, + ZR, + ZL, + start, + select, + home, + ) @property def joystick_R(self): # pylint: disable=invalid-name diff --git a/examples/classic_controller_simpletest.py b/examples/classic_controller_simpletest.py index 6fa611c..afcbab2 100644 --- a/examples/classic_controller_simpletest.py +++ b/examples/classic_controller_simpletest.py @@ -52,4 +52,3 @@ print("Button Pressed: Plus") if controller.button_minus: print("Button Pressed: Minus") - \ No newline at end of file From d5ca450e9753d8ac67538af91795bab0c81b44c8 Mon Sep 17 00:00:00 2001 From: David Glaude Date: Sun, 17 Jan 2021 23:34:57 +0100 Subject: [PATCH 16/16] Create udraw_mouse.py My local pylint complain... I hope the CI version will be OK. --- examples/udraw_mouse.py | 55 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 examples/udraw_mouse.py diff --git a/examples/udraw_mouse.py b/examples/udraw_mouse.py new file mode 100644 index 0000000..5414b15 --- /dev/null +++ b/examples/udraw_mouse.py @@ -0,0 +1,55 @@ +# SPDX-FileCopyrightText: 2021 David Glaude +# +# SPDX-License-Identifier: MIT +import board +import usb_hid +from adafruit_hid.mouse import Mouse +from adafruit_nunchuk.udraw import UDraw + +udraw = UDraw(board.I2C()) + +m = Mouse(usb_hid.devices) + +zDown = False +pDown = False + +oldx = 4095 +oldy = 4095 + +while True: + + px, py, pen, C, Z, pressure = udraw.values + + P = pen or C # Both the C button and pen button work as LEFT mouse + + # Handeling the button to create mouse click + if P and not pDown: + m.press(Mouse.LEFT_BUTTON) + pDown = True + elif not P and pDown: + m.release(Mouse.LEFT_BUTTON) + pDown = False + + if Z and not zDown: + m.press(Mouse.RIGHT_BUTTON) + zDown = True + elif not Z and zDown: + m.release(Mouse.RIGHT_BUTTON) + zDown = False + + # Values (4095,4095) mean the pen is not near the tablet + if px == 4095 or py == 4095: + oldx = 4095 + oldy = 4095 + continue # We remember that the pen was raised UP + + # If we reach here, the pen was UP and is now DOWN + if oldx == 4095 or oldy == 4095: + oldx = px + oldy = py + continue + + if (px != oldx) or (py != oldy): # PEN has moved we move the HID mouse + m.move((px - oldx), (oldy - py), 0) + oldx = px + oldy = py