Skip to content

Instantly share code, notes, and snippets.

@joac
Created June 20, 2013 21:23
Show Gist options
  • Save joac/5826770 to your computer and use it in GitHub Desktop.
Save joac/5826770 to your computer and use it in GitHub Desktop.
import pygame
from pygame.locals import QUIT
from bluetooth import BluetoothSocket, BluetoothError, L2CAP
import thread, time
WIDTH = 800 #512
HEIGHT = 256 # 256 Don't change this one
#ADDRESS = '00:1F:32:8C:A7:B0' # Cambiar por la mac del wiimote que estan probando, la mac se averigua con el comando "hcitool scan"
#ADDRESS = '00:1E:35:DE:29:82' # Cambiar por la mac del wiimote que estan probando, la mac se averigua con el comando "hcitool scan"
ADDRESS = "00:18:00:2E:72:41"
# Listening thread
def listener():
global connected, sensor, first, second
fdin.settimeout(0.1)
while connected == 1:
try:
msg = fdin.recv(23)
except BluetoothError:
continue
if len(msg) >= 7:
first, second = ord(msg[2]) , ord(msg[3])
for c in range(3):
sensor[c] = ord(msg[4 + c])
fdin.close()
fdout.close()
connected = -1
# Try to connect to Wiimote
print 'connecting to Wiimote ' + ADDRESS + ', please press buttons 1 and 2'
fdin = BluetoothSocket(L2CAP)
fdin.connect((ADDRESS, 0x13))
fdout = BluetoothSocket(L2CAP)
fdout.connect((ADDRESS, 0x11))
if not fdin or not fdout:
raise 'could not connect to Wiimote, check the address'
# Open window
pygame.init()
window = pygame.display.set_mode((WIDTH, HEIGHT), 0)
pygame.display.set_caption('Wiiewer')
# Run listener
first , second = 0, 0
old = [127] * 3
sensor = [127] * 3
p_old = 127
pend_old = 0
connected = 1
thread.start_new_thread(listener, ())
fdout.send("\x52\x12\x00\x31")
# Main display loop
while connected == 1:
pixels = pygame.surfarray.pixels3d(window)
for c in range(3):
for t in range(min(old[c], sensor[c]) + 1, max(old[c], sensor[c])):
pixels[WIDTH - 3][t][c] = 255
pixels[WIDTH - 2][sensor[c]][c] = 255
promedio = p_old + 0.6 * (sensor[2] -p_old)
for t in range(int(min(p_old, promedio)) + 1, int(max(p_old, promedio))):
pixels[WIDTH - 3][t][0] = 255
pixels[WIDTH - 3][t][1] = 255
pixels[WIDTH - 3][t][2] = 255
pixels[WIDTH - 2][int(promedio)][0] = 255
pixels[WIDTH - 2][int(promedio)][1] = 255
pixels[WIDTH - 2][int(promedio)][2] = 255
pixels[WIDTH - 2][120][0] = 255
pixels[WIDTH - 2][120][1] = 255
pixels[WIDTH - 2][100][0] = 255
pixels[WIDTH - 2][100][1] = 255
for c in range(3):
old[c] = sensor[c]
a_button = (second & 0x8 ) == 0x8
b_button = (second & 0x4) == 0x4
pend_new = promedio - p_old
if pend_new > 0 and pend_old < 0 and sensor[2] < 115:
speed = min(160 - sensor[2], 127)
print "BEAT que suena!!", sensor[2], speed
if b_button:
print "Apretaron el boton b"
if a_button:
print "apretaron el boton a"
pend_old = pend_new
p_old = promedio
del pixels
window.unlock()
window.blit(window, (-1, 0))
pygame.display.flip()
for ev in pygame.event.get():
if ev.type == QUIT:
connected = 0
time.sleep(0.01)
while connected == 0:
time.sleep(0.01)
pygame.display.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment