|
#!/usr/bin/env python |
|
|
|
# Phone-Setup for the exhibition <Privacy Arena>. |
|
# This script makes a Raspberry Pi create sounds with a piezo speaker and plays sounds from a USB stick |
|
# when a flex sensor is bended. |
|
|
|
# SETUP: LIBRARIES |
|
import RPi.GPIO as GPIO, time, os |
|
import pygame |
|
from random import randint |
|
import json |
|
|
|
# SETUP: MUSICTRACKS FROM AN EXTERNAL DEVICE |
|
# Use your USB stick's name instead of 'USBSTICK' and adjust the tracknames to your liking. |
|
trackOne = "/media/pi/USBSTICK/01.mp3" |
|
trackTwo = "/media/pi/USBSTICK/02.mp3" |
|
trackThree = "/media/pi/USBSTICK/03.mp3" |
|
trackFour = "/media/pi/USBSTICK/04.mp3" |
|
trackBusy = "/media/pi/USBSTICK/hangup.mp3" |
|
|
|
# SETUP: PINS |
|
# Piezo / output is connected to pin 22 on the Rasperry Pi. |
|
DEBUG = 1 |
|
GPIO.setmode(GPIO.BCM) |
|
piezo = 22 #piezo |
|
|
|
# SETUP: IMPORT GLOBAL VARIABLES FROM SETTINGS.JSON |
|
# This section reads variables from a .json file on the external USB stick. With it you can individualize different phones. |
|
with open("/media/pi/USBSTICK/settings.json") as data_file: |
|
data = json.load(data_file) |
|
variables = data["data"] |
|
|
|
timeBetweenCalls = variables["timeBetweenCalls"] #defines how long a phone will wait until it rings again |
|
ringPause = variables["ringPause"] #defines the frequency of the ringing |
|
ringDuration = variables["ringDuration"] #defines the length of the ringing |
|
hzFrequency = variables["hzFrequency"] #defines the altitude of the ringing |
|
threshold = variables["threshold"] #number to compare with sensorinput |
|
|
|
|
|
|
|
# PLAY A SOUNDSNIPPET |
|
def playSound( soundSnippet ): |
|
pygame.mixer.init() #initialize mixer |
|
pygame.mixer.music.load(soundSnippet) #load the sound snippet |
|
pygame.mixer.music.play() #play the soundsnippet |
|
|
|
while pygame.mixer.music.get_busy() == True: |
|
continue |
|
return |
|
|
|
# CHOOSE A SOUNDSNIPPET, THEN CALL PLAYSOUND (ABOVE) |
|
def chooseSound ( i ): |
|
if (i == 1): |
|
playSound( soundSnippet=trackOne ) |
|
elif (i == 2): |
|
playSound( soundSnippet=trackTwo ) |
|
elif (i == 3): |
|
playSound( soundSnippet=trackThree ) |
|
elif (i == 4): |
|
playSound( soundSnippet=trackFour ) |
|
|
|
playSound( soundSnippet=trackBusy ) |
|
time.sleep(float(timeBetweenCalls)) |
|
|
|
return |
|
|
|
# PIEZO OUTPUT |
|
def playPiezo ( pin, hz ): |
|
GPIO.setup(piezo, GPIO.OUT) #piezo setup |
|
GPIO.output(piezo, GPIO.LOW) #piezo output setup |
|
|
|
p = GPIO.PWM(pin, hz) |
|
|
|
return p |
|
|
|
|
|
# READ PINS |
|
# Reads the pins in a defined frequency and returns the result |
|
def RCtime (RCpin): |
|
reading = 0 |
|
GPIO.setup(RCpin, GPIO.OUT) |
|
GPIO.output(RCpin, GPIO.LOW) |
|
|
|
time.sleep(0.1) #frequency |
|
|
|
GPIO.setup(RCpin, GPIO.IN) |
|
|
|
while (GPIO.input(RCpin) == GPIO.LOW): |
|
reading += 1 |
|
|
|
return reading |
|
|
|
|
|
|
|
while True: |
|
#piezo setup |
|
p = playPiezo ( pin=piezo, hz=float(hzFrequency) ) |
|
p.start(0) |
|
|
|
#piezo ranges |
|
for dc in range(0, 20, 20): |
|
p.ChangeDutyCycle(dc) |
|
|
|
#test every 0.01 if new input is higher than threshold |
|
for ringPauseTimer in range (0, int(ringPause)): |
|
print(ringPauseTimer) |
|
time.sleep(0.01) |
|
|
|
reading = RCtime(4)/100 #this calibrates the sensor's reading. |
|
if (reading >= float(threshold)): #if the threshold is higher than what was read >> |
|
r = randint(1, 4) #generate a random number and pass it on to the function chooseSound |
|
chooseSound ( i=r ) |
|
|
|
for dc in range(int(ringDuration), -1, -5): |
|
p.ChangeDutyCycle(dc) |
|
time.sleep(0.1) |
|
|
|
time.sleep(0.1) |
|
|