Skip to content

Instantly share code, notes, and snippets.

@Fordi
Last active August 29, 2015 03:31
Show Gist options
  • Save Fordi/426c9536f7af3468aecd to your computer and use it in GitHub Desktop.
Save Fordi/426c9536f7af3468aecd to your computer and use it in GitHub Desktop.
Bluetooth joypad-watching daemon for RetroPie
#!/bin/bash
# For my RetroPie-based Game Frame
# When a bluetooth controller is connected, will turn on the screen, put the processor into
# "performance" mode, and start EmulationStation
# When any controller presses L+R+Start+Select, the reverse will occur:
# ES is killed, the processor is put into "ondemand" mode, HDMI is turned off, and all BT
# devices are disconnected.
watch_pad() {
DEV=/dev/input/$1
CONNECT="$2"
DISCONNECT="$3"
SIG_POWER="$4"
CONNECT_STATE="$(test -e /dev/input/$1 && echo "yes" || echo "no")"
while true; do
SKIP=4
if [[ ! -e /dev/input/$1 ]]; then
sleep 5
else
if [[ "$CONNECT_STATE" == "no" ]]; then
$CONNECT $DEV
fi
CONNECT_STATE="yes"
jstest --event /dev/input/$1 2>/dev/null |
while read event; do
if [[ $SKIP -eq 0 ]]; then
event=$(echo "$event" | cut -d : -f 2)
type=$(echo "$event" | cut -d ',' -f 1 | cut -d \ -f 3)
if [[ $type -ge 128 ]]; then
type=$(expr $type - 128)
fi
number=$(echo "$event" | cut -d ',' -f 3 | cut -d \ -f 3)
state=$(echo "$event" | cut -d ',' -f 4 | cut -d \ -f 3)
eval "T${type}_N${number}=$state"
if [[ $T1_N6 == 1 && $T1_N7 == 1 && $T1_N10 == 1 && $T1_N11 == 1 ]]; then
$SIG_POWER $DEV
fi
else
SKIP=$(expr $SKIP - 1)
fi
done
CONNECT_STATE="no"
$DISCONNECT $DEV
fi
done
trap
}
PIDS=
watch_pad js0 'retropie-wake.sh' 'echo disconnect' 'retropie-suspend.sh' & PIDS="$PIDS $!"
watch_pad js1 'retropie-wake.sh' 'echo disconnect' 'retropie-suspend.sh' & PIDS="$PIDS $!"
watch_pad js2 'retropie-wake.sh' 'echo disconnect' 'retropie-suspend.sh' & PIDS="$PIDS $!"
watch_pad js3 'retropie-wake.sh' 'echo disconnect' 'retropie-suspend.sh' & PIDS="$PIDS $!"
kill_jobs() {
for pid in $PIDS; do
CHILDREN=$(ps -opid,ppid -A | grep -E '[0-9]+ '"$pid" | grep -Eo '^[0-9]+')
echo "killing $CHILDREN $pid"
kill -9 $CHILDREN > /dev/null
kill -9 $pid > /dev/null
done
sleep 5
}
trap kill_jobs EXIT
for pid in $PIDS; do
wait $pid >/dev/null
done
#!/bin/bash
tv_is_on() {
if [[ -n "$(tvservice -s | grep '0x120002')" ]]; then
return 1
else
return 0
fi
}
any_bt_connected() {
if [[ -n "$(hidd --list)" ]]; then
return 0
else
return 1
fi
}
echo "ondemand" | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor >/dev/null
if ps -C emulationstation > /dev/null; then
killall emulationstation
fi
if tv_is_on; then
tvservice -o
fi
if any_bt_connected; then
sudo hciconfig hci0 down
sleep 1
sudo hciconfig hci0 up
sudo hciconfig hci0 pscan
fi
#!/bin/bash
tv_is_off() {
if [[ -z "$(tvservice -s | grep '0x120002')" ]]; then
return 1
else
return 0
fi
}
if tv_is_off; then
tvservice -p
sleep 1
fbset -accel false
fbset -accel true
fi
echo "performance" | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor >/dev/null
if ! ps -C emulationstation > /dev/null 2>&1; then
echo "Starting EmulationStation"
emulationstation &
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment