2626* Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
2727"""
2828import time
29+ from collections import namedtuple
2930from adafruit_bus_device .i2c_device import I2CDevice
3031
3132__version__ = "0.0.0-auto.0"
@@ -48,6 +49,11 @@ class Nunchuk:
4849 :type i2c_read_delay: float, optional
4950 """
5051
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+
5157 def __init__ (self , i2c , address = 0x52 , i2c_read_delay = 0.002 ):
5258 self .buffer = bytearray (8 )
5359 self .i2c_device = I2CDevice (i2c , address )
@@ -61,32 +67,66 @@ def __init__(self, i2c, address=0x52, i2c_read_delay=0.002):
6167 i2c_dev .write (b"\xFB \x00 " )
6268
6369 @property
64- def joystick (self ):
65- """Return tuple of current joystick position ."""
70+ def values (self ):
71+ """The current state of all values ."""
6672 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+ )
6878
6979 @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 ( )
7383
7484 @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 ( )
7888
7989 @property
8090 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+ )
90130
91131 def _read_data (self ):
92132 return self ._read_register (b"\x00 " )
0 commit comments