rahlstedt-audio/testgpio.py
2021-05-01 11:18:03 +01:00

219 lines
4.9 KiB
Python

import RPi.GPIO as GPIO
from mfrc522 import MFRC522
import signal
from time import sleep
from soundplayer import SoundPlayer
import os
import glob
#from interval import IntervalTimer
maxload = 50
maxtitel = 15
titelnr = 1
load = 50
titel = "/home/pi/1-43ImmergruenePartyKnueller.mp3"
p = SoundPlayer(titel, 1)
musik = {}
playlist = True
lastcheck = 0
def loadSongs():
global maxtitel
names = glob.glob("/home/pi/*.mp3")
for x in names:
p = int(x.split('-')[0].split('/')[3])
musik[p] = x
maxtitel = len(musik)
def showSongs():
for i in range(len(musik)+1):
print(i,musik.get(i))
def playSong(nr):
global p
global titelnr
titelnr = nr
titel = musik.get(titelnr)
print("Now playing "+titel)
p.stop()
sleep(2)
p = SoundPlayer(titel,1)
p.play(1) # non-blocki
def nextSong():
global titelnr
global maxtitel
if titelnr >= maxtitel:
titelnr = 1
else:
titelnr = titelnr +1
playSong(titelnr)
def previousSong():
global titelnr
global maxtitel
if titelnr <= 1:
titelnr = maxtitel
else:
titelnr = titelnr -1
playSong(titelnr)
def gibLaut():
global lastcheck
global playlist
oks = p.isPlaying()
if oks == True:
lastcheck = 0
else:
if lastcheck == 0:
lastcheck = 1
else:
if lastcheck == 1:
lastcheck = 0
if playlist == True:
nextSong()
#inter = IntervalTimer(2,gibLaut)
continue_reading = True
def setLoad(load):
s = "amixer -c 1 cset numid=6 "+str(load)+ "," + str(load) + " > /home/pi/loud.txt"
#print(s)
os.system(s)
f = open("/home/pi/loud.txt")
f.readline()
f.readline()
s=f.readline()
x = s.split(',')
print(x[1])
def change_loudness():
global load
f = open("/home/pi/start.txt","w")
f.write(str(load))
print(str(load))
f.close()
def read_info():
global maxload
global load
f = open("/home/pi/sound.txt")
f.readline()
s = f.readline()
x = s.split(',')[4].split('=')
maxload = int(x[1])
g = open("/home/pi/start.txt")
s = g.readline()
load = int(s)
print(load)
setLoad(load)
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print ("Ctrl+C captured, ending read.")
inter.stop()
continue_reading = False
GPIO.cleanup()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522()
#Lauter
GPIO.setup(37, GPIO.IN)
#Leiser
GPIO.setup(36, GPIO.IN)
#Next Song
GPIO.setup(33, GPIO.IN)
#prevoius Song
GPIO.setup(35, GPIO.IN)
#set loudness
GPIO.setup(38, GPIO.IN)
# Welcome message
print("Welcome to the jfs World of Sounds")
loadSongs()
showSongs()
info1 = os.system("amixer -c 1 cget numid=6 > /home/pi/sound.txt")
read_info()
print ("Press Ctrl-C to stop.")
p.play(1)
#os.system("amixer -c 1 cset numid=6 50,50")
#inter.start()
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
# other pins
if GPIO.input(37) == 1:
#print("+")
if load < maxload:
load = load +5
setLoad(load)
if GPIO.input(36) == 1:
#print("-")
if load > 0:
load = load - 5
setLoad(load)
if GPIO.input(33) == 1 :
print(">>>>>")
nextSong()
if GPIO.input(35) == 1 :
print("<<<<<")
previousSong()
if GPIO.input(38) == 1 :
print("set loudness")
change_loudness()
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
print ("Card detected")
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
# Print UID
print ("Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3]) )
# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
# Check if authenticated
if status == MIFAREReader.MI_OK:
data = []
text_read = ''
#print(MIFAREReader.MFRC522_Read(8))
block = MIFAREReader.MFRC522_Read(8)
if block:
data += block
if data:
text_read = ''.join(chr(i) for i in data)
MIFAREReader.MFRC522_StopCrypto1()
print(text_read)
s = text_read.strip()
i = int(s)
print("Card No "+str(i))
playSong(i)
else:
print ( "Authentication error")