same funcionality as the music rocket

This commit is contained in:
tilman
2016-12-28 22:48:04 +01:00
parent 44bcf2942f
commit 6227639c6d
3 changed files with 121 additions and 114 deletions
+34 -35
View File
@@ -1,22 +1,21 @@
# piplayer2 # piplayer2
# http://www.tilman.de/piplayer2 # http://www.tilman.de/piplayer2
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import logging import logging
import time import time
import subprocess import subprocess
import select # see http://stackoverflow.com/a/10759061/3761783 import select # for polling zbarcam, see http://stackoverflow.com/a/10759061/3761783
import os import os
# Configuration # Configuration
MUSIC_BASE_DIRECTORY = "/home/pi/audio/" MUSIC_BASE_DIRECTORY = "/home/pi/music/"
SOUND_SCANNING = "/home/pi/piplayer2/sounds/scanning.mp3" SOUND_SCANNING = "/home/pi/piplayer2/sounds/scanning.mp3"
SOUND_OK = "/home/pi/piplayer2/sounds/ok.mp3" SOUND_OK = "/home/pi/piplayer2/sounds/ok.mp3"
SOUND_SCAN_FAIL = "/home/pi/piplayer2/sounds/fail.mp3" SOUND_SCAN_FAIL = "/home/pi/piplayer2/sounds/fail.mp3"
SOUND_PLAYBACK_ERROR = "/home/pi/piplayer2/sounds/no.mp3" SOUND_PLAYBACK_ERROR = "/home/pi/piplayer2/sounds/error.mp3"
QR_SCANNER_TIMEOUT = 3 QR_SCANNER_TIMEOUT = 3
BUTTON_PAUSE = 0.4 BUTTON_PAUSE = 400
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)d %(levelname)s - %(message)s') logging.basicConfig(level=logging.DEBUG, format='%(asctime)s.%(msecs)d %(levelname)s - %(message)s')
@@ -41,8 +40,23 @@ GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def btn1_callback(channel):
logging.debug('mocp --previous')
subprocess.call(["mocp", "--previous"])
photo_sensor_still_active = False def btn2_callback(channel):
logging.debug('mocp --toggle-pause')
subprocess.call(["mocp", "--toggle-pause"])
def btn3_callback(channel):
logging.debug('mocp --next')
subprocess.call(["mocp", "--next"])
GPIO.add_event_detect(9, GPIO.FALLING, callback=btn1_callback, bouncetime=BUTTON_PAUSE)
GPIO.add_event_detect(10, GPIO.FALLING, callback=btn2_callback, bouncetime=BUTTON_PAUSE)
GPIO.add_event_detect(11, GPIO.FALLING, callback=btn3_callback, bouncetime=BUTTON_PAUSE)
try: try:
logging.info('Start moc server') logging.info('Start moc server')
@@ -51,27 +65,9 @@ try:
subprocess.call(["mocp", "-l", SOUND_OK]) subprocess.call(["mocp", "-l", SOUND_OK])
while True: while True:
time.sleep(0.05) logging.debug('Wait for photo sensor')
GPIO.wait_for_edge(17, GPIO.RISING)
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') logging.debug('Photo sensor active, activating light and camera')
subprocess.call(["mocp", "-l", SOUND_SCANNING]) subprocess.call(["mocp", "-l", SOUND_SCANNING])
@@ -79,7 +75,7 @@ try:
GPIO.output(27, GPIO.HIGH) GPIO.output(27, GPIO.HIGH)
# scan QR code # scan QR code
zbarcam = subprocess.Popen(['zbarcam', '--nodisplay', '--raw', '-Sdisable', '-Sqrcode.enable', '--prescale=320x240', '/dev/video0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) zbarcam = subprocess.Popen(['zbarcam', '--quiet', '--nodisplay', '--raw', '-Sdisable', '-Sqrcode.enable', '--prescale=320x240', '/dev/video0'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
poll_obj = select.poll() poll_obj = select.poll()
poll_obj.register(zbarcam.stdout, select.POLLIN) poll_obj.register(zbarcam.stdout, select.POLLIN)
@@ -93,7 +89,6 @@ try:
if (poll_result): if (poll_result):
try: try:
qr_code = zbarcam.stdout.readline().rstrip() qr_code = zbarcam.stdout.readline().rstrip()
qr_code = qr_code.decode("utf-8") # python3 qr_code = qr_code.decode("utf-8") # python3
logging.debug("QR Code: {}".format(qr_code)) logging.debug("QR Code: {}".format(qr_code))
@@ -140,7 +135,6 @@ try:
logging.debug("Add directory {}".format(directory)) logging.debug("Add directory {}".format(directory))
subprocess.check_call(["mocp", "-a", "."]) subprocess.check_call(["mocp", "-a", "."])
# subprocess.check_call(["mocp", "-a", target, "-p"])
logging.debug("Start playback") logging.debug("Start playback")
subprocess.check_call(["mocp", "-p"]) subprocess.check_call(["mocp", "-p"])
@@ -151,26 +145,31 @@ try:
except FileNotFoundError as e: except FileNotFoundError as e:
logging.debug("Could not open directory {}".format(directory)) logging.debug("Could not open directory {}".format(directory))
subprocess.call(["mocp", "-l", SOUND_PLAYBACK_ERROR]) subprocess.call(["mocp", "-l", SOUND_PLAYBACK_ERROR])
else: else:
logging.debug('Timeout on zbarcam') logging.debug('Timeout on zbarcam')
subprocess.call(["mocp", "-l", SOUND_SCAN_FAIL]) subprocess.call(["mocp", "-l", SOUND_SCAN_FAIL])
# consider the photo sensor to be blocked
photo_sensor_still_active = True
zbarcam.terminate() zbarcam.terminate()
# LED off # LED off
GPIO.output(27, GPIO.LOW) GPIO.output(27, GPIO.LOW)
elif (GPIO.input(17) == GPIO.LOW): # wait until sensor is not blocked anymore
# the photo sensor is not blocked (anymore) if (GPIO.input(17) == GPIO.HIGH):
photo_sensor_still_active = False GPIO.wait_for_edge(17, GPIO.FALLING)
time.sleep(1)
# Exit when Ctrl-C is pressed # Exit when Ctrl-C is pressed
except KeyboardInterrupt: except KeyboardInterrupt:
logging.info('Shutdown')
finally:
logging.info('Close moc server') logging.info('Close moc server')
subprocess.call(["mocp", "--exit"]) subprocess.call(["mocp", "--exit"])
logging.info('Reset GPIO configuration and close') logging.info('Reset GPIO configuration and close')
GPIO.cleanup() GPIO.cleanup()
+1 -1
View File
@@ -8,7 +8,7 @@ GPIO.setup(9, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(10, 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) GPIO.setup(11, GPIO.IN, pull_up_down=GPIO.PUD_UP)
#sensor on PIN 17 # Sensor on PIN 17
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
def btn1_callback(channel): def btn1_callback(channel):
+8
View File
@@ -39,6 +39,7 @@ sudo nano /boot/config.txt
# dtparam=act_led_trigger=none # dtparam=act_led_trigger=none
# dtparam=act_led_activelow=off # dtparam=act_led_activelow=off
# configure NTP
sudo apt install ntp sudo apt install ntp
sudo /etc/init.d/ntp restart sudo /etc/init.d/ntp restart
sudo nano /etc/ntp.conf sudo nano /etc/ntp.conf
@@ -52,10 +53,17 @@ sudo apt install lirc zbar-tools mc moc fswebcam python3 python-rpi.gpio python3
# PHAT DAC (https://learn.pimoroni.com/tutorial/phat/raspberry-pi-phat-dac-install) # PHAT DAC (https://learn.pimoroni.com/tutorial/phat/raspberry-pi-phat-dac-install)
curl -sS get.pimoroni.com/phatdac | bash curl -sS get.pimoroni.com/phatdac | bash
# check for errors
dmesg
# MP3-Test # MP3-Test
mocp mocp
sudo nano /etc/modprobe.d/alsa-base.conf sudo nano /etc/modprobe.d/alsa-base.conf
# Webcam-Auflösung bestimmen (http://askubuntu.com/questions/214977/how-can-i-find-out-the-supported-webcam-resolutions)
lsusb
lsusb -s 001:006 -v | egrep "Width|Height"
# Webcam-Test # Webcam-Test
python3 lighton.py python3 lighton.py
sudo fswebcam -r 320x240 -d /dev/video0 -v ~/test.jpg sudo fswebcam -r 320x240 -d /dev/video0 -v ~/test.jpg