1

I am a newbie with Python 3 and I have been working on code that will play 4 different videos when one of the five assigned buttons is pressed, and 3 of the videos have timed blinking of LEDs using time.sleep(). I have all the timing working well; however I can not get one video to play/blink LEDs and then on another button do the same even if it interrupts the previous video/blinking. Once the button is pressed and the LEDs blink, they no longer blink if the video ends and the button is pressed again. The 5th button is set to kill the program (as emergency) but sometimes I have to perform "sudo killall omxplayer" manually from LXterminal. Plus I am receiving new errors with omxplayer.bin 'terminating on line 67' after OS upgrade. I need some assistance with coding this and what I may be missing or need to remove:

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

def playerExit(code):
    global command1
    print('exit',code)
    os.system(command1)
    global videoPlaying
    videoPlaying=False

def playVideo(video):
    global player,videoPlaying
    if player==None:
        player = OMXPlayer(video, args=['--no-osd', '--no-keys',]) 
        player.exitEvent += lambda _, exit_code: playerExit(exit_code)
        if buttonVideo[1] == "/home/pi/Videos/noman.mp4":
            time.sleep(164)
            nomanh16()
            nomanr16()
        if buttonVideo[1] == "/home/pi/Videos/cobro.mp4":
            time.sleep(45)
            cobro1()
            cobro2()
            cobror1()
    else:
        player.load(video)
videoPlaying = True


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

def quitPlayerLED():
    global ledGPIO
    for index in ledGPIO:
        GPIO.output(index,GPIO.LOW)
    if player != None:
        player.exit()

def nomanh16():
    for f in range(0,62):
        GPIO.output(13,GPIO.HIGH)   #<<< Turn ON LED
        time.sleep(0.7)             #<<< wait 1/8 of a second
        GPIO.output(13,GPIO.LOW)    #<<< Turn OFF LED
        time.sleep(0.7)             #<<< wait 1/8 of a second

def nomanr16():
     for f in range(0,34):
        GPIO.output(13,GPIO.HIGH)  
        GPIO.output(17,GPIO.HIGH)
        GPIO.output(18,GPIO.HIGH)
        GPIO.output(23,GPIO.HIGH)
        time.sleep(0.7)
        GPIO.output(13,GPIO.LOW)
        GPIO.output(17,GPIO.LOW)
        GPIO.output(18,GPIO.LOW)
        GPIO.output(23,GPIO.LOW)    
        time.sleep(0.7)

def cobro1():
    for f in range(0,28):
        GPIO.output(13,GPIO.HIGH)   #<<< Turn ON LED
        time.sleep(0.6)             #<<< wait 1/8 of a second
        GPIO.output(13,GPIO.LOW)    #<<< Turn OFF LED
        time.sleep(0.6)             #<<< wait 1/8 of a second

def cobro2():
    for f in range(0,107):
        if f >= 86:
            time.sleep(0.4)
            GPIO.output(17,GPIO.HIGH)
            GPIO.output(18,GPIO.HIGH)
            GPIO.output(23,GPIO.HIGH)

        GPIO.output(13,GPIO.HIGH)   
        time.sleep(0.4)            
        GPIO.output(13,GPIO.LOW)    
        GPIO.output(17,GPIO.LOW)
        GPIO.output(18,GPIO.LOW)
        GPIO.output(23,GPIO.LOW)    
        time.sleep(0.4)            

def cobror1():
    for j in range(0,104):
        if j >= 42:
            GPIO.output(13,GPIO.HIGH)

        GPIO.output(17,GPIO.HIGH)
        GPIO.output(18,GPIO.HIGH)
        GPIO.output(23,GPIO.HIGH)
        time.sleep(0.7)
        GPIO.output(13,GPIO.LOW)
        GPIO.output(17,GPIO.LOW)
        GPIO.output(18,GPIO.LOW)
        GPIO.output(23,GPIO.LOW)    
        time.sleep(0.7)


# gpio,video/quit,blink T/F
buttonVideos = [[16,"/home/pi/Videos/noman.mp4",False],
                [20,"/home/pi/Videos/cobro.mp4",False],
                [26,"quit",False]]

ledGPIO = [13,17,18,23]   #<====  LEDS

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

