Wiki

WIIMOTE

This is the a sample python script that use serial to send command to the arduino board. it's still not complete but should help you study the wiimote and specialy the #bot.write command. pressind A+B quit the script. Those motions are based on OGA arduino sketch that add a command to turn off the servos.

import time
import sys
import serial
import os
import time
import datetime 
import cwiid

#Serial connection for interfacting with Arduino board on Rapiro
rapiro = serial.Serial('/dev/ttyAMA0', 57600, timeout = 10)

#Container for joystik input
data = []

#Define colors codes and maximum intensity for the RGB LED
MAXRED = 255
MAXGREEN = 225
MAXBLUE = 255
MAXTIME = 010

RED = MAXRED
GREEN = MAXGREEN
BLUE = MAXBLUE
TIME = MAXTIME

###################################################
def timeSleep(t):
    if t>0:
        time.sleep(t)
def a_calc(x, c):
    x = 255 & (x + 128)
    y = (int(x/c[0]) + c[1] ) * c[2]
    z = int((y + c[3]) * c[4] + c[5])
    if z >= c[6]:
        z = c[6]
    elif z <= c[7]:
        z = c[7]
    return(z)
################################################

#RGB COLOR program with timing.
def led(RED,GREEN,BLUE,TIME):
    s = "#PR" + str(RED).zfill(3) + "G" + str(GREEN).zfill(3) + "B" + str(BLUE).zfill(3) + "T" + str(TIME).zfill(3)
    return (s)
	
#Print Max values, and Changing colors. All max= White.
#TIME is the fading speed 000 change the color instantaneously, 255 very slowly.

rapiro.write(led(RED,GREEN,BLUE,TIME))
print(led(RED,GREEN,BLUE,TIME))
time.sleep(0.2)
rapiro.write(led(147,023,239,020))
print rapiro.write('#H')
print 'now we play with the Wiimote..\n'
time.sleep(0.5)



#connecting to the wiimote. This allows several attempts
# as first few often fail.

print 'Press 1 + 2 on your Wii Remote now ...'
time.sleep(1)
wm = None
i=2
while not wm:
	try:
		wm=cwiid.Wiimote()
		
	except RuntimeError:
		if (i>5):
			print("cannot create connection")
			
			quit()
		print "Error opening wiimote connection"
		print "attempt " + str(i)
		i +=1
print 'Wii Remote connected...\n'
print 'Press PLUS and MINUS together to disconnect and quit.\n'
wm.rumble = 1
time.sleep(0.5)
wm.rumble = 0
#set wiimote to report button presses and accelerometer state
wm.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC #| cwiid.RPT_CLASSIC | cwiid.RPT_NUNCHUK

#turn on led to show connection has been established
wm.led = 1

button_delay = 0.3



#PS3 joystik input code, adapt it for the wiimote..
'''
while 1:
    for character in file.read(1):
        data += ['%02X' % ord(character)]
        if len(data) == 8:
            if mode6[data[6]] == 'button':
                #print(data)
                mode    = mode6[data[6]]
                state   = state4[data[4]]
                type    = button7[data[7]]['type']
                act     = button7[data[7]][state]
                actWait = button7[data[7]][tconv[state]]
                sys.stdout.write('You %s the %s %s. action=%s\n' % (state, type, mode, act))
                timeSleep(actWait)
                com.write(act)
            elif mode6[data[6]] == 'analog':
                now = datetime.datetime.now()
                now_time = now.minute * 60000 + now.second * 1000 + now.microsecond/1000
                a_data = int(data[5],16)

                if data[7] == '00': #Left stick L-R     PS06
                    LLR = a_calc(a_data, stick7[data[7]])
                    joy = True
					RED = a_calc(a_data, wiiaccdata[0])
					
'''

print 'connected, starting the loop........'

while True:
	#wiimote
	wiibuttons = wm.state['buttons']
	buttons = wiibuttons
	wiiacc = wm.state['acc']
	
	if (buttons & cwiid.BTN_1):
		print (wm.state)
		time.sleep(0.8)
	if (buttons & cwiid.BTN_2):
		print (wm.state)
		time.sleep(0.2)
	if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0): 
		print '\nClosing connection ...'
		wm.rumble = 1
		time.sleep(0.4)
		wm.rumble = 0
		rapiro.write('#M08')
		time.sleep(5)
		rapiro.write('#M00')
		time.sleep(3)
		print '\nExiting program ...'
		rapiro.write('#H')
		exit(wm)  
	if (buttons - cwiid.BTN_A - cwiid.BTN_B == 0):
		print '\nExtra rapiro movement STOP'
		time.sleep(1)
		rapiro.write('#H')
	if (buttons & cwiid.BTN_HOME):
		print 'Home Button pressed'
		time.sleep(button_delay)           
		rapiro.write("#H")
	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_A):
		print 'Button A pressed'
		time.sleep(button_delay)          
	if (buttons & cwiid.BTN_B):
		print 'INFORMATION YOU NEED TO HACK'
		print(wm.state)
		time.sleep(button_delay)
	
		print('\nThis is nupposed to change the ligh so adjust the colors to match 0 to 255\n'+"#PR" + str(RED) + "G" + str(GREEN) +  "B" + str(BLUE) + 'T' + str(TIME))
		print str(wiiacc) + 'wii ACC data in str format\n'		
		rapiro.write(led(wiiacc[0],wiiacc[1],wiiacc[2],'010'))
		
	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)