You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
1.6 KiB

  1. #!/bin/bash
  2. SERIAL=/dev/ttyUSB0
  3. VOLUME_CONTROL="Line Out"
  4. RADIO1_URL="http://us2.internet-radio.com:8443/stream"
  5. RADIO2_URL="https://rozhlas.stream/ddur_mp3_128.mp3"
  6. RADIO3_URL="https://rozhlas.stream/brno_mp3_128.mp3"
  7. #https://rozhlas.stream/radiozurnal_aac_128.aac
  8. RADIO4_URL="https://ice3.abradio.cz/croretro128.mp3"
  9. killStream() {
  10. kill `ps aux | grep "$1" | awk '{print $2}'` 2>/dev/null
  11. }
  12. playStream() {
  13. mplayer "$1" </dev/null >/dev/null 2>&1 &
  14. }
  15. handleStream() {
  16. STREAM=$1
  17. STATE=$2
  18. #killStream "$STREAM"
  19. if [ $STATE = 1 ]; then
  20. playStream "$STREAM"
  21. echo "Started radio: $STREAM"
  22. else
  23. echo "Stopped radio: $STREAM"
  24. fi
  25. }
  26. execVolume() {
  27. VOLUME_RAW=`echo "scale=4; $1/255*100" | bc`
  28. VOLUME=`printf "%.0f%%" "$VOLUME_RAW"`
  29. amixer sset "$VOLUME_CONTROL" "$VOLUME" >/dev/null &
  30. echo Volume set to $VOLUME
  31. #echo Volume set to $1
  32. }
  33. execButton() {
  34. case $1 in
  35. 1)
  36. handleStream "$RADIO1_URL" "$2"
  37. ;;
  38. 2)
  39. handleStream "$RADIO2_URL" "$2"
  40. ;;
  41. 3)
  42. handleStream "$RADIO3_URL" "$2"
  43. ;;
  44. 4)
  45. handleStream "$RADIO4_URL" "$2"
  46. ;;
  47. *)
  48. echo "Unknown button '$1'." >&2
  49. ;;
  50. esac
  51. }
  52. process_event() {
  53. case $1 in
  54. volume)
  55. execVolume "$2"
  56. ;;
  57. button*)
  58. NUM=`echo $1 | tr -dc [:digit:]`
  59. execButton "$NUM" "$2"
  60. ;;
  61. "")
  62. ;;
  63. *)
  64. echo "Unknown event '$1'." >&2
  65. ;;
  66. esac
  67. }
  68. consume_events() {
  69. while true; do
  70. IFS=$'\r'= read EVENT VALUE
  71. if [ $? == 0 ]; then
  72. #echo EVENT: $EVENT = $VALUE
  73. process_event "$EVENT" "$VALUE"
  74. else
  75. echo Invalid input. >&2
  76. fi
  77. done
  78. }
  79. stty -F $SERIAL 9600 cs8 -cstopb -parenb
  80. cat $SERIAL | consume_events