Advertisement
CGar

LimitlessLED controller bash program using sendip

Jun 10th, 2016
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 7.57 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. tty -s; if [ $? -ne 0 ]; then konsole -e "$0"; exit; fi
  4. # tty returns the path of the terminal or "not a tty" into stdout. As well as the exit codes 0 and 1 respectivly.
  5. # -s means tty will only return the exit code and not output to stdout. this is retrived with $? (holds exit code for last executed statement)
  6. # if its nonzero, it means its not a terminal, such as double clicking to open, this leads the if to launch the script path again, which is held in $0, with the desired terminal
  7.  
  8. # Codes:
  9. #
  10. # Off = 0x4100
  11. # On = 0x4200
  12. # DiscoSpeedSlower = 0x4300
  13. # DiscoSpeedFaster = 0x4400
  14. # Group1AllOn = 0x4500
  15. # Group1AllOff = 0x4600
  16. # Group2AllOn = 0x4700
  17. # Group2AllOff = 0x4800
  18. # Group3AllOn = 0x4900
  19. # Group3AllOff = 0x4A00
  20. # Group4AllOn = 0x4B00
  21. # Group4AllOff = 0x4C00
  22. # DiscoMode = 0x4D00
  23. # SetColorToWhite = 0xC200
  24. # SetColorToWhiteGroup1 = 0xC500
  25. # SetColorToWhiteGroup2 = 0xC700
  26. # SetColorToWhiteGroup3 = 0xC900
  27. # SetColorToWhiteGroup4 = 0xCB00
  28. # Brightness = 0x4EXX (XX brightness in hex from 02 to 1B, 25 levels)
  29. # Color = 0x40XX (XX color from 0 to 255 in hex, 00 to FF)
  30. #
  31. # Via - https://github.com/danvy/miled/blob/master/src/MiLED.SharedLib/LEDCommands.cs
  32. #
  33. #sudo sendip -d 0xCA00 -p ipv4 -p udp -ud 8899 192.168.1.110 - group3 nightmode lower than 0x4E02
  34. #
  35. # To run. remove sudo and run as root, or
  36. # add:
  37. #    <user> ALL=(root) NOPASSWD: /usr/bin/sendip 192<WiFi box ip>
  38. # to sudoers file using visudo. replacing <user> and <WiFi box ip> with correct values.
  39. # udp hex values are correct for port 8899. Port 50000 uses different values and/or tcp.
  40.  
  41. wifiControllerIP="192.168.1.110"
  42.  
  43. commandStart="sudo sendip -d 0x"
  44. commandEnd=" -p ipv4 -p udp -ud 8899 $wifiControllerIP"
  45.  
  46. on="4200"
  47. off="4100"
  48.  
  49. white="c200"
  50.  
  51. colorPrefix="40"
  52. brightnessPrefix="4e"
  53.  
  54. Group1On="4500"
  55. Group1Off="4600"
  56. Group2On="4700"
  57. Group2Off="4800"
  58. Group3On="4900"
  59. Group3Off="4A00"
  60. Group4On="4B00"
  61. Group4Off="4C00"
  62.  
  63. wmctrl -a "lightControl.sh"
  64.  
  65. function properClear {
  66.    clear
  67.    echo -en "\e[3J"
  68. }
  69.  
  70. function wakeUpBox {
  71.    wget -q --tries=5 --timeout=10 --spider -O - http://192.168.1.110 > /dev/null
  72.    
  73.    echo "Wake up box command sent..."
  74.    echo
  75. }
  76.  
  77. function displayMenu {
  78.    properClear
  79.    echo
  80.    echo "What do you want to do?"
  81.    echo
  82.    echo "1. Turn All Lights On"
  83.    echo "2. Turn All Lights Off"
  84.    echo "3. Turn Specific Group On/Off"
  85.    echo "4. Switch to White"
  86.    echo "5. Color Selection"
  87.    echo "6. Brightness Selection"
  88.    echo
  89.    echo "---"
  90.    echo
  91.    echo "W. Wake up LED WiFi Box"
  92.    echo "N. Night time mode (G1 - 1% / G2 - 30%)"
  93.    echo "Q. Quit"
  94.    echo
  95. }
  96.  
  97. function turnOn {
  98.    command="$commandStart$on$commandEnd"
  99.    eval $command
  100.    
  101.    echo "Light on command sent..."
  102.    echo
  103. }
  104.  
  105. function turnOff {
  106.    command="$commandStart$off$commandEnd"
  107.    eval $command
  108.    
  109.    echo "Light off command sent..."
  110.    echo
  111. }
  112.  
  113. function whiteMode {
  114.    command="$commandStart$white$commandEnd"
  115.    eval $command
  116.    
  117.    echo "White mode command sent..."
  118.    echo
  119. }
  120.  
  121. function setColor {
  122.    properClear
  123.    echo
  124.    echo "Enter a 2 character hex color code from 00 to FF. Or enter Q to return to menu."
  125.    echo
  126.    
  127.    while :; do
  128.       read -p "Color Code [00-ff]: " code; echo
  129.      
  130.       if [[ "$code" == "q" || "$code" == "Q" ]]; then displayMenu; return; fi
  131.      
  132.       decCode=$(echo "ibase=16; $(echo -n $code | tr '[:lower:]' '[:upper:]')" | bc)
  133.      
  134.       if [ $decCode -ge 0 -a $decCode -le 255 ] 2>/dev/null; then
  135.          code=$(echo -n $code | tr '[:upper:]' '[:lower:]')
  136.          break
  137.       else
  138.          properClear
  139.          echo
  140.          echo "\"$code\" is an invalid selection. The code must be 2 characters in the range of 00 to FF."
  141.          echo
  142.          echo "Enter a 2 character hex color code from 00 to ff. Or enter q to return to menu."
  143.          echo
  144.       fi
  145.    done
  146.    
  147.    command="$commandStart$colorPrefix$code$commandEnd"
  148.    eval $command
  149.    
  150.    displayMenu
  151.    
  152.    echo "Set color to $code command sent..."
  153.    echo
  154. }
  155.  
  156. function setBrightness {
  157.    properClear
  158.    echo
  159.    echo "Enter a brightness from 1 to 100. Or enter q to return to menu."
  160.    echo
  161.    
  162.    while :; do
  163.       read -p "Brightness [1-100]: " percent; echo
  164.      
  165.       if [[ "$percent" == "q" || "$percent" == "Q" ]]; then displayMenu; return; fi
  166.      
  167.       if [ $percent -ge 1 -a $percent -le 100 ] 2>/dev/null; then
  168.          brightness=$(( percent / 4 ))
  169.          break
  170.       else
  171.          properClear
  172.          echo
  173.          echo "\"$percent\" is an invalid selection. The number must be in the range of 1 to 100."
  174.          echo
  175.          echo "Enter a brightness from 1 to 100. Or enter q to return to menu."
  176.          echo
  177.       fi
  178.    done
  179.    
  180.    brightness=$(echo "obase=16; $(( brightness + 2 ))" | bc)
  181.    brightness=$(echo -n $brightness | tr '[:upper:]' '[:lower:]')
  182.    
  183.    if [ ${#brightness} -eq 1 ]; then brightness="0$brightness"; fi
  184.    
  185.    command="$commandStart$brightnessPrefix$brightness$commandEnd"
  186.    eval $command
  187.    
  188.    displayMenu
  189.    
  190.    echo "Set brightness to $percent% command sent..."
  191.    echo
  192. }
  193.  
  194. function nightMode {
  195.    wakeUpBox
  196.    displayMenu
  197.  
  198.    command="$commandStart$Group2On$commandEnd"
  199.    eval $command
  200.    sleep 0.1
  201.    eval $command
  202.    
  203.    sleep 0.2
  204.    command="$commandStart$brightnessPrefix""09$commandEnd"
  205.    eval $command
  206.    sleep 0.1
  207.    eval $command
  208.    
  209.    sleep 0.2
  210.    command="$commandStart$Group1On$commandEnd"
  211.    eval $command
  212.    sleep 0.1
  213.    eval $command
  214.    
  215.    sleep 0.2
  216.    command="$commandStart$brightnessPrefix""02$commandEnd"
  217.    eval $command
  218.    sleep 0.1
  219.    eval $command
  220.    
  221.    echo "Night mode commands sent..."
  222.    echo
  223. }
  224.  
  225. function groupToggle {
  226.    properClear
  227.    echo
  228.    echo "Select group to turn on/off."
  229.    echo "NOTE: Last group turned on becomes selected light for brightness/color changes."
  230.    echo
  231.    echo "1. Turn group 1 on"
  232.    echo "2. Turn group 1 off"
  233.    echo "3. Turn group 2 on"
  234.    echo "4. Turn group 2 off"
  235.    echo "5. Turn group 3 on"
  236.    echo "6. Turn group 3 off"
  237.    echo "7. Turn group 4 on"
  238.    echo "8. Turn group 4 off"
  239.    echo
  240.    echo "---"
  241.    echo
  242.    echo "Q. Return to menu"
  243.    echo
  244.    
  245.   while :
  246.   do
  247.     read -sn 1 -p "Select Function [1-8/Q]: " ret; echo
  248.    
  249.     command="$commandStart"
  250.     msg=""
  251.    
  252.     case $ret in
  253.         1 ) command+="$Group1On"; msg="Turn group 1 on"; break ;;
  254.         2 ) command+="$Group1Off"; msg="Turn group 1 off"; break ;;
  255.         3 ) command+="$Group2On"; msg="Turn group 2 on"; break ;;
  256.         4 ) command+="$Group2Off"; msg="Turn group 2 off"; break ;;
  257.         5 ) command+="$Group3On"; msg="Turn group 3 on"; break ;;
  258.         6 ) command+="$Group3Off"; msg="Turn group 3 off"; break ;;
  259.         7 ) command+="$Group4On"; msg="Turn group 4 on"; break ;;
  260.         8 ) command+="$Group4Off"; msg="Turn group 4 off"; break ;;
  261.         q|Q) displayMenu; return ;;
  262.         * ) echo -e "\"$ret\" is an invalid selection. Must be N, W, Q or number in the range 1-6.\n" ;;
  263.     esac
  264.    done
  265.    
  266.    command+="$commandEnd"
  267.    eval $command
  268.    
  269.    displayMenu
  270.    
  271.    echo "$msg command sent..."
  272.    echo
  273. }
  274.  
  275. wakeUpBox
  276. displayMenu
  277.  
  278. while :
  279. do
  280.    read -sn 1 -p "Select Function [1-6/W/N/Q]: " ret; echo
  281.    
  282.    displayMenu
  283.    
  284.    case $ret in
  285.       1 ) turnOn ;;
  286.       2 ) turnOff ;;
  287.       3 ) groupToggle ;;
  288.       4 ) whiteMode ;;
  289.       5 ) setColor ;;
  290.       6 ) setBrightness ;;
  291.       W|w ) wakeUpBox ;;
  292.       N|n ) nightMode ;;
  293.       Q|q ) break ;;
  294.       * ) echo -e "\"$ret\" is an invalid selection. The number must be in the range of 1 to 8.\n" ;;
  295.    esac
  296. done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement