/ adafruit_nunchuk.py
adafruit_nunchuk.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2019 Carter Nelson for Adafruit Industries 4 # 5 # Permission is hereby granted, free of charge, to any person obtaining a copy 6 # of this software and associated documentation files (the "Software"), to deal 7 # in the Software without restriction, including without limitation the rights 8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 # copies of the Software, and to permit persons to whom the Software is 10 # furnished to do so, subject to the following conditions: 11 # 12 # The above copyright notice and this permission notice shall be included in 13 # all copies or substantial portions of the Software. 14 # 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 # THE SOFTWARE. 22 """ 23 `adafruit_nunchuk` 24 ================================================================================ 25 26 CircuitPython library for Nintendo Nunchuk controller 27 28 29 * Author(s): Carter Nelson 30 31 Implementation Notes 32 -------------------- 33 34 **Hardware:** 35 36 * `Wii Remote Nunchuk <https://en.wikipedia.org/wiki/Wii_Remote#Nunchuk>`_ 37 * `Wiichuck <https://www.adafruit.com/product/342>`_ 38 39 **Software and Dependencies:** 40 41 * Adafruit CircuitPython firmware for the supported boards: 42 https://github.com/adafruit/circuitpython/releases 43 * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice 44 """ 45 import time 46 from adafruit_bus_device.i2c_device import I2CDevice 47 48 __version__ = "0.0.0-auto.0" 49 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Nunchuk.git" 50 51 _DEFAULT_ADDRESS = 0x52 52 _I2C_INIT_DELAY = 0.1 53 _I2C_READ_DELAY = 0.01 54 55 56 class Nunchuk: 57 """Class which provides interface to Nintendo Nunchuk controller.""" 58 59 def __init__(self, i2c, address=_DEFAULT_ADDRESS): 60 self.buffer = bytearray(6) 61 self.i2c_device = I2CDevice(i2c, address) 62 time.sleep(_I2C_INIT_DELAY) 63 with self.i2c_device as i2c_dev: 64 # turn off encrypted data 65 # http://wiibrew.org/wiki/Wiimote/Extension_Controllers 66 i2c_dev.write(b"\xF0\x55") 67 time.sleep(_I2C_INIT_DELAY) 68 i2c_dev.write(b"\xFB\x00") 69 70 @property 71 def joystick(self): 72 """Return tuple of current joystick position.""" 73 self._read_data() 74 return self.buffer[0], self.buffer[1] 75 76 @property 77 def button_C(self): # pylint: disable=invalid-name 78 """Return current pressed state of button C.""" 79 return not bool(self._read_data()[5] & 0x02) 80 81 @property 82 def button_Z(self): # pylint: disable=invalid-name 83 """Return current pressed state of button Z.""" 84 return not bool(self._read_data()[5] & 0x01) 85 86 @property 87 def acceleration(self): 88 """Return 3 tuple of accelerometer reading.""" 89 self._read_data() 90 x = (self.buffer[5] & 0xC0) >> 6 91 x |= self.buffer[2] << 2 92 y = (self.buffer[5] & 0x30) >> 4 93 y |= self.buffer[3] << 2 94 z = (self.buffer[5] & 0x0C) >> 2 95 z |= self.buffer[4] << 2 96 return x, y, z 97 98 def _read_data(self): 99 return self._read_register(b"\x00") 100 101 def _read_register(self, address): 102 with self.i2c_device as i2c: 103 time.sleep(_I2C_READ_DELAY) 104 i2c.write(address) 105 time.sleep(_I2C_READ_DELAY) 106 i2c.readinto(self.buffer) 107 time.sleep(_I2C_READ_DELAY) 108 return self.buffer