Tarcas
Posts: 741
Joined: Thu Jan 09, 2014 5:38 am
Location: USA

Scripts for controlling HDMI devices from the RasPi

Wed Jan 29, 2014 12:00 am

I wrote a pair of scripts for myself to control the TV and Bluray from the command line using simple commands like "tv on" and "bd eject" so I thought I'd share. I expect there are other people who will find these useful.

Please note that I have NOT implemented all features, for example buttons on the remote are mostly unimplemented. As my TV does not seem to support any audio commands, or receiving control forwarding from other connected devices, I have not added these into these scripts. Feel free, of course, to add such functionality if your hardware supports it. Also note that since this was for my own use, some of the aliases for my TV's inputs (input 1 being aliased to input bluray, for example) won't make sense for others who have their devices connected to different inputs. These are easy enough to change for your own use. You may also find that the "tx 14" beginning of the bluray commands might be wrong for you. The format here is that 1 is the address of the RasPi sending the command, and the 4 is the address of the BD player receiving the command. If this is not correct for you, you can change these addresses to match your setup. You can find the address of your pi by issuing the cec-client -l command. If it's not 1, you'll have to change both files.

Code: Select all

#!/bin/bash

# must be called with a parameter
# tv.sh [command]
# will send the associated command to the TV over CEC.

if [ $# -lt 1 ] #Check to see if at least one parameter was supplied
then
  echo "Must be called with the command to send to the television"
  echo "Examples include on, off, status, and input."
  echo "example: " $0 "input PC"        # $0 is the name of the program
  echo "For help, use: " $0 " -? "
  exit 1
fi

case $1 in
  "-?")       echo "Supported commands include: on, off, status,"
              echo "input [source]" ;;
  ## begin list of commands.
  ## most of these came from http://www.cec-o-matic.com/
  ## more can be added, including proprietary commands.
  "on")       echo "on 0" | cec-client -s ;;
  "off")      echo "standby 0" | cec-client -s ;;
  "status")   echo "pow 0" | cec-client -s |grep "power status:" ;;
  "as")       echo "as" | cec-client -s ;; ## Switch Active Source to this device.
  "active")   echo "as" | cec-client -s ;; ## Same as as. This is better for voice control.

  "input")
  if [ $# -ge 2 ]       # if there were 2 or more parameters
  then
    case $2 in          # check the second one
        # NOTE: These must all be broadcast to work. (2nd nibble must be F)
      "1")      echo "tx 1F 82 10 00" | cec-client -s ;;
      "bluray") echo "tx 1F 82 10 00" | cec-client -s ;; # same as 1
      "2")      echo "tx 1F 82 20 00" | cec-client -s ;;
      "3")      echo "tx 1F 82 30 00" | cec-client -s ;;
      "pc")     echo "tx 1F 82 30 00" | cec-client -s ;; # same as 3
      "raspi")  echo "tx 1F 82 30 00" | cec-client -s ;; # same as 3
      "4")      echo "tx 1F 82 40 00" | cec-client -s ;;
    esac
  else
    echo "input needs a second parameter"
    echo "usage: " $0 " input [input name]"
    echo "input name is 1-4, bluray, pc, or raspi"
  fi
  ;;  # end of the input case

  *) echo $1 "is not a recognized parameter. " $0 " -? for a list." ;;
esac
exit 0
And for the Blu-Ray player:

Code: Select all

#!/bin/bash

# must be called with a parameter
# bd.sh [command]
# will send the associated command to the Blu-Ray Disc player over CEC.

if [ $# -lt 1 ] #Check to see if at least one parameter was supplied
then
  echo "Must be called with the command to send to the BluRay player"
  echo "Examples include on, off, and pause."
  echo "example: " $0 "pause"           # $0 is the name of the program
  echo "for help, use: " $0 " -?"
  exit 1
fi

case $1 in
  "-?")       echo "Supported commands include: on, off, status,"
              echo "pause, forward, reverse, stop, eject, "
              echo "ff, rew, play, backward, slow." ;;
  ## begin list of commands.
  ## most of these came from http://www.cec-o-matic.com/
  ## more can be added, including proprietary commands.
  "on")       echo "on 4" | cec-client -s ;;
  "off")      echo "standby 4" | cec-client -s ;;
  "status")   echo "pow 4" | cec-client -s |grep "power status:";;
  "pause")    echo "tx 14 44 46" | cec-client -s ;;
  "forward")  echo "tx 14 42 01" | cec-client -s ;;
  "reverse")  echo "tx 14 42 02" | cec-client -s ;;
  "stop")     echo "tx 14 42 03" | cec-client -s ;;
  "eject")    echo "tx 14 42 04" | cec-client -s ;;
  "ffslow")   echo "tx 14 41 05" | cec-client -s ;;
  "ffmed")    echo "tx 14 41 06" | cec-client -s ;;
  "ffmax")    echo "tx 14 41 07" | cec-client -s ;;
  "ff")
  if [ $# -ge 2 ]       # if there were 2 or more parameters
  then
    case $2 in # check the second one
      "slow")   echo "tx 14 41 05" | cec-client -s ;;
      "med")    echo "tx 14 41 06" | cec-client -s ;;
      "medium") echo "tx 14 41 06" | cec-client -s ;; # medium is same as med
      "fast")   echo "tx 14 41 07" | cec-client -s ;;
      "max")    echo "tx 14 41 07" | cec-client -s ;; # max is same as fast
    esac
  else
    echo "tx 14 41 06" | cec-client -s  # FF with no parameters will be medium.
  fi
  ;;  # end of the ff case
  "rew")
  if [ $# -ge 2 ]       # if there were 2 or more parameters
  then
    case $2 in          # check the second one
      "slow")   echo "tx 14 41 09" | cec-client -s ;;
      "med")    echo "tx 14 41 0A" | cec-client -s ;;
      "medium") echo "tx 14 41 0A" | cec-client -s ;; # medium is same as med
      "fast")   echo "tx 14 41 0B" | cec-client -s ;;
      "max")    echo "tx 14 41 0B" | cec-client -s ;; # max is same as fast
    esac
  else
    echo "tx 14 41 06" | cec-client -s  # FF with no parameters will be medium.
  fi
  ;;                    # End the rew case.
  "rewslow")  echo "tx 14 41 09" | cec-client -s ;;
  "rewmed")   echo "tx 14 41 0A" | cec-client -s ;;
  "rewmax")   echo "tx 14 41 0B" | cec-client -s ;;
  "play")     echo "tx 14 41 24" | cec-client -s ;;
  "backward") echo "tx 14 41 20" | cec-client -s ;;
  "slow")     echo "tx 14 1B 15" | cec-client -s ;;

  *) echo $1 "is not a recognized parameter. " $0 " -? for a list." ;;
esac
exit 0


Enjoy! I look forward to hearing from others who find this helpful

Return to “Beginners”