diff --git a/code/piplayer2.py b/code/piplayer2.py index cb0529e..84c3a04 100644 --- a/code/piplayer2.py +++ b/code/piplayer2.py @@ -1,31 +1,174 @@ # piplayer2 # http://www.tilman.de/piplayer2 - import RPi.GPIO as GPIO +import logging import time +import subprocess +import select # see http://stackoverflow.com/a/10759061/3761783 +import os + + +# Configuration +MUSIC_BASE_DIRECTORY = "/media/audio/" +SOUND_SCANNING = "/home/pi/piplayer2/sounds/scanning.mp3" +SOUND_OK = "/home/pi/piplayer2/sounds/ok.mp3" +SOUND_SCAN_FAIL = "/home/pi/piplayer2/sounds/fail.mp3" +SOUND_PLAYBACK_ERROR = "/home/pi/piplayer2/sounds/no.mp3" +QR_SCANNER_TIMEOUT = 3 +BUTTON_PAUSE = 0.4 + + +logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)d %(levelname)s - %(message)s') +logging.info('Initializing') # photo sensor on PIN 17 GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN) +# IR LED on PIN 22 +GPIO.setup(22, GPIO.OUT) +GPIO.output(22, GPIO.LOW) + # LED on PIN 27 GPIO.setup(27, GPIO.OUT) GPIO.output(27, GPIO.LOW) +# Buttons on PINs 9, 10 and 11 +GPIO.setup(9, GPIO.IN, pull_up_down=GPIO.PUD_UP) +GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_UP) +GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) + + + +photo_sensor_still_active = False + try: - print('Start sensor test') + logging.info('Start moc server') + subprocess.call(["padsp", "mocp", "--server"]) + subprocess.call(["mocp", "--clear"]) + subprocess.call(["mocp", "-l", SOUND_OK]) while True: - time.sleep(0.25) - if (GPIO.input(17) == GPIO.LOW): - print('LOW') - GPIO.output(27, GPIO.LOW) - else: - print('HIGH') + time.sleep(0.01) + + if (GPIO.input(9) == False): + logging.debug('mocp --previous') + subprocess.call(["mocp", "--previous"]) + time.sleep(BUTTON_PAUSE) + # TODO wait for ~0.25 s, if button is still pressed, seek instead of skipping + + if (GPIO.input(10) == False): + logging.debug('mocp --toggle-pause') + subprocess.call(["mocp", "--toggle-pause"]) + time.sleep(BUTTON_PAUSE) + + if (GPIO.input(11) == False): + logging.debug('mocp --next') + subprocess.call(["mocp", "--next"]) + time.sleep(BUTTON_PAUSE) + # TODO wait for ~0.25 s, if button is still pressed, seek instead of skipping + + # check photo sensor + if ((not photo_sensor_still_active) and (GPIO.input(17) == GPIO.HIGH)): + logging.debug('Photo sensor active, activating light and camera') + subprocess.call(["mocp", "-l", SOUND_SCANNING]) + + # turn LED on GPIO.output(27, GPIO.HIGH) - + + # scan QR code + zbarcam = subprocess.Popen(['zbarcam', '--nodisplay', '--raw', '-Sdisable', '-Sqrcode.enable', '--prescale=320x240', '/dev/video0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + + poll_obj = select.poll() + poll_obj.register(zbarcam.stdout, select.POLLIN) + + # wait for scan result (or timeout) + start_time = time.time() + poll_result = False + while ((time.time() - start_time) < QR_SCANNER_TIMEOUT and (not poll_result)): + poll_result = poll_obj.poll(100) + + if (poll_result): + + try: + + qr_code = zbarcam.stdout.readline().rstrip() + qr_code = qr_code.decode("utf-8") # python3 + logging.debug("QR Code: {}".format(qr_code)) + + if (not qr_code.startswith("http://")): + # create full path + full_path = MUSIC_BASE_DIRECTORY + qr_code + logging.debug("Full Path {}".format(full_path)) + + if (not os.path.isfile(full_path)): + logging.debug("not a file, add as directory") + directory = full_path + logging.debug("Directory {}".format(directory)) + else: + logging.debug("it's a file") + directory = os.path.dirname(os.path.realpath(full_path)) + filename = os.path.basename(full_path) + logging.debug("Directory {}".format(directory)) + logging.debug("Filename {}".format(filename)) + + os.chdir(directory) + + else: + logging.debug("URL, open as stream") + stream_url = qr_code + + subprocess.call(["mocp", "--clear"]) + subprocess.call(["mocp", "--stop"]) + + logging.debug("Stopped") + + # play confirmation sound + subprocess.call(["mocp", "-l", SOUND_OK]) + + if ('stream_url' in locals()): + logging.debug("Add stream") + subprocess.check_call(["mocp", "-a", stream_url]) + del stream_url + elif ('filename' in locals()): + logging.debug("Add file {}".format(filename)) + subprocess.check_call(["mocp", "-a", filename]) + del filename + else: + logging.debug("Add directory {}".format(directory)) + subprocess.check_call(["mocp", "-a", "."]) + + # subprocess.check_call(["mocp", "-a", target, "-p"]) + logging.debug("Start playback") + subprocess.check_call(["mocp", "-p"]) + + except subprocess.CalledProcessError as e: + logging.debug("Error starting playback, mocp returned {}".format(e.returncode)) + logging.debug(e.output) + subprocess.call(["mocp", "-l", SOUND_PLAYBACK_ERROR]) + except FileNotFoundError as e: + logging.debug("Could not open directory {}".format(directory)) + subprocess.call(["mocp", "-l", SOUND_PLAYBACK_ERROR]) + else: + logging.debug('Timeout on zbarcam') + subprocess.call(["mocp", "-l", SOUND_SCAN_FAIL]) + + # consider the photo sensor to be blocked + photo_sensor_still_active = True + + zbarcam.terminate() + + # LED off + GPIO.output(27, GPIO.LOW) + + elif (GPIO.input(17) == GPIO.LOW): + # the photo sensor is not blocked (anymore) + photo_sensor_still_active = False + +# Exit when Ctrl-C is pressed except KeyboardInterrupt: - print('bye') + logging.info('Close moc server') + subprocess.call(["mocp", "--exit"]) + logging.info('Reset GPIO configuration and close') GPIO.cleanup() - diff --git a/installation.txt b/installation.txt index 0118124..bd0866e 100644 --- a/installation.txt +++ b/installation.txt @@ -58,13 +58,6 @@ sudo nano /etc/modprobe.d/alsa-base.conf - - - - - - - -- HERE BE DRAGONS ------------------------------------------------------------ diff --git a/misc/QR-Code-Vorlage.odt b/misc/QR-Code-Vorlage.odt new file mode 100644 index 0000000..89d2e12 Binary files /dev/null and b/misc/QR-Code-Vorlage.odt differ diff --git a/notes.md b/notes.md index aa2b2f2..e8db085 100644 --- a/notes.md +++ b/notes.md @@ -1,8 +1,10 @@ # To Do + ## Hardware - PHAT DAC installieren - http://www.instructables.com/id/Multiroom-Client-With-Raspberry-Pi-ZERO-and-PHAT-D/?ALLSTEPS - Lautstärke über IR regeln - JBL-Fernbedienung nach Musikraketen-Anleitung auslesen + ### CNY70 - https://www.youtube.com/watch?v=KLkNqmqXVtA - Fehler in der Schaltungsskizze! Muss NPN- statt PNP-Transistor sein. - http://www.vishay.com/docs/83751/cny70.pdf @@ -14,8 +16,12 @@ ## Scripting +- Bugfix +- PoC IR-LED ## Gehäuse - Buttons per Acryl-Transfer beschriften? https://shop.heise.de/katalog/inhalt-333f53 + +