Skip to content

Instantly share code, notes, and snippets.

@MagicMicky
Created September 23, 2015 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MagicMicky/6f14b37ec9f551959d50 to your computer and use it in GitHub Desktop.
Save MagicMicky/6f14b37ec9f551959d50 to your computer and use it in GitHub Desktop.
Audio detection turn on hdmi
#!/bin/bash
SWITCH_ON_CMD="echo 'on 0' > /tmp/cec.fifo"
CHANGE_HDMI="echo 'as' > /tmp/cec.fifo"
if [ -e "/tmp/cec.fifo" ]; then
echo proceeding: file exists
else
echo You need to start the server.
exit
fi
old_state=Idle
while true; do
if grep -q RUNNING /proc/asound/card*/*p/*/status 2>&1; then
if [ "$old_state" != "playing" ]; then
echo "should cec"
eval $SWITCH_ON_CMD
eval $CHANGE_HDMI
old_state=playing
fi
echo "Playing"
else
if [ "$old_state" != "idle" ]; then
old_state=idle
fi
echo "Idle"
fi
sleep 0.5
done;
#!/bin/bash
# found on http://www.whizzy.org/wp-content/uploads/2012/11/cecserver.sh_.txt
# Set up a fifo and connect cec-client to it
#
# By Will Cooke. http://www.whizzy.org
# It's a very hacky solution, but it seems to just about work.
#
# Version 1. Does the job. November 2012
#
CECLOG=/tmp/cec.log
CECDEV=/dev/ttyACM0
CECFIFO=/tmp/cec.fifo
CECCLIENT="/usr/local/bin/cec-client -d 8 -p 1 -b 5 -t p -o MythTV -f $CECLOG $CECDEV"
log(){
echo "SERVER: $1" >> $CECLOG
}
stop(){
# Kill the right proceses
log "Begin shutting down..."
# Using this hacky grep so that we only match those tail processes looking
# at /dev/null rather than, say, syslog
declare -a TAILPIDS=(`ps aux | grep 'tailf /dev/null' | egrep -v grep | awk '{print $2}'`)
declare -a CATPIDS=(`ps aux | grep 'cat $CECFIFO' | egrep -v grep | awk '{print $2}'`)
if [ ${#TAILPIDS[@]} -gt 0 ]
then
# Found some old tail processes to kill
log "Found some tail processes..."
for i in "${TAILPIDS[@]}"
do
log "Killing $i"
kill $i
done
fi
if [ ${#CATPIDS[@]} -gt 0 ]
then
# Found some old cat processes to kill
# It's unlikely we will ever get in here, because the previous tail
# processes have been killed and so shut down this end of the pipe
# already.
log "Found some cat processes..."
for i in "${CATPIDS[@]}"
do
log "Killing $i"
kill $i
done
fi
log "Asking cec-client to stop if it's running..."
# Using signal 2, the same as a ctrl-c
killall -s 2 cec-client 2> $CECLOG
log "Trying to remove FIFO..."
rm $CECFIFO 2> $CECLOG
log "Done shutting down."
}
case "${1}" in
start|restart)
log "Starting server. Since only one server can run at a time, stopping first."
stop
log "Done stopping, now starting..."
log "Setting up FIFOs..."
# We use a FIFO to pass in CEC commands to the cec-client which comes
# with libcec.
mkfifo $CECFIFO
log "Open pipe for writing..."
# We use tailf /dev/null because it doesn't disconnect from stdin when
# put in the background and it doesn't cause any load when running.
tailf /dev/null > $CECFIFO &
log "Opening pipe for reading and start cec-client..."
# Since we're writing to a log file anyway we don't need the output from
# cec-client. Put the whole thing in brackets to background the lot.
(cat $CECFIFO | $CECCLIENT &) > /dev/null
log "Start up complete."
;;
stop)
stop
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment