diff --git a/exec-cycle.sh b/exec-cycle.sh index 94b68b5..b9e8f40 100755 --- a/exec-cycle.sh +++ b/exec-cycle.sh @@ -2,22 +2,50 @@ trap 'stty sane; clear; exit 0' SIGINT SIGTERM +dir="$(dirname "$0")" +[ -f "$dir/webcam.conf" ] && source "$dir/webcam.conf" + +BRIGHTNESS="${DEFAULT_BRIGHTNESS:-auto}" +EXPOSURE="${DEFAULT_EXPOSURE:-auto}" +STEP="${STEP:-5}" + +# clamps $1+$2 to 0-100, treating "auto" as a 50 starting point +adjust() { + local cur=$1 delta=$2 val + [ "$cur" = "auto" ] && cur=50 + val=$((cur + delta)) + (( val < 0 )) && val=0 + (( val > 100 )) && val=100 + echo "$val" +} + while true; do clear + export BRIGHTNESS EXPOSURE ./webcam2ascii.sh echo - echo "[c] capture [Ctrl+C] quit" + echo "brightness: ${BRIGHTNESS} exposure: ${EXPOSURE}" + echo "[+/-] brightness [./,] exposure [r] reset to auto [c] capture [Ctrl+C] quit" - if read -n 1 -t 0.05 -s key && [[ "$key" == "c" || "$key" == "C" ]]; then - timestamp=$(date +%Y%m%d-%H%M%S) - cp web-cam-shot.jpg "photo-$timestamp.jpg" - cp ascii-img.txt "ascii-img-$timestamp.txt" - clear - cat "ascii-img-$timestamp.txt" - echo - echo "Saved photo-$timestamp.jpg and ascii-img-$timestamp.txt" - ./txt2printr.sh "ascii-img-$timestamp.txt" - read -n 1 -s -p "Press any key to resume live preview, Ctrl+C to quit..." + if read -n 1 -t 0.05 -s key; then + case "$key" in + "+") BRIGHTNESS=$(adjust "$BRIGHTNESS" "$STEP") ;; + "-") BRIGHTNESS=$(adjust "$BRIGHTNESS" "-$STEP") ;; + ".") EXPOSURE=$(adjust "$EXPOSURE" "$STEP") ;; + ",") EXPOSURE=$(adjust "$EXPOSURE" "-$STEP") ;; + r|R) BRIGHTNESS="${DEFAULT_BRIGHTNESS:-auto}"; EXPOSURE="${DEFAULT_EXPOSURE:-auto}" ;; + c|C) + timestamp=$(date +%Y%m%d-%H%M%S) + cp web-cam-shot.jpg "photo-$timestamp.jpg" + cp ascii-img.txt "ascii-img-$timestamp.txt" + clear + cat "ascii-img-$timestamp.txt" + echo + echo "Saved photo-$timestamp.jpg and ascii-img-$timestamp.txt" + ./txt2printr.sh "ascii-img-$timestamp.txt" + read -n 1 -s -p "Press any key to resume live preview, Ctrl+C to quit..." + ;; + esac fi done diff --git a/webcam.conf b/webcam.conf new file mode 100644 index 0000000..d74c3fa --- /dev/null +++ b/webcam.conf @@ -0,0 +1,19 @@ +# ASCII Photobooth webcam configuration +# Sourced by webcam2ascii.sh and exec-cycle.sh. + +# Capture settings +RESOLUTION=640x480 +JPEG_QUALITY=85 + +# Starting brightness/exposure (0-100, or "auto" for the camera default) +# exec-cycle.sh's live +/- and ./, controls adjust away from these at runtime +DEFAULT_BRIGHTNESS=auto +DEFAULT_EXPOSURE=auto + +# Step size (percentage points) for each live-preview +/- adjustment +STEP=5 + +# v4l2 control names as reported by: fswebcam --list-controls +# These vary per camera/driver - edit to match your hardware +BRIGHTNESS_CONTROL="Brightness" +EXPOSURE_CONTROL="Exposure (Absolute)" diff --git a/webcam2ascii.sh b/webcam2ascii.sh index 9d11300..74ec9ef 100755 --- a/webcam2ascii.sh +++ b/webcam2ascii.sh @@ -1,5 +1,23 @@ #/bin/bash +# +# Captures a frame and converts it to ascii art. +# Reads defaults from webcam.conf. BRIGHTNESS/EXPOSURE env vars (set by +# exec-cycle.sh's live preview controls) override the config's defaults. -fswebcam -q --no-banner -r 640x480 --jpeg 85 web-cam-shot.jpg &> /dev/null +dir="$(dirname "$0")" +[ -f "$dir/webcam.conf" ] && source "$dir/webcam.conf" + +: "${RESOLUTION:=640x480}" +: "${JPEG_QUALITY:=85}" +: "${BRIGHTNESS:=${DEFAULT_BRIGHTNESS:-auto}}" +: "${EXPOSURE:=${DEFAULT_EXPOSURE:-auto}}" +: "${BRIGHTNESS_CONTROL:=Brightness}" +: "${EXPOSURE_CONTROL:=Exposure (Absolute)}" + +set_opts=() +[ "$BRIGHTNESS" != "auto" ] && set_opts+=(--set "${BRIGHTNESS_CONTROL}=${BRIGHTNESS}%") +[ "$EXPOSURE" != "auto" ] && set_opts+=(--set "${EXPOSURE_CONTROL}=${EXPOSURE}%") + +fswebcam -q --no-banner -r "$RESOLUTION" --jpeg "$JPEG_QUALITY" "${set_opts[@]}" web-cam-shot.jpg &> /dev/null ascii-image-converter web-cam-shot.jpg | tee ascii-img.txt