rewrite with wiat for gpio instead of active polling
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 2 Dec 2023 14:19:40 +0000 (15:19 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 2 Dec 2023 14:19:40 +0000 (15:19 +0100)
wait.py [new file with mode: 0755]

diff --git a/wait.py b/wait.py
new file mode 100755 (executable)
index 0000000..311f37e
--- /dev/null
+++ b/wait.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python
+
+import RPi.GPIO as GPIO
+from signal import pause
+import pygame
+import glob
+import time
+
+GPIO.setmode(GPIO.BCM)
+
+GPIO.setup(26, GPIO.IN, pull_up_down=GPIO.PUD_UP)
+
+pygame.mixer.init()
+pygame.mixer.music.load( glob.glob("/run/media/system/*/muzika.mp3")[0] )
+
+def play(channel):
+    print("play", channel)
+    pygame.mixer.music.play()
+    while pygame.mixer.music.get_busy() == True:
+        time.sleep(0.1)
+        continue
+
+GPIO.add_event_detect(26, GPIO.FALLING, callback=play, bouncetime=300)
+
+try:
+    play(42)
+    pause()
+except KeyboardInterrupt:
+    GPIO.cleanup()
+