Last active
August 29, 2015 14:27
-
-
Save rothgar/c2f2d60822aba67894eb to your computer and use it in GitHub Desktop.
set hdmi matrix output
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
#!/bin/bash | |
# Set matrix spliter display | |
# Designed for Monoprice 4x4 Matrix Switch | |
# http://www.monoprice.com/Product?p_id=5704 | |
declare -A INPUT_HASH | |
declare -A OUTPUT_HASH | |
CHECKSUM='\xd5\x7b' | |
if [[ $# -eq 0 ]] && [ -z $MATRIX_CONFIG ]; then | |
set -- "--help" | |
echo "No options passed and \$MATRIX_CONFIG not set." | |
echo "Showing help" | |
fi | |
while [[ $# -ge 1 ]] | |
do | |
option="$1" | |
case $option in | |
-h|--help) | |
echo "" | |
echo "-h: show help" | |
echo "" | |
echo "-f: set input file" | |
echo "input file should say which output (in order abcd) should be set" | |
echo "-f will override MATRIX_CONFIG environment variable" | |
echo "e.g. 1231 (no spaces)" | |
echo "" | |
echo "-p: set serial port" | |
echo "e.g. /dev/ttyS0 (default)" | |
echo "" | |
echo "0: turn off device (only disables output, device can still be controlled)" | |
echo "" | |
echo "[1234abcdABCD] set input/output separated by a space" | |
echo " doesn't need to be in order, just separated by space" | |
echo "e.g. 1 a b 3 c 2 4 d" | |
echo "" | |
echo "run examples:" | |
echo "sudo ./set-matrix.sh 1 a" | |
echo "sudo ./set-matrix.sh a 4 b 2 c 4 d 1" | |
echo "sudo ./set-matrix.sh -f $(hostname)" | |
echo "sudo ./set-matrix.sh -p /dev/ttyS1 2 b 3 c" | |
echo "# get verbose output" | |
echo "VERBOSE=1 sudo ./set-matrix.sh 1 c" | |
echo "# set config file from environment variable" | |
echo "MAXTRIX_CONFIG=$(hostname) sudo ./set-matrix.sh" | |
exit 0 | |
;; | |
-f|--file) | |
FILE="${2}" | |
shift | |
;; | |
1|2|3|4) | |
INPUT_HASH[$2]=$1 | |
shift | |
;; | |
a|b|c|d|A|B|C|D) | |
INPUT_HASH[$1]=$2 | |
shift | |
;; | |
-p|--port) | |
PORT="${2}" | |
shift | |
;; | |
0) | |
OUTPUT_HASH["0"]='\x10\xef'$CHECKSUM | |
;; | |
*) | |
echo "Unknown option $1" | |
exit | |
;; | |
esac | |
shift | |
done | |
# use -f or $MATRIX_CONFIG env variable | |
FILE=${FILE:-$MATRIX_CONFIG} | |
PORT=${PORT:-/dev/ttyS0} | |
# get config from file | |
if [ -n "$FILE" ]; then | |
# read input file | |
if [[ $VERBOSE ]]; then echo "Input File = $FILE"; fi | |
j=0 | |
array=( a b c d ) | |
while IFS= read -r -n1 OUTPUT | |
do | |
if [ -n "$OUTPUT" ]; then | |
INPUT_HASH[${array[j]}]=$OUTPUT | |
fi | |
j=$((j + 1)) | |
done < ${FILE} | |
fi | |
# set output hex based on input values | |
for i in "${!INPUT_HASH[@]}"; do | |
if [[ $VERBOSE ]]; then echo "output $i = input ${INPUT_HASH[$i]}"; fi | |
case $i in | |
a|A) | |
case ${INPUT_HASH["a"]} in | |
1) | |
OUTPUT_HASH["a"]='\x00\xff'$CHECKSUM | |
;; | |
2) | |
OUTPUT_HASH["a"]='\x01\xfe'$CHECKSUM | |
;; | |
3) | |
OUTPUT_HASH["a"]='\x02\xfd'$CHECKSUM | |
;; | |
4) | |
OUTPUT_HASH["a"]='\x03\xfc'$CHECKSUM | |
;; | |
esac | |
;; | |
b|B) | |
case ${INPUT_HASH["b"]} in | |
1) | |
OUTPUT_HASH["b"]='\x04\xfb'$CHECKSUM | |
;; | |
2) | |
OUTPUT_HASH["b"]='\x05\xfa'$CHECKSUM | |
;; | |
3) | |
OUTPUT_HASH["b"]='\x06\xf9'$CHECKSUM | |
;; | |
4) | |
OUTPUT_HASH["b"]='\x07\xf8'$CHECKSUM | |
;; | |
esac | |
;; | |
c|C) | |
case ${INPUT_HASH["c"]} in | |
1) | |
OUTPUT_HASH["c"]='\x08\xf7'$CHECKSUM | |
;; | |
2) | |
OUTPUT_HASH["c"]='\x09\xf6'$CHECKSUM | |
;; | |
3) | |
OUTPUT_HASH["c"]='\x0a\xf5'$CHECKSUM | |
;; | |
4) | |
OUTPUT_HASH["c"]='\x0b\xf4'$CHECKSUM | |
;; | |
esac | |
;; | |
d|D) | |
case ${INPUT_HASH["d"]} in | |
1) | |
OUTPUT_HASH["d"]='\x0c\xf3'$CHECKSUM | |
;; | |
2) | |
OUTPUT_HASH["d"]='\x0d\xf2'$CHECKSUM | |
;; | |
3) | |
OUTPUT_HASH["d"]='\x0e\xf1'$CHECKSUM | |
;; | |
4) | |
OUTPUT_HASH["d"]='\x0f\xf0'$CHECKSUM | |
;; | |
esac | |
;; | |
esac | |
done | |
if [[ $VERBOSE ]]; then echo "Serial port = $PORT"; fi | |
if [[ $(id -u) -ne 0 ]] ; then echo "Please run with sudo" ; exit 1 ; fi | |
# run the main loop to set input and output | |
for x in ${!OUTPUT_HASH[@]}; do | |
if [[ $VERBOSE ]]; then printf "echo -ne '%s' > ${PORT}\n" ${OUTPUT_HASH[$x]}; fi | |
echo -ne "${OUTPUT_HASH["$x"]}" > ${PORT} | |
# device requires 50ms wait | |
sleep 1s | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment