With this code I have connected a breadboard to a Raspberry pi 3. When a button on the breadboard is pressed a random audiofile from a specified folder is played.
import RPi.GPIO as GPIO
import vlc
import random
import os
def button_callback(channel):
print("Button was pushed!")
path = "/home/pi/Downloads/Slutprojekts_inspelningar_Kung-Fu_Panda"
files=os.listdir(path)
d = random.choice(files)
p = vlc.MediaPlayer(f"/home/pi/Downloads/Slutprojekts_inspelningar_Kung-Fu_Panda/{d}")
p.play()
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) # Setup event on pin 10 rising edge
message = input("Press enter to quit\n\n") # Run until someone presses enter
GPIO.cleanup() # Clean up
After pressing the button a couple of times, it stops working and I get these error messages:
Failed to create permanent mapping for memfd region with ID = 4052867084
Failed to regester memfd mempool. Reason: could not attach memfd SHM ID to pipe
Cannot send block reference with non-registered memfd ID = 4052867084
Fallig back to copying full block data over socket
mmap() failed: Cannot allocate memory
Expected output:
Button was pushed! #while playing the audiofile