Skip to content

Instantly share code, notes, and snippets.

@t413
Created May 4, 2018 09:26
Show Gist options
  • Save t413/bd8dcad552c2a3b2c1b2e97c78d8af0f to your computer and use it in GitHub Desktop.
Save t413/bd8dcad552c2a3b2c1b2e97c78d8af0f to your computer and use it in GitHub Desktop.
led backpack 2.0
#!/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 echo_gr() { [ "$COLOR_SHELL" = true ] && echo "$(clr_gr) $@ $(endcolor)" || echo "$@"; }
function echo_bl() { [ "$COLOR_SHELL" = true ] && echo "$(clr_bl) $@ $(endcolor)" || echo "$@"; }
function endcolor() { [ "$COLOR_SHELL" = true ] && echo "$(tput sgr 0)" || echo "$@"; }
function logme() { echo_bl "$@" >&2; }
FT_PATH=/home/pi/flaschen-taschen
FT_DEMOS_PATH=/home/pi/ft-demos
GIFSDIR="/home/pi/remote_gifs/gifs";
sizepartition="200k";
function textme() {
${FT_PATH}/client/send-text -g96x20+0+44 -h localhost -f "${FT_PATH}/client/fonts/10x20.bdf" "$@" -c d85720 -o e8d96a -s10 -O
}
function getRandom() {
declare -a biggifs smallgifs shuffled;
while IFS= read -d $'\0' -r file ; do biggifs+=("$file"); done < <(find "$1" -size +${sizepartition} -print0 | shuf -z)
while IFS= read -d $'\0' -r file ; do smallgifs+=("$file"); done < <(find "$1" -size -${sizepartition} -print0 | shuf -z)
inbetween="$(( ${#smallgifs[@]} / ${#biggifs[@]} ))";
logme "big: ${#biggifs[@]}, small: ${#smallgifs[@]}, inbetween $inbetween";
for f in "${biggifs[@]}"; do
for i in $(seq 1 $inbetween); do
echo -n "${smallgifs[0]} " >&2;
shuffled+=("${smallgifs[0]}");
smallgifs=("${smallgifs[@]:1}");
done
echo -n "$(clr_gr)${f}$(endcolor) " >&2;
shuffled+=("${f}");
done
for f in "${smallgifs[@]}"; do echo -n "${f} "; shuffled+=("${f}"); done #and all leftovers
logme "done adding ${#shuffled[@]} files";
logme "done";
echo "${shuffled[@]}";
}
if [[ "${BASH_SOURCE[0]}" = "${0}" ]]; then
if [[ "$1" == 'backend' ]]; then
[[ $EUID -ne 0 ]] && { echo "$0 needs to be run as root!"; exit 1; }
while true; do
${FT_PATH}/server/ft-server --led-rows=32 --led-chain=4 --led-pixel-mapper="U-mapper;Rotate:90" --led-brightness=20
sleep 1;
done
exit;
elif [[ "$1" == 'start' ]]; then
[[ $EUID -ne 0 ]] && { echo "$0 needs to be run as root!"; exit 1; }
## Launch backend
screen -S ledbackend -X quit;
screen -AdmS ledbackend $0 backend;
screen -S ledbackend -X zombie qr;
screen -S ledmonitor -X quit;
screen -AdmS ledmonitor $0 monitor; #main script
screen -S ledmonitor -X zombie qr;
exit;
elif [[ "$1" == 'play' ]]; then
echo_gr "playing ${@:2}";
${FT_PATH}/client/send-video -h localhost -g64x64 -c -t4 -v "${@:2}";
exit;
elif [[ "$1" == 'random' ]]; then
while true; do
random=($(getRandom "${2}")) &&
echo "playing ${random[@]}" &&
"$0" play "${random[@]}"; # || { [[ $? -eq 1 ]] && exit; }
sleep 2;
done
exit;
elif [[ "$1" == 'monitor' ]]; then
echo 2 > /sys/class/gpio/export;
echo low > /sys/class/gpio/gpio2/direction;
function readMe() { cat /sys/class/gpio/gpio2/value; }
trap ctrl_c INT
function ctrl_c() {
screen -S ledplay -X quit;
keepRunning='false';
}
lastSwitch="NA";
keepRunning='true';
( while [[ $keepRunning == "true" ]]; do sleep $[ ( $RANDOM % 10 ) + 1 ]s; textme "BIKE PARTY"; done )&
while [[ $keepRunning == "true" ]]; do
switch="$(readMe)" || exit 2;
fastSwitch=0;
if [[ "$switch" != "$lastSwitch" ]]; then
screen -S ledplay -X quit;
killall send-video;
[[ "$switch" == "0" ]] && filed="${GIFSDIR}" || filed="/home/pi/gifs";
echo "switch change to $switch .. playing ${filed}";
screen -AdmS ledplay "$0" random "${filed}";
screen -S ledplay -X zombie qr;
lastSwitch="$switch";
for i in {1..10}; do
switch="$(readMe)";
if [[ "$switch" != "$lastSwitch" ]]; then
echo "fast switch $fastSwitch (three to poweroff)";
fastSwitch=$(( $fastSwitch + 1 ));
if [[ "$fastSwitch" -ge 3 ]]; then
killall video-viewer;
textme "cya later";
echo "powering down"; ##TODO print on led array
sleep 1;
poweroff
fi
lastSwitch="$switch";
fi
sleep 0.1;
done
fi
sleep 0.5;
done
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment