-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnunchuck.py
More file actions
27 lines (26 loc) · 839 Bytes
/
nunchuck.py
File metadata and controls
27 lines (26 loc) · 839 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import smbus
import time
bus = smbus.SMBus(1)
bus.write_byte_data(0x52,0x40,0x00)
time.sleep(0.1)
while True:
try:
bus.write_byte(0x52,0x00)
time.sleep(0.1)
data0 = bus.read_byte(0x52)
data1 = bus.read_byte(0x52)
data2 = bus.read_byte(0x52)
data3 = bus.read_byte(0x52)
data4 = bus.read_byte(0x52)
data5 = bus.read_byte(0x52)
joy_x = data0
joy_y = data1
accel_x = (data2 << 2) + ((data5 & 0x0c) >> 2)
accel_y = (data3 << 2) + ((data5 & 0x30) >> 4)
accel_z = (data4 << 2) + ((data5 & 0xc0) >> 6)
buttons = data5 & 0x03
button_c = (buttons == 1) or (buttons == 2)
button_z = (buttons == 0) or (buttons == 2)
print 'Jx: %s Jy: %s Ax: %s Ay: %s Az: %s Bc: %s Bz: %s' % (joy_x, joy_y, accel_x, accel_y, accel_z, button_c, button_z)
except IOError as e:
print e