Skip to content

Commit 3090c14

Browse files
authored
Merge pull request #29 from tekktrik/doc/add-typing
Add type annotations, documentation tweaks
2 parents 5b04087 + 2a88a27 commit 3090c14

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

adafruit_nunchuk.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,37 @@
2929
from collections import namedtuple
3030
from adafruit_bus_device.i2c_device import I2CDevice
3131

32+
try:
33+
from typing import Type
34+
from busio import I2C
35+
except ImportError:
36+
pass
37+
3238
__version__ = "0.0.0-auto.0"
3339
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Nunchuk.git"
3440

3541
_I2C_INIT_DELAY = 0.1
3642

3743

3844
class Nunchuk:
39-
"""
40-
Class which provides interface to Nintendo Nunchuk controller.
45+
"""Class which provides interface to Nintendo Nunchuk controller.
4146
42-
:param i2c: The `busio.I2C` object to use.
43-
:param address: The I2C address of the device. Default is 0x52.
44-
:type address: int, optional
45-
:param i2c_read_delay: The time in seconds to pause between the
47+
:param ~I2C i2c: The `busio.I2C` object to use.
48+
:param int address: (Optional) The I2C address of the device. Default is 0x52.
49+
:param float i2c_read_delay: (Optional) The time in seconds to pause between the
4650
I2C write and read. This needs to be at least 200us. A
4751
conservative default of 2000us is used since some hosts may
4852
not be able to achieve such timing.
49-
:type i2c_read_delay: float, optional
5053
"""
5154

5255
_Values = namedtuple("Values", ("joystick", "buttons", "acceleration"))
5356
_Joystick = namedtuple("Joystick", ("x", "y"))
5457
_Buttons = namedtuple("Buttons", ("C", "Z"))
5558
_Acceleration = namedtuple("Acceleration", ("x", "y", "z"))
5659

57-
def __init__(self, i2c, address=0x52, i2c_read_delay=0.002):
60+
def __init__(
61+
self, i2c: I2C, address: int = 0x52, i2c_read_delay: float = 0.002
62+
) -> None:
5863
self.buffer = bytearray(8)
5964
self.i2c_device = I2CDevice(i2c, address)
6065
self._i2c_read_delay = i2c_read_delay
@@ -67,7 +72,7 @@ def __init__(self, i2c, address=0x52, i2c_read_delay=0.002):
6772
i2c_dev.write(b"\xFB\x00")
6873

6974
@property
70-
def values(self):
75+
def values(self) -> Type[tuple]:
7176
"""The current state of all values."""
7277
self._read_data()
7378
return self._Values(
@@ -77,33 +82,33 @@ def values(self):
7782
)
7883

7984
@property
80-
def joystick(self):
85+
def joystick(self) -> Type[tuple]:
8186
"""The current joystick position."""
8287
return self._joystick()
8388

8489
@property
85-
def buttons(self): # pylint: disable=invalid-name
86-
"""The current pressed state of button Z."""
90+
def buttons(self) -> Type[tuple]: # pylint: disable=invalid-name
91+
"""The current pressed state of buttons C and Z."""
8792
return self._buttons()
8893

8994
@property
90-
def acceleration(self):
95+
def acceleration(self) -> Type[tuple]:
9196
"""The current accelerometer reading."""
9297
return self._acceleration()
9398

94-
def _joystick(self, do_read=True):
99+
def _joystick(self, do_read: bool = True) -> Type[tuple]:
95100
if do_read:
96101
self._read_data()
97102
return self._Joystick(self.buffer[0], self.buffer[1]) # x, y
98103

99-
def _buttons(self, do_read=True):
104+
def _buttons(self, do_read: bool = True) -> Type[tuple]:
100105
if do_read:
101106
self._read_data()
102107
return self._Buttons(
103108
not bool(self.buffer[5] & 0x02), not bool(self.buffer[5] & 0x01) # C # Z
104109
)
105110

106-
def _acceleration(self, do_read=True):
111+
def _acceleration(self, do_read: bool = True) -> Type[tuple]:
107112
if do_read:
108113
self._read_data()
109114
return self._Acceleration(
@@ -112,10 +117,10 @@ def _acceleration(self, do_read=True):
112117
((self.buffer[5] & 0x0C) >> 2) | (self.buffer[4] << 2), # az
113118
)
114119

115-
def _read_data(self):
120+
def _read_data(self) -> bytearray:
116121
return self._read_register(b"\x00")
117122

118-
def _read_register(self, address):
123+
def _read_register(self, address) -> bytearray:
119124
with self.i2c_device as i2c:
120125
i2c.write(address)
121126
time.sleep(self._i2c_read_delay) # at least 200us

0 commit comments

Comments
 (0)