5
5
"""
6
6
`adafruit_nunchuk`
7
7
================================================================================
8
-
9
8
CircuitPython library for Nintendo Nunchuk controller
10
-
11
-
12
9
* Author(s): Carter Nelson
13
-
14
10
Implementation Notes
15
11
--------------------
16
-
17
12
**Hardware:**
18
-
19
13
* `Wii Remote Nunchuk <https://en.wikipedia.org/wiki/Wii_Remote#Nunchuk>`_
20
14
* `Wiichuck <https://www.adafruit.com/product/342>`_
21
-
22
15
**Software and Dependencies:**
23
-
24
16
* Adafruit CircuitPython firmware for the supported boards:
25
17
https://github.com/adafruit/circuitpython/releases
26
18
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
38
30
class Nunchuk :
39
31
"""
40
32
Class which provides interface to Nintendo Nunchuk controller.
41
-
42
33
:param i2c: The `busio.I2C` object to use.
43
34
:param address: The I2C address of the device. Default is 0x52.
44
35
:type address: int, optional
@@ -58,7 +49,6 @@ def __init__(self, i2c, address=0x52, i2c_read_delay=0.002):
58
49
self .buffer = bytearray (8 )
59
50
self .i2c_device = I2CDevice (i2c , address )
60
51
self ._i2c_read_delay = i2c_read_delay
61
- self ._do_read = True
62
52
time .sleep (_I2C_INIT_DELAY )
63
53
with self .i2c_device as i2c_dev :
64
54
# turn off encrypted data
@@ -69,55 +59,52 @@ def __init__(self, i2c, address=0x52, i2c_read_delay=0.002):
69
59
70
60
@property
71
61
def values (self ):
72
- """Return named tuple of all the input values."""
62
+ """The current state of all values."""
73
63
self ._read_data ()
74
- self ._do_read = False
75
- values = self ._Values (
76
- self ._Joystick (self .joystick [0 ], self .joystick [1 ]),
77
- self ._Buttons (self .button_C , self .button_Z ),
78
- self ._Acceleration (
79
- self .acceleration [0 ], self .acceleration [1 ], self .acceleration [2 ]
80
- ),
64
+ return self ._Values (
65
+ self ._joystick (do_read = False ),
66
+ self ._buttons (do_read = False ),
67
+ self ._acceleration (do_read = False ),
81
68
)
82
- self ._do_read = True
83
- return values
84
69
85
70
@property
86
71
def joystick (self ):
87
- """Return tuple of current joystick position."""
88
- if self ._do_read :
89
- self ._read_data ()
90
- return self .buffer [0 ], self .buffer [1 ]
72
+ """The current joystick position."""
73
+ return self ._joystick ()
91
74
92
75
@property
93
- def button_C (self ): # pylint: disable=invalid-name
94
- """Return current pressed state of button C."""
95
- if self ._do_read :
96
- self ._read_data ()
97
- return not bool (self .buffer [5 ] & 0x02 )
76
+ def buttons (self ): # pylint: disable=invalid-name
77
+ """The current pressed state of button Z."""
78
+ return self ._buttons ()
98
79
99
80
@property
100
- def button_Z (self ): # pylint: disable=invalid-name
101
- """Return current pressed state of button Z."""
102
- if self ._do_read :
81
+ def acceleration (self ):
82
+ """The current accelerometer reading."""
83
+ return self ._acceleration ()
84
+
85
+ def _joystick (self , do_read = True ):
86
+ if do_read :
103
87
self ._read_data ()
104
- return not bool (self .buffer [5 ] & 0x01 )
88
+ return self . _Joystick (self .buffer [0 ], self . buffer [ 1 ]) # x, y
105
89
106
- @property
107
- def acceleration (self ):
108
- """Return 3 tuple of accelerometer reading."""
109
- if self ._do_read :
90
+ def _buttons (self , do_read = True ):
91
+ if do_read :
110
92
self ._read_data ()
111
- x = (self .buffer [5 ] & 0xC0 ) >> 6
112
- x |= self .buffer [2 ] << 2
113
- y = (self .buffer [5 ] & 0x30 ) >> 4
114
- y |= self .buffer [3 ] << 2
115
- z = (self .buffer [5 ] & 0x0C ) >> 2
116
- z |= self .buffer [4 ] << 2
117
- return x , y , z
93
+ return self ._Buttons (
94
+ not bool (self .buffer [5 ] & 0x02 ), # C
95
+ not bool (self .buffer [5 ] & 0x01 ) # Z
96
+ )
97
+
98
+ def _acceleration (self , do_read = True ):
99
+ if do_read :
100
+ self ._read_data ()
101
+ return self ._Acceleration (
102
+ ((self .buffer [5 ] & 0xC0 ) >> 6 ) | (self .buffer [2 ] << 2 ), #ax
103
+ ((self .buffer [5 ] & 0x30 ) >> 4 ) | (self .buffer [3 ] << 2 ), #ay
104
+ ((self .buffer [5 ] & 0x0C ) >> 2 ) | (self .buffer [4 ] << 2 ), #az
105
+ )
118
106
119
107
def _read_data (self ):
120
- """Reads all of the raw input data from i2c."""
121
108
return self ._read_register (b"\x00 " )
122
109
123
110
def _read_register (self , address ):
0 commit comments