Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- tty -s; if [ $? -ne 0 ]; then konsole -e "$0"; exit; fi
- # tty returns the path of the terminal or "not a tty" into stdout. As well as the exit codes 0 and 1 respectivly.
- # -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)
- # 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
- # Codes:
- #
- # Off = 0x4100
- # On = 0x4200
- # DiscoSpeedSlower = 0x4300
- # DiscoSpeedFaster = 0x4400
- # Group1AllOn = 0x4500
- # Group1AllOff = 0x4600
- # Group2AllOn = 0x4700
- # Group2AllOff = 0x4800
- # Group3AllOn = 0x4900
- # Group3AllOff = 0x4A00
- # Group4AllOn = 0x4B00
- # Group4AllOff = 0x4C00
- # DiscoMode = 0x4D00
- # SetColorToWhite = 0xC200
- # SetColorToWhiteGroup1 = 0xC500
- # SetColorToWhiteGroup2 = 0xC700
- # SetColorToWhiteGroup3 = 0xC900
- # SetColorToWhiteGroup4 = 0xCB00
- # Brightness = 0x4EXX (XX brightness in hex from 02 to 1B, 25 levels)
- # Color = 0x40XX (XX color from 0 to 255 in hex, 00 to FF)
- #
- # Via - https://github.com/danvy/miled/blob/master/src/MiLED.SharedLib/LEDCommands.cs
- #
- #sudo sendip -d 0xCA00 -p ipv4 -p udp -ud 8899 192.168.1.110 - group3 nightmode lower than 0x4E02
- #
- # To run. remove sudo and run as root, or
- # add:
- # <user> ALL=(root) NOPASSWD: /usr/bin/sendip 192<WiFi box ip>
- # to sudoers file using visudo. replacing <user> and <WiFi box ip> with correct values.
- # udp hex values are correct for port 8899. Port 50000 uses different values and/or tcp.
- wifiControllerIP="192.168.1.110"
- commandStart="sudo sendip -d 0x"
- commandEnd=" -p ipv4 -p udp -ud 8899 $wifiControllerIP"
- on="4200"
- off="4100"
- white="c200"
- colorPrefix="40"
- brightnessPrefix="4e"
- Group1On="4500"
- Group1Off="4600"
- Group2On="4700"
- Group2Off="4800"
- Group3On="4900"
- Group3Off="4A00"
- Group4On="4B00"
- Group4Off="4C00"
- wmctrl -a "lightControl.sh"
- function properClear {
- clear
- echo -en "\e[3J"
- }
- function wakeUpBox {
- wget -q --tries=5 --timeout=10 --spider -O - http://192.168.1.110 > /dev/null
- echo "Wake up box command sent..."
- echo
- }
- function displayMenu {
- properClear
- echo
- echo "What do you want to do?"
- echo
- echo "1. Turn All Lights On"
- echo "2. Turn All Lights Off"
- echo "3. Turn Specific Group On/Off"
- echo "4. Switch to White"
- echo "5. Color Selection"
- echo "6. Brightness Selection"
- echo
- echo "---"
- echo
- echo "W. Wake up LED WiFi Box"
- echo "N. Night time mode (G1 - 1% / G2 - 30%)"
- echo "Q. Quit"
- echo
- }
- function turnOn {
- command="$commandStart$on$commandEnd"
- eval $command
- echo "Light on command sent..."
- echo
- }
- function turnOff {
- command="$commandStart$off$commandEnd"
- eval $command
- echo "Light off command sent..."
- echo
- }
- function whiteMode {
- command="$commandStart$white$commandEnd"
- eval $command
- echo "White mode command sent..."
- echo
- }
- function setColor {
- properClear
- echo
- echo "Enter a 2 character hex color code from 00 to FF. Or enter Q to return to menu."
- echo
- while :; do
- read -p "Color Code [00-ff]: " code; echo
- if [[ "$code" == "q" || "$code" == "Q" ]]; then displayMenu; return; fi
- decCode=$(echo "ibase=16; $(echo -n $code | tr '[:lower:]' '[:upper:]')" | bc)
- if [ $decCode -ge 0 -a $decCode -le 255 ] 2>/dev/null; then
- code=$(echo -n $code | tr '[:upper:]' '[:lower:]')
- break
- else
- properClear
- echo
- echo "\"$code\" is an invalid selection. The code must be 2 characters in the range of 00 to FF."
- echo
- echo "Enter a 2 character hex color code from 00 to ff. Or enter q to return to menu."
- echo
- fi
- done
- command="$commandStart$colorPrefix$code$commandEnd"
- eval $command
- displayMenu
- echo "Set color to $code command sent..."
- echo
- }
- function setBrightness {
- properClear
- echo
- echo "Enter a brightness from 1 to 100. Or enter q to return to menu."
- echo
- while :; do
- read -p "Brightness [1-100]: " percent; echo
- if [[ "$percent" == "q" || "$percent" == "Q" ]]; then displayMenu; return; fi
- if [ $percent -ge 1 -a $percent -le 100 ] 2>/dev/null; then
- brightness=$(( percent / 4 ))
- break
- else
- properClear
- echo
- echo "\"$percent\" is an invalid selection. The number must be in the range of 1 to 100."
- echo
- echo "Enter a brightness from 1 to 100. Or enter q to return to menu."
- echo
- fi
- done
- brightness=$(echo "obase=16; $(( brightness + 2 ))" | bc)
- brightness=$(echo -n $brightness | tr '[:upper:]' '[:lower:]')
- if [ ${#brightness} -eq 1 ]; then brightness="0$brightness"; fi
- command="$commandStart$brightnessPrefix$brightness$commandEnd"
- eval $command
- displayMenu
- echo "Set brightness to $percent% command sent..."
- echo
- }
- function nightMode {
- wakeUpBox
- displayMenu
- command="$commandStart$Group2On$commandEnd"
- eval $command
- sleep 0.1
- eval $command
- sleep 0.2
- command="$commandStart$brightnessPrefix""09$commandEnd"
- eval $command
- sleep 0.1
- eval $command
- sleep 0.2
- command="$commandStart$Group1On$commandEnd"
- eval $command
- sleep 0.1
- eval $command
- sleep 0.2
- command="$commandStart$brightnessPrefix""02$commandEnd"
- eval $command
- sleep 0.1
- eval $command
- echo "Night mode commands sent..."
- echo
- }
- function groupToggle {
- properClear
- echo
- echo "Select group to turn on/off."
- echo "NOTE: Last group turned on becomes selected light for brightness/color changes."
- echo
- echo "1. Turn group 1 on"
- echo "2. Turn group 1 off"
- echo "3. Turn group 2 on"
- echo "4. Turn group 2 off"
- echo "5. Turn group 3 on"
- echo "6. Turn group 3 off"
- echo "7. Turn group 4 on"
- echo "8. Turn group 4 off"
- echo
- echo "---"
- echo
- echo "Q. Return to menu"
- echo
- while :
- do
- read -sn 1 -p "Select Function [1-8/Q]: " ret; echo
- command="$commandStart"
- msg=""
- case $ret in
- 1 ) command+="$Group1On"; msg="Turn group 1 on"; break ;;
- 2 ) command+="$Group1Off"; msg="Turn group 1 off"; break ;;
- 3 ) command+="$Group2On"; msg="Turn group 2 on"; break ;;
- 4 ) command+="$Group2Off"; msg="Turn group 2 off"; break ;;
- 5 ) command+="$Group3On"; msg="Turn group 3 on"; break ;;
- 6 ) command+="$Group3Off"; msg="Turn group 3 off"; break ;;
- 7 ) command+="$Group4On"; msg="Turn group 4 on"; break ;;
- 8 ) command+="$Group4Off"; msg="Turn group 4 off"; break ;;
- q|Q) displayMenu; return ;;
- * ) echo -e "\"$ret\" is an invalid selection. Must be N, W, Q or number in the range 1-6.\n" ;;
- esac
- done
- command+="$commandEnd"
- eval $command
- displayMenu
- echo "$msg command sent..."
- echo
- }
- wakeUpBox
- displayMenu
- while :
- do
- read -sn 1 -p "Select Function [1-6/W/N/Q]: " ret; echo
- displayMenu
- case $ret in
- 1 ) turnOn ;;
- 2 ) turnOff ;;
- 3 ) groupToggle ;;
- 4 ) whiteMode ;;
- 5 ) setColor ;;
- 6 ) setBrightness ;;
- W|w ) wakeUpBox ;;
- N|n ) nightMode ;;
- Q|q ) break ;;
- * ) echo -e "\"$ret\" is an invalid selection. The number must be in the range of 1 to 8.\n" ;;
- esac
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement