0

I am a newbie with Python and I am working on a project that I would have people press a button to play a video event. Video#1 will be the default normal video while video#2 will play and flash an LED from GPIO 13 (BCM).

Here is my dilemma: Button 1, Button 2, and button 3. If I press 1 then interrupt it by press Button 3, usually I can continue with the program still running. However when I press Button 2, Button 3 will not stop the video but does kill the job and the flashing LED will NOT clear in GPIO. It usually sits "ON" . I have been fighting this for a week or two and would appreciate some help. You are welcome to rewrite it if necessary; I am nursing a headache staring so long at the screen. The code is from various other codes on this site and others consolidated with some of my own modifications. Please can you help?

#!/usr/bin/env python3

import RPi.GPIO as GPIO
from omxplayer.player import OMXPlayer
from pathlib import Path
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(13, GPIO.OUT)
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)

VIDEO_PATH1 = Path("/home/pi/Videos/ship.mp4")
VIDEO_PATH2 = Path("/home/pi/Videos/twist.mp4")

def blink():
    for j in range(0,5):
        GPIO.output(13, GPIO.HIGH)  # led on
        time.sleep(0.8)
        GPIO.output(13, GPIO.LOW) # led off
        time.sleep(0.8)

last_state1 = True
last_state2 = True

input_state1 = True
input_state2 = True
quit_video = True

playit = False

playit2 = False

while True:
    #Read states of inputs
    input_state1 = GPIO.input(17)
    input_state2 = GPIO.input(18)
    quit_video = GPIO.input(24)


    #If GPIO(17) is shorted to Ground
    if input_state1 == last_state1:
        if (playit and not input_state1):
            player.quit()
            player = OMXPlayer(VIDEO_PATH1)
            playit = True
        elif not input_state1:
            player = OMXPlayer(VIDEO_PATH1)
            playit = True

    #If GPIO(18) is shorted to Ground
    if input_state2 == last_state2:
        if (playit2 and not input_state2):
            player.quit()
            player = OMXPlayer(VIDEO_PATH2)
            blink()
            playit2 = True
        elif not input_state2:
            player = OMXPlayer(VIDEO_PATH2)
            blink()
            playit2 = True

    #If omxplayer is running and GIOP(17) and GPIO(18) are not
        elif (playit and playit2 and input_state1 and input_state2):
            player.quit()
            playit = False
            playit2 = False

    #GPIO(24) to close omxplayer manually - used during debug
    if quit_video == False:
        player.quit()
        GPIO.output(13,GPIO.LOW)   # led off
        GPIO.cleanup()             # Release resource
        playit = False
        playit2 = False


    #Set last_input states
    last_state1 = input_state1
    last_state2 = input_state2'

UPDATED:

    '#!/usr/bin/env python3

    import RPi.GPIO as GPIO
    import time
    from omxplayer.player import OMXPlayer
    from pathlib import Path
    from time import sleep

    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False) 

    GPIO.setup(13,GPIO.OUT)
    GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
    GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)


    VIDEO_PATH1 = Path("/home/pi/Videos/ship.mp4")
    VIDEO_PATH2 = Path("/home/pi/Videos/twist.mp4")

    last_state1 = True
    last_state2 = True
    last_state3 = True

    input_state1 = True
    input_state2 = True
    quit_video = True

    playit = False
    playit2 = False

    def blink():
        while player.is_playing():
            GPIO.output(13,GPIO.HIGH)
            time.sleep(0.8)
            print("LED is ON!")
            GPIO.output(13,GPIO.LOW)
            time.sleep(0.8)
            print("LED is OFF!!")

    while True:
        #Read states of inputs
        input_state1 = GPIO.input(17)
        input_state2 = GPIO.input(18)
        quit_video = GPIO.input(24)

        #If GPIO(17) is shorted to Ground
        if input_state1 == last_state1:
            if (playit and not input_state1):
                player.quit()
                player = OMXPlayer(VIDEO_PATH1)
                playit = True
            elif not input_state1:
                player = OMXPlayer(VIDEO_PATH1)
                playit = True

        #If GPIO(18) is shorted to Ground
        if input_state2 == last_state2:
            if (playit2 and not input_state2):
                player.quit()
                player = OMXPlayer(VIDEO_PATH2)
                blink()
                playit2 = True

            elif not input_state2:
                player = OMXPlayer(VIDEO_PATH2)
                print("program is running")
                blink()
                playit2 = True


        #If omxplayer is running and GIOP(17) and GPIO(18) are not shorted to Ground
        elif (playit and playit2 and input_state1 and input_state2):
            player.quit()
            playit = False
            playit2 = False

        #GPIO(24) to close omxplayer manually - used during debug
        if quit_video == False:
            player.quit()
            playit = False
            playit2 = False
            GPIO.output(13, GPIO.LOW)   # led off

        #Set last_input states
        last_state1 = input_state1
        last_state2 = input_state2
