├── LICENSE.md ├── README.md └── Raspberry Pi ├── README.md ├── nunchuck.py └── setup.py /LICENSE.md: -------------------------------------------------------------------------------- 1 | Nunchuck Python module by Jason Barnett 2 | 3 | This module and examples are licensed under the "Attribution-ShareAlike 3.0 Unported" (CC BY-SA 3.0) 4 | 5 | You may copy and modify the code, but keep original credit to the author and list changes made. 6 | 7 | A link to the full description is available: 8 | http://creativecommons.org/licenses/by-sa/3.0/ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Nunchuck 2 | 3 | This contains files required for using the Multichuck Adaptor for Raspberry Pi and Arduino. 4 | 5 | The adaptor will be available soon. 6 | 7 | ## Files 8 | 9 | - Raspberry Pi/nunchuck.py - Python module you'll import into your script 10 | 11 | 12 | 13 | 14 | Enjoy! 15 | 16 | Jason 17 | 18 | Twitter @boeeerb 19 | 20 | http://www.boeeerb.co.uk 21 | 22 | Buy from http://xxx 23 | 24 | Donations are always welcome and much appreciated! 25 | BTC - 152kFrxyJ4GNVmqNfbywzvVFBFhJmnPBsV 26 | -------------------------------------------------------------------------------- /Raspberry Pi/README.md: -------------------------------------------------------------------------------- 1 | ## Raspberry Pi 2 | 3 | ### Requirements 4 | 5 | sudo apt-get install python-smbus 6 | 7 | 8 | ### Functions 9 | 10 | The functions of nunchuk are: 11 | 12 | ```python 13 | from nunchuck import nunchuck 14 | wii = nunchuck() 15 | 16 | wii.raw() # Returns all the data in raw 17 | wii.joystick() # Returns just the X and Y positions of the joystick 18 | wii.accelerometer() # Returns X, Y and Z positions of the accelerometer 19 | wii.button_c() # Returns True if C button is pressed, False if not 20 | wii.button_z() # Returns True if Z button is pressed, False if not 21 | 22 | wii.joystick_x() # Returns just the X position of the joystick 23 | wii.joystick_y() # Returns just the Y position of the joystick 24 | wii.accelerometer_x() # Returns just the X position of the accelerometer 25 | wii.accelerometer_y() # Returns just the Y position of the accelerometer 26 | wii.accelerometer_z() # Returns just the Z position of the accelerometer 27 | 28 | wii.scale(value,min,max,out_min,out_max) # Works the same as Arduino Map, perfect for changing values returned to a different scale, ie -100 - +100 29 | ``` 30 | 31 | 32 | ### Installation instructions 33 | 34 | #### Preparation 35 | 36 | First to download and install the required libraries 37 | 38 | sudo apt-get update 39 | sudo apt-get install python-smbus -y 40 | 41 | 42 | - SMBus is required to talk over i2c bus with the Nunchuck 43 | 44 | 45 | To enable the i2c driver you need to make a few changes 46 | 47 | sudo nano /etc/modules 48 | 49 | Then make sure the following is at the end of the file 50 | 51 | i2c-dev 52 | i2c-bcm2708 53 | 54 | So it looks like this: 55 |

56 | etc-modules 57 |

58 | 59 | Ctrl + x and Y to exit save the file, now edit the next 60 | 61 | sudo nano /etc/modprobe.d/raspi-blacklist.conf 62 | 63 | And add the #'s to the beginning of each line so it looks like: 64 | 65 |

66 | etc-modprobe 67 |

