26
26
* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
27
27
"""
28
28
import time
29
+ from collections import namedtuple
29
30
from adafruit_bus_device .i2c_device import I2CDevice
30
31
31
32
__version__ = "0.0.0-auto.0"
@@ -48,6 +49,11 @@ class Nunchuk:
48
49
:type i2c_read_delay: float, optional
49
50
"""
50
51
52
+ _Values = namedtuple ("Values" , ("joystick" , "buttons" , "acceleration" ))
53
+ _Joystick = namedtuple ("Joystick" , ("x" , "y" ))
54
+ _Buttons = namedtuple ("Buttons" , ("C" , "Z" ))
55
+ _Acceleration = namedtuple ("Acceleration" , ("x" , "y" , "z" ))
56
+
51
57
def __init__ (self , i2c , address = 0x52 , i2c_read_delay = 0.002 ):
52
58
self .buffer = bytearray (8 )
53
59
self .i2c_device = I2CDevice (i2c , address )
@@ -61,32 +67,66 @@ def __init__(self, i2c, address=0x52, i2c_read_delay=0.002):
61
67
i2c_dev .write (b"\xFB \x00 " )
62
68
63
69
@property
64
- def joystick (self ):
65
- """Return tuple of current joystick position ."""
70
+ def values (self ):
71
+ """The current state of all values ."""
66
72
self ._read_data ()
67
- return self .buffer [0 ], self .buffer [1 ]
73
+ return self ._Values (
74
+ self ._joystick (do_read = False ),
75
+ self ._buttons (do_read = False ),
76
+ self ._acceleration (do_read = False ),
77
+ )
68
78
69
79
@property
70
- def button_C (self ): # pylint: disable=invalid-name
71
- """Return current pressed state of button C ."""
72
- return not bool ( self ._read_data ()[ 5 ] & 0x02 )
80
+ def joystick (self ):
81
+ """The current joystick position ."""
82
+ return self ._joystick ( )
73
83
74
84
@property
75
- def button_Z (self ): # pylint: disable=invalid-name
76
- """Return current pressed state of button Z."""
77
- return not bool ( self ._read_data ()[ 5 ] & 0x01 )
85
+ def buttons (self ): # pylint: disable=invalid-name
86
+ """The current pressed state of button Z."""
87
+ return self ._buttons ( )
78
88
79
89
@property
80
90
def acceleration (self ):
81
- """Return 3 tuple of accelerometer reading."""
82
- self ._read_data ()
83
- x = (self .buffer [5 ] & 0xC0 ) >> 6
84
- x |= self .buffer [2 ] << 2
85
- y = (self .buffer [5 ] & 0x30 ) >> 4
86
- y |= self .buffer [3 ] << 2
87
- z = (self .buffer [5 ] & 0x0C ) >> 2
88
- z |= self .buffer [4 ] << 2
89
- return x , y , z
91
+ """The current accelerometer reading."""
92
+ return self ._acceleration ()
93
+
94
+ @property
95
+ def button_C (self ): # pylint: disable=invalid-name
96
+ """
97
+ The current pressed state of button C.
98
+ """
99
+ print ("`button_C` is deprecated. Please use the `buttons` property instead" )
100
+ return self ._buttons ().C
101
+
102
+ @property
103
+ def button_Z (self ): # pylint: disable=invalid-name
104
+ """
105
+ The current pressed state of button Z.
106
+ """
107
+ print ("`button_Z` is deprecated. Please use the `buttons` property instead" )
108
+ return self ._buttons ().Z
109
+
110
+ def _joystick (self , do_read = True ):
111
+ if do_read :
112
+ self ._read_data ()
113
+ return self ._Joystick (self .buffer [0 ], self .buffer [1 ]) # x, y
114
+
115
+ def _buttons (self , do_read = True ):
116
+ if do_read :
117
+ self ._read_data ()
118
+ return self ._Buttons (
119
+ not bool (self .buffer [5 ] & 0x02 ), not bool (self .buffer [5 ] & 0x01 ) # C # Z
120
+ )
121
+
122
+ def _acceleration (self , do_read = True ):
123
+ if do_read :
124
+ self ._read_data ()
125
+ return self ._Acceleration (
126
+ ((self .buffer [5 ] & 0xC0 ) >> 6 ) | (self .buffer [2 ] << 2 ), # ax
127
+ ((self .buffer [5 ] & 0x30 ) >> 4 ) | (self .buffer [3 ] << 2 ), # ay
128
+ ((self .buffer [5 ] & 0x0C ) >> 2 ) | (self .buffer [4 ] << 2 ), # az
129
+ )
90
130
91
131
def _read_data (self ):
92
132
return self ._read_register (b"\x00 " )
0 commit comments