# import modules import pygame import random import pygame.mixer from time import sleep import RPi.GPIO as GPIO import os import sys from subprocess import Popen from sys import exit GPIO.setmode(GPIO.BCM) GPIO.setup(23, GPIO.IN) GPIO.setup(24, GPIO.IN) GPIO.setup(25, GPIO.IN) GPIO.setmode(GPIO.BCM) 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) sndA = pygame.mixer.Sound("buzzer.wav") sndB = pygame.mixer.Sound("clap.wav") sndC = pygame.mixer.Sound("laugh.wav") movie1 = ("/home/pi/Videos/movie1.mp4") movie2 = ("/home/pi/Videos/movie2.mp4") soundChannelA = pygame.mixer.Channel(1) soundChannelB = pygame.mixer.Channel(2) soundChannelC = pygame.mixer.Channel(3) last_state1 = True last_state2 = True input_state1 = True input_state2 = True quit_video = True player = False # Define screen size WIDTH = 800 HEIGHT = 600 FPS = 30 # define colors WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 0, 0) GREEN = (0, 255, 0) BLUE = (0, 0, 255) # initialize pygame and create window pygame.init() pygame.mixer.init(48000, -16, 1, 1024) set_num_channels(8) screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Sprite Example") clock = pygame.time.Clock() print "Sampler Ready." while True: try: if (GPIO.input(23) == True): soundChannelA.play(sndA) if (GPIO.input(24) == True): soundChannelB.play(sndB) if (GPIO.input(25) == True): soundChannelC.play(sndC) #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 (player and not input_state1): os.system('killall omxplayer.bin') omxc = Popen(['omxplayer', '-b', movie1]) player = True elif not input_state1: omxc = Popen(['omxplayer', '-b', movie1]) player = True #If GPIO(18) is shorted to Ground elif input_state2 != last_state2: if (player and not input_state2): os.system('killall omxplayer.bin') omxc = Popen(['omxplayer', '-b', movie2]) player = True elif not input_state2: omxc = Popen(['omxplayer', '-b', movie2]) player = True #If omxplayer is running and GIOP(17) and GPIO(18) are not shorted to Ground elif (player and input_state1 and input_state2): os.system('killall omxplayer.bin') player = False #GPIO(24) to close omxplayer manually - used during debug if quit_video == False: os.system('killall omxplayer.bin') player = False # keep loop running at the right speed clock.tick(FPS) # Process input (events) for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False # Draw / render screen.fill(BLACK) # *after* drawing everything, flip the display pygame.display.flip() #Set last_input states last_state1 = input_state1 last_state2 = input_state2 sleep(.01) except KeyboardInterrupt: exit() pygame.quit()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question