105 lines
2.5 KiB
Python
Executable File
105 lines
2.5 KiB
Python
Executable File
#!/usr/bin/env python2
|
|
"""
|
|
thinkpad-rotate.py
|
|
|
|
Rotates any detected screen, wacom digitizers, touchscreens,
|
|
touchpad/trackpoint based on orientation gathered from accelerometer.
|
|
|
|
Tested with Lenovo Thinkpad Yoga S1
|
|
|
|
https://github.com/admiralakber/thinkpad-yoga-scripts
|
|
|
|
Acknowledgements:
|
|
Modified from source:
|
|
https://gist.githubusercontent.com/ei-grad/4d9d23b1463a99d24a8d/raw/rotate.py
|
|
|
|
"""
|
|
|
|
### BEGIN Configurables
|
|
|
|
rotate_pens = True # Set false if your DE rotates pen for you
|
|
disable_touchpads = False # Don't use in conjunction with tablet-mode
|
|
ignore_touchpads = True
|
|
grace_time = 3
|
|
|
|
### END Configurables
|
|
|
|
|
|
from time import sleep, time
|
|
from os import path as op
|
|
import sys
|
|
from subprocess import check_call, check_output
|
|
from glob import glob
|
|
from os import environ
|
|
|
|
def bdopen(fname):
|
|
return open(op.join(basedir, fname))
|
|
|
|
|
|
def read(fname):
|
|
return bdopen(fname).read()
|
|
|
|
|
|
for basedir in glob('/sys/bus/iio/devices/iio:device*'):
|
|
if 'accel' in read('name'):
|
|
break
|
|
else:
|
|
sys.stderr.write("Can't find an accellerator device!\n")
|
|
sys.exit(1)
|
|
|
|
|
|
env = environ.copy()
|
|
|
|
scale = float(read('in_accel_scale'))
|
|
|
|
g = 7.0 # (m^2 / s) sensibility, gravity trigger
|
|
|
|
STATES = [
|
|
{'rot': 'normal', 'pen': 'none', 'coord': '1 0 0 0 1 0 0 0 1', 'touchpad': 'enable',
|
|
'check': lambda x, y: y <= -g},
|
|
{'rot': 'inverted', 'pen': 'half', 'coord': '-1 0 1 0 -1 1 0 0 1', 'touchpad': 'disable',
|
|
'check': lambda x, y: y >= g},
|
|
{'rot': 'left', 'pen': 'ccw', 'coord': '0 -1 1 1 0 0 0 0 1', 'touchpad': 'disable',
|
|
'check': lambda x, y: x >= g},
|
|
{'rot': 'right', 'pen': 'cw', 'coord': '0 1 0 -1 0 1 0 0 1', 'touchpad': 'disable',
|
|
'check': lambda x, y: x <= -g},
|
|
]
|
|
|
|
|
|
def rotate(state):
|
|
s = STATES[state]
|
|
check_call(['/usr/local/bin/thinkpad-rotate.rb', s['rot']], env=env)
|
|
|
|
def read_accel(fp):
|
|
fp.seek(0)
|
|
return float(fp.read()) * scale
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
accel_x = bdopen('in_accel_x_raw')
|
|
accel_y = bdopen('in_accel_y_raw')
|
|
|
|
current_state = None
|
|
|
|
next_state=None
|
|
next_ts=0.0
|
|
|
|
while True:
|
|
x = read_accel(accel_x)
|
|
y = read_accel(accel_y)
|
|
for i in range(4):
|
|
if i == current_state:
|
|
continue
|
|
if STATES[i]['check'](x, y):
|
|
ts = time()
|
|
if not next_state == i:
|
|
next_state = i
|
|
next_ts = ts
|
|
elif ts - next_ts >= grace_time:
|
|
current_state = i
|
|
rotate(i)
|
|
break
|
|
sleep(1)
|
|
|