3 Derived from https://github.com/togald/thinkpad-l390-yoga-scripts/
4 Derived from https://github.com/ffejery/thinkpad-l380-yoga-scripts
6 Rotates any detected screen, wacom digitizers, touchscreens,
7 touchpad/trackpoint based on orientation gathered from accelerometer.
8 Tested with Lenovo ThinkPad L380 Yoga
9 https://github.com/ffejery/thinkpad-l380-yoga-scripts
10 Originally from AdmiralAkber:
11 Tested with Lenovo Thinkpad Yoga S1
12 https://github.com/admiralakber/thinkpad-yoga-scripts
15 https://gist.githubusercontent.com/ei-grad/4d9d23b1463a99d24a8d/raw/rotate.py
25 class IIODevice(object):
26 def __init__(self, device_dir):
27 # raise an exception if the directory does not exist
29 self.device_dir = device_dir
31 def _prop(self, name, ret_type = None):
32 data = open(os.path.join(self.device_dir, name), 'r').read()
33 if ret_type is not None:
39 return self._prop('name')
43 return self.accel_x, self.accel_y
47 return self._prop('in_accel_x_raw', float) * self._prop('in_accel_x_scale', float)
51 return self._prop('in_accel_y_raw', float) * self._prop('in_accel_y_scale', float)
54 for device_dir in glob('/sys/bus/iio/devices/iio:device*'):
55 iio_device = IIODevice(device_dir)
56 if 'accel' in iio_device.name:
59 sys.stderr.write("Can't find an accellerator device!\n")
63 g = 7.0 # (m^2 / s) sensibility, gravity trigger
68 'condition': lambda x, y: y > g,
71 'condition': lambda x, y: y <= -g,
74 'condition': lambda x, y: x > g,
77 'condition': lambda x, y: x <= -g,
82 def notify_new_orientation(orientation):
83 open('/dev/orientation', 'w').write(orientation)
86 if __name__ == '__main__':
87 current_orientation_name = None
90 x, y = iio_device.accel_xy
92 for orientation_name, orientation in orientations.items():
93 if orientation_name == current_orientation_name:
96 if orientation['condition'](x, y):
97 notify_new_orientation(orientation_name)
98 current_orientation_name = orientation_name
100 # TODO listen on events instead of periodic polling