rpi-gpio-switch/main.py
2022-07-16 22:49:24 +02:00

34 lines
1.1 KiB
Python
Executable File

#!/bin/python3
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import pygame
import threading
import time
def play_sound():
if not pygame.mixer.music.get_busy() :
print("Start Sound")
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
pygame.time.Clock().tick(10)
else :
print("Stop Sound")
pygame.mixer.music.stop()
def button_callback(channel):
print("Button was pushed!")
x = threading.Thread(target=play_sound)
x.start()
## init pygame
pygame.mixer.init()
pygame.mixer.music.load('/home/docker/gpio/audio.mp3')
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # Set pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback, bouncetime=10000) # Setup event on pin 10 rising edge 10s wait
message = input("Press enter to quit\n\n") # Run until someone presses enter
GPIO.cleanup() # Clean up