#!/bin/bash # get-photos.sh for Mk2 PhotoFrame, last updated 28 Sept. 2016 # GPIO setup (GPIO27 (hdr. pin 13) is used to pull LED lo when loading) # Start by clearing any current setting (gpio remains set after the script crashes) sudo sh -c 'echo "27" > /sys/class/gpio/unexport' sudo sh -c 'echo "27" > /sys/class/gpio/export' # Tri-state the pin (set it's direction to 'in') - to pull the pin low, we set it's direction 'low' sudo sh -c 'echo "in" > /sys/class/gpio/gpio27/direction' # # source photos are in root of USB stick, set mount point (only done on launch, so doesn't matter if we get an 'already exists' error) mkdir -p /usbroot # copy resize is to /photos/ folder (existing files of same name are overwritten) # loop forever checking for USB stick insert while [ true ]; do # 1) Wait for a USB stick to be inserted (fdisk lists devices, grep looks for sda1 found = true, so need ! (for not found) while ! fdisk -l | grep -q "sda1"; do sleep 1 done # OK, usb stick found, lets mount it (read only) mount /dev/sda1 /usbroot -o noatime # 2) Turn on the Loading LED (pull GPIO 27, pin13 Low sudo sh -c 'echo "low" > /sys/class/gpio/gpio27/direction' # 3) Make/save thumbnials of all .jpg / .JPG in the /usbroot/ to /photos/?.jpg for filename in $(ls -A1 /usbroot/*.jpg); do convert $filename -resize '1600x1200' /photos$filename done # some camera jpg's use upper case (.JPG) file extension for filename in $(ls -A1 /usbroot/*.JPG); do # use SED to substitute (s) JPG with jpg (/g means 'replace all occurances') outname="$(echo $filename | sed 's/JPG/jpg/g')" convert $filename -resize '1600x1200' /photos$outname done # all files copied, unmount the usb drive umount /dev/sda1 # 4) Turn off the Loading LED (set GPIO27, pin13 as input) sudo sh -c 'echo "in" > /sys/class/gpio/gpio27/direction' # 5) Wait until the USB stick is removed while fdisk -l | grep -q "sda1"; do sleep 1 done # 6) Loop back done