29
29
from collections import namedtuple
30
30
from adafruit_bus_device .i2c_device import I2CDevice
31
31
32
+ try :
33
+ from typing import Type
34
+ from busio import I2C
35
+ except ImportError :
36
+ pass
37
+
32
38
__version__ = "0.0.0-auto.0"
33
39
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Nunchuk.git"
34
40
35
41
_I2C_INIT_DELAY = 0.1
36
42
37
43
38
44
class Nunchuk :
39
- """
40
- Class which provides interface to Nintendo Nunchuk controller.
45
+ """Class which provides interface to Nintendo Nunchuk controller.
41
46
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
46
50
I2C write and read. This needs to be at least 200us. A
47
51
conservative default of 2000us is used since some hosts may
48
52
not be able to achieve such timing.
49
- :type i2c_read_delay: float, optional
50
53
"""
51
54
52
55
_Values = namedtuple ("Values" , ("joystick" , "buttons" , "acceleration" ))
53
56
_Joystick = namedtuple ("Joystick" , ("x" , "y" ))
54
57
_Buttons = namedtuple ("Buttons" , ("C" , "Z" ))
55
58
_Acceleration = namedtuple ("Acceleration" , ("x" , "y" , "z" ))
56
59
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 :
58
63
self .buffer = bytearray (8 )
59
64
self .i2c_device = I2CDevice (i2c , address )
60
65
self ._i2c_read_delay = i2c_read_delay
@@ -67,7 +72,7 @@ def __init__(self, i2c, address=0x52, i2c_read_delay=0.002):
67
72
i2c_dev .write (b"\xFB \x00 " )
68
73
69
74
@property
70
- def values (self ):
75
+ def values (self ) -> Type [ tuple ] :
71
76
"""The current state of all values."""
72
77
self ._read_data ()
73
78
return self ._Values (
@@ -77,33 +82,33 @@ def values(self):
77
82
)
78
83
79
84
@property
80
- def joystick (self ):
85
+ def joystick (self ) -> Type [ tuple ] :
81
86
"""The current joystick position."""
82
87
return self ._joystick ()
83
88
84
89
@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."""
87
92
return self ._buttons ()
88
93
89
94
@property
90
- def acceleration (self ):
95
+ def acceleration (self ) -> Type [ tuple ] :
91
96
"""The current accelerometer reading."""
92
97
return self ._acceleration ()
93
98
94
- def _joystick (self , do_read = True ):
99
+ def _joystick (self , do_read : bool = True ) -> Type [ tuple ] :
95
100
if do_read :
96
101
self ._read_data ()
97
102
return self ._Joystick (self .buffer [0 ], self .buffer [1 ]) # x, y
98
103
99
- def _buttons (self , do_read = True ):
104
+ def _buttons (self , do_read : bool = True ) -> Type [ tuple ] :
100
105
if do_read :
101
106
self ._read_data ()
102
107
return self ._Buttons (
103
108
not bool (self .buffer [5 ] & 0x02 ), not bool (self .buffer [5 ] & 0x01 ) # C # Z
104
109
)
105
110
106
- def _acceleration (self , do_read = True ):
111
+ def _acceleration (self , do_read : bool = True ) -> Type [ tuple ] :
107
112
if do_read :
108
113
self ._read_data ()
109
114
return self ._Acceleration (
@@ -112,10 +117,10 @@ def _acceleration(self, do_read=True):
112
117
((self .buffer [5 ] & 0x0C ) >> 2 ) | (self .buffer [4 ] << 2 ), # az
113
118
)
114
119
115
- def _read_data (self ):
120
+ def _read_data (self ) -> bytearray :
116
121
return self ._read_register (b"\x00 " )
117
122
118
- def _read_register (self , address ):
123
+ def _read_register (self , address ) -> bytearray :
119
124
with self .i2c_device as i2c :
120
125
i2c .write (address )
121
126
time .sleep (self ._i2c_read_delay ) # at least 200us
0 commit comments