Fairytale Phone
#!/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