Guest User

ws2801 pixel led control RaspberryPi

a guest
Mar 20th, 2013
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.70 KB | None | 0 0
  1. #!/bin/bash
  2. # programado por Juanmol para http://rsppi.blogspot.com
  3. RESPONSE=/tmp/webresp
  4. log=/var/log/pixel-access.log
  5. status=/tmp/estados.led
  6. leds2=/tmp/leds2.sh
  7. temp=/tmp/estados.led.tmp
  8. num_leds=50
  9. max=ff
  10.  
  11. [ -p $RESPONSE ] || mkfifo $RESPONSE
  12.  
  13. while true ;
  14. do
  15.         ( cat $RESPONSE ) | nc -l -p 8080 | (
  16.         REQUEST=`while read L && [ " " "<" "$L" ] ; do echo "$L" ; done`
  17.         REQ="`echo \"$REQUEST\" | head -n 1`"
  18.         pixel=$(echo $REQ | cut -f3 -d\/ | cut -f1 -d\ )
  19.  
  20.         if [ "$(cat $status | grep ^$pixel\ | wc -l)" -eq "1" ] || [ $pixel = reset ];
  21.         then
  22.  
  23.                 echo "[ `date '+%Y-%m-%d %H:%M:%S'` ] $REQ" >>$log
  24.                 if [[ $REQ =~ ^GET\ /pixel[\ \/\#?] ]]; then
  25.                         estado=$(echo $REQ | cut -f4 -d\/ | cut -f1 -d\ )
  26.                         case $estado in
  27.                         1 )
  28.                                 color="\\\\x$max\\\\x00\\\\x00"
  29.                         ;;
  30.                         2 )
  31.                                 color="\\\\x00\\\\x$max\\\\x00"
  32.                         ;;
  33.                         3 )
  34.                                 color="\\\\x00\\\\x00\\\\x$max"
  35.                         ;;
  36.                         4 )
  37.                                 color="\\\\x$max\\\\x$max\\\\x00"
  38.                         ;;
  39.                         5 )
  40.                                 color="\\\\x$max\\\\x00\\\\x$max"
  41.                         ;;
  42.                         6 )
  43.                                 color="\\\\x00\\\\x$max\\\\x$max"
  44.                         ;;
  45.                         7 )
  46.                                 color="\\\\x$max\\\\x$max\\\\x$max"
  47.                         ;;
  48.                         * )
  49.                                 color="\\\\x00\\\\x00\\\\x00"
  50.                         ;;
  51.                         esac
  52.                         cat $status | sed "s/^"$pixel"\ .*/"$pixel"\ "$color"/g" > $temp
  53.                         cp $temp $status
  54.                 fi
  55.                 if [ $(echo $pixel) = reset ];
  56.                 then
  57.                         rm $status;
  58.                         for n in $(seq 1 $[$num_leds+1]);
  59.                         do
  60.                                 echo $n\ \\\\x00\\\\x00\\\\x00 >> $status;
  61.                         done
  62.                 fi
  63.                 cat $status | awk '{ print $2 }' | sed 's/^\\/echo\ \-ne\ \"\\/g' | tr "\n" "\"" | sed 's/\"e/\"\ \> \/dev\/spidev0\.0\ \&\&\ e/g' | sed 's/\"\"/\"\ \> \/dev\/spidev0\.0/g' > $leds2;
  64.                 /bin/bash $leds2;
  65.         fi
  66.         cat >$RESPONSE <<EOF
  67. HTTP/1.0 200 OK
  68. Cache-Control: no-cache
  69. Content-Type: text/html
  70. Server: bash/2.0
  71. Connection: Close
  72. Content-Length: 0
  73.  
  74. EOF
  75.     )
  76. done
Advertisement
Add Comment
Please, Sign In to add comment