I wrote this really simple python frontend for communication with the wiimote over a serial connection. The code is kinda sloppy but it gets the job done. The python script requires you to have Cwiid installed and also Pygame. I stole most of it from the SimpleUI example. I hope to increase it's usability by adding other options like being able to choose what information you send. Right now it's sends only data from two of the accelerometers.
import pygame
from pygame.locals import *
import cwiid
import serial
class SimpleUI:
def __init__(self):
# Initialize PyGame
self.buttonlist = []
self.wiimote = 0
pygame.init()
pygame.display.set_caption('Wiiserial')
self.font = pygame.font.Font(None, 25)
self.screen = pygame.display.set_mode((640,480))
self.screen.fill((255,255,255))
self.ser = serial.Serial()
self.ser.port = '/dev/ttyUSB0' #I had to hardcode this so you should change it!
self.ser.baudrate = 9600
self.ser.timeout = 1
self.sending = 0
self.portopen = 0
def send(self):
if not self.ser.isOpen() and self.portopen:
self.ser.open()
if self.sending and self.ser.isOpen():
try:
#self.ser.write(chr((self.wiimote.state['acc'][cwiid.X]-95)*2))
#self.ser.write(chr((self.wiimote.state['acc'][cwiid.Y]-95)*2))
self.ser.write(chr((self.wiimote.state['acc'][cwiid.X]-95)*2) + chr((self.wiimote.state['acc'][cwiid.Y]-95)*2))
self.ser.read()
except AttributeError:
print "Either there is no wiimote or it is not talking correctly!"
self.sending = 0
except ValueError: pass
elif self.ser.isOpen() and not self.portopen: self.ser.close()
def run(self):
# Run the event loop
self.loop()
# Close the port so you don't break it!
self.ser.close()
print "Port open:", self.ser.isOpen()
# Close the Pygame window
pygame.quit()
def loop(self):
exit = False
while not exit:
exit = self.handleEvents()
self.draw()
self.send()
def handleEvents(self):
exit = False
for event in pygame.event.get():
if event.type == QUIT:
exit = True
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
exit = True
elif event.type == MOUSEBUTTONDOWN:
self.handleMouseDown(pygame.mouse.get_pos())
return exit
def handleMouseDown(self, (x, y)):
for button in self.buttonlist:
if (button[0].collidepoint(x, y)):
button[2]()
def button(self, text, x, y, function):
text = self.font.render(text, 1, (10, 10, 10, 0))
textRect = text.get_rect()
self.buttonlist.append((Rect(x, y, textRect.width+30, 2*textRect.height), text, function))
def draw(self):
for button in self.buttonlist:
pygame.draw.rect(self.screen, SimpleUI.RED, button[0])
self.screen.blit(button[1], (button[0].x+15,button[0].y+.25*button[0].height))
pygame.display.update()
def Pair():
print "You should press 1 and 2 on your wiimote now."
try:
ThisUI.wiimote = cwiid.Wiimote()
ThisUI.wiimote.rpt_mode = cwiid.RPT_ACC
print "Wiimote paired sucessfuly!"
except RuntimeError:
print "Thank you come again"
def OpenSer():
if ThisUI.portopen == 0:
print "Serial port opening at", ThisUI.ser.portstr
ThisUI.portopen = 1
else:
print "Closing serial port! Stand back!!!"
ThisUI.portopen = 0
ThisUI.sending = 0
def SendStuff():
if ThisUI.portopen == 1:
if ThisUI.sending == 0:
print "Starting communications"
ThisUI.sending = 1
else:
print "Communication terminated"
ThisUI.sending = 0
else: print "You must open the port first!"
ThisUI = SimpleUI()
ThisUI.button('Pair your wiimote', 100, 100, Pair)
ThisUI.button('This button is really long and I hope it works', 100, 150, Pair)
ThisUI.button('Open/close Serial port', 100, 200, OpenSer)
ThisUI.button('Send/stop sending data', 100, 250, SendStuff)
# Start the game
ThisUI.run()
I've run into a few problems with the USB port not working after a successful termination. The only way I've found to get around the Error #5 is to completely restart. I'm not sure if it's my code or my machine so any help would be nice. The code I'm running on my arduino is just a slight variation of the Dimmer example. It is as follows:
const int Blue = 9; // Blue LED
const int Green = 10; // Green LED
void setup()
{
// initialize the serial communication:
Serial.begin(9600);
// initialize the ledPin as an output:
pinMode(Green, OUTPUT);
pinMode(Blue, OUTPUT);
}
void loop() {
byte g;
byte b;
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (which will be from 0 to 255):
g = Serial.read();
delay(10);
b = Serial.read();
Serial.println(1);
// set the brightness of the LED:
analogWrite(Green, g);
analogWrite(Blue, b);
}
}