Talk:Freerunner LightSaber

From Openmoko

(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
<nowiki>
+
<code>
 
#!/usr/bin/python
 
#!/usr/bin/python
 
import struct
 
import struct
Line 157: Line 157:
 
     gtk.main()
 
     gtk.main()
 
     gtk.gdk.threads_leave()
 
     gtk.gdk.threads_leave()
</nowiki>
+
</code>

Revision as of 00:34, 2 September 2009

  1. !/usr/bin/python

import struct from math import sqrt import pygame.mixer import os import threading import gtk import time

  1. Created by Kris Occhipinti
  2. www.filmsbykris.com
  3. Jan. 16 2009
  4. Copyright 2009 Kris Occhipinti
  5. Some changes by tomix
  6. program is distributed under the terms of the GNU GPL
  1. This program is free software: you can redistribute it and/or modify
  2. it under the terms of the GNU General Public License as published by
  3. the Free Software Foundation, either version 3 of the License, or
  4. (at your option) any later version.
  1. This program is distributed in the hope that it will be useful,
  2. but WITHOUT ANY WARRANTY; without even the implied warranty of
  3. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  4. GNU General Public License for more details.
  1. You should have received a copy of the GNU General Public License
  2. along with this program. If not, see <http://www.gnu.org/licenses/>.


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
  1. create the table
       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()

Personal tools

#!/usr/bin/python import struct from math import sqrt import pygame.mixer import os import threading import gtk import time #Created by Kris Occhipinti #www.filmsbykris.com #Jan. 16 2009 #Copyright 2009 Kris Occhipinti #Some changes by tomix #program is distributed under the terms of the GNU GPL # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. 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 #create the table 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()