Created
September 13, 2019 20:19
-
-
Save elinorbgr/74b75bd4d1867c37ae1218690452a7cf to your computer and use it in GitHub Desktop.
Simple & basic script to monitor device orientation & rotate sway display accordingly
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import math | |
import time | |
from subprocess import call | |
DEVICE_PATH = '/sys/bus/iio/devices/iio:device0' | |
POOL_PERIOD = 0.5 | |
MONITOR_NAME = 'eDP-1' | |
ROT_NORMAL = 0 | |
ROT_90 = 1 | |
ROT_180 = 2 | |
ROT_270 = 3 | |
SQRT2 = math.sqrt(2) | |
def read_accel(): | |
with open(DEVICE_PATH + '/in_accel_scale', 'r') as f: | |
s = float(f.read()) | |
with open(DEVICE_PATH + '/in_accel_x_raw', 'r') as f: | |
x = float(f.read()) | |
with open(DEVICE_PATH + '/in_accel_y_raw', 'r') as f: | |
y = float(f.read()) | |
with open(DEVICE_PATH + '/in_accel_z_raw', 'r') as f: | |
z = float(f.read()) | |
return (s*x, s*y, s*z) | |
def compute_direction(x, y): | |
tan_2 = y / (math.sqrt(x**2 + y**2) + x) | |
if tan_2 < -1.0 - SQRT2: | |
return ROT_90 | |
elif tan_2 < 1.0 - SQRT2: | |
return ROT_NORMAL | |
elif tan_2 < SQRT2 - 1.0: | |
return ROT_270 | |
elif tan_2 < SQRT2 + 1.0: | |
return ROT_180 | |
else: | |
return ROT_90 | |
def change_monitor(direction): | |
if direction == ROT_90: | |
transform = "90" | |
elif direction == ROT_180: | |
transform = "180" | |
elif direction == ROT_270: | |
transform = "270" | |
else: | |
transform = "normal" | |
call(["swaymsg", "output", MONITOR_NAME, "transform", transform]) | |
if __name__ == "__main__": | |
previous_direction = None | |
while True: | |
(x,y,z) = read_accel() | |
direction = compute_direction(x,y) | |
if previous_direction is not None and direction != previous_direction: | |
# change orientation | |
change_monitor(direction) | |
previous_direction = direction | |
time.sleep(POOL_PERIOD) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment