#!/usr/bin/python import bluetooth import threading import time import sys import wave from threading import Thread from visual import * wiimote_adress="00:21:BD:01:68:92" receiveSocket = bluetooth.BluetoothSocket(bluetooth.L2CAP) controlSocket = bluetooth.BluetoothSocket(bluetooth.L2CAP) etat = "deconnecte" def connect() : global etat receiveSocket.connect((wiimote_adress, 0x13)) controlSocket.connect((wiimote_adress, 0x11)) if receiveSocket and controlSocket : etat = "connecte" wiiThread = threading.Thread(None, receive, None, (1,), None) wiiThread.start() return True else : print "connexion rejetee" return False def actionsOnButtons(etat, data) : octet1 = ord(data[2]) octet2 = ord(data[3]) if (octet1 & 0x01) == 1 : print "pad gauche" if (octet1 & 0x02) == 2 : print "pad droit" if (octet1 & 0x04) == 4 : print "pad bas" if (octet1 & 0x08) == 8 : print "pad haut" if (octet1 & 0x10) == 0x10 : print "plus" if (octet2 & 0x01) == 1 : print "2" if (octet2 & 0x02) == 2 : print "1" if (octet2 & 0x04) == 4 : print "B" if (octet2 & 0x08) == 8 : print "deconnection" etat = "deconnecte" return etat def actionsOnMode30(etat) : while etat == "connecte" : try : data = receiveSocket.recv(23) chaine = "" if len(data) == 4 : etat = actionsOnButtons(etat, data) except bluetooth.BluetoothError : pass receiveSocket.close() controlSocket.close() etat = "deconnecte" return def actionsOnMode31(etat, mode) : scene2 = display(title='Graph of position',width=750, height=750, center=(5,0,0), background=(0,0,0)) scene2.autoscale = 0 mybox = frame(pos=(5,-2,0)) sph=sphere(frame=mybox, radius=0.4)# length=0.4, width=2, height=0.4) box(frame=mybox, length=0.05, width=0.05, height=100, color=(0,1,0)) box(frame=mybox, length=100, width=0.05, height=0.05, color=(0,1,0)) box(frame=mybox, length=0.05, width=100, height=0.05, color=(0,1,0)) box(pos=(0,0,0), length=0.01, width=10, height=10, color=(0.6,0.6,0.6)) box(pos=(10,0,0), length=0.01, width=10, height=10, color=(0.6,0.6,0.6)) box(pos=(5,-5,0), length=10, width=10, height=0.01, color=(0.6,0.6,0.6)) box(pos=(5,5,0), length=10, width=10, height=0.01, color=(0.6,0.6,0.6)) box(pos=(5,0,-5), length=10, width=0.01, height=10, color=(0.3,0.3,0.3)) inc = [0, 0, 0] myarrow=arrow(pos=(0,0,1), axis=[0,1,0] , shaftwidth=0.5, color=color.red) while etat == "connecte" : try : if not(10 > mybox.pos[0] > 0 and 5 > mybox.pos[1] > -5 and 5 > mybox.pos[2] > -5) : mybox.pos = (5,-2,0) myarrow.axis = (0,0,0) print "HEY" sph.color = color.red controlSocket.send(chr(0x52) + chr(0x11) + chr(0x01)) time.sleep(0.2) sph.color = color.white time.sleep(0.2) sph.color = color.red time.sleep(0.2) sph.color = color.white time.sleep(0.2) sph.color = color.red time.sleep(0.2) controlSocket.send(chr(0x52) + chr(0x11) + chr(0x00)) sph.color = color.white inc[0] = 0 inc[1] = 0 inc[2] = 0 controlSocket.send(chr(0x14) + chr(0x04)) time.sleep(0.3) controlSocket.send(chr(0x19) + chr(0x04)) time.sleep(0.3) controlSocket.send(chr(0x16) + chr(0x04) + chr(0xa2) + chr(0x00) + chr(0x09) + chr(0x01) + chr(0x01)) time.sleep(0.3) controlSocket.send(chr(0x16) + chr(0x04) + chr(0xa2) + chr(0x00) + chr(0x01) + chr(0x01) + chr(0x08)) time.sleep(0.3) controlSocket.send(chr(0x16) + chr(0x04) + chr(0xa2) + chr(0x00) + chr(0x01) + chr(0x07) + chr(0x00) + chr(0x40) + chr(0x40) + chr(0x1f) + chr(0x40) + chr(0x00) + chr(0x00)) time.sleep(0.3) controlSocket.send(chr(0x16) + chr(0x04) + chr(0xa2) + chr(0x00) + chr(0x08) + chr(0x01) + chr(0x01)) time.sleep(0.3) controlSocket.send(chr(0x19) + chr(0x00)) time.sleep(0.3) read = wave.open("sound.wav", 'r') for i in range(100) : controlSocket.send(chr(0x18) + chr(read.getsampwidth()*10) + read.readframes(read.getsampwidth()*10)) data = receiveSocket.recv(23) chaine = "" if len(data) == 7 : etat = actionsOnButtons(etat, data) if (ord(data[3]) & 0x04) == 4 : X = ord(data[4]) Y = ord(data[5]) Z = ord(data[6]) inc[0] = (X - 120) inc[1] = (Z - 140) inc[2] = -(Y - 120) myarrow.axis = 1.3 * norm(inc) inc[0] *= 0.001 inc[1] *= 0.001 inc[2] *= 0.001 mybox.pos += inc myarrow.pos = mybox.pos except bluetooth.BluetoothError : pass receiveSocket.close() controlSocket.close() etat = "deconnecte" sph.color = color.blue myarrow.color = color.blue return def receive(nb) : global etat global mode controlSocket.send(chr(0x52) + chr(0x12) + chr(0x00) + chr(mode)) receiveSocket.settimeout(0.1) if mode == 0x30 : print "mode touches" return actionsOnMode30(etat) elif mode == 0x31 : print "mode accelerometre" return actionsOnMode31(etat, mode) mode = 0 if len(sys.argv) > 1 : if sys.argv[1] == "touches" : mode = 0x30 elif sys.argv[1] == "accelerometre" : mode = 0x31 if mode : connect() else : print "veuillez specifier un mode"