View source for Talk:Freerunner LightSaber
From Openmoko
You do not have permission to edit this page, for the following reasons:
You can view and copy the source of this page:
Return to Talk:Freerunner LightSaber.
You do not have permission to edit this page, for the following reasons:
You can view and copy the source of this page:
Return to Talk:Freerunner LightSaber.
import struct from math import sqrt import pygame.mixer import os import threading import gtk import time
saberoff="/usr/share/ko-lightsaber/saberoff.ogg"
pygame.mixer.init() soff=pygame.mixer.Sound(saberoff)
ledon="echo '1'> /sys/devices/platform/gta02-led.0/leds\:gta02-power\:blue/brightness" ledoff="echo '0'> /sys/devices/platform/gta02-led.0/leds\:gta02-power\:blue/brightness"
class MyThread ( threading.Thread ):
def run (self):
self.hum="/usr/share/ko-lightsaber/hum.ogg" self.saberon="/usr/share/ko-lightsaber/saberon.ogg" self.crash="/usr/share/ko-lightsaber/crash1.ogg" self.swing="/usr/share/ko-lightsaber/swing1.ogg"
self.on=pygame.mixer.Sound(self.saberon) self.on.play()
self.sound = pygame.mixer.Sound(self.hum) self.sound.play(loops=-1, maxtime=0, fade_ms=0)
self.swingsound=pygame.mixer.Sound(self.swing) self.crashsound=pygame.mixer.Sound(self.crash)
self.q = 0
self.x = 0
self.y = 0
self.z = 0
self.secondsensorfile = "/dev/input/event3"
#int, int, short, short, int
fmt = 'iihhi'
#open file in binary mode
in_file = open(self.secondsensorfile,"rb")
event = in_file.read(16)
while event:
(time1,time2, type, code, value) = \ struct.unpack(fmt,event) #timea = time1 + time2 / 1000.0
timea = time2 / 1000.0
#if type == 2: if type == 2 or type == 3:
if code == 0: self.x = value
if code == 1:
self.y = value if code == 2:
self.z = value if type == 0 and code == 0:
sum = int(sqrt(self.x*self.x + self.y*self.y + self.z*self.z)) print timea, self.x, self.y, self.z, sum if self.y < 0: if self.q == 0:
self.swingsound.play()
print "LEFT" self.q = 1
if self.y > 0: if self.q == 1:
self.crashsound.play() os.system(ledon) os.system("echo '255' > /sys/class/leds/neo1973\:vibrator/brightness") #time.sleep(.1) os.system("echo '0' > /sys/class/leds/neo1973\:vibrator/brightness") os.system(ledoff)
print "RIGHT" self.q = 0
event = in_file.read(16)
in_file.close()
gtk.gdk.threads_init() class ImageClick(object):
def __init__(self):
MyThread().start()
self.window = gtk.Window(gtk.WINDOW_TOPLEVEL) self.window.connect('destroy', lambda w: gtk.main_quit()) self.window.set_title("KO-Lightsaber by: Kris Occhipinti")
self.window.set_border_width(10)
#get the curront path
self.box = gtk.VBox()
self.window.add(self.box)
self.box.show()
self.label1 = gtk.Label("KO-Lightsaber by: Kris Occhipinti")
self.box.pack_start(self.label1, False, False, 0) self.label1.show()
button = gtk.Button("Exit")
button.connect("clicked", self.destroy) self.box.pack_start(button) button.show()
self.window.show()
def destroy(self, widget, data=None):
soff.play() time.sleep(2) os.system("echo '0'> /sys/devices/platform/gta02-led.0/leds\:gta02-power\:blue/brightness")
os.system("echo '0' > /sys/class/leds/neo1973\:vibrator/brightness") t2 = os.getpid() print t2 os.system("kill " + str(t2)) print "EXIT" gtk.main_quit()
def main(self): gtk.main()
if __name__ == "__main__":
image_click = ImageClick() image_click.main()
gtk.gdk.threads_enter() gtk.main() gtk.gdk.threads_leave()