Last active
December 20, 2021 10:51
-
-
Save simonjenny/8d6c29db8b8a995a4d89 to your computer and use it in GitHub Desktop.
Fairytale Phone
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python3 | |
import RPi.GPIO as GPIO | |
import math, sys, os | |
import subprocess | |
import socket | |
GPIO.setwarnings(False) | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP) | |
c=0 | |
last = 1 | |
def count(pin): | |
global c | |
c = c + 1 | |
GPIO.add_event_detect(18, GPIO.BOTH) | |
while True: | |
try: | |
if GPIO.event_detected(18): | |
try: | |
player.kill() | |
except NameError: | |
pass | |
current = GPIO.input(18) | |
if(last != current): | |
if(current == 0): | |
GPIO.add_event_detect(23, GPIO.BOTH, callback=count, bouncetime=5) | |
else: | |
GPIO.remove_event_detect(23) | |
number = math.floor(c/4) | |
player = subprocess.Popen(["mpg123", "/media/" + str(number) + ".mp3", "-q"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
c=0 | |
last = GPIO.input(18) | |
except KeyboardInterrupt: | |
break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment