Skip to content

Instantly share code, notes, and snippets.

@t413
Created December 10, 2017 13:35
Show Gist options
  • Save t413/78a97ecc165dc07017de1e4bdbf872b3 to your computer and use it in GitHub Desktop.
Save t413/78a97ecc165dc07017de1e4bdbf872b3 to your computer and use it in GitHub Desktop.
LED Backpack with raspberry pi 3
#!/bin/bash
[ -t 0 ] && hash tput 2>/dev/null && COLOR_SHELL=true || COLOR_SHELL=false
function clr_gr() { [ "$COLOR_SHELL" = true ] && echo "$(tput setaf 0)$(tput setab 2)"; }
function clr_bl() { [ "$COLOR_SHELL" = true ] && echo "$(tput setaf 7)$(tput setab 4)"; }
function clr_rd() { [ "$COLOR_SHELL" = true ] && echo "$(tput setaf 7)$(tput setab 1)"; }
function clr_or() { [ "$COLOR_SHELL" = true ] && echo "$(tput setaf 0)$(tput setab 3)"; }
function clr_ma() { [ "$COLOR_SHELL" = true ] && echo "$(tput setaf 7)$(tput setab 5)"; }
function endcolor() { [ "$COLOR_SHELL" = true ] && echo "$(tput sgr 0)" || echo "$@"; }
function echo_gr() { [ "$COLOR_SHELL" = true ] && echo "$(clr_gr) $@ $(endcolor)" || echo "$@"; }
function echo_bl() { [ "$COLOR_SHELL" = true ] && echo "$(clr_bl) $@ $(endcolor)" || echo "$@"; }
function echo_rd() { [ "$COLOR_SHELL" = true ] && echo "$(clr_rd) $@ $(endcolor)" || echo "$@"; }
function echo_or() { [ "$COLOR_SHELL" = true ] && echo "$(clr_or) $@ $(endcolor)" || echo "$@"; }
function echo_ma() { [ "$COLOR_SHELL" = true ] && echo "$(clr_ma) $@ $(endcolor)" || echo "$@"; }
GIFSDIR="/home/pi/gifs";
RPIProj="/home/pi/rpi-rgb-led-matrix";
TEXTUTIL="$RPIProj/examples-api-use/text-example";
PLAYUTIL="$RPIProj/utils/video-viewer";
sizepartition="200k";
if [[ "$1" == "run" ]]; then
if [[ $EUID -ne 0 ]]; then
echo "$0 needs to be run as root!";
exit 1;
fi
function msg() { echo "${@}"; screen -S text -p0 -X stuff "${@}^M"; }
function statusmsg() {
if ! screen -ls | grep -q "text"; then
screen -dmS text $TEXTUTIL --led-rows=32 --led-chain=4 -b 10 -L -f "$RPIProj/fonts/tom-thumb.bdf" -C 255,10,10
sleep 0.1;
fi
msg "LED Backpack";
msg " by Tim O'Brien";
msg " ";
msg " $(ls $GIFSDIR | wc -l) gifs loaded";
msg " "; msg " ";
msg " $(iwgetid | awk '{ print $2; }')";
msg " $(hostname -I | awk '{print $1}')";
}
statusmsg;
sleep 2;
msg "^D" # send EOF, closes service
while true; do
cd "$GIFSDIR";
declare -a biggifs smallgifs shuffled;
while IFS= read -d $'\0' -r file ; do biggifs+=("$file"); done < <(find . -size +${sizepartition} -print0 | shuf)
while IFS= read -d $'\0' -r file ; do smallgifs+=("$file"); done < <(find . -size -${sizepartition} -print0 | shuf)
inbetween="$(( ${#smallgifs[@]} / ${#biggifs[@]} ))";
echo "big: ${#biggifs[@]}, small: ${#smallgifs[@]}, inbetween $inbetween";
for f in "${biggifs[@]}"; do
for i in $(seq 1 $inbetween); do
echo -n "${smallgifs[0]} ";
shuffled+=("${smallgifs[0]}");
smallgifs=("${smallgifs[@]:1}");
done
echo -n "$(clr_gr)${f}$(endcolor) "
shuffled+=("${f}");
done
for f in "${smallgifs[@]}"; do echo -n "${f} "; shuffled+=("${f}"); done #and all leftovers
echo_bl "done adding ${#shuffled[@]} files";
echo_bl "done";
$PLAYUTIL --led-rows=32 --led-chain=4 -L --led-brightness=50 -r4 "${shuffled[@]}" || exit;
statusmsg;
sleep 2;
msg "^D" # send EOF, closes service
done
else
if { screen -ls; sudo screen -ls; } | grep -q "runled|text"; then
echo "runled screen already running!"
exit 1;
fi
if [[ $EUID -ne 0 ]]; then
echo "starting screen as sudo";
sudo screen -dmS runled sudo "$0" run
else
screen -dmS runled sudo "$0" run
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment