Skip to content

add values property and refactor other properties #24

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

Merged
merged 3 commits into from
Jan 29, 2021
Merged
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
60 changes: 42 additions & 18 deletions adafruit_nunchuk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
6 changes: 4 additions & 2 deletions examples/nunchuk_accel_mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions examples/nunchuk_analog_mouse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
25 changes: 0 additions & 25 deletions examples/nunchuk_mouse.py

This file was deleted.

5 changes: 3 additions & 2 deletions examples/nunchuk_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)