forked from the-raspberry-pi-guy/Wiimote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiimote.py
90 lines (68 loc) · 2.51 KB
/
wiimote.py
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# This program utilises the cwiid Python library in order to get input over bluetooth from a wiimote.
# The following lines of code demonstrate many of the features realted to wiimotes, such as capturing button presses and rumbling the controller.
# I have managed to map the home button to the accelerometer - simply hold it and values will appear!
# Coded by The Raspberry Pi Guy. Work based on some of Matt Hawkins's!
import cwiid, time
button_delay = 0.1
print 'Please press buttons 1 + 2 on your Wiimote now ...'
time.sleep(1)
# This code attempts to connect to your Wiimote and if it fails the program quits
try:
wii=cwiid.Wiimote()
except RuntimeError:
print "Cannot connect to your Wiimote. Run again and make sure you are holding buttons 1 + 2!"
quit()
print 'Wiimote connection established!\n'
print 'Go ahead and press some buttons\n'
print 'Press PLUS and MINUS together to disconnect and quit.\n'
time.sleep(3)
wii.rpt_mode = cwiid.RPT_BTN
while True:
buttons = wii.state['buttons']
# Detects whether + and - are held down and if they are it quits the program
if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
print '\nClosing connection ...'
# NOTE: This is how you RUMBLE the Wiimote
wii.rumble = 1
time.sleep(1)
wii.rumble = 0
exit(wii)
# The following code detects whether any of the Wiimotes buttons have been pressed and then prints a statement to the screen!
if (buttons & cwiid.BTN_LEFT):
print 'Left pressed'
time.sleep(button_delay)
if(buttons & cwiid.BTN_RIGHT):
print 'Right pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_UP):
print 'Up pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_DOWN):
print 'Down pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_1):
print 'Button 1 pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_2):
print 'Button 2 pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_A):
print 'Button A pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_B):
print 'Button B pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_HOME):
wii.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC
check = 0
while check == 0:
print(wii.state['acc'])
time.sleep(0.01)
check = (buttons & cwiid.BTN_HOME)
time.sleep(button_delay)
if (buttons & cwiid.BTN_MINUS):
print 'Minus Button pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_PLUS):
print 'Plus Button pressed'
time.sleep(button_delay)