2 # -*- coding: UTF-8 -*-
7 Accelerometer code: Martin Senkerik <martinsenkerik@gmail.com>
15 import threading
, time
, gtk
21 firstsensorfile
= "/dev/input/event3"
23 #int, int, short, short, int
26 #open file in binary mode
27 accel1
= open(firstsensorfile
,"rb")
32 except ZeroDivisionError:
35 class Worker(threading
.Thread
):
38 def __init__(self
, form
):
39 if debug
: print "DEBU:","Accelerometer thread initialized"
43 threading
.Thread
.__init
__ ( self
, name
="Worker" )
46 event
= accel1
.read(16)
48 if debug
: "DEBUG:","Accelerometer thread running"
49 self
.form
.ready_semaphore
.acquire()
51 ## Repeat while not received signal to stop (through bRun)
52 while event
and self
.bRun
:
54 (time1
, time2
, type, code
, value
) = struct
.unpack(fmt
,event
)
55 ## Only reading X, Y now
58 self
.x
.low_pass(time1
, time2
, value
)
59 self
.y
.low_pass(time1
, time2
)
60 #self.z.low_pass(time1, time2)
62 self
.x
.low_pass(time1
, time2
)
63 self
.y
.low_pass(time1
, time2
, value
)
64 #self.z.low_pass(time1, time2)
66 #self.x.low_pass(time1, time2)
67 #self.y.low_pass(time1, time2)
68 #self.z.low_pass(time1, time2, value)
70 if type == 0 and code
== 0:
71 ## When new values read, change ship speed in game
72 self
.form
.drawing_area
.shipratio
= self
.y
.val
/-1000.0
73 event
= accel1
.read(16)
75 ## If finished, clean up
80 """ Calibrate if sensor readings are not centered where they ought to """
81 self
.x
.calib
= self
.x
.val
82 self
.y
.calib
= self
.y
.val
93 def _get_delta(self
, time1
, time2
):
94 new_time1
= long(time1
) - long(self
.old_time1
)
95 new_time2
= long(time2
) - long(self
.old_time2
)
101 self
.old_time1
= time1
102 self
.old_time2
= time2
104 return float("%s.%s" % (str(new_time1
), str(new_time2
)))
106 def low_pass(self
, time1
, time2
, value
= None):
109 #---------------------------
112 value
= self
.val
- self
.calib
115 value
= value
- self
.calib
117 #dt = self._get_delta(time1, time2)
118 #alpha = dt / float(self.RC + dt)
120 #self.val_lp = self.last_val_lp + alpha * (value - self.last_val_lp)
123 #self.last_val_lp = self.val_lp
127 return (float(self
.val_lp
) / 1000) * (pi
/2)
130 return "<axis %s (%s)>" % (str(int(self
.val_lp
)), str(self
.val
))