-
Notifications
You must be signed in to change notification settings - Fork 15
/
photoframe.sh
executable file
·360 lines (295 loc) · 7.42 KB
/
photoframe.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#! /bin/bash
CONF_DIR=/data/photoframe
MOUNTPOINT_DAV=/data/photoframe/images_webdav
FOLDER_IMAGES=/data/photoframe/images_local
WEBDAV_CONF=${CONF_DIR}/conf/webdav.conf
if [ -e ${CONF_DIR}/conf/webdav_cert.pem ]
then
DAVFS_CONF=/etc/photoframe/davfs2_owncert.conf
else
DAVFS_CONF=/etc/photoframe/davfs2.conf
fi
#File that lists all available photos. Will be overwritten on sync
PHOTO_FILE_LIST=${CONF_DIR}/conf/filelist.txt
NO_IMAGES="/usr/share/photoframe/noimages.png"
ERROR_DIR="/tmp/photoframe"
mkdir -p $ERROR_DIR
SLIDESHOW_DELAY=3
SHUFFLE=true
START_RANDOM=false
SHOW_FILENAME=false
SHOW_VIDEOS=false
SMARTFIT=30
CEC_DEVICE_ID=-1
GPIO_PIN_NEXT=-1 # show next file
GPIO_PIN_PREVIOUS=-1 # show previous file
GPIO_PIN_PLAY=-1 # start/pause rotating images automatically
GPIO_ACTION_VALUE=0 # value to identify action, for an pullup the value should be 0, for pulldown 1
if [ -e ${CONF_DIR}/conf/photoframe.conf ]
then
source ${CONF_DIR}/conf/photoframe.conf
fi
PARAMS_FBV="--noclear --smartfit ${SMARTFIT} --delay 1"
# configure control buttons
function init_gpio_input_pin() {
if [ "${1}" != "-1" ]
then
if [ ! -d /sys/class/gpio/gpio${1} ]
then
echo ${1} > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio${1}/direction
fi
else
echo not configuring gpio
fi
}
init_gpio_input_pin ${GPIO_PIN_NEXT}
init_gpio_input_pin ${GPIO_PIN_PLAY}
init_gpio_input_pin ${GPIO_PIN_PREVIOUS}
function read_conf {
read -r firstline< $WEBDAV_CONF
array=($firstline)
echo ${array[0]}
}
function sync {
error_settopic 10_Sync
if [ -f "$WEBDAV_CONF" ]; then
chmod 0600 ${WEBDAV_CONF}
mkdir -p $FOLDER_IMAGES
mkdir -p $MOUNTPOINT_DAV
REMOTE_DAV=$(read_conf)
ERROR=$(mount.davfs -o ro,conf=$DAVFS_CONF "$REMOTE_DAV" $MOUNTPOINT_DAV 2>&1 > /dev/null)
if [ $? -ne 0 ]
then
error_write "Mounting $REMOTE_DAV failed: $ERROR"
fi
# Check if dav is mounted before starting rsync
mount | grep $MOUNTPOINT_DAV > /dev/null
if [ $? -eq 0 ]
then
# Only sync supported files
if [ "$SHOW_VIDEOS" = true ]
then
ERROR=$(rsync -vtrm --include '*.png' --include '*.PNG' --include '*.jpg' --include '*.JPG' --include '*.jpeg' --include '*.JPEG' --include '*.mp4' --include '*.MP4' --include '*.mov' --include '*.MOV' --include '*/' --exclude '*' --delete $MOUNTPOINT_DAV/ $FOLDER_IMAGES 2>&1 > /dev/null)
else
ERROR=$(rsync -vtrm --include '*.png' --include '*.PNG' --include '*.jpg' --include '*.JPG' --include '*.jpeg' --include '*.JPEG' --include '*/' --exclude '*' --delete $MOUNTPOINT_DAV/ $FOLDER_IMAGES 2>&1 > /dev/null)
fi
[ $? -eq 0 ] || error_write "Syncing images failed: $ERROR"
umount $MOUNTPOINT_DAV
find $FOLDER_IMAGES -type f -iname '*\.jpg' -o -iname '*\.jpeg' -o -iname '*\.png' -o -iname '*\.mp4' -o -iname '*\.mov' | sort > $PHOTO_FILE_LIST
fi
else
error_write "No WebDAV server configured. Go to http://$(hostname)"
fi
}
ERROR_TOPIC="";
function error_display {
TTY=/dev/tty0
echo -en "\e[H" > $TTY # Move tty cursor to beginning (1,1)
for f in $ERROR_DIR/*.txt; do
[[ -f $f ]] || continue
cat $f > $TTY
done
}
function error_settopic {
ERROR_TOPIC=$1.txt;
> $ERROR_DIR/$ERROR_TOPIC
}
function error_write {
echo $1 >> $ERROR_DIR/$ERROR_TOPIC
}
num_files=0;
file_num=0;
if [ "$START_RANDOM" = true ]
then
file_num=$RANDOM
fi
function get_image {
local rnd_num
rnd_num=-1
local counter
counter=0
num_files=0
if [ -f $PHOTO_FILE_LIST ]
then
num_files=$(wc -l "$PHOTO_FILE_LIST" | awk '{print $1}')
fi
if [ $num_files -gt 0 ]
then
if [ "$SHUFFLE" = true ]
then
# sed counts from 1 to N (not 0 to N-1)
rnd_num=$(( ( $RANDOM % $num_files ) + 1 ))
else
# sed counts from 1 to N (not 0 to N-1)
file_num=$((file_num % $num_files));
file_num=$((file_num+1));
rnd_num=$file_num
fi
IMAGE=$(sed "${rnd_num}q;d" $PHOTO_FILE_LIST)
fi
if [ -z "$IMAGE" ]
then
if [ $num_files -eq 0 ]
then
IMAGE=$NO_IMAGES
else
get_image
fi
fi
}
function get_previous_image() {
file_num=$((file_num + $num_files));
file_num=$((file_num-2));
get_image
}
function is_gpio_pressed() {
if [ ! -d /sys/class/gpio/gpio${1} ]
then
false
return
fi
if [ "$(cat /sys/class/gpio/gpio${1}/value)" -eq "${GPIO_ACTION_VALUE}" ]
then
true
return
else
false
return
fi
}
function start {
local IMAGE=$NO_IMAGES
local AUTO_NEXT_MODE=true # show next file after SLIDESHOW_DELAY
local IS_IMAGE=false
local PID=-1 # pid of omxplayer do detect video end
local LAST_IMAGE_UPDATE=0
counter=0
error_settopic 01_Startup
error_write "Go to http://$(hostname) to configure photOS"
UPDATE_MEDIA=false
while true; do
if is_gpio_pressed ${GPIO_PIN_NEXT}
then
get_image
UPDATE_MEDIA=true
read -p "Pausing NEXT" -t 0.5
continue
elif is_gpio_pressed ${GPIO_PIN_PREVIOUS}
then
get_previous_image
UPDATE_MEDIA=true
read -p "Pausing PREVIOUS" -t 0.5
continue
elif is_gpio_pressed ${GPIO_PIN_PLAY}
then
read -p "Pausing PLAY" -t 0.5
if ${AUTO_NEXT_MODE}
then
AUTO_NEXT_MODE=false
else
AUTO_NEXT_MODE=true
fi
continue
fi
if ${AUTO_NEXT_MODE}
then
NOW=$(date +%s)
if [ "$IS_IMAGE" = true ]
then
# image was shown for slide show delay
if [ $(( NOW - LAST_IMAGE_UPDATE )) -gt ${SLIDESHOW_DELAY} ]
then
UPDATE_MEDIA=true
get_image
fi
else
# Check if video has ended
if ! [ -d "/proc/$PID" ]; then
# Video has ended, new media can be shown
UPDATE_MEDIA=true
get_image
fi
fi
fi
if ${UPDATE_MEDIA}
then
UPDATE_MEDIA=false
echo $IMAGE
IS_IMAGE=false
if file "$IMAGE" | cut -d':' -f2 |grep -qE 'image|bitmap'
then
IS_IMAGE=true
fi
if ${IS_IMAGE}
then
LAST_IMAGE_UPDATE=$(date +%s)
IMAGE2=/tmp/photoframe.image
cp "$IMAGE" "$IMAGE2"
jhead -autorot $IMAGE2 &> /dev/null
fbv $PARAMS_FBV "$IMAGE2"
else
omxplayer --no-keys "$IMAGE" &
PID=$!
fi
if [ "$SHOW_FILENAME" = true ]
then
# abuse error reporting to show the path of the current picture
error_settopic 02_Current
#don't show the FOLDER_IMAGES prefix
error_write "$( echo "$IMAGE" | sed -e "s|^${FOLDER_IMAGES}/||" )"
fi
error_display
counter=$((counter+1))
if [ $counter -eq 10 ]
then
error_settopic 01_Startup
fi
fi
done
}
function display {
case "$1" in
on)
vcgencmd display_power 1
if [ "${CEC_DEVICE_ID}" != "-1" ]
then
echo "on ${CEC_DEVICE_ID}" | cec-client -s -d 1
fi
;;
off)
vcgencmd display_power 0
if [ "${CEC_DEVICE_ID}" != "-1" ]
then
echo "standby ${CEC_DEVICE_ID}" | cec-client -s -d 1
fi
;;
*)
echo "Usage: $0 display {on|off}"
exit 1
esac
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
sync)
sync
;;
display)
display $2
;;
test)
get_image
;;
*)
echo "Usage: $0 {start|stop|restart|sync|display on/off}"
exit 1
esac