for index in ledGPIO:
    GPIO.setup(index,GPIO.OUT)
    GPIO.output(index,GPIO.LOW)

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

signal.signal(signal.SIGINT,signalHandler)

command1 = "sudo killall omxplayer.bin"

player = None
videoPlaying = False

try:
    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])
                break

except KeyboardInterrupt:
    command1 = "sudo killall -s 9 omxplayer.bin"
    os.system(command1)
    os.system("exit")

except NameError as error:
    command1 = "sudo killall -s 9 omxplayer.bin"
    os.system(command1)
    os.system("exit")

except TypeError as error:
    command1 = "sudo killall -s 9 omxplayer.bin"
    os.system(command1)
    os.system("exit")

except Exception as exception:
    command1 = "sudo killall -s 9 omxplayer.bin"
    os.system(command1)
    os.system("exit")
Katyman
  • 11
  • 3
  • Please try to post a [mcve], changing the code mostly to be more Minimal. – Eb946207 Jan 14 '19 at 02:44
  • I'll clean out what I can. I am new at this. The program works but not in the way I need it to work. – Katyman Jan 14 '19 at 06:24
  • I cleaned out the code removing the pygame part since it is not the issue. If I press button GPIO 20 to play corbo.mp4 it plays and blinks the LEDs correctly. When the video ends, if you press button GPIO 20 again, the video will play but NO Blinking LEDs. Also true if you press GPIO button 16 then GPIO button 20. I would like to be able to stop one and play the other and keep the LED blinking for each button assignment. – Katyman Jan 14 '19 at 06:38

1 Answers1

0

After several trial and errors and researching, I was able to resolve the problem. It could use some cleanup in places. What I could use help with is removing the pygame screen on Ctrl-c since pygame.display.quit() and/or pygame.quit() doesn't seem to work without clicking on the "Stop" button on Thonny Python IDE or rebooting the OS.

#!/usr/bin/env python3

import RPi.GPIO as GPIO
import time,signal,sys,os
from omxplayer.player import OMXPlayer
from sys import exit
import pygame

os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
screen = pygame.display.set_mode((1920,1080), pygame.RESIZABLE)

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

def playerExit(code):
    os.system("sudo killall omxplayer.bin")
    global videoPlaying
    videoPlaying=False

def playVideo(video):
    global player,videoPlaying

    if player == None:
        player=OMXPlayer(video, args=['--o', 'local', '--no-osd', '--no-key'])
        player.exitEvent += lambda _, exit_code: playerExit(exit_code)
        setLEDS()
    else:
        player.load(video)
        player.exitEvent += lambda _, exit_code: playerExit(exit_code)
        setLEDS()

    player = None
    videoPlaying = False

def signalHandler(sig,frame):
    print('Ctrl+C pressed')
    os.system("sudo killall omxplayer.bin")
    player.quit()

def quitPlayerLED():
    os.system("sudo killall omxplayer.bin")
    for index in ledGPIO:
        GPIO.output(index,GPIO.LOW)

def setLEDS():
    if buttonVideo[1] == "/home/pi/Videos/noman.mp4":
        snore(164)
        nomanh16()
        nomanr16()

    if buttonVideo[1] == "/home/pi/Videos/cobro.mp4":
        snore(45)
        cobro1()
        cobro2()
        cobror1()

    if buttonVideo[1] == "/home/pi/Videos/terror.mp4":
        snore(10)
        nomanr16()

def snore(seconds):
    for i in range(seconds):
        if GPIO.input(26) != True:
            quitPlayerLED()
            playerExit(143)
            break
        time.sleep(1)

def nomanh16():
    for f in range(0,62):
        if GPIO.input(26) != True:
            quitPlayerLED()
            playerExit(143)
            break

        GPIO.output(13,GPIO.HIGH)   #<<< Turn ON LED
        time.sleep(0.7)             #<<< wait 1/8 of a second
        GPIO.output(13,GPIO.LOW)    #<<< Turn OFF LED
        time.sleep(0.7)             #<<< wait 1/8 of a second

