From f3f37a9166d4170099e1b1100e6e79b9557b8d4b Mon Sep 17 00:00:00 2001 From: Tim Schilling | IuK Date: Thu, 5 Sep 2024 17:13:03 +0200 Subject: [PATCH] Fixing LFS --- .gitattributes | 1 + DocForWiki.md | 41 +++++++++++- README.md | 42 +++++++++++- reload.sh | 23 ++++++- setup-old.sh | 173 +++++++++++++++++++++++++++++++++++++++++++++++- setup.sh | 176 ++++++++++++++++++++++++++++++++++++++++++++++++- start.sh | 22 ++++++- stop.sh | 23 ++++++- video.py | 45 ++++++++++++- 9 files changed, 522 insertions(+), 24 deletions(-) diff --git a/.gitattributes b/.gitattributes index 7430b7d..9f654dd 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1,2 +1,3 @@ *.mp4 filter=lfs diff=lfs merge=lfs -text *.webm filter=lfs diff=lfs merge=lfs -text +demo* !filter !diff !merge -text \ No newline at end of file diff --git a/DocForWiki.md b/DocForWiki.md index ec00fcf..64f1faa 100644 --- a/DocForWiki.md +++ b/DocForWiki.md @@ -1,3 +1,38 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:3b7749a0473ffd825a98affb554c4b6681db7b68111dd0798267c8e1514c2122 -size 2192 +# Überblick +Diese Codebase wured 08/09 2024 von Tim Schilling entwickelt um ein alltes gerät im Lechfeldmuseum der Stadt Königsbrunn zu ersetzten. Das Script ist für die Verwendung auf einen Raspberry Pi gedacht. +Dieses Script verwendet für das Spielen von Videos VLC bzw die Commandline variante '''cvlc''', das Scipt weches für das Automatische Abspeielen der Videos sowie der wechsel zwischen den zwei Videos ist in Pyhton geschrieben. + +## Abhängigkeiten +- vlc -> Video Lan Client, anwendung zum Abspielen von Videos +- python3-rpi.gpio -> Python Bibliothek welche für die Verwendung der GPIO gebraucht wird. + +## Skripte +Zusätzlich wurden für die Leichte Installation des Systems sowie für die Kontrolle im betrieb einige Shellskipts angelegt welche folgende Aufgaben haben: + +- setup.sh -> Setup Script welches mehrere aufgaben erfüllt, dies führt (in Reienfolge der Ausführtung) + 1. Fragt den Nutzer ob für diesen der Autostart dienst Verwendet werden soll. + 2. Fragt den Nutzer ob die GPIO Shutdown funktion des Raspberry Pi verwendet werden soll. + 3. Führt ein Systemupdate über den APT aus + 4. Installiert die Abhängigkeiten + 5. Herunterladen der Aktuellen Scripte aus dem GitHub Repo + 6. Herunterladen der Videos aus dem GitHub Repo (**ACHTUNG: Wenn sich die Datein im Repo Geänder haben müssen die Datein im Skript entsprechend Angepasst werden.**) + 7. Festlegen von SystemD dienst Datein Inhalt + 8. Erstellen der System D Service Datein und Einschalten dieser je nach vorheriger Nutzerauswahl + 9. Einfügen der zeile in /boot/config.txt für GPIO Shutdown + +- video.py -> Script welches die Videos Abspielt +- start.sh -> Skript zum starten der Wiedergabe, schaltet gleichzeitig den Autostart der Anwendung über einen SystemD dienst **EIN** +- stop.sh -> Skript zum stoppen der Wiedergabe, schaltet gleichzeitig den Autostart der Anwendung über einen SystemD dienst **AUS** +- reload.sh -> Skript zum Neustarten der Wiedergabe, stop diese über System D und startet diese wieder + +## Dateistrucktur +``` +- PiVideo/ + |- videos/ + | |- loop.mp4 + | |- trigger.mp4 + |- video.py + |- start.sh + |- stop.sh + |- reload.sh +``` diff --git a/README.md b/README.md index 1cdb32c..ef38e1a 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,39 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:7bcd6d1ffdddbcc316a45ba93e7f1e93eccbd6c67439e9e9085723014614aa57 -size 1536 +# PiVideo + +## TO-DO +-~~ Add GPIO Shutdown and Start option to Script [see](https://raspberrypi.stackexchange.com/questions/117013/raspberry-pi-4-b-gpio-boot-and-shutdown-buttons) ~~ +- Remove old files when reinstalling + +## Overview +This is a collection of scripts which run a video in a loop until a Button connected to GPIO is Pressed. +Then another video is played once. + +## Installation +For installation download and run setup.sh. This will install the nesecary dependencys, create the needed folder structure and download the Script. +It will also create a SystemD service to automaticly start the program when the Pi is booted. + +**CAUTION: IF THE VIDEO FILES IN THE REPO HAVE CHANGED PLEASE CHECK THE SCRIPT IF THEY ARE CORRECT!** + +```bash +wget -L "https://raw.githubusercontent.com/miT-nib-hcI/PiVideo/main/setup.sh" && +chmod 755 setup.sh && +./setup.sh +``` + +## Configuration +Since it is a realativly simple Python script you can modify as needed. +The videos which are played are in the videos folder, as the nameing of the files suggests the loop.mp4 is played on loop and the trigger.mp4 is played once when the GPIO button has been pressed. +You can replace the files as needed with your own files. The files included right now are just for demonstration. + +``` +- PiVideo/ + |- videos/ + | |- loop.mp4 + | |- trigger.mp4 + |- video.py + |- start.sh + |- stop.sh + |- reload.sh +``` + +The button to trigger the video switch is by default conected to GPIO 17 and GND, you can change this in the Scipt itself if needed. diff --git a/reload.sh b/reload.sh index 0fc6be3..07687d0 100755 --- a/reload.sh +++ b/reload.sh @@ -1,3 +1,20 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:3050f26489db40d2f46197d0deab69a52429c9ee7547160798e3acda471556bc -size 504 +#! /bin/bash + +## A simple whitch stops the current service and Starts it again +## used to run the current version of the script after modification + +# Definition of colors for the echo outputs +GREEN='\033[0;32m' +NOCOLOR='\033[0m' + +# Servicename which this script affects +SERVICE_NAME="pivideo.service" + +echo -e "${GREEN}======== Stoppe Dienst =========${NOCOLOR}" +sudo systemctl stop $SERVICE_NAME + + +echo -e "${GREEN}======== Starte Dienst =========${NOCOLOR}" +sudo systemctl start $SERVICE_NAME + +exit 0 diff --git a/setup-old.sh b/setup-old.sh index 46905db..f367ce9 100644 --- a/setup-old.sh +++ b/setup-old.sh @@ -1,3 +1,170 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:bf354256f48bc2131f3d622b77d14be3f0ba7fd49858ca55c1843ea26b2f95cc -size 4369 +#! /bin/bash + +# Definition of colors for text output +GREEN='\033[0;32m' +CYAN_BACK='\x1b[46m' + +NOCOLOR='\033[0m' + +echo -e "${GREEN}======== Starte Installation =========${NOCOLOR}" + +read -p "Do you want to add a SystemD Service for Autostart [y|n]" INPUT +SYSD=1 + +case $INPUT in + y|Y) SYSD=0 ;; + n|N) SYSD=1 ;; + *) echo "SystemD Service will not be Added...." ;; +esac +INPUT="" + +read -p "Do you want to add GPIO Power Controle [y|n]" INPUT +GPPower=1 + +case $INPUT in + y|Y) GPPower=0 ;; + n|N) GPPower=1 ;; + *) echo "GPIO Power controll will not be Addaded.........." ;; +esac + + +CURRENT_USER=$(whoami) +SERVICE_NAME="pivideo.service" +AUTOLOGIN_SERVICE_NAME="autologin@tty1.service" + +PYTHON_SCRIPT="/home/$CURRENT_USER/PiVideo/video.py" + + +echo -e "${GREEN}======== Führe Update durch =========${NOCOLOR}" + +sudo apt update && sudo apt upgrade -y + +echo -e "${GREEN}======== Installiere Abhängigkeiten =========${NOCOLOR}" +sudo apt install vlc python3-rpi.gpio git -y + + +echo -e "${GREEN}======== Holle Datein ========${NOCOLOR}" + +if ls ~/PiVideo/ 1> /dev/null 2>&1; # Checking if file exsists +then + echo "Folders Already Exist Scipping" +else + echo "Creating Folders" + mkdir ~/PiVideo/ ~/PiVideo/videos/ + cd ~/PiVideo/ +fi + +GH_URL="https://raw.githubusercontent.com/miT-nib-hcI/PiVideo/main/" + +if ls ~/PiVideo/video.py 1> /dev/null 2>&1; # Checking if file exsists +then + echo "Script exists, updateing ..." + rm ~/PiVideo/video.py # Removing Old File + + wget -L $GH_URL"video.py" + + rm ~/PiVideo/*.sh # Removing Old Files + wget -L $GH_URL"start.sh" + wget -L $GH_URL"stop.sh" + wget -L $GH_URL"reload.sh" +else + wget -L $GH_URL"video.py" + wget -L $GH_URL"start.sh" + wget -L $GH_URL"stop.sh" + wget -L $GH_URL"reload.sh" +fi + + + +chmod 755 *.sh + +echo -e "${GREEN}======== Holle Videos========${NOCOLOR}" + +cd videos/ + +if ls ~/PiVideo/videos/loop* 1> /dev/null 2>&1; # Checking if file exsists +then + echo "Video exists, updateing ..." + rm ~/PiVideo/videos/loop* # Removing Old Files + + wget -L $GH_URL"videos/loop.mp4" +else + wget -L $GH_URL"videos/loop.mp4" +fi + + + +if ls ~/PiVideo/videos/trigger* 1> /dev/null 2>&1; # Checking if file exsists +then + echo "Video exists, updateing ..." + rm ~/PiVideo/videos/trigger* # Removing Old Files + + wget -L $GH_URL"videos/trigger.webm" +else + wget -L $GH_URL"videos/trigger.webm" +fi +cd .. + +echo -e "${GREEN}======== Erestele Systemd Service ========${NOCOLOR}" + +AUTOLOGIN_SERVICE_CONTENT="[Service] +ExecStart= +ExecStart=-/sbin/agetty --autologin $CURRENT_USER --noclear tty1 linux +" + +# Erstelle die Autologin-Service-Datei +echo "Erstelle die Autologin-Service-Datei unter /etc/systemd/system/getty@tty1.service.d/override.conf..." +sudo mkdir -p /etc/systemd/system/getty@tty1.service.d/ + +SERVICE_CONTENT="[Unit] +Description=Video Control Script +After=network.target + +[Service] +ExecStart=/usr/bin/python3 $PYTHON_SCRIPT +WorkingDirectory=$(dirname $PYTHON_SCRIPT) +StandardOutput=inherit +StandardError=inherit +Restart=always +User=$CURRENT_USER + +[Install] +WantedBy=multi-user.target +" + + +echo "$AUTOLOGIN_SERVICE_CONTENT" | sudo tee /etc/systemd/system/getty@tty1.service.d/override.conf > /dev/null + +# Erstelle der PiVideo Service-Datei +echo "$SERVICE_CONTENT" | sudo tee /etc/systemd/system/$SERVICE_NAME > /dev/null + +# Lade die Systemd-Dienste neu +echo -e "${GREEN}======== Lade Dienste neu ========${NOCOLOR}" +sudo systemctl daemon-reload + +if [ $SYSD -eq 0 ] +then + # Aktiviere den Service, damit er beim Booten gestartet wird + echo -e "${GREEN}======== Aktiviern des Dienstes ========${NOCOLOR}" + + echo "Aktivire den PiVideo-Service..." + sudo systemctl enable $SERVICE_NAME + + # Starten des Services + echo -e "${GREEN}======== Starente des Dienstes ========${NOCOLOR}" + sudo systemctl start $SERVICE_NAME +else + echo -e "${CYAN_BACK} Automatic start of Playback disabled ${NOCOLOR}" + echo -e "${CYAN_BACK} Did not start Service, to start use \"sudo systemctl start pivideo.service\" ${NOCOLOR}" +fi + +if [ $GPPower -eq 0 ] +then + echo "dtoverlay=gpio-shutdown,gpio-pin=3" | sudo tee /boot/config.txt > /dev/null +fi + +echo -e "${GREEN}=========================================================${NOCOLOR}" +echo -e "${GREEN}======== Das Script wurde Erfolgreich Ausgeführt ========${NOCOLOR}" +echo -e "${GREEN}=========================================================${NOCOLOR}" + +exit 0 diff --git a/setup.sh b/setup.sh index 5eea5ba..7b9ec69 100755 --- a/setup.sh +++ b/setup.sh @@ -1,3 +1,173 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:29ac60f696d1a71b3df07f2c029aa9e082b3830879a7c4a837c6fd14fb796d47 -size 5274 +#!/bin/bash + +# Define colors for text output +GREEN='\033[0;32m' +CYAN_BACK='\x1b[46m' +RED='\033[0;31m' +NOCOLOR='\033[0m' + +# Function for error handling +function check_error { + if [ $? -ne 0 ]; then + echo -e "${RED}Error: $1${NOCOLOR}" + exit 1 + fi +} + + +# Check if the script is being run as root +if [ "$(id -u)" -ne 0 ]; then + echo -e "${RED}This script must be run as root (e.g., with sudo).${NOCOLOR}" + exit 1 +fi + +echo -e "${GREEN}======== Starting Installation =========${NOCOLOR}" + +# Prompt the user to add a systemd service for autostart +read -p "Do you want to add a systemd service for autostart? [y|n] " INPUT +SYSD=1 + +# Decide whether to set up systemd based on user input +case $INPUT in + y|Y) SYSD=0 ;; + n|N) SYSD=1 ;; + *) echo "Systemd service will not be added...." ;; +esac + +# Prompt the user to add GPIO power control +read -p "Do you want to add GPIO power control? [y|n] " INPUT +GPPower=1 + +# Decide whether to set up GPIO power control based on user input +case $INPUT in + y|Y) GPPower=0 ;; + n|N) GPPower=1 ;; + *) echo "GPIO power control will not be added.........." ;; +esac + +# Variables for paths and files +CURRENT_USER=$(logname) # Get the username of the person who invoked the script +SERVICE_NAME="pivideo.service" +PYTHON_SCRIPT="/home/$CURRENT_USER/PiVideo/video.py" +GH_URL="https://raw.githubusercontent.com/miT-nib-hcI/PiVideo/main/" + + +# Update and install dependencies +echo -e "${GREEN}======== Performing system update =========${NOCOLOR}" +sudo apt update && sudo apt upgrade -y +check_error "Failed to update packages." + + +echo -e "${GREEN}======== Installing dependencies =========${NOCOLOR}" +sudo apt install -y vlc python3-rpi.gpio git +check_error "Failed to install dependencies." + + +# Create directories and download files +echo -e "${GREEN}======== Downloading files ========${NOCOLOR}" + + +# Ensure directories exist and change to the correct directory +mkdir -p /home/$CURRENT_USER/PiVideo/videos/ +cd /home/$CURRENT_USER/PiVideo/ + + +# Function to download a file and check for errors +function download_file { + local file=$1 + wget -q "$GH_URL$file" + check_error "Failed to download $file" +} + +# Download necessary scripts +download_file "video.py" +download_file "start.sh" +download_file "stop.sh" +download_file "reload.sh" + +# Make the scripts executable +chmod 755 *.sh + +echo -e "${GREEN}======== Downloading videos ========${NOCOLOR}" + +cd videos/ + +# Download video files +download_file "videos/loop.mp4" +download_file "videos/trigger.webm" + +cd .. + +# Create the systemd service for autologin and video control +echo -e "${GREEN}======== Creating systemd service ========${NOCOLOR}" + +# Autologin service content +AUTOLOGIN_SERVICE_CONTENT="[Service] +ExecStart= +ExecStart=-/sbin/agetty --autologin $CURRENT_USER --noclear tty1 linux +" + +# Video control service content +SERVICE_CONTENT="[Unit] +Description=Video Control Script +After=network.target + +[Service] +ExecStart=/usr/bin/python3 $PYTHON_SCRIPT +WorkingDirectory=$(dirname $PYTHON_SCRIPT) +StandardOutput=inherit +StandardError=inherit +Restart=always +User=$CURRENT_USER + +[Install] +WantedBy=multi-user.target +" + +# If the user opted for autostart, create the service files +if [ $SYSD -eq 0 ]; then + # Creating Service file for Autologin of the Current User + echo "$AUTOLOGIN_SERVICE_CONTENT" | sudo tee /etc/systemd/system/getty@tty1.service.d/override.conf > /dev/null + check_error "Failed to create autologin service." + + # Craating Service file for Video Playback + echo "$SERVICE_CONTENT" | sudo tee /etc/systemd/system/$SERVICE_NAME > /dev/null + check_error "Failed to create PiVideo service." + + # Reload systemd and enable the service + echo -e "${GREEN}======== Reloading services ========${NOCOLOR}" + sudo systemctl daemon-reload + check_error "Failed to reload systemd services." + + echo -e "${GREEN}======== Enabling the service ========${NOCOLOR}" + sudo systemctl enable $SERVICE_NAME + check_error "Failed to enable PiVideo service." + + echo -e "${GREEN}======== Starting the service ========${NOCOLOR}" + sudo systemctl start $SERVICE_NAME + check_error "Failed to start PiVideo service." +else + # Creating Service file for Autologin of the Current User + echo "$AUTOLOGIN_SERVICE_CONTENT" | sudo tee /etc/systemd/system/getty@tty1.service.d/override.conf > /dev/null + check_error "Failed to create autologin service." + + # Creating Service file for Video Playback + echo "$SERVICE_CONTENT" | sudo tee /etc/systemd/system/$SERVICE_NAME > /dev/null + check_error "Failed to create PiVideo service." + + echo -e "${CYAN_BACK}Automatic start of playback disabled${NOCOLOR}" + echo -e "${CYAN_BACK}Did not start service, to start use \"sudo systemctl start $SERVICE_NAME\"${NOCOLOR}" +fi + +# Configure GPIO power control if the user opted in +if [ $GPPower -eq 0 ]; then + echo "dtoverlay=gpio-shutdown,gpio-pin=3" | sudo tee -a /boot/config.txt > /dev/null + check_error "Failed to configure GPIO power control." +fi + +echo -e "${GREEN}=========================================================${NOCOLOR}" +echo -e "${GREEN}======== The script was executed successfully ========${NOCOLOR}" +echo -e "${GREEN}=========================================================${NOCOLOR}" + +exit 0 + diff --git a/start.sh b/start.sh index 18cc284..05b59a3 100755 --- a/start.sh +++ b/start.sh @@ -1,3 +1,19 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:c1329a6096f844315d2c23d36a39f6eee6a08d2598da86777b345dc6b8ac294b -size 469 +#! /bin/bash + +## A simple script which starts the playing of the Video and enables the SystemD service + +# Definition of colors for the echo outputs +GREEN='\033[0;32m' +NOCOLOR='\033[0m' + +# Servicename which this script affects +SERVICE_NAME="pivideo.service" + +echo -e "${GREEN}======== STARTING SERVICE =========${NOCOLOR}" +sudo systemctl start $SERVICE_NAME + + +echo -e "${GREEN}======== ENABELING SERVICE =========${NOCOLOR}" +sudo systemctl enable $SERVICE_NAME + +exit 0 diff --git a/stop.sh b/stop.sh index 7f819a1..0ae0dec 100755 --- a/stop.sh +++ b/stop.sh @@ -1,3 +1,20 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:4145d41abc5e8197288acbd8e8e90959587bf52ccb61f63bcc4c7b38a03634bd -size 470 +#! /bin/bash + +## A simple script which stops the playing of the Video and disables the SystemD service + +# Definition of colors for the echo outputs +GREEN='\033[0;32m' +NOCOLOR='\033[0m' + +# Servicename which this script affects +SERVICE_NAME="pivideo.service" + + +echo -e "${GREEN}======== STOPING SERVICE =========${NOCOLOR}" +sudo systemctl stop $SERVICE_NAME + + +echo -e "${GREEN}======== DISABELING SERVICE =========${NOCOLOR}" +sudo systemctl disable $SERVICE_NAME + +exit 0 diff --git a/video.py b/video.py index 8407e8f..798fd58 100755 --- a/video.py +++ b/video.py @@ -1,3 +1,42 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:bf9f383eb5377f11d41c4be97bc3cb4174c1c52a67f7a15aa15553666e855e34 -size 1346 +#! /usr/bin/python3 + +import RPi.GPIO as GPIO +import subprocess +import time + +# GPIO-Setup +BUTTON_PIN = 17 # Ersetze dies durch den tatsächlichen GPIO-Pin, den du verwendest +GPIO.setmode(GPIO.BCM) +GPIO.setup(BUTTON_PIN, GPIO.IN, pull_up_down=GPIO.PUD_UP) + +loop_video = 'videos/loop.mp4' +trigger_video = 'videos/trigger.mp4' + +# Funktion zum Abspielen eines Videos +def play_video(video_file): + subprocess.run(['cvlc', '--play-and-exit', '--fullscreen', '--no-video-title-show', video_file]) + +# Endlosschleife Video 1 starten +loop_video = subprocess.Popen(['cvlc', '--loop', '--fullscreen', '--no-video-title-show', loop_video]) + +try: + while True: + # Überprüfen, ob der Button gedrückt wurde + button_state = GPIO.input(BUTTON_PIN) + if button_state == GPIO.LOW: # Button gedrückt + # Stoppe das loop_video + loop_video.terminate() + + # Video 2 abspielen + play_video(trigger_video) + + # Nach dem Abspielen von Video 2 wieder Video 1 in Schleife starten + loop_video = subprocess.Popen(['cvlc', '--loop', '--fullscreen', '--no-video-title-show', loop_video]) + + time.sleep(0.1) # Entprellen des Buttons + +except KeyboardInterrupt: + # Bei einem Tastaturabbruch das laufende Video stoppen und GPIO reinigen + loop_video.terminate() + GPIO.cleanup() + \ No newline at end of file