/elec/audio-switcher

To get this branch, use:
bzr branch http://bzr.ed.am/elec/audio-switcher
8 by Tim Marston
updated notes, and added control shell script
1
#!/bin/bash
2
3
APP=`basename "$0"`
4
umask 002
5
shopt -s nullglob
6
function die()
7
{
8
    [[ -n "$@" ]] && echo -e "$APP: $@" >&2
9
    exit 1
10
}
11
12
function version
13
{
14
    echo "$APP: audio switch control, version 0.1"
15
}
16
17
function usage
18
{
19
    echo "Usage: $APP [OPTION]... COMMAND"
20
    echo
21
    echo "Options:"
22
    echo "      --quiet          Do not complain on error"
23
    echo "  -v, --version        Display version information"
24
    echo "  -h, --help           This message"
25
    echo
26
    echo "Commands:"
27
    echo "  on   Switch audio routing on"
28
    echo "  off  Switch audio routing off"
29
    #echo
30
    #     01234567890123456789012345678901234567890123456789012345678901234567890123456789
31
}
32
33
# defaults
34
QUIET=0
35
36
# parse command line args
37
PROGRAMARGS=`getopt -n "$APP" -l device:,quiet,version,help vh "$@"`
38
[[ $? != 0 ]] && exit 1
39
eval set -- "$PROGRAMARGS"
40
while true; do
41
    case "$1" in
42
	--quiet) QUIET=1; shift;;
43
	-v|--version) version; exit 0;;
44
	-h|--help) usage; exit 0;;
45
	--) shift; break;;
46
    esac
47
done
48
49
# check other args
50
[[ -z "$1" ]] && die "you did not specify a command, see --help"
51
[[ -n "$2" ]] && die "too many arguments, see --help"
52
53
# work out command
54
COMMAND=
55
case "$1" in
56
    on) COMMAND=1;;
57
    off) COMMAND=0;;
58
    *) die "invalid comand, see --help";;
59
esac
60
61
# detect audio switch
62
DEVICES=`grep -l edam /sys/bus/usb/devices/*/manufacturer | sed 's/\/[^\/]*$//'`
63
DEVICES=`for DIR in "$DEVICES"; do grep -li ba7f "$DIR"/idProduct; done | sed 's/\/[^\/]*$//'`
64
DEVICES=`for DIR in "$DEVICES"; do grep -li 03eb "$DIR"/idVendor; done | sed 's/\/[^\/]*$//'`
65
DEVICES=`for DIR in "$DEVICES"; do [[ -d "$DIR":1.0/tty ]] && echo "$DIR":1.0/tty/*; done | sed 's/^.*\///'`
66
TEMP=( $DEVICES )
67
[[ ${#TEMP[*]} -gt 1 ]] && die "more than one audio switch detetced, use --device"    
68
DEVICE=$DEVICES
69
70
# no device?
71
[[ -z "$DEVICE" ]] && die "audio switch not found"
72
[[ -c /dev/"$DEVICE" ]] || die "audio switch device should be there, but isn't!"
73
74
# do command
75
echo $COMMAND > /dev/$DEVICE