def nomanr16():
    for f in range(0,34):
        if GPIO.input(26) != True:
            quitPlayerLED()
            playerExit(143)
            break

        GPIO.output(13,GPIO.HIGH)  
        GPIO.output(17,GPIO.HIGH)
        GPIO.output(18,GPIO.HIGH)
        GPIO.output(23,GPIO.HIGH)
        time.sleep(0.7)
        GPIO.output(13,GPIO.LOW)
        GPIO.output(17,GPIO.LOW)
        GPIO.output(18,GPIO.LOW)
        GPIO.output(23,GPIO.LOW)    
        time.sleep(0.7)

def cobro1():
    for f in range(0,28):
        if GPIO.input(26) != True:
            quitPlayerLED()
            playerExit(143)
            break

        GPIO.output(13,GPIO.HIGH)   #<<< Turn ON LED
        time.sleep(0.6)             #<<< wait 1/8 of a second
        GPIO.output(13,GPIO.LOW)    #<<< Turn OFF LED
        time.sleep(0.6)             #<<< wait 1/8 of a second

def cobro2():
    for j in range(0,107):
        if GPIO.input(26) != True:
            quitPlayerLED()
            playerExit(143)
            break

        if j >= 86:
            time.sleep(0.4)
            GPIO.output(17,GPIO.HIGH)
            GPIO.output(18,GPIO.HIGH)
            GPIO.output(23,GPIO.HIGH)

        GPIO.output(13,GPIO.HIGH)   
        time.sleep(0.4)            
        GPIO.output(13,GPIO.LOW)    
        GPIO.output(17,GPIO.LOW)
        GPIO.output(18,GPIO.LOW)
        GPIO.output(23,GPIO.LOW)    
        time.sleep(0.4)            

def cobror1():
    for k in range(0,109):
        if GPIO.input(26) != True:
            quitPlayerLED()
            playerExit(143)
            break

        if k >= 42:
            GPIO.output(13,GPIO.HIGH)

        GPIO.output(17,GPIO.HIGH)
        GPIO.output(18,GPIO.HIGH)
        GPIO.output(23,GPIO.HIGH)
        time.sleep(0.7)
        GPIO.output(13,GPIO.LOW)
        GPIO.output(17,GPIO.LOW)
        GPIO.output(18,GPIO.LOW)
        GPIO.output(23,GPIO.LOW)    
        time.sleep(0.7)


buttonVideos = [[16,"/home/pi/Videos/noman.mp4",False],
                [20,"/home/pi/Videos/cobro.mp4",False],
                [21,"/home/pi/Videos/terror.mp4",False],
                [19,"/home/pi/Videos/normal.mp4",False],
                [26,"quit",False]]

ledGPIO = [13,17,18,23]

for index in ledGPIO:
    GPIO.setup(index,GPIO.OUT)
    GPIO.output(index,GPIO.LOW)

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

signal.signal(signal.SIGINT,signalHandler)

my_image = ('/home/pi/Videos/wallpaper.png')
view_screen = pygame.image.load(my_image).convert_alpha()
view_screen_rect = view_screen.get_rect()

screen.fill((0, 0, 0))
view_screen_rect.centerx = 970      
view_screen_rect.centery = 540      
screen.blit(view_screen,view_screen_rect)
pygame.display.flip()

player = None
videoPlaying = False

try:
    while True:
        # check buttons
        for buttonVideo in buttonVideos:
            if GPIO.input(buttonVideo[0]) == GPIO.LOW:
                if(buttonVideo[1] == "quit"):
                    quitPlayerLED()
                else:
                    quitPlayerLED()
                    playVideo(buttonVideo[1])
                break

except KeyboardInterrupt:
    command1 = "sudo killall omxplayer.bin"
    os.system(command1)
    pygame.display.quit()
    pygame.quit() 
    exit(0)

except NameError as error:
    command1 = "sudo killall omxplayer.bin"
    os.system(command1)
    pygame.display.quit()
    pygame.quit() 
    exit(0)

except TypeError as error:
    command1 = "sudo killall omxplayer.bin"
    os.system(command1)
    pygame.display.quit()
    pygame.quit() 
    exit(0)

except Exception as exception:
    command1 = "sudo killall omxplayer.bin"
    os.system(command1)
    pygame.display.quit()
    pygame.quit() 
    exit(0)
Katyman
  • 11
  • 3