-
Notifications
You must be signed in to change notification settings - Fork 1
/
stackshooter
executable file
·1330 lines (1181 loc) · 42.6 KB
/
stackshooter
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# stackshooter - Generate stackshots with arduino + steppermotor + camera
#
# License: MIT
#
# Dependencies:
# - align_image_stack (hugin tools)
# - ffmpeg
# - geeqie
# - gphoto2
# - kaptain https://github.com/mviereck/kaptain
#### Messages
note() {
echo "${Colgreen}stackshooter:${Colnorm} $*" >&2
}
failure() {
echo "${Colred}stackshooter FAILURE:${Colnorm} $*" >&2
statusmessage "$*"
setvar "error=yes"
showpercent error
}
statusmessage() {
sendkaptain "status='$(cut -c1-37 <<< "$*")'"
}
#### Misc
isnum() {
[ "1" = "$(awk -v a="${1:-}" 'BEGIN {print (a == a + 0)}')" ]
}
milisleep() {
local Time
Time="$(awk "BEGIN { print ${1:-1000} / 1000 }")"
sleep $Time
}
#### File operations
get_latest_file() {
## Funktion: Neueste erstellte Datei ermitteln
# $1 kann Suche auf Dateien DATEI* begrenzen
#ls -p -t $1* 2> /dev/null | grep -v "/" | head -n 1
find ${1:-"."} -maxdepth 1 -type f -printf "%T@ %p\n" | sort -n | awk '{print $2}' | tail -n1
}
get_last_file() {
# Find alphanumerically last file beginning with $1 in pwd or $2.
local Filelist Dir
Dir="${2:-$(pwd)}"
# Filelist="$(find $Dir/${1:-}* -mindepth 1 -maxdepth 1 | xargs realpath --relative-base "$Dir" | grep "^${1:-}")"
Filelist="$(find $Dir/${1:-}* -maxdepth 1)"
Filelist="$(sort -V <<< "$Filelist")"
tail -n1 <<< "$Filelist"
}
get_free_filenumber() {
# Provide next free file number to file base name $1.
# Provide $1 without _00N.
# $1 must not contain _
local Number Dir Basename
Dir="$(dirname "${1:-}")"
Dir="${Dir:-$(pwd)}"
Basename="$(basename "${1:-}")"
Number="$(get_last_file "$Basename" "$Dir" | rev | cut -d_ -f1 | rev | cut -d. -f1)"
Number="${Number:-0}"
Number="$(awk "BEGIN {print \"$Number\" + 1}" )"
printf "%04d" "$Number"
}
setup_projectdir() {
local Projecthome Projectname Projectdate
Projecthome="$(getvar storagebasedir)"
Projecthome="${Projecthome:-$HOME/stackshooter}"
Projectbasename="$(getvar projectbasename)"
Projectbasename="${Projectbasename:-project}"
Projectdate="$(date +%Y-%m-%d)"
[ "${1:-}" ] && Projectname="${1:-}.${Projectdate}"
Projectname="${Projectname:-${Projectbasename}.${Projectdate}}"
setvar "projectname=$Projectname"
cd "$Projecthome"
mkdir -p "$Projectname"
cd "$Projectname"
}
showimage() {
[ -f "${1:-}" ] || {
failure "Image not found: ${1:-}"
return 1
}
geeqie -t -r File:"${1:-}"
}
#### Device communication
device_init() {
# Init Device and start watching background process
local Device Watchdevicepid i Return
Device="$(getvar device)"
# Stop a running watching instance
Watchdevicepid="$(getvar pidwatchdevice)"
ps -p "$Watchdevicepid" >/dev/null 2>&1 && {
kill $Watchdevicepid
wait $Watchdevicepid 2>/dev/null
exec 6>&-
}
# Device auf Gültigkeit prüfen
[ -e "$Device" ] || {
failure "Device not found: $Device"
Device="$Devicefallback"
failure "Fallback device: $Device"
setvar "device=$Device"
}
# Daten von Device auf fifo umleiten
note "Beginning to watch device: $Device"
exec 6<>$Device
cat <&6 >>$Devicemessagefifo & Watchdevicepid=$!
storepid $Watchdevicepid
setvar "pidwatchdevice=$Watchdevicepid"
# Seriellen Port initialisieren
stty -F $Device raw ispeed 9600 ospeed 9600 cs8 -hupcl -ignpar -cstopb -echo || {
failure "Failed to initialize device $Device"
return 1
}
sleep $Devicetimeout
for ((i=1;i<=10;i++)); do
device_check && break || Return=1
done
[ "$Return" = "1" ] && failure "Device init failed: $Device"
return ${Return:-0}
}
device_send() {
## Funktion: Daten an Arduino senden
# Alle übergebenen Optionen werden an $Deviceadresse gesendet
note "Send to device: $*"
echo "$*" >&6
}
device_wait() {
## Funktion: Auf ein beliebiges Signal vom Arduino warten
local Answer=
read -t ${1:-$Devicetimeout} -r Answer <&6
[ "$Answer" ] || {
failure "Device does not respond: $(getvar device) "
echo "TIMEOUT"
return 1
}
note "Device responses: $Answer"
echo "$Answer"
return 0
}
device_check() {
# Prüfen, ob Arduino auf Kontrollsignal "c" antwortet
device_send "$(getvar signalcheck)"
[ "$(device_wait)" = "TIMEOUT" ] && return 1 && failure "Device check failed: $(getvar device)" || return 0
}
#### Device navigation
position_store() {
setvar "poscurrent=$Poscurrent"
setvar "pos1=$Pos1"
setvar "pos2=$Pos2"
setvar "stepwidth=$Stepwidth"
sendkaptain "poscurrent='$Poscurrent'"
sendkaptain "pos1='$Pos1'"
sendkaptain "pos2='$Pos2'"
sendkaptain "difference='$(awk "BEGIN {print $Pos2 - $Pos1 }" )'"
sendkaptain "distance='$(awk "BEGIN {print $Stepdistance * ($Pos2 - $Pos1) }") $Stepunit'"
sendkaptain "steps='$(awk "BEGIN {print ($Pos2 - $Pos1)/$Stepwidth }" )'"
sendkaptain "stepmy='$(awk "BEGIN {print $Stepdistance * $Stepwidth}") $Stepunit'"
}
position_read() {
local Newstepwidth
Poscurrent="$(getvar poscurrent)"
Pos1="$(getvar pos1)"
Pos2="$(getvar pos2)"
Stepwidth="$(getvar stepwidth)"
Stepdistance="$(getvar stepdistance)"
Stepunit="$(getvar stepunit)"
}
stepcmd() {
local Return Answer
position_read
case ${1:-} in
go)
steplauf "${2:-$Poscurrent}"
;;
stepin)
steplauf "$(awk "BEGIN {print $Poscurrent + $Stepwidth }" )"
;;
stepout)
steplauf "$(awk "BEGIN {print $Poscurrent - $Stepwidth }" )"
;;
setpos0)
Poscurrent=0
#device_send "$(getvar signalsetpos0)"
#Answer="$(device_wait)"
;;
setpos1)
Pos1="$Poscurrent"
;;
setpos2)
Pos2="$Poscurrent"
;;
gopos0)
steplauf "0"
;;
gopos1)
steplauf "$Pos1"
;;
gopos2)
steplauf "$Pos2"
;;
stepwidth)
Stepwidth="${2:-25}"
device_send "$(getvar signalstepwidth),$Stepwidth"
Answer="$(device_wait)"
;;
esac
note "stepcmd $*: current:$Poscurrent pos1:$Pos1 pos2:$Pos2 stepwidth:$Stepwidth"
Return=$?
[ "$Answer" = "TIMEOUT" ] && Return=1
position_store
return "${Return:-0}"
}
steplauf() {
# Go to position $1
local Direction Distance Timetowait Stepwith_store
device_check || return 1
Stepwidth_store="$Stepwidth"
# Use Distance from Poscurrent to $1 as temporary Stepwidth
Distance="$(awk "BEGIN {print ${1:-} - $Poscurrent }" )"
[ "$Distance" = "0" ] && return 0
[ "$Distance" -gt "0" ] && {
Direction="$(getvar signalstepin)"
} || {
Direction="$(getvar signalstepout)"
Distance="$(awk "BEGIN {print -1 * $Distance }" )"
}
[ "$Distance" = "$Stepwidth" ] || {
device_send "$(getvar signalstepwidth),$Distance"
[ "$(device_wait)" = "TIMEOUT" ] && return 1
}
Timetowait="$(awk "BEGIN {print 18 * $Distance / 10000 + $Devicetimeout }" )" ### FIXME needs more general solution
device_send "$Direction"
Answer="$(device_wait "$Timetowait")"
[ "$Answer" = "TIMEOUT" ] && return 1
case $Direction in
"$(getvar signalstepin)") Poscurrent="$(awk "BEGIN {print $Poscurrent + $Distance }" )" ;;
"$(getvar signalstepout)") Poscurrent="$(awk "BEGIN {print $Poscurrent - $Distance }" )" ;;
esac
[ "$Distance" = "$Stepwidth_store" ] || {
device_send "$(getvar signalstepwidth),$Stepwidth"
device_wait >/dev/null
}
}
stackshot() {
## Funktion: Fotoserie schießen, dabei Motor weiterdrehen. Geht von $Pos1 zu $Pos2.
# -p Default: Serie aus Preview-Bildern.
# -c Serie aus richtigen Fotos / Captures.
local Posmem=
local Stackshotdelay Anzahl Count= Loopcount=0 Warten
local Fotomodus Ordnername= Glob
local Mediannumber Medianmode Mediancount
local Stackshotdir Stackshotbasename Stackshotname Projectname Storagebase
gphoto2_cmd check || {
dropvar stackshot
return 1
}
device_check || {
dropvar stackshot
return 1
}
[ "$1" = "-c" ] && Fotomodus="capture" && shift
[ "$1" = "-p" ] && Fotomodus="preview" && shift
Fotomodus="${Fotomodus:-preview}"
Mediannumber="$(getvar mediannumber)"
Mediannumber="${Mediannumber:-1}"
[ "$Mediannumber" = "1" ] && Medianmode="no" || Medianmode="yes"
Stackshotdelay="$(getvar stackshotdelay)"
Stackshotdelay="${Stackshotdelay:-500}"
Storagebasedir="$(getvar storagebasedir)"
Storagebasedir="${Storagebasedir:-$HOME/stackshooter}"
Projectname="$(getvar projectname)"
Projectname="${Projectname:-project_unknown}"
Stackshotbasename="$(getvar stackshotbasename)"
Stackshotbasename="${Stackshotbasename:-st}" ### FIXME declare in GUI
cd "$Storagebasedir/$Projectname"
Stackshotname="${Stackshotbasename}_$(get_free_filenumber "$Storagebasedir/$Projectname/$Stackshotbasename")"
Stackshotdir="$Storagebasedir/$Projectname/$Stackshotname"
setvar "lateststack=$Stackshotdir"
mkdir -p "$Stackshotdir/stackshot"
[ "$Medianmode" = "yes" ] && mkdir -p $Stackshotdir/median.source
cd $Stackshotdir
# ggf. Positionen für stepin +/- korrigieren
position_read
[ "$Pos1" -gt "$Pos2" ] && {
note "stack: Switching Pos1:$Pos1 and Pos2:$Pos2"
Posmem=$Pos1
Pos1=$Pos2
Pos2=$Posmem
position_store
}
# Stackshot
#Anzahl="$(bc <<< "($Pos2 - $Pos1) / $Stepwidth" | cut -d. -f1)"
Anzahl="$(awk "BEGIN {print ($Pos2 - $Pos1) / $Stepwidth }" | cut -d. -f1)"
note "Shooting stack $Stackshotdir with $Anzahl steps."
sendkaptain "info='Shooting stack $(basename $Stackshotdir) with $Anzahl steps.'"
# An Startposition gehen
stepcmd go $Pos1
position_read
[ "$Poscurrent" = "$Pos1" ] || {
failure "stack: Error: Missed position $Pos1: $Poscurrent."
dropvar stackshot
return 1
}
# Auf Start warten
#sleep $(getvar startdelay)
Warten="$(getvar startdelay)"
Count=0
while [ "$Count" -lt "$Warten" ] ; do
statusmessage "Delay before start: Countdown $((Warten-Count))s"
showpercent $Count $Warten
testvar cmdbreak && break
Count="$((Count+1))"
sleep 1
done
showpercent busy
statusmessage "mirror up"
gphoto2_cmd mirrorup
while [ "$Poscurrent" -lt "$Pos2" ] ; do
Loopcount=$((Loopcount +1 ))
testvar cmdbreak && break
showpercent $Loopcount $Anzahl
statusmessage "$Fotomodus $Loopcount / $Anzahl"
for ((Mediancount=1 ; Mediancount<=$Mediannumber ; Mediancount++)); do
# wait after step before capture
Count=1
while [ "$((Count*1000))" -lt "$Stackshotdelay" ] ; do
statusmessage "Delay before next capture: Countdown $((Stackshotdelay/1000-Count))s"
Count="$((Count+1))"
sleep 1
done
Count="$((Count-1))"
milisleep $((Stackshotdelay - Count*1000))
# capture
case $Fotomodus in
capture)
Filename="$(LC_ALL=C gphoto2_wrap --capture-image-and-download --force-overwrite | grep 'Saving file as')"
gphoto2_cmd mirrorup || setvar "cmdbreak=yes"
Filename="$(awk '{print $4}' <<< "$Filename")"
Latestimage="$Stackshotdir/stackshot/capture_$(printf "%04d" $Loopcount).jpg"
[ -e "$Filename" ] || {
failure "Capture failed"
dropvar stackshot
return 1
}
mv "$Filename" "$Latestimage"
;;
preview)
gphoto2_wrap --capture-preview || setvar "cmdbreak=yes"
Latestimage="$Stackshotdir/stackshot/preview_$(printf "%04d" $Loopcount).jpg"
mv capture_preview.jpg "$Latestimage"
setvar "latestimage=$Latestimage"
;;
esac
case $Medianmode in
yes)
Mediangroup="$Stackshotdir/median.source/img_$(printf "%04d" $Loopcount)"
Latestmedian="${Mediangroup}_m$(printf "%02d" $Mediancount).jpg"
cp $Latestimage $Latestmedian
note "stack: Image stored as $Latestmedian"
setvar "latestimage=$Latestmedian"
;;
no)
note "stack: Image stored as $Latestimage"
;;
esac
showimage "$Latestimage"
done
[ "$Medianmode" = "yes" ] && {
note "stack: Aligning captures of step $Loopcount."
statusmessage "Aligning median captures of step $Loopcount."
align_image_stack -l --use-given-order -a ${Mediangroup}_align $Mediangroup*.jpg 2>&1 | grep -v "Unable to read EXIF data"
Latestimage="$Stackshotdir/stackshot/${Fotomodus}_$(printf "%04d" $Loopcount).tif"
$Magickbin ${Mediangroup}_align*.tif -evaluate-sequence median "$Latestimage"
showimage "$Latestimage"
}
# step in
statusmessage "next step"
stepcmd stepin
position_read
done
note "stack: Ready: Stackshot $Stackshotname with $((Loopcount * Mediannumber)) images in $Loopcount steps."
note "stack: Location: $Stackshotdir"
dropvar stackshot
return 0
}
#### gphoto2 camera control
gphoto2_wrap() {
local Cameraport
Cameraport="$(getvar cameraport)"
[ "$Cameraport" ] && Cameraport="--port=$Cameraport"
note "gphoto2 $Cameraport $@"
gphoto2 $Cameraport $@ || {
failure "gphoto2 failed. Is the camera plugged in and turned on?"
return 1
}
return 0
}
gphoto2_cmd () {
## Funktion: Sammlung und Koordination der gphoto2-Aufrufe
#
local Command Return=0 Workdir
Command="${1:-}"
shift
case $Command in
check)
gphoto2_wrap --list-config >/dev/null 2>&1
Return=$?
;;
set-config-value|scv) # Kameraeinstellung setzen (Wert)
gphoto2_wrap --set-config-value $@
Return=$?
;;
set-config-index|sci) # Kameraeinstellung setzen (Indexnummer)
gphoto2_wrap --set-config-index $@
Return=$?
;;
capture-image-and-download) # Bild aufnehmen und herunterladen
Workdir="$(pwd)/captures"
mkdir -p "$Workdir"
# %C als Suffix läßt gphoto2 Dateiende selbst richtig einsetzen (jpg oder cr2 oder ...)
Latestimage="$Workdir/capture_$(get_free_filenumber $Workdir/capture).%C"
gphoto2_wrap --capture-image-and-download --filename $Latestimage
Return=$?
# Dateiname mit tatsächlichem Suffix ermitteln.
Latestimage="$(get_latest_file "$Workdir/capture*.jpg")"
[ "$Latestimage" ] || Latestimage="$(get_latest_file "$Workdir/capture*")"
setvar "latestimage=$Latestimage"
;;
capture-preview) # Einzelnes Videobild aufnehmen, ohne daß der Spiegel zuklappt
## Funktioniert ab gphoto2 Version >= 2.5.9 ##
Workdir="$(pwd)/captures"
mkdir -p "$Workdir"
gphoto2_cmd mirrorup
gphoto2_wrap --capture-preview
Return=$?
[ "$Return" = "0" ] && {
Latestimage="$Workdir/preview_$(get_free_filenumber $Workdir/preview).jpg"
mv capture_preview.jpg "$Latestimage"
setvar "latestimage=$Latestimage"
}
;;
mirrorup) # Viewfinder einschalten = Spiegel hoch
gphoto2_wrap --set-config-value viewfinder="1"
Return=$?
;;
zoom) # Viewfinder-Zoom setzen. Zulässige Werte sind 1, 5, und 10
gphoto2_wrap --set-config-value eoszoom=$1
Return=$?
;;
move-zoom) # Position von Viewfinder-Zoom ändern
case $1 in
x) ZOOM_XPOS="$[$ZOOM_XPOS+$2]" ;;
y) ZOOM_YPOS="$[$ZOOM_YPOS+$2]" ;;
esac
[ "$ZOOM_XPOS" -lt "0" ] && ZOOM_XPOS="0"
[ "$ZOOM_YPOS" -lt "0" ] && ZOOM_YPOS="0"
gphoto2_wrap --set-config-value eoszoomposition="$ZOOM_XPOS,$ZOOM_YPOS"
Return=$?
;;
reset)
gphoto2_wrap --reset
Return=$?
;;
*)
failure "gphoto2_cmd(): invalid command: $Command $@"
Return=1
;;
esac
return $Return
}
video() {
## Funktion: Videoaufnahme starten oder stoppen, mit Speichern auf Festplatte oder ohne
# $1 Aufnahmemodus
local Videodauer Videomode Pidffplay
local GPHOTO2 FFMPEG FFPLAY FFPLAY_LOCATION
local Windowid Windowgeometry Ffplay_x Ffplay_y
local Workdir
Pidffplay="$(getvar pidffplay)"
[ "$Pidffplay" ] && ps -p "$Pidffplay" >/dev/null 2>&1 && {
# store window position
Windowid=$(xdotool search --pid "$Pidffplay" | tail -n1)
Windowgeometry="$(xdotool getwindowgeometry --shell $Windowid)"
grep -q "X=" <<< "$Windowgeometry" && {
Ffplay_x="$(grep "^X=" <<< "$Windowgeometry" | cut -d= -f2)"
Ffplay_y="$(grep "^Y=" <<< "$Windowgeometry" | cut -d= -f2)"
setvar "ffplay_x=$Ffplay_x"
setvar "ffplay_y=$Ffplay_y"
}
# kill ffplay
kill $Pidffplay
dropvar pidffplay
Pidffplay=""
}
# Restore ffplay position
testvar ffplay_x && FFPLAY_LOCATION="-left $(getvar ffplay_x) -top $(getvar ffplay_y)"
Videodauer="3600s"
Videomode="${1:-preview}"
GPHOTO2="gphoto2 --capture-movie=$Videodauer --stdout"
FFMPEG="ffmpeg -loglevel error -i - -c:v copy -f mpjpeg - "
FFPLAY="ffplay -loglevel error -sync ext -window_title stackshooter-Video $FFPLAY_LOCATION - "
case $Videomode in
preview)
$GPHOTO2 | $FFPLAY & Pidffplay=$!
;;
capture)
Workdir="$(pwd)/video"
mkdir -p "$Workdir"
# Dateiname für neues Video
Latestvideo="$Workdir/video_$(get_free_filenumber "$Workdir/video").mjpg"
$GPHOTO2 | $FFMPEG | tee $Latestvideo | $FFPLAY & Pidffplay=$!
;;
stop)
# Videowiedergabe/aufnahme stoppen.
# Da das bereits am Anfang dieser Funktion geschieht,
# muß weiter nichts getan werden
;;
esac
[ "$Pidffplay" ] && ps -p "$Pidffplay" >/dev/null 2>&1 && setvar "pidffplay=$Pidffplay" && storepid $Pidffplay
}
cameralist() {
local Camera Cameralist Cameralistkaptain
Camera="$(getvar camera)"
Cameralist="$(gphoto2 --auto-detect | tail -n+3)"
while read Line; do
Cameralist="$Cameralist
$(echo "$Line" | xargs)"
done <<< "$Cameralist"
Cameralist="$(tail -n+2 <<< "$Cameralist")"
[ -z "$Cameralist" ] && failure "No camera detected."
grep -q "$Camera" <<< "$Cameralist" || Camera=""
[ -z "$Camera" ] && Camera="$(head -n1 <<< "$Cameralist")"
[ -z "$Camera" ] && Camera="autodetect"
Cameraport="$(rev <<< "$Camera" | cut -d' ' -f1 | rev)"
Cameralistkaptain="'autodetect'"
while read Line; do
Cameralistkaptain="$Cameralistkaptain,'$Line'"
done <<< "$Cameralist"
sendkaptain "camera($Cameralistkaptain)='$Camera'"
case $Camera in
autodetect)
dropvar camera
dropvar cameraport
;;
*)
setvar "camera=$Camera"
setvar "cameraport=$Cameraport"
;;
esac
}
camerainit() {
local Camera Cameraport
local Configlist Kaptainlist Setting Value
Camera="${1:-}"
[ -z "$Camera" ] && Camera="$(getvar camera)"
Cameraport="$(rev <<< "$Camera" | cut -d' ' -f1 | rev)"
setvar "camera=$Camera"
setvar "cameraport=$Cameraport"
gphoto2_cmd reset
gphoto2_cmd check || {
return 1
}
gphoto2_cmd set-config-index autoexposuremode=3 # mode "Manuell"
gphoto2_cmd set-config-index imageformat=0 # Large fine JPG. 6=Large fine JPG + RAW
for Setting in iso shutterspeed; do
note "Checking camera config options for $Setting"
Configlist="$(gphoto2_wrap --get-config $Setting | grep Choice | awk '{print $3}')"
Kaptainlist=""
while read Line; do
Kaptainlist="$Kaptainlist,'$Line'"
done <<< "$Configlist"
Kaptainlist="${Kaptainlist#,}"
Value="$(getvar $Setting)"
[ "$Value" ] && grep -q -x "$Value" <<< "$Configlist" || {
grep -q -x "1/80" <<< "$Configlist" && Value="1/80"
grep -q -x "800" <<< "$Configlist" && Value="800"
[ -z "$Value" ] && Value="$(head -n1 <<< "$Configlist")"
}
setvar "$Setting=$Value"
gphoto2_cmd set-config-value $Setting=$Value
sendkaptain "$Setting($Kaptainlist)='$Value'"
done
}
#### Keyboard control
keyboard_stepin() {
while :; do
[ -e "$Keyboardstepinfile" ] && stepcmd stepin || sleep 0.2
done
}
keyboard_stepout() {
while :; do
[ -e "$Keyboardstepoutfile" ] && stepcmd stepout || sleep 0.2
done
}
keyboard_watch() {
local Keyhub Keycode
while read Line <$Keyboardfifo ; do
Keyhub=$(awk '{print $2}' <<< "$Line")
Keycode=$(awk '{print $3}' <<< "$Line")
#note "Keycode: $Keycode ; Keyhub: $Keyhub"
[ -e "$Keyboardwatchfile" ] && case $Keyhub in
press)
case $Keycode in
# Durchgehender Lauf von Stepin/Stepout
133) touch "$Keyboardstepinfile" ;; # win
64) touch "$Keyboardstepoutfile" ;; # L-alt
esac
;;
release)
case $Keycode in
133) rm -f "$Keyboardstepinfile" ;; # win
64) rm -f "$Keyboardstepoutfile" ;; # L-alt
esac
;;
esac
done
}
#### Variable storage in parallel subshell
dropvar() {
echo "unset ${1:-}" >$Storevarinfifo
read Dummy <$Storevaroutfifo
echo "export ${1:-}" >$Storevarinfifo
read Dummy <$Storevaroutfifo
}
dumpvar() {
local Line Var
echo "env" >$Storevarinfifo
while read -t1 Line; do
Var="$(cut -d= -f1 <<< "$Line")"
[ "$Var" = "${Var^^}" ] || echo "$Line"
done <$Storevaroutfifo
}
getvar() {
local Result
echo "echo \$${1:-}" >$Storevarinfifo
read -t1 Result <$Storevaroutfifo || failure "getvar(): No response for ${1:-}"
#milisleep 10
echo "$Result"
}
setvar() {
echo "export '${1:-}'" >$Storevarinfifo
read Dummy <$Storevaroutfifo
}
storevar() {
local Line
#set -x
while :; do
read Line <$Storevarinfifo
eval "$Line" >$Storevaroutfifo
done
failure "storevar(): terminated"
}
testvar() {
local Value
Value="$(getvar "${1:-}")"
test -n "$Value"
}
#### GUI
gui_main() {
local Kaptainpid Kaptaingrammar Kaptainstdinfile Kaptainstdoutfile Kaptainanswerfile Kaptainpipefile Kaptainsignal
Kaptaingrammar="$Maincachedir/gui_main.grammar"
Kaptainstdinfile="$Maincachedir/gui_main.stdin"
Kaptainstdoutfile="$Maincachedir/gui_main.stdout"
Kaptainanswerfile="$Maincachedir/gui_main.reply"
Kaptainpipefile="$Maincachedir/gui_main.pipe.sh"
:> "$Kaptainstdinfile"
:> "$Kaptainstdoutfile"
gui_main_grammar > $Kaptaingrammar
#nl -ba $Kaptaingrammar
pipekaptain
tail -f "$Kaptainstdoutfile" >> $Kaptainanswerfile & storepid $!
kaptain --pipe $Kaptainpipefile $Kaptaingrammar & Kaptainpid=$!
setvar "pidkaptain=$Kaptainpid"
storepid $Kaptainpid
gui_main_parser "camera:autodetect"
gui_main_parser "devicecombo:autodetect"
position_store
while ps -p $Kaptainpid >/dev/null; do
read -t1 Kaptainsignal
[ "$Kaptainsignal" ] && {
note "Signal from kaptain: $Kaptainsignal"
Kaptainsignal="${Kaptainsignal%:pressed}"
}
[ "$Kaptainsignal" ] && gui_main_parser "$Kaptainsignal"
done < <(tail -f "$Kaptainstdoutfile")
}
gui_main_grammar() {
echo "#! /usr/bin/kaptain
start 'stackshooter' -> mainframe;
mainframe:framed -> naviframe cameraframe stackshotframe statusframe;
naviframe 'Device' -> device stepline pos0line pos1line pos2line positionline poscalcline keyboardline;
device:beside 'Device file' -> devicecombo devicebutton ;
devicecombo -> @combo('autodetect')='autodetect' ;
devicebutton -> @button=' initialize' ;
stepline:beside -> stepin stepout stepwidth stepmy;
stepin -> @button='Step in';
stepout -> @button='Step out';
stepwidth:beside 'Step width:' -> @combo('1','2','5','10','15','20','25','30','40','50','60','75','100','150','200','250','300','400','500','750','1000','1500','2000','3000','5000','7500','10000')='50' ;
stepmy -> @text='' ;
pos0line:beside -> setpos0 gopos0 ;
setpos0 -> @button='Set current position as position zero' ;
gopos0 -> @button='Go to pos0';
pos1line:beside -> setpos1 gopos1;
setpos1 -> @button='Set current position as start point of stack' ;
gopos1 -> @button='Go to pos1' ;
pos2line:beside -> setpos2 gopos2 ;
setpos2 -> @button='Set current position as end point of stack' ;
gopos2 -> @button='Go to pos2' ;
positionline:beside -> poscurrent pos1 pos2;
poscurrent:beside 'Current:' -> @text='' ;
pos1:beside 'Pos1:' -> @text='' ;
pos2:beside 'Pos2:' -> @text='' ;
poscalcline:beside -> difference distance steps;
difference:beside 'Difference:' -> @text='' ;
distance:beside 'Distance:' -> @text='' ;
steps:beside 'Steps:' -> @text='' ;
keyboardline:beside 'Watch Keyboard (Super=stepin, L-Alt=stepout)' -> "" | "" ;
cameraframe 'Camera' -> cameraline settingline captureline videoline;
settingline:beside -> zoom iso shutterspeed ;
zoom:beside 'Zoom:' -> @combo('1','10') ;
iso:beside 'ISO:' -> @combo('____') ;
shutterspeed:beside 'Shutterspeed:' -> @combo('________') ;
cameraline:beside -> camera ;
camera:beside -> @combo('autodetect')='autodetect' ;
captureline:beside -> capturepreview capturefullres ;
capturepreview -> @button='Capture preview' ;
capturefullres -> @button='Capture full resolution' ;
videoline:beside -> videostart videocapture videostop;
videostart -> @button='Start video' ;
videocapture -> @button='Capture video' ;
videostop -> @button='Stop video' ;
stackshotframe 'Stackshot' -> stackshotdelayline stackshotmedian stackshotline;
stackshotline:beside -> stackshotpreview stackshotfullres ;
stackshotpreview -> @button='Run preview stackshot' ;
stackshotfullres -> @button='Run full resolution stackshot' ;
stackshotmedian:beside 'Median captures to delete moved objects:' -> @combo('3','4','5','6','7','8','9','10')='5' | ! "";
stackshotdelayline:beside -> stackshotdelay startdelay;
stackshotdelay:beside 'Delay between captures (ms):' -> @combo('100','200','300','400','500','750','1000','1500','2000','2500','3000','4000','5000')='500';
startdelay:beside 'Delay before start:' -> @combo('1','10','30','60','300','600')='1';
statusframe 'Status' -> statusline ;
statusline:beside -> cmdpercent status config;
cmdpercent -> @icon ;
status -> @text ;
config -> @button='config' ;
"
}
gui_main_parser() {
local Kaptainsignal Argument
local Projecthome Projectname
local Device
Kaptainsignal="${1:-}"
Argument="$(cut -s -d: -f2 <<< "$Kaptainsignal")"
position_read
statusmessage "$Kaptainsignal"
showpercent busy
dropvar cmdbreak
# Provide project dir if needed, check settings in GUI
case $Kaptainsignal in
*capture*|stackshot*)
testvar projectname || setup_projectdir
Projecthome="$(getvar storagebasedir)"
Projectname="$(getvar projectname)"
cd "$Projecthome/$Projectname"
;;
esac
# Stop video if gphoto2 access
case "$Kaptainsignal" in # Laufende Videoaufnahme bei gphoto2 Aufruf unterbrechen, ggf. nach Kommando wieder starten
iso*|shutter*|capture*|video*|zoom*|stackshot*)
video stop
;;
esac
case $Kaptainsignal in
devicecombo*)
Device="$(cut -d: -f2 <<< "$Kaptainsignal")"
case $Device in
autodetect)
Devicecombo=""
Devicebasename="$(getvar devicebasename)"
Devicelist="$(find ${Devicebasename}* 2>/dev/null)"
for Device in $Devicelist; do
Devicecombo="$Devicecombo,'$Device'"
done
Devicecombo="'autodetect'$Devicecombo,'/dev/null'"
Device="$(head -n1 <<< "$Devicelist")"
Device="${Device:-autodetect}"
sendkaptain "devicecombo($Devicecombo)='$Device'"
;;
esac
setvar "device=$Device"
;;
devicebutton*)
Device="$(getvar device)"
case $Device in
autodetect)
gui_main_parser "devicecombo:autodetect"
Device="$(getvar device)"
;;
esac
case $Device in
autodetect)
Device="/dev/null"
setvar "device=$Device"
;;
esac
device_init
sendkaptain "devicecombo($Devicecombo)='$Device'"
stepcmd setpos0
stepcmd stepwidth $(getvar stepwidth)
;;
camera:autodetect|camera:)
cameralist
camerainit
;;
camera:*)
camerainit "$Argument"
;;
zoom*)
gphoto2_cmd zoom $Argument
setvar "zoom=$Argument"
;;
iso*)
gphoto2_cmd set-config-value iso="$Argument"
setvar "iso=$Argument"
;;
shutterspeed*)
gphoto2_cmd set-config-value shutterspeed="$Argument"
setvar "shutterspeed=$Argument"
;;
capturepreview)
gphoto2_cmd capture-preview
showimage "$(getvar latestimage)"
;;
capturefullres)
gphoto2_cmd capture-image-and-download
showimage "$(getvar latestimage)"
;;
videostart)
setvar "videomode=preview"
;;
videocapture)
setvar "videomode=capture"
;;
videostop)
dropvar videomode
;;
setpos*|gopos*|stepin|stepout)
stepcmd $Kaptainsignal
;;
stepwidth*)
stepcmd stepwidth $Argument
;;
keyboardline:on)
keyboard_stepin & setvar "pidkeyboardstepin=$!" && storepid $(getvar pidkeyboardstepin)
keyboard_stepout & setvar "pidkeyboardstepout=$!" && storepid $(getvar pidkeyboardstepout)
touch "$Keyboardwatchfile"
;;
keyboardline:off)
testvar pidkeyboardstepin && kill $(getvar pidkeyboardstepin)
testvar pidkeyboardstepout && kill $(getvar pidkeyboardstepout)
rm "$Keyboardwatchfile"
;;
stackshotpreview|stackshotfullres)
kill -s STOP $(getvar pidkeyboardwatch)
setup_projectdir
Argument="$(askkaptain stackshotmedian)"
Argument="${Argument:-1}"
setvar "mediannumber=$Argument"
Argument="$(askkaptain stackshotdelay)"
Argument="${Argument:-500}"
setvar "stackshotdelay=$Argument"
Argument="$(askkaptain startdelay)"
setvar "startdelay=$Argument"
Argument="${Argument:-100}"
gui_stackshot $Kaptainsignal
kill -s CONT $(getvar pidkeyboardwatch)
;;
config)
gui_config
read_configfile "$Configfile"
position_read
position_store
;;
esac
# restart video
testvar videomode && {
ps -p "$(getvar pidffplay)" >/dev/null 2>&1 || video "$(getvar videomode)"
}
testvar error || {
statusmessage ""
showpercent idle
}
dropvar error
}
gui_config() {
local Kaptainpid Kaptaingrammar Kaptainstdinfile Kaptainstdoutfile Kaptainanswerfile Kaptainpipefile Kaptainsignal
local Setting
Kaptaingrammar="$Maincachedir/gui_config.grammar"
Kaptainstdinfile="$Maincachedir/gui_config.stdin"
Kaptainstdoutfile="$Maincachedir/gui_config.stdout"