commit 1070b53aa0ee5784ed7ff59d9eb4eb91e81ff6ee Author: Luca Rullo Date: Sat Jul 16 22:49:24 2022 +0200 Ejemplo script diff --git a/README.md b/README.md new file mode 100644 index 0000000..14b449a --- /dev/null +++ b/README.md @@ -0,0 +1,31 @@ +# Script para usar la Rpi con un pulsador en GPIO + +Una prueba para usar el puerto GPIO 8 de la Rpi con un pulsador y ejecutar un sonido. + +## Descargar audio + +Ejemplo: + +``` +$ wget http://www.soinumapa.net/wp-content/uploads/2015/10/amurrio_diana.mp3 -O /home/docker/gpio/audio.mp3 +``` + +## Lanzar el script + +``` +# python3 main.py +``` + +o + +``` +# ./main.py +``` + +## Enlaces + +* https://raspberrypihq.com/use-a-push-button-with-raspberry-pi-gpio/ +* https://www.jeffgeerling.com/blog/2022/playing-sounds-python-on-raspberry-pi +* https://sourceforge.net/p/raspberry-gpio-python/wiki/Inputs/ +* https://realpython.com/intro-to-python-threading/ + diff --git a/main.py b/main.py new file mode 100755 index 0000000..0dedc7c --- /dev/null +++ b/main.py @@ -0,0 +1,33 @@ +#!/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