HaaTa

keyboardLEDStats

Jul 17th, 2015
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 8.77 KB | None | 0 0
  1. #!/bin/bash
  2. #| Jacob Alexander (2010)
  3. #| Script controlling the keyboard leds based upon the modifiers pressed
  4. #| Commands
  5. setLed='setleds -L'
  6. #| Defines
  7. scroll[0]='-scroll'
  8. scroll[1]='+scroll'
  9.    num[0]='-num'
  10.    num[1]='+num'
  11.   caps[0]='-caps'
  12.   caps[1]='+caps'
  13. declare -i maxLEDs=3
  14. declare -i floatMathDecimal=4
  15. declare -i debugMode=0
  16. #| Default Variables
  17. maxCycle=0.5     # Time for each cycle (in seconds)
  18. declare -i liltMode=0
  19. declare -i totalStates=216  # Total number of light states (8, 27, 64, 125, 216, 343, etc.  e.g. 111 base 2, 222, base 3, 333 base 4, etc.)
  20. #| General Variables
  21. declare -i inputState=0
  22. declare -i quit=-1
  23. #| Event Loop Variables
  24. napTime=0
  25. sleepCycleCurrent=0
  26. runtimeTotal=0
  27. declare -a ledStatus
  28. ledStatus[0]=0
  29. ledStatus[1]=0
  30. ledStatus[2]=0
  31. #| Error Handling
  32. error() {
  33.         echo "Bug or invalid input"
  34.         exit 1
  35. }
  36. #| Debug Info
  37. debug() {
  38.         if (( debugMode )); then
  39.                 parm1=$1
  40.                 case "$parm1" in
  41.                 1)
  42.                         echo "       States: $states"
  43.                         echo "     New Base: ${newBase[@]}"
  44.                         echo "State Periods: ${statePeriod[@]}"
  45.                         echo "  Sleep Count: $minSleepState"
  46.                         ;;
  47.                 2)
  48.                         echo " Set LED Args: $toSet"
  49.                         ;;
  50.                 3)
  51.                         echo "Current Place: ${tmpCount[@]}"
  52.                         echo "Current Cycle: $sleepCycleCurrent"
  53.                         ;;
  54.                 4)
  55.                         echo "   Total Time: $runtimeTotal"
  56.                 esac
  57.         fi
  58. }
  59. #| Floating Point Math
  60. floatMath() {
  61.         echo "scale=$floatMathDecimal; $1" | bc
  62. }
  63. #| Blank LEDs
  64. blankLEDs() {
  65.         $setLed ${num[0]} ${caps[0]} ${scroll[0]}
  66.         ledStatus[0]=0
  67.         ledStatus[1]=0
  68.         ledStatus[2]=0
  69. }
  70. #| Inverse LED
  71. #|
  72. #| Parameter 1: LED to Invert
  73. #|              0:    NumLock
  74. #|              1:   CapsLock
  75. #|              2: ScrollLock
  76. inverseLEDStatus() {
  77.         parm=$1
  78.         if (( ledStatus[$parm] == 0 )); then
  79.                 ledStatus[$parm]=1
  80.         else
  81.                 ledStatus[$parm]=0
  82.         fi
  83.         return ${ledStatus[$parm]}
  84. }
  85. #| Event Loop
  86. eventLoop() {
  87.         #| Copy the array (TODO: Find a better way to do this, if there is one)
  88.         for (( c = 0; c < minSleepState + 1; c++ )); do
  89.                 tmpCount[c]=${statePeriod[c]}
  90.         done
  91.         #| Get highest possible sleep time
  92.         napTime=${statePeriod[$minSleepState]}
  93.         sleepCycleCurrent=0
  94.         #| If state 0 0 0, a 0 second sleep will be requested
  95.         #| Changing to cycle sleep amount
  96.         if (( `floatMath "$napTime == 0"` )); then
  97.                 napTime=${statePeriod[1]}
  98.         fi
  99.         #| Set first state of the LEDs
  100.         for (( c = 0; c < maxLEDs; c++ )); do
  101.                 off=0
  102.                 if (( newBase[c] != 0 )); then
  103.                         off=1
  104.                 fi
  105.                 case "$c" in
  106.                 0)
  107.                         toSet="${num[$off]}"
  108.                         ;;
  109.                 1)
  110.                         toSet="$toSet ${caps[$off]}"
  111.                         ;;
  112.                 2)
  113.                         toSet="$toSet ${scroll[$off]}"
  114.                         ;;
  115.                 esac
  116.                 ledStatus[$c]=$off
  117.         done
  118.         $setLed $toSet # Set initial LED state
  119.         #| Run pattern until cycle completes
  120.         while (( `floatMath "$sleepCycleCurrent < $maxCycle"` )); do
  121.                 #| Update status variables
  122.                 sleepCycleCurrent=`floatMath "$sleepCycleCurrent + $napTime"`
  123.                 runtimeTotal=`floatMath "$runtimeTotal + $napTime"`
  124.                 sleep $napTime
  125.                 toSet=""
  126.                 #| Determine which LEDs to change (starts at 1, as 0 is the off LEDs)
  127.                 for (( c = 1; c < minSleepState + 1; c++ )); do
  128.                         tmpCount[$c]=`floatMath "${tmpCount[$c]} - $napTime"`
  129.                         if (( `floatMath "${tmpCount[$c]} == 0"` && `floatMath "${statePeriod[$c]} != 1"` )); then
  130.                                 if [[ "$c" == ${newBase[0]} ]]; then
  131.                                         inverseLEDStatus 0
  132.                                         toSet="$toSet ${num[$?]}"
  133.                                 fi
  134.                                 if [[ "$c" == ${newBase[1]} ]]; then
  135.                                         inverseLEDStatus 1
  136.                                         toSet="$toSet ${caps[$?]}"
  137.                                 fi
  138.                                 if [[ "$c" == ${newBase[2]} ]]; then
  139.                                         inverseLEDStatus 2
  140.                                         toSet="$toSet ${scroll[$?]}"
  141.                                 fi
  142.                                 # Reset Value
  143.                                 tmpCount[c]=${statePeriod[c]}
  144.                         fi
  145.                 done
  146.                 debug 3
  147.                 # Set LED
  148.                 if [[ "$toSet" != "" ]]; then
  149.                         $setLed $toSet # Set new LED state
  150.                         debug 2
  151.                 fi
  152.         done
  153.         debug 4
  154.         return 0
  155. }
  156. #| Process Cmd Line Args
  157. cmdLineArgs() {
  158.         displayText=""
  159.         #| Loop through args
  160.         while (( $# > 0 )); do
  161.                 #| Match Args
  162.                 # Display Help
  163.                 if [[ "$1" == "-h" || "$1" == "--help" ]]; then
  164.                         echo "Help!"
  165.                         quit=0; shift 1; continue
  166.                 fi
  167.                 # Display Version
  168.                 if [[ "$1" == "-v" || "$1" == "--version" ]]; then
  169.                         echo "Version!"
  170.                         quit=0; shift 1; continue
  171.                 fi
  172.  
  173.                 # Debug Mode
  174.                 if [[ "$1" == "-d" || "$1" == "--debug" ]]; then
  175.                         echo "Debug Mode"
  176.                         debugMode=1
  177.  
  178.                         shift 1; continue
  179.                 fi
  180.  
  181.                 # Cycle Length
  182.                 if [[ "$1" == "-c" || "$1" == "--cycle" ]]; then
  183.                         # TODO Check
  184.                         maxCycle=$2
  185.  
  186.                         shift 2; continue
  187.                 fi
  188.  
  189.  
  190.                 # State
  191.                 if [[ "$1" == "-s" || "$1" == "--state" ]]; then
  192.  
  193.                         # TODO Test if integer between 0 and totalStates
  194.                         inputState=$2
  195.                         #| Error if state is too high
  196.                         if (( inputState >= totalStates )); then
  197.                                 error 1 $inputState
  198.                         fi
  199.  
  200.                         shift 2; continue
  201.                 fi
  202.  
  203.                 # Max States
  204.                 if [[ "$1" == "-m" || "$1" == "--max" ]]; then
  205.                         # TODO Check
  206.                         totalStates=$2
  207.  
  208.                         shift 2; continue
  209.                 fi
  210.  
  211.  
  212.                 #| Invalid Argument
  213.                 displayText="$displayText Invalid Argument: $1\n"
  214.                 quit=1
  215.  
  216.                 shift 1
  217.         done
  218.  
  219.  
  220.         #| Display Info
  221.         if [[ "$displayText" != "" ]]; then
  222.                 quit=1
  223.                 echo -e "$displayText"
  224.         fi
  225.  
  226.  
  227.         #| Quit if invalid arg
  228.         if [[ $quit -ne -1 ]]; then
  229.                 exit $quit
  230.         fi
  231.  
  232.  
  233.         return 0
  234. }
  235.  
  236.  
  237. #| Initial Entry Point
  238. main() {
  239.         #| Determine States per Keyboard LED (e.g. 2, 3, 4, etc.)
  240.         for (( states = maxLEDs - 1; totalStates > states ** maxLEDs; states++ )); do
  241.                 continue
  242.         done
  243.  
  244.  
  245.         #| Convert to base $states (e.g. 010 base 2)
  246.         remainder=$inputState
  247.         for (( digit = maxLEDs - 1; digit >= 0; digit-- )); do
  248.                 (( base           = states ** digit ))
  249.                 (( newBase[digit] = remainder / base ))
  250.                 (( remainder      = remainder - newBase[digit] * base ))
  251.         done
  252.  
  253.  
  254.         #| Determine Used Light States (e.g. No light, Solid, 1/2 second flash; No light, Solid, 1/2 second flash, 1/4 second flash)
  255.         statePeriod[0]=0
  256.         for (( c = 1; c < states; c++ )); do
  257.                 statePeriod[c]=`floatMath "$maxCycle / 2^($c-1)"`
  258.         done
  259.  
  260.  
  261.         #| Min Sleep Amount
  262.         minSleepState=0
  263.         for (( c = 0; c < maxLEDs; c++ )); do
  264.                 if (( newBase[c] > minSleepState )); then
  265.                         minSleepState=${newBase[c]}
  266.                 fi
  267.         done
  268.  
  269.  
  270.         #| Debug Information
  271.         debug 1
  272.  
  273.  
  274.         #| Reset All LEDs
  275.         blankLEDs
  276.  
  277.  
  278.         #| Run Event Loop
  279.         while eventLoop; do
  280.                 continue
  281.         done
  282. }
  283.  
  284. cmdLineArgs $@
  285. main
  286.  
  287. exit 0
Advertisement
Add Comment
Please, Sign In to add comment