Tuesday, December 7, 2010

More video

Roughly the same program as in the previous post, though I have fiddled with it slightly.




#!/usr/bin/python

import cwiid
from time import sleep

print "Press 1+2 on the Wiimote now"
wiimote = cwiid.Wiimote()

# Rumble to indicate a connection
wiimote.rumble = 1
print "Connection established - release buttons"
sleep(0.2)
wiimote.rumble = 0
sleep(2.0)
print "Up / Down / Left / Right = toggle LEDs."
print "A/B = long/short vibrate."
print "+/- = start/stop display of accelerometer data."
print "1/2 = start/stop display of infra-red data."
print "Home = end program."

wiimote.enable(cwiid.FLAG_MESG_IFC)
wiimote.rpt_mode = cwiid.RPT_ACC | cwiid.RPT_BTN | cwiid.RPT_IR | cwiid.RPT_STATUS

loop = True
show_acc = False
show_infrared = False
last_acc = (0, 0, 0)
delta_acc = [0, 0, 0]
led_status = 0
last_ir = ""
rumble_counter = 0

while (loop):
sleep(0.01)
messages = wiimote.get_mesg()
if rumble_counter:
rumble_counter = rumble_counter - 1
if rumble_counter == 0:
wiimote.rumble = 0
for mesg in messages:
# Accelerometer:
if mesg[0] == cwiid.MESG_ACC:
if show_acc:
acc = mesg[1]
if acc != last_acc:
for i in range(3):
delta_acc[i] = acc[i] - last_acc[i]
print "Acc: {0[0]:5} {0[1]:5} {0[2]:5} {1}".format(
delta_acc, acc)
last_acc = acc
# Button:
elif mesg[0] == cwiid.MESG_BTN:
if mesg[1] & cwiid.BTN_HOME:
print "Ending Program"
loop = False
if mesg[1] & cwiid.BTN_PLUS and not show_acc:
show_acc = True
if mesg[1] & cwiid.BTN_MINUS and show_acc:
print "Ending accelerometer display"
show_acc = False
if mesg[1] & cwiid.BTN_B:
rumble_counter = 10
wiimote.rumble = 1
if mesg[1] & cwiid.BTN_A:
rumble_counter = 30
wiimote.rumble = 1
if mesg[1] & cwiid.BTN_UP:
led_status = led_status ^ cwiid.LED1_ON
wiimote.led = led_status
if mesg[1] & cwiid.BTN_DOWN:
led_status = led_status ^ cwiid.LED2_ON
wiimote.led = led_status
if mesg[1] & cwiid.BTN_LEFT:
led_status = led_status ^ cwiid.LED3_ON
wiimote.led = led_status
if mesg[1] & cwiid.BTN_RIGHT:
led_status = led_status ^ cwiid.LED4_ON
wiimote.led = led_status
if mesg[1] & cwiid.BTN_1 and not show_infrared:
show_infrared = True
last_ir = ""
if mesg[1] & cwiid.BTN_2 and show_infrared:
print "Ending IR display"
show_infrared = False
# Infra-red
elif mesg[0] == cwiid.MESG_IR:
if show_infrared:
sources = mesg[1]
output = "IR: "
for spot in sources:
if spot:
output = output + \
"S {0} P {1:12}".format(spot["size"], spot["pos"])
found = True
if output != last_ir:
print output
last_ir = output
else:
print mesg

No comments:

Post a Comment