How the set-photo script works

All this script has to do is find the 'next' photo, copy it to the 'free' buffer and re-link display.jpg to the just 'filled' buffer (thus freeing the other buffer). Of course it's not quite that simple :-) To avoid any 'missing file' errors (and speed up the appearance of new photos), the /photos folder contents must be checked just before choosing the next photo - which is then immediately copied to ram-disk (to avoid it being deleted). Further, when choosing 'next' we have to cope with the current photo being deleted and also with new photos being added. Set up the /photos folder and the ram-disk (/photos/ram-disk) This script has to check for and set-up the /photos folder and files (because get-photo script is optonal and fbi has to be launched last) #!/bin/bash # set-photo.sh script for Mk2 PhotoFrame, last modified 28 Sept. 2016 # start by creating the required folders (don't matter if they already exist) sudo mkdir /photos sudo mkdir /photos/ram-disk # check if we need to add ram-disk to the fstab file mkram="tmpfs /photos/ram-disk tmpfs nodev,nosuid,size=5M 0 0" # (5M bytes should be plenty) if grep -Fxq "$mkram" /etc/fstab; then echo "ram-disk found in fstab, going for mount" else sudo sh -c "echo \"$mkram\" >> /etc/fstab" # alternate is: echo "$mkram" | sudo tee -a /etc/fstab > /dev/null fi # mount /photos/ram-disk to tmpfs sudo mount -a # show status (check if the /photos/ram-disk has 'mounted' to tmpfs) :- df -HT # create the buffer files (just in case we try to link before copying a photo) touch /photos/ram-disk/buffer1.jpg touch /photos/ram-disk/buffer2.jpg touch /photos/ram-disk/display.jpg Housekeeping done, now choose the next photo to be displayed. The directory listing is in 'date' order, so any newly created thumbnail(s) will be listed first. If the current top of list differs from previous, it's a new photo and it is chosen next. Otherwise we saerch for the current photo and choose the one after that - if the current is not found (or it's bottom of the listing) the top of the list if chosen. Finally, the new photo is to be copied to ram-disk/bufferX (and the free/busy buffers switched) The /photos folder holds pre-sized .jpg's to be displayed, and the /photos/ram-disk (tmpfs) folder holds both 'buffers' (buffer1.jpg, buffer2.jpg) and the display.jpg 'virtual' file name (which will be 'linked' to one of the buffers) The 3 'alias' files needed by fbi will be created (and linked actual to virtual, display.jpg to alias) by the fbi script. One thing to watch out for is to avoid 'overwriting' the 'just free' buffer too quickly = it takes fbi 1 second to stop 'reading' the old buffer and follow the alias - display - new buffer link. # check we have at least one photo while ![ ls -U /photos/*.jpg 1> /dev/null 2>&1 ]; do echo "waiting for jpgs in photos folder" sleep 5 done # # OK, we have a photo, lets go # # Select and copy the next photo into the free buffer and then updates the display alias # Source photos are held as thumbnails in the /photos/ folder (fetching and resizing is performed by the get-photo script) # Both buffers1&2.jpg and display.jpg are held in the /ram-disk/ folder (the actual display is performed by the fbi script) showtime=4 #photo show time -1 (so 4 = 5 seconds total) cfile="0" # current file name ctop="0" # current top by date nbuf="buffer1.jpg" # next (free) buffer # loop forever while [ true ]; do top="0" # no top found next="0" # no next found # Scan the photos dir for the current file for filename in $(ls -A1c /photos/*.jpg); do # note, when you ls a different folder, the path is added to each file name (so file is /photos/name.jpg ) if [ $top = "0" ]; then top=$filename if [ $filename != $ctop ]; then next=$filename; fi fi # find the next file AFTER the current if [ $next = "0" ]; then if [ $cfile = "0" ]; then next=$filename; fi if [ $filename = $cfile ]; then cfile="0"; fi fi done # if no next found, use top if [ $next = "0" ]; then next=$top; fi ctop=$top # OK, we have the next file, pause in case fbi is still reading the free buffer sleep 1 cp -f $next /photos/ram-disk/$nbuf cfile=$next if [ $nbuf = "buffer1" ] then; nbuf="buffer2.jpg" else nbuf="buffer1.jpg" fi # now switch the link on first pass should creates display.jpg) ln -s -f /photos/ram-disk/$nbuf /photos/ram-disk/display.jpg # wait for the rest of the show time sleep $showtime # now go find the next done After creating the new script, don't forget to make it 'executable' :- $ sudo chmod +x /photos/set-photo.sh To test this from the command line (after you cd to /photos i.e. the directory where the script resides) just type :-
./set-photo.sh