-1

i want to start and stop this script with a buttons 1 button to start and one to stop using gpio pins 35 and 37

#!/usr/bin/env python
from random import randint
import SimpleMFRC522
import time
import subprocess
import os
import logging
import random
import glob
import RPi.GPIO as GPIO


def playmovie(video, aspect = 0):

    """plays a video."""

    global myprocess
    global directory

    logging.debug('linux: omxplayer %s' % video)

    proccount = isplaying()

    if proccount == 1 or proccount == 0:

        logging.debug('No videos playing, so play video')

    else:

        logging.debug('Video already playing, so quit current video, then play')
        myprocess.communicate(b"q")

    if aspect == 0:
        myprocess = subprocess.Popen(['omxplayer',directory + video],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)

    else:
    #This is for videos that come in 16:9 that should be played as 4:3
    #if your video file is already in 4:3, you don't need to set this flag.
        myprocess = subprocess.Popen(['omxplayer','--win','250,0,1650,1050',directory + video],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE, close_fds=True)

    time.sleep(3)

def isplaying():

        """check if omxplayer is running
        if the value returned is a 1 or 0, omxplayer is NOT playing a video
        if the value returned is a 2, omxplayer is playing a video"""

        processname = 'omxplayer'
        tmp = os.popen("ps -Af").read()
        proccount = tmp.count(processname)

        return proccount

#program start

logging.basicConfig(level=logging.DEBUG)

reader = SimpleMFRC522.SimpleMFRC522()

directory = '/media/pi/BILLYUSB1/'

print("Begin Player")

try:
    while True: 

        proccount = isplaying()

        if proccount == 1 or proccount == 0:

            current_movie_id = long(10)

        start_time = time.time()

        logging.debug("Waiting for ID to be scanned")
        id, movie_name = reader.read()

        logging.debug("ID: %s" % id)
        logging.debug("Movie Name: %s" % movie_name)

        movie_name = movie_name.rstrip()

        if current_movie_id != id:

            logging.debug('New Movie')
            #this is a check in place to prevent omxplayer from restarting video if ID is left over the reader.
            #better to use id than movie_name as there can be a problem reading movie_name occasionally


            if movie_name.endswith(('.mp4', '.avi', '.m4v','.mkv')):
                current_movie_id = id   #we set this here instead of above bc it may mess up on first read
                logging.debug("playing: omxplayer %s" % movie_name)
                playmovie(movie_name)

            elif 'folder' in movie_name:
            #randomly plays video files from a certain folder
                current_movie_id = id
                movie_directory = movie_name.replace('folder',"") 
                movie_name = random.choice(glob.glob(os.path.join(directory + movie_directory, '*')))
                movie_name = movie_name.replace(directory,"")

                logging.debug("randomly playing: omxplayer %s" % movie_name)
                playmovie(movie_name)


            elif 'fourthree' in movie_name:
            #video files randomly played from a folder and that should be played in 4:3 aspect ratio
                current_movie_id = id
                movie_directory = movie_name.replace('fourthree',"")
                movie_name = random.choice(glob.glob(os.path.join(directory + movie_directory, '*')))
                movie_name = movie_name.replace(directory,"")

                logging.debug("randomly playing: omxplayer %s" % movie_name)
                playmovie(movie_name,1)

        else:

            end_time = time.time()
            elapsed_time = end_time - start_time
            proccount = isplaying()

            if proccount != 1 and proccount != 0:

                if elapsed_time > 0.6:
                    #pause, unpause movie

                    logging.debug('Pausing movie - or - Playing movie')
                    myprocess.stdin.write("p")


except KeyboardInterrupt:
    GPIO.cleanup()
    print("\nAll Done")
  • 2
    I see no code that attempts to read buttons. What have you tried? – Milliways Feb 22 at 8:19
  • It should Be on there dirk – james cole Feb 22 at 14:10
0

You just want to trigger these on button push? Save them as a program, write a secont program that imports them, and states 'if pin X=1(or True)(*pin number X is end pin for button. Hook Vin to 3.3v out on Pi) and then tell it the name of the function your running. Or program a GUI and put the programs as functions, and call the function on pinX=True. If you need help with Tkinter im pretty good at it having just programmed an R2D2 for remote access, control, and video output using a Rpi zeroW

  • yah i am now to all of this i have no idea how to do any of that – james cole Feb 22 at 1:55
  • from time import sleep import os import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) button1=35 GPIO.setup(button1,GPIO.IN,pull_up_down=GPIO.PUD_UP) while(1): if GPIO.input(button1)==0: os.system("python /home/pi/MFRC522-python/bplay.py") print "Button 1 Pressed" sleep(.1) – james cole Feb 22 at 1:59
  • that is the code i made it works but once clicked never stops – james cole Feb 22 at 1:59
  • You have to add an end point or it wont. What you have is an infinite while loop. You need if else. If button=1 do blah, else stop. Plus ove noticed with rpi: use BCM numbering. I started with board numbering and alot of python libraries dont like it. Like PWM doesnt work right 100% of the time unless you use BCM. – charles beason Feb 22 at 3:01
  • Charles beason Can you add it to my code so I get it right – james cole Feb 22 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.