Skip to content

Instantly share code, notes, and snippets.

@mattwilliamson
Created August 17, 2010 04:05
Show Gist options
  • Save mattwilliamson/528413 to your computer and use it in GitHub Desktop.
Save mattwilliamson/528413 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import pygame
from pygame.locals import *
import serial, time, random, os, datetime
ser = None
last_transmit = None
pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption('Controller')
pygame.mouse.set_visible(0)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((150, 150, 150))
bulb = pygame.image.load("ktip.png")
bulb = bulb.convert_alpha()
antenna_off = pygame.image.load("irkickoff.png")
antenna_off = antenna_off.convert_alpha()
antenna_on = pygame.image.load("irkickflash.png")
antenna_on = antenna_on.convert_alpha()
def send(data):
global ser, last_transmit
last_transmit = datetime.datetime.now()
if ser == None:
ser = serial.Serial('/dev/tty.usbserial-A70064Mh', 9600)
ser.write(data)
is_on = False
done = False
while not done:
for event in pygame.event.get():
if (event.type == KEYDOWN):
# print event
if (event.key == K_ESCAPE):
done = True
elif event.key in [K_SPACE, 66, 98, 13]:
print 'Light On'
is_on = True
send('1')
elif (event.type == KEYUP):
is_on = False
print 'Light OFF'
send('0')
screen.blit(background, (0,0))
alpha = 255 if is_on else 10
bulb.set_alpha(alpha)
if is_on:
mouseX, mouseY = pygame.mouse.get_pos()
screen.blit(bulb, (mouseX - 24, mouseY - 24))
transmit_diff = datetime.datetime.now() - last_transmit if last_transmit else None
if transmit_diff and transmit_diff.seconds < .1:
screen.blit(antenna_on, (100 - 24, 100 - 24))
else:
last_transmit = None
screen.blit(antenna_off, (100 - 24, 100 - 24))
pygame.display.flip()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment