Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Convert to a package and add support for the Classic Controller #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 16 additions & 37 deletions adafruit_nunchuk.py → adafruit_nunchuk/__init__.py
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
# SPDX-FileCopyrightText: 2019 Carter Nelson for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
`adafruit_nunchuk`
================================================================================
Base Library for the Nintento Nunchuck Extension Controller libraries.

CircuitPython library for Nintendo Nunchuk controller


* Author(s): Carter Nelson
* Author(s): Carter Nelson, John Furcean

Implementation Notes
--------------------
Expand All @@ -29,15 +26,19 @@
from adafruit_bus_device.i2c_device import I2CDevice

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Nunchuk.git"
__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 = 0.05


class Nunchuk:
"""Class which provides interface to Nintendo Nunchuk controller."""
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)
Expand All @@ -49,37 +50,15 @@ def __init__(self, i2c, address=_DEFAULT_ADDRESS):
i2c_dev.write(b"\xF0\x55")
time.sleep(_I2C_INIT_DELAY)
i2c_dev.write(b"\xFB\x00")
time.sleep(_I2C_INIT_DELAY)

@property
def joystick(self):
"""Return tuple of current joystick position."""
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."""
return not bool(self._read_data()[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)

@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
self.last_updated = 0

def _read_data(self):
return self._read_register(b"\x00")
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")

def _read_register(self, address):
with self.i2c_device as i2c:
Expand Down
196 changes: 196 additions & 0 deletions adafruit_nunchuk/classic_controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
# 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 https://en.wikipedia.org/wiki/Classic_Controller

**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
"""
from adafruit_nunchuk import NunchukBase


_DEFAULT_ADDRESS = 0x52


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 values(self): # pylint: disable=too-many-locals
"""returns tuple of values"""

self.read_data()

# https://wiibrew.org/wiki/Wiimote/Extension_Controllers/Classic_Controller

jrx = (self.buffer[0] & 0xC0) >> 3
jrx |= (self.buffer[1] & 0xC0) >> 5
jrx |= (self.buffer[2] & 0x80) >> 7
jry = self.buffer[2] & 0x1F

# left joystick
jlx = self.buffer[0] & 0x3F
jly = self.buffer[1] & 0x3F

# analog trigger - left
atl = (self.buffer[2] & 0x60) >> 2 # pylint: disable=invalid-name
atl |= (self.buffer[3] & 0xE0) >> 5 # 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
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
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
start = not bool(self.buffer[4] & 0x4)
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,
)

@property
def joystick_R(self): # pylint: disable=invalid-name
"""Return tuple of current right joystick position."""

return self.values[0], self.values[1]

@property
def joystick_L(self): # pylint: disable=invalid-name
"""Return tuple of current left joystick position."""
return self.values[2], self.values[3]

@property
def dpad_L(self): # pylint: disable=invalid-name
"""Return current pressed state of D-pad left."""
return self.values[4]

@property
def dpad_R(self): # pylint: disable=invalid-name
"""Return current pressed state of D-pad right"""
return self.values[5]

@property
def dpad_U(self): # pylint: disable=invalid-name
"""Return current pressed state of D-pad up."""
return self.values[6]

@property
def dpad_D(self): # pylint: disable=invalid-name
"""Return current pressed state of D-pad down"""
return self.values[7]

@property
def button_A(self): # pylint: disable=invalid-name
"""Return current pressed state of the A button"""
return self.values[8]

@property
def button_B(self): # pylint: disable=invalid-name
"""Return current pressed state of the B button"""
return self.values[9]

@property
def button_X(self): # pylint: disable=invalid-name
"""Return current pressed state of the X button"""
return self.values[10]

@property
def button_Y(self): # pylint: disable=invalid-name
"""Return current pressed state of the Y button"""
return self.values[11]

@property
def button_RT(self): # pylint: disable=invalid-name
"""Return current pressed state of the right trigger button"""
return self.values[14]

@property
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[16]

@property
def button_ZL(self): # pylint: disable=invalid-name
"""Return current pressed state of the Zl button"""
return self.values[17]

@property
def button_start(self):
"""Return current pressed state of the Start button"""
return self.values[18]

@property
def button_select(self):
"""Return current pressed state of the Select button"""
return self.values[19]

@property
def button_home(self):
"""Return current pressed state of the Home button"""
return self.values[20]

@property
def button_plus(self):
"""Return current pressed state of the Plus(Start) button"""
return self.button_start

@property
def button_minus(self):
"""Return current pressed state of the Minus(Select) button"""
return self.button_select
83 changes: 83 additions & 0 deletions adafruit_nunchuk/nunchuk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# SPDX-FileCopyrightText: 2019 Carter Nelson for Adafruit Industries
#
# SPDX-License-Identifier: MIT

"""
`adafruit_nunchuk.nunchuck`
================================================================================

CircuitPython library for Nintendo Nunchuk controller

* Author(s): Carter Nelson, John Furcean

Implementation Notes
--------------------

**Hardware:**

* `Wii Remote Nunchuk <https://en.wikipedia.org/wiki/Wii_Remote#Nunchuk>`_
* `Wiichuck <https://www.adafruit.com/product/342>`_

**Software and Dependencies:**

* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
"""
from adafruit_nunchuk import NunchukBase

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Nunchuk.git"

_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 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."""
return self.values[0], self.values[1]

@property
def button_C(self): # pylint: disable=invalid-name
"""Return current pressed state of button C."""
return self.values[2]

@property
def button_Z(self): # pylint: disable=invalid-name
"""Return current pressed state of button Z."""
return self.values[3]

@property
def acceleration(self):
"""Return 3 tuple of accelerometer reading."""
return self.values[4], self.values[5], self.values[6]
Loading