#!/bin/bash
set -e
# command to make rotation
# normal, left, inverted or right will be passed as the first arg
COMMAND="$(dirname "$0")/rotate.sh"
# paths to sensor files
DEVICE=/sys/bus/iio/devices/iio:device0
DEVICE_X="$DEVICE/in_accel_x_raw"
DEVICE_Y="$DEVICE/in_accel_y_raw"
# min sensor value to call rotation
THRESHOLD=10000
# time interval in seconds
INTERVAL=2
PREV_ROT=
while true; do
X="$(cat "$DEVICE/in_accel_x_raw")"
Y="$(cat "$DEVICE/in_accel_y_raw")"
ROT="$PREV_ROT"
if [ "$X" -gt "$THRESHOLD" ]; then
ROT="normal"
elif [ "$X" -lt "-$THRESHOLD" ]; then
ROT="inverted"
elif [ "$Y" -gt "$THRESHOLD" ]; then
ROT="right"
elif [ "$Y" -lt "-$THRESHOLD" ]; then
ROT="left"
fi
if [ "$ROT" != "$PREV_ROT" ]; then
"$COMMAND" "$ROT"
PREV_ROT="$ROT"
# double wait immediatelly after rotation
sleep "$INTERVAL"
fi
sleep "$INTERVAL"
done