Added bash script to control projector using the serial BT link.
authorAndreas Schiermeier <as@ccc504j.site>
Sun, 23 Feb 2014 12:32:17 +0000 (13:32 +0100)
committerAndreas Schiermeier <as@ccc504j.site>
Sun, 23 Feb 2014 12:32:17 +0000 (13:32 +0100)
docs/mitsubishixd460-xd490-rs232c.pdf [new file with mode: 0644]
script/beamer.sh [new file with mode: 0755]

diff --git a/docs/mitsubishixd460-xd490-rs232c.pdf b/docs/mitsubishixd460-xd490-rs232c.pdf
new file mode 100644 (file)
index 0000000..36d6ac4
Binary files /dev/null and b/docs/mitsubishixd460-xd490-rs232c.pdf differ
diff --git a/script/beamer.sh b/script/beamer.sh
new file mode 100755 (executable)
index 0000000..7d78527
--- /dev/null
@@ -0,0 +1,250 @@
+#!/bin/bash
+
+# Andreas Schiermeier <andreas@schiermeier.name>
+
+# BT Pairing using a PIN
+# Debian squeeze:
+# bluetooth-agent PIN 00:07:80:43:76:05
+# Debian wheezy:
+# bluez-simple-agent hci0 00:07:80:43:76:05
+
+BT_IF="rfcomm0"
+BT_SPEED="9600"
+
+control_pipe="/dev/stdin"
+output="/dev/stdout"
+rfcomm_init=0
+pipe_client=0
+
+declare -A commands
+commands=(
+  ['POWERON']='!'
+  ['POWEROFF']='"'
+  ['INPUTCOMP1']='_r1'
+  ['INPUTCOMP2']='_r2'
+  ['INPUTDVI']='_d1'
+  ['INPUTVIDEO']='_v1'
+  ['INPUTSVIDEO']='_v2'
+  ['MUTEON']='MUTE1'
+  ['MUTEOFF']='MUTE0'
+  ['VOLUP']='r06'
+  ['VOLDOWN']='r07'
+  ['KEYSTONE']='r43'
+  ['EXPAND']='r02'
+  ['PINP']='r04'
+  ['MUTE']='ra6'
+  ['UP']='r53'
+  ['DOWN']='r2b'
+  ['LEFT']='r4f'
+  ['RIGHT']='r59'
+  ['MENU']='r54'
+  ['ENTER']='r10'
+  ['AUTOPOS']='r09'
+  ['STILL']='ra4'
+  ['ASPECT']='re2'
+  ['WALLSCREEN']='rd7'
+  ['CE']='re7'
+  )
+
+function spawnRfcomm {
+  # try to establish a conecting to the projectors BT dongle
+  # in the background
+  (
+    while true
+    do
+      if [ -c "/dev/${BT_IF}" ]
+      then
+       rfcomm release "${BT_IF}" &> /dev/null
+      fi
+      # establish raw (-r) connection to projectors serial if
+      rfcomm -r connect "${BT_IF}" &> /dev/null
+      sleep 2
+    done
+  ) &
+  # clean up process group processes (rfcomm…) in case the script exits
+  trap 'kill 0' EXIT
+}
+
+function sendCmd {
+  trycount=0
+  # is the device available?
+  while [ ! -c "/dev/${BT_IF}" ]
+  do
+    # try three times before giving up
+    let trycount++
+    if [ ${trycount} -gt 3 ]
+    then
+      return 1
+    fi
+    sleep 1 
+  done
+  # is the device the one we've already initialized?
+  rfcomm_created=$(stat -c %Z "/dev/${BT_IF}")
+  if [ ${rfcomm_created} -gt ${rfcomm_init} ]
+  then
+    # configure speed and enable CR to NL conversion for incoming data
+    stty -F "/dev/${BT_IF}" "${BT_SPEED}" icrnl
+    # \r ensures there is no grabage left on the serial connection to the projector
+    echo -en '\r' > "/dev/${BT_IF}"
+    rfcomm_init=${rfcomm_created}
+  fi
+  # empty action is used to init the connection
+  if [ -n "${1}" ]
+  then
+    echo -en '00'${commands[$1]}'\r' > "/dev/${BT_IF}"
+    # with for projectors reply up to one second
+    read -t1 ret < "/dev/${BT_IF}"
+    # projector replies back accepted commands
+    if [ "${ret}" != "00${commands[$1]}" ]
+    then
+      return 2
+    fi
+  fi
+}
+
+function usage {
+  cat <<__EOF>&2
+  
+usage:  $0 [-c] [/path/to/namedpipe]
+
+
+__EOF
+  exit
+}
+
+while getopts ":c" optname; do
+    case "${optname}" in
+        c)
+            pipe_client=1
+            ;;
+        *)
+            usage
+            ;;
+    esac
+done
+shift $((OPTIND-1))
+
+if [ -p "${1}" ]
+then
+  control_pipe="${1}"
+  if [ ${pipe_client} -eq 0 ]
+  then
+    output="/dev/null"
+  fi
+fi
+
+if [ ${pipe_client} -eq 0 ]
+then
+  spawnRfcomm
+fi
+
+cat <<'EOF' > "${output}"
+MENU m, POWERON p, POWEROFF P, UP k, DOWN j, LEFT h, RIGHT l, STILL s
+INPUTCOMP1 1, INPUTCOMP2 2. INPUTDVI 3, INPUTVIDEO 4, INPUTSVIDEO 5, ENTER Return
+TOGGLE INPUTCOMP1/DVI1 ~
+QUIT Q
+
+ready.
+EOF
+
+while true
+do
+  # read from STDIN when acting as pipe client
+  if [ ${pipe_client} -eq 0 ]
+  then
+    read_pipe="${control_pipe}"
+  else
+    read_pipe="/dev/stdin"
+  fi
+  cmd=""
+  # read one key each time from STDIN / pipe
+  read -s -N 1 key < "${read_pipe}"
+  case "${key}" in
+    "m")
+      cmd='MENU'
+      ;;
+    "p")
+      cmd='POWERON'
+      ;;
+    "P")
+      cmd='POWEROFF'
+      ;;
+    "k"|"A") # A is part of the escape sequence sent by the arrow key (^]A)
+      cmd='UP'
+      ;;
+    "j"|"B") # B is part of the escape sequence sent by the arrow key (^]B)
+      cmd='DOWN'
+      ;;
+    "h"|"D") # K is part of the escape sequence sent by the arrow key (^]D)
+      cmd='LEFT'
+      ;;
+    "l"|"C") # C is part of the escape sequence sent by the arrow key (^]C)
+      cmd='RIGHT'
+      ;;
+    "s")
+      cmd='STILL'
+      ;;
+    "1")
+      cmd='INPUTCOMP1'
+      ;;
+    "2")
+      cmd='INPUTCOMP2'
+      ;;
+    "3")
+      cmd='INPUTDVI'
+      ;;
+    "4")
+      cmd='INPUTVIDEO'
+      ;;
+    "5")
+      cmd='INPUTSVIDEO'
+      ;;
+    "")
+      cmd='ENTER'
+      ;;
+    "~")
+      # SPECIAL CMD: toggle VGA1 and DVI
+      # --------------------------------
+      if [ ${pipe_client} -eq 0 ]
+      then
+       # init connection
+       sendCmd ''
+       # request selected input
+       echo -en '00vI\r' > "/dev/${BT_IF}"
+       read -t1 ret < "/dev/${BT_IF}"
+       # ret contains 00vIr1 for VGA1, 00vId1 for DVI
+       case "${ret}" in
+         00vIr1)
+           # VGA1 is active, switch to DVI
+           sendCmd 'INPUTDVI'
+           ;;
+         *)
+           # otherwise switch to VGA1
+           sendCmd 'INPUTCOMP1'
+           ;;
+       esac
+      fi  
+      ;;
+    "Q")
+      exit
+      ;;
+  esac
+  # just write key to pipe when acting as pipe client
+  # otherwise call sendCmd
+  if [ ${pipe_client} -eq 1 ]
+  then
+    echo -n "${key}" > "${control_pipe}"
+  elif [ -n "${cmd}" ]
+  then
+    echo -n "${cmd}: " > "${output}"
+    # sendCmd returns 0 if projector replied back the sent command
+    #                 1 if serial device is not available
+    #                 2 if projector doesn't reply the sent command within one second
+    if sendCmd "${cmd}"
+    then
+      echo "OK." > "${output}"
+    else
+      echo "failed (err: $?)." > "${output}"
+    fi
+  fi
+done