Yet another one - this time in Python. Should be easy to port, though.
Features:
- no dimming
- no stopping if backlight not lit
- badly adjusted timers
- reopening the file at every check
- able to set the right position even if device is nearly lying
- does not switch if on verge of two modes
...and the two latter things are what I like in it.
The timers are really bad - it takes up to 10 seconds to adjust (checking
position every 3 seconds, 1.5 second to make sure plus some unidentified delay).
#!/usr/bin/env python
import struct
import subprocess
from time import sleep
secondsensorfile = "/dev/input/event3"
#int, int, short, short, int
fmt = 'iihhi'
def get_data(path):
sensor = open(path, 'rb')
ret = [0, 0, 0]
while True:
pass
event = sensor.read(16)
if not event:
break
(time1, time2, type_, code, value) = struct.unpack(fmt, event)
if type_ == 0 and code == 0:
break
if type_ == 2:
ret[code] = value
sensor.close()
return ret
def get_direction():
ind = None
di = None
for i in range(3):
data = get_data(secondsensorfile)
print data
a = map(abs, data)
max_val = max(a[:2])
if max_val * 4 < sum(a):
return
index = a.index(max_val)
if index < 2 and a[index] < a[-index + 1] * 2:
return
if ind is None:
ind = index
elif not ind == index:
return
direction = data[index] > 0
if di is None:
di = direction
elif not di == direction:
return
sleep(0.5)
# print data, index
return ind, di
def rotate(name):
subprocess.Popen(['/usr/bin/xrandr', '-o', name])
def rotation(index, direction):
d = ['<>', 'v^', '.o']
r = [['right', 'left'], ['normal', 'inverted']]
print d[index][int(direction)]
if index < len(r):
pass
# subprocess.Popen(['/usr/bin/xrandr', '-o', r[index][int(direction)]])
return r[index][int(direction)]
last = 'normal'
while True:
print 'vGet'
d = get_direction()
if d:
print 'got'
index, direction = d
r = rotation(index, direction)
if r:
if not r == last:
last = r
rotate(r)
sleep(3)
_______________________________________________
Openmoko community mailing list
community@lists.openmoko.org
http://lists.openmoko.org/mailman/listinfo/community