Katyman
  • 11
  • 3
  • I worked on it some more and found out blink() is holding up the termination of the second video on Button #2. It works without that code but I need the LED to flash every 0.8 seconds. Any help would be appreciated. – Katyman Dec 19 '18 at 21:58
  • I fixed the LED Flash/Video issue, however the video will not stop on pressing button#3 (GPIO 24) and comes back with: Traceback (most recent call last): File "/home/pi/Desktop/Master.print.py", line 69, in blink() File "/home/pi/Desktop/Master.print.py", line 33, in blink while player.is_playing(): File "", line 2, in is_playing File "/home/pi/.local/lib/python3.5/site-packages/omxplayer/player.py", line 50, in wrapped raise OMXPlayerDeadError('Process is no longer alive, can\'t run command') – Katyman Dec 20 '18 at 04:54

1 Answers1

0

I'd use a tight loop that checks buttons and toggles the led based on a timedelta. Something like the following:

#!/usr/bin/env python3

import RPi.GPIO as GPIO
import time,datetime,signal,sys
from omxplayer.player import OMXPlayer

def playerExit(code):
    print('exit',code)
    GPIO.output(ledGPIO,GPIO.LOW)
    global videoPlaying
    videoPlaying=False

def playVideo(video):
    global player,videoPlaying
    if player==None:
        player=OMXPlayer(video)
        player.exitEvent += lambda _, exit_code: playerExit(exit_code)
    else:
        player.load(video)
    videoPlaying=True

def signalHandler(sig,frame):
    print('Ctrl+C pressed')
    quitting()
    sys.exit(0)

def quitPlayerLED():
    GPIO.output(ledGPIO,GPIO.LOW)
    if player!=None:
        player.quit()

# gpio,video/quit,blink T/F
buttonVideos = [[17,"/home/pi/Videos/ship.mp4",False],
                [18,"/home/pi/Videos/twist.mp4",True],
                [24,"quit",False]]
ledGPIO = 13

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False) 

GPIO.setup(ledGPIO,GPIO.OUT)
GPIO.output(ledGPIO,GPIO.LOW)
for buttonVideo in buttonVideos:
    GPIO.setup(buttonVideo[0], GPIO.IN, pull_up_down=GPIO.PUD_UP)

signal.signal(signal.SIGINT,signalHandler)

player = None
videoPlaying = False
blink = False
ledOn = False
toggle=datetime.datetime.now()

while True:
    # check buttons
    for buttonVideo in buttonVideos:
        if GPIO.input(buttonVideo[0]) == GPIO.LOW:
            print(buttonVideo[0],'pressed')
            if buttonVideo[1] == "quit":
                quitPlayerLED()
            else:
                playVideo(buttonVideo[1])
            blink = buttonVideo[2]
            if blink:
                ledOn = True
                toggle=datetime.datetime.now()+datetime.timedelta(seconds=0.8)
            else:
                GPIO.output(ledGPIO,GPIO.LOW)
            break

    # blink led        
    if blink and videoPlaying:
        if datetime.datetime.now() > toggle:
            print(ledOn)
            GPIO.output(ledGPIO,ledOn)
            ledOn = not ledOn
            toggle = datetime.datetime.now()+datetime.timedelta(seconds=0.8)

    time.sleep(0.1)
CoderMike
  • 301
  • 1
  • 7
  • I will try this. I found out that the video will not stop if there is an exception error and will stop without errors. I found a simple way to halt the video without having to wait for the video to end: except KeyboardInterrupt: command1 = "sudo killall -s 9 omxplayer.bin" os.system(command1). There is also deadall(). – Katyman Jan 03 '19 at 15:28
  • Personally I don't like just killing processes. – CoderMike Jan 03 '19 at 16:04
  • I agree but while testing I need a quick kill or wait 2 mins. – Katyman Jan 04 '19 at 21:05
  • Your version works great. How can I setup this to have 1 main video (normal.mp4) loop until a button is pressed, then when ship.mp4 or twist.mp4 end, the main video will start up again unless button 3(24) is pressed? – Katyman Jan 04 '19 at 22:56
  • Probably best if you feel this answer is correct to mark it as such. Then ask another question for your new requirement. – CoderMike Jan 06 '19 at 12:20