Skip to content

Instantly share code, notes, and snippets.

@Pigankle
Last active March 10, 2017 01:18
Show Gist options
  • Save Pigankle/c5a5d5e18a70049cfb5f71ca90c83c61 to your computer and use it in GitHub Desktop.
Save Pigankle/c5a5d5e18a70049cfb5f71ca90c83c61 to your computer and use it in GitHub Desktop.
Turn a rotary dial phone into an mp3 mixtape, with each number corresponding to one track, except for 8, which plays a random track (Magic 8-Ball). When the phone is on cradle, dialing a number sets the volume. Thanks to simonjenny, of the Fairytale Phone project, (https://gist.github.com/simonjenny/8d6c29db8b8a995a4d89) for the basic approach.
#!/usr/bin/python3
import RPi.GPIO as GPIO
import math, sys, os
import subprocess
import socket
import glob
#Global setting to control whether random play plays on.
#Set GPIO Pin Behavior
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
#24 goes to GREEN
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#27 Goes to YELLOW - Cradle Status
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#5 Goes to ORANGE
#Needs New Pin umber to not conflict with Adafruit Sound Bonnet
GPIO.setup(5, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#Open a Devnull Stream to send unwanted output
DEVNULL = open(os.devnull, "w")
#My sound files are in /media/mp3s. in /media, I have softlinks to the
#Top 9 tracks in /media/mp3s. #8 is reserved for a random track from the
#full collection. For example, if I want SotrmyMonday.mp3 to play when
#I dial a 5,
# ln -s /media/mp3s/StormyMonday.mp3 5.mp3
# The next three lines count the number of available mp3s and print
# an informative message. They are not otherwise necessary, and can
# be commented out.
import fnmatch
mp3Count = len(fnmatch.filter(os.listdir("/media/mp3s"), '*.mp3'))
print(str(mp3Count) + " files are available.\n\n")
c=0
last = 1
#initialize volume
volArg ="-g 65"
isOffHook = 0
def count(pin):
global c
c = c + 1
sys.stderr.write("A Cradle Active - isOffHook = "+ str(isOffHook)+"\n")
GPIO.add_event_detect(27, GPIO.BOTH)
def checkCradle():
global isOffHook
newIsOffHook = GPIO.input(27)
# sys.stderr.write("B Cradle Active - newIsOffHook = "+ str(newIsOffHook)+"\n")
if isOffHook != newIsOffHook:
sys.stderr.write("C Cradle Active\n GPIO.input(27): = "+ str(GPIO.input(27)) +"\n")
if GPIO.input(27)==0:
isOffHook = 0
# sys.stderr.write("D Cradle Active\n GPIO.input(27): = "+ str(GPIO.input(27)) +"\n")
sys.stderr.write("Hanging up\n\n\n")
try:
player.kill() #Stops currently playing song (if applicable)
except NameError:
pass
else:
isOffHook = 1
# sys.stderr.write("Ec Cradle Active\n GPIO.input(27): = "+ str(GPIO.input(27)) +"\n")
sys.stderr.write("Picking up\n\n\n")
GPIO.add_event_detect(5, GPIO.BOTH)
while True:
try:
checkCradle() # Get cradle status
if GPIO.event_detected(5): #Dialing any number
checkCradle() # Get cradle status
current = GPIO.input(5)
if(last != current):
if(current == 0):
GPIO.add_event_detect(24, GPIO.BOTH, callback=count, bouncetime=10)
else:
GPIO.remove_event_detect(24)
number = math.floor(c/2)
sys.stderr.write("c = "+ str(c)+ "\n")
sys.stderr.write("number = "+ str(number)+ "\n")
if isOffHook == 0:
volume = number*10
volArg = "-g "+ str(volume)
sys.stderr.write("changing volume to "+ str(volume)+ "%\n\n\n")
else:
try:
player.kill() #Stops currently playing song (if applicable)
except NameError:
pass
filename = "/media/"+str(number)+".mp3"
if number == 8: # If I dial an 8, play the whole directory randomly
player = subprocess.Popen(["mpg123", "-K","-Bz", "/media/mp3s/" ,volArg ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
else:
print("filename = "+ filename)
sys.stderr.write(filename+"\n\n\n")
player = subprocess.Popen(["mpg123", filename,"-K", "-q", volArg], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
c=0
last = GPIO.input(5)
except KeyboardInterrupt:
break
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment