software (svp.sh)

#!/bin/bash
#  Notice, must be run explicitly as "/bin/bash", not "/bin/sh",
#  because advanced bash features like arrays are used.

#  svp.sh

VID_LOCATION='/mnt/videos'
VID_SUFFIX='.mpg'
IMG_LOCATION='/mnt/images'
IMG_SUFFIX='.jpg'
IMG_NOTFOUND='notfound.jpg'
VID_CMD='mpv --fs --ontop --no-resume-playback --audio-device=alsa/hdmi:CARD=vc4hdmi,DEV=0'
IMG_CMD='fim -N -q'

exec 2> /dev/null

#  Delay for 10 seconds after startup before trying to display anything.
#  This is a kluge (obviously).  The .service file has the
#  "WantedBy=multi-user.target" line, which is supposed to pause activity
#  until the system is ready, but this doesn't work.  Maybe I should be using
#  a different .target?  Anyway, without the delay here, the first image
#  does not get displayed, instead there is just a mess of console text on
#  the screen until the first button is pressed, because the initial image
#  display occurred before the graphics system was ready.
echo "waiting for video to initialize..."
T=10
while true; do
    T=`expr $T - 1`
    if [ ${T} -le 0 ]; then
        break
    fi
    sleep 1
done
echo
echo "OK, let's try it"

#  Find the list of video filenames and store in array VID[], then create
#  the associated cover image filenames in array IMG[].  If one of the
#  video files does not have an associated cover image, then the
#  IMG_NOTFOUND image is displayed instead.
N=0
for F in `ls -1 ${VID_LOCATION}`; do
    BN=`basename ${F} ${VID_SUFFIX}`
    VID[${N}]="${VID_LOCATION}/${BN}${VID_SUFFIX}"
    I="${IMG_LOCATION}/${BN}${IMG_SUFFIX}"
    IMG[${N}]="${I}"
    if [ ! -r "${I}" ]; then
        IMG[${N}]="${IMG_LOCATION}/${IMG_NOTFOUND}"
    fi
    N=`expr $N + 1`
done
TOP=`expr $N - 1`

#  This is the main running loop.  Initially, the first cover image is
#  displayed.  If forward or back arrows are hit, then the image changes
#  accordingly.  When the play button is hit, the corresponding video
#  for the cover image currently onscreen is played.  Any button hits
#  during video playback will terminate the playback and return to the
#  cover images.
#
#  Button presses are detected by reading the three GPIO lines with
#  "gpioget".  A value of 1 means the button is up; a value of 0 means
#  the button is pressed.  All actions happen at the release of the
#  buttons, i.e., as the value changes from 0 to 1.
SEL=0
result=0
playing=0
img_active=0
while true; do
    if [ ${playing} -eq 0 ]; then
        if [ ${img_active} -eq 0 ]; then
            ${IMG_CMD} ${IMG[${SEL}]} &
            ourpid=$!
            img_active=1
        fi
    fi
    while true; do
        B1=`gpioget gpiochip0 17`
        B2=`gpioget gpiochip0 27`
        B3=`gpioget gpiochip0 22`
        past_result=${result}
        result=0
        if [ $B1 -eq 0 ]; then result=1; fi
        if [ $B2 -eq 0 ]; then result=2; fi
        if [ $B3 -eq 0 ]; then result=3; fi
        if [ ${result} -eq 0 ]; then
            if [ ${past_result} -eq 1 ]; then SEL=`expr $SEL - 1`; break;
            elif [ ${past_result} -eq 2 ]; then break;
            elif [ ${past_result} -eq 3 ]; then SEL=`expr $SEL + 1`; break;
            fi
        fi
        if [ ${playing} -eq 1 ]; then
            if kill -0 ${vid_pid}; then
                #  video is still actually playing
                playing=1
            else
                #  video has ended
                playing=0
                break
            fi
        fi
    done
    if [ ${SEL} -ge ${N} ]; then
        SEL=0
    fi
    if [ ${SEL} -lt 0 ]; then
        SEL=${TOP}
    fi
    if [ ${img_active} -eq 1 ]; then
        kill ${ourpid}
        img_active=0
    fi
    if [ $past_result -eq 2 ]; then
        if [ ${playing} -eq 0 ]; then
            playing=1
            ${VID_CMD} ${VID[${SEL}]} &
            vid_pid=$!
        else
            playing=0
            kill ${vid_pid}
        fi
    else
        if [ ${playing} -eq 1 ]; then
            playing=0
            kill ${vid_pid}
        fi
    fi
done

exit 0
exit 0
exit 0

Comments

Popular posts from this blog

obtaining the content

photos