68 | 69 | Ctrl + x and Y to exit and save the file, now reboot 70 | 71 | sudo reboot 72 | 73 | #### Downloading 74 | 75 | To be completed 76 | -------------------------------------------------------------------------------- /Raspberry Pi/nunchuck.py: -------------------------------------------------------------------------------- 1 | ########################################## 2 | ## Python module to read a Wii nunchuck ## 3 | ## ## 4 | ## Written by Jason - @Boeeerb ## 5 | ## jase@boeeerb.co.uk ## 6 | ########################################## 7 | ## 8 | ## v0.1 03/05/14 - Initital release 9 | ## v0.2 21/06/14 - Retrieve one byte at a time [Simon Walters - @cymplecy] 10 | ## v0.3 22/06/14 - Minor Refactoring [Jack Wearden - @JackWeirdy] 11 | ## v0.32 25/6/14 - XOR each data byte with 0x17 and then add 0x17 to produce corrent values - Simon Walters @cymplecy 12 | ## v0.4 26/6/14 - Change method of XOR and add delay parameter - Simon Walters @cymplecy 13 | ## v0.41 30/3/15 - Adding support for RPI_REVISION 3 - John Lumley @Jelby-John 14 | 15 | from smbus import SMBus 16 | import RPi.GPIO as rpi 17 | import time as time 18 | 19 | bus = 0 20 | 21 | class nunchuck: 22 | 23 | def __init__(self,delay = 0.05): 24 | self.delay = delay 25 | if rpi.RPI_REVISION == 1: 26 | i2c_bus = 0 27 | elif rpi.RPI_REVISION == 2: 28 | i2c_bus = 1 29 | elif rpi.RPI_REVISION == 3: 30 | i2c_bus = 1 31 | else: 32 | print "Unable to determine Raspberry Pi revision." 33 | exit 34 | self.bus = SMBus(i2c_bus) 35 | self.bus.write_byte_data(0x52,0x40,0x00) 36 | time.sleep(0.1) 37 | 38 | def read(self): 39 | self.bus.write_byte(0x52,0x00) 40 | time.sleep(self.delay) 41 | temp = [(0x17 + (0x17 ^ self.bus.read_byte(0x52))) for i in range(6)] 42 | return temp 43 | 44 | def raw(self): 45 | data = self.read() 46 | return data 47 | 48 | def joystick(self): 49 | data = self.read() 50 | return data[0],data[1] 51 | 52 | def accelerometer(self): 53 | data = self.read() 54 | return data[2],data[3],data[4] 55 | 56 | def button_c(self): 57 | data = self.read() 58 | butc = (data[5] & 0x02) 59 | 60 | return butc == 0 61 | 62 | def button_z(self): 63 | data = self.read() 64 | butc = (data[5] & 0x01) 65 | 66 | return butc == 0 67 | 68 | def joystick_x(self): 69 | data = self.read() 70 | return data[0] 71 | 72 | def joystick_y(self): 73 | data = self.read() 74 | return data[1] 75 | 76 | def accelerometer_x(self): 77 | data = self.read() 78 | return data[2] 79 | 80 | def accelerometer_y(self): 81 | data = self.read() 82 | return data[3] 83 | 84 | def accelerometer_z(self): 85 | data = self.read() 86 | return data[4] 87 | 88 | def setdelay(self,delay): 89 | self.delay = delay 90 | 91 | 92 | def scale(self,value,_min,_max,_omin,_omax): 93 | return (value - _min) * (_omax - _omin) // (_max - _min) + _omin 94 | 95 | -------------------------------------------------------------------------------- /Raspberry Pi/setup.py: -------------------------------------------------------------------------------- 1 | from distutils.core import setup 2 | 3 | classifiers = ['Development Status :: 4 - Beta', 4 | 'Operating System :: POSIX :: Linux', 5 | 'Intended Audience :: Developers', 6 | 'Programming Language :: Python :: 2.7', 7 | 'Topic :: Software Development', 8 | 'Topic :: System :: Hardware'] 9 | 10 | setup(name = 'Nunchuck_pi', 11 | version = '0.1', 12 | author = 'Jason Barnett', 13 | author_email = 'jase@boeeerb.co.uk', 14 | description = 'A module to read a wii nunchuck for Raspberry Pi', 15 | long_description= 'A module to read a wii nunchuck for Raspberry Pi', 16 | license = 'CC BY-SA 3.0', 17 | keywords = 'Raspberry Pi Nunchuck', 18 | url = 'http://www.boeeerb.co.uk', 19 | classifiers = classifiers, 20 | py_modules = ['nunchuck'], 21 | install_requires= ['rpi.gpio >= 0.5.4'] 22 | ) --------------------------------------------------------------------------------