Skip to content

Commit ab94af5

Browse files
committed
expose read_data() to user and add values property
1 parent b653d73 commit ab94af5

File tree

6 files changed

+27
-12
lines changed

6 files changed

+27
-12
lines changed

adafruit_nunchuk.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2727
"""
2828
import time
29+
from collections import namedtuple
2930
from adafruit_bus_device.i2c_device import I2CDevice
3031

3132
__version__ = "0.0.0-auto.0"
@@ -60,26 +61,37 @@ def __init__(self, i2c, address=0x52, i2c_read_delay=0.002):
6061
time.sleep(_I2C_INIT_DELAY)
6162
i2c_dev.write(b"\xFB\x00")
6263

64+
@property
65+
def values(self):
66+
"""Return named tuple of all the input values."""
67+
self.read_data()
68+
(sx, sy), bc, bz, (ax, ay, az) = ( # pylint: disable=invalid-name
69+
self.joystick,
70+
self.button_C,
71+
self.button_Z,
72+
self.acceleration,
73+
)
74+
Values = namedtuple("Values", "sx sy bc bz ax ay az")
75+
return Values(sx, sy, bc, bz, ax, ay, az)
76+
6377
@property
6478
def joystick(self):
6579
"""Return tuple of current joystick position."""
66-
self._read_data()
6780
return self.buffer[0], self.buffer[1]
6881

6982
@property
7083
def button_C(self): # pylint: disable=invalid-name
7184
"""Return current pressed state of button C."""
72-
return not bool(self._read_data()[5] & 0x02)
85+
return not bool(self.buffer[5] & 0x02)
7386

7487
@property
7588
def button_Z(self): # pylint: disable=invalid-name
7689
"""Return current pressed state of button Z."""
77-
return not bool(self._read_data()[5] & 0x01)
90+
return not bool(self.buffer[5] & 0x01)
7891

7992
@property
8093
def acceleration(self):
8194
"""Return 3 tuple of accelerometer reading."""
82-
self._read_data()
8395
x = (self.buffer[5] & 0xC0) >> 6
8496
x |= self.buffer[2] << 2
8597
y = (self.buffer[5] & 0x30) >> 4
@@ -88,7 +100,8 @@ def acceleration(self):
88100
z |= self.buffer[4] << 2
89101
return x, y, z
90102

91-
def _read_data(self):
103+
def read_data(self):
104+
"""Reads all of the raw input data from i2c."""
92105
return self._read_register(b"\x00")
93106

94107
def _read_register(self, address):

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
# Uncomment the below if you use native CircuitPython modules such as
2626
# digitalio, micropython and busio. List the modules you use. Without it, the
2727
# autodoc module docs will fail to generate with a warning.
28-
# autodoc_mock_imports = ["digitalio", "busio"]
28+
autodoc_mock_imports = ["adafruit_bus_device"]
2929

3030

3131
intersphinx_mapping = {

examples/nunchuk_accel_mouse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
# print((0 if nc.button_C else 1, 0 if nc.button_Z else 1))
2828

2929
while True:
30+
nc.read_data()
3031
accel = nc.acceleration
3132
# print(accel)
3233
# x, y = nc.joystick

examples/nunchuk_analog_mouse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
# print((0 if nc.button_C else 1, 0 if nc.button_Z else 1))
2828

2929
while True:
30+
nc.read_data()
3031
x, y = nc.joystick
3132
# Eliminate spurious reads
3233
if x == 255 or y == 255:

examples/nunchuk_mouse.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
nc = adafruit_nunchuk.Nunchuk(board.I2C())
1313

1414
while True:
15+
nc.read_data()
1516
x, y = nc.joystick
1617
x = (x - 128) // 2
1718
y = (128 - y) // 2

examples/nunchuk_simpletest.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
nc = adafruit_nunchuk.Nunchuk(board.I2C())
99

1010
while True:
11-
x, y = nc.joystick
12-
ax, ay, az = nc.acceleration
13-
print("joystick = {},{}".format(x, y))
14-
print("accceleration ax={}, ay={}, az={}".format(ax, ay, az))
15-
if nc.button_C:
11+
values = nc.values
12+
print("joystick = {},{}".format(values.sx, values.sy))
13+
print("accceleration ax={}, ay={}, az={}".format(values.ax, values.ay, values.az))
14+
if values.bc:
1615
print("button C")
17-
if nc.button_Z:
16+
if values.bz:
1817
print("button Z")
1918
time.sleep(0.5)

0 commit comments

Comments
 (0)