-
Notifications
You must be signed in to change notification settings - Fork 407
Simple powered up led #387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 32 commits
a53a4a9
420e414
2d3c2d1
5c8fc12
5926ee3
3ebaccc
09951e1
2025b9c
f11a22b
29e46e2
c853cd8
d4ec46b
f69c6f0
0e6fc41
4bd0880
b751c77
eedaeb7
16d2997
9770955
82cf063
d0bf4ef
9aae284
6c7799e
27d0af1
2c4fa26
4838b6c
e4707c7
a265213
3aeb444
713fed5
af46a6b
7857bf9
cefbd5c
8503922
79b13fd
cf7a370
74d8690
b9a7de6
cc58dad
0e5ab5e
b0619ab
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,17 @@ | ||
#!/usr/bin/python3 | ||
from gpiozero import Button | ||
import RPi.GPIO as GPIO | ||
from signal import pause | ||
from subprocess import check_call | ||
import time | ||
from time import sleep | ||
|
||
# This script will block any I2S DAC e.g. from Hifiberry, Justboom, ES9023, PCM5102A | ||
# due to the assignment of GPIO 19 and 21 to a buttons | ||
|
||
# 2019-05-14 | ||
# * Rewrite using RPi.GPIO for Raspberry pi zero compatibility. | ||
# * Added power LED functionality | ||
# | ||
# 2018-10-31 | ||
# Added the function on holding volume + - buttons to change the volume in 0.3s interval | ||
# | ||
|
@@ -19,59 +25,115 @@ from subprocess import check_call | |
# I have not yet had the time to test is, so I placed it in the misc folder. | ||
# If anybody has ideas or tests or experience regarding this solution, please create pull requests or contact me. | ||
|
||
def def_shutdown(): | ||
check_call("./scripts/playout_controls.sh -c=shutdown", shell=True) | ||
|
||
def def_volU(): | ||
check_call("./scripts/playout_controls.sh -c=volumeup", shell=True) | ||
|
||
def def_volD(): | ||
check_call("./scripts/playout_controls.sh -c=volumedown", shell=True) | ||
# This function takes a holding time (fractional seconds), a channel, a GPIO state and an action reference (function). | ||
# It checks if the GPIO is in the state since the function was called. If the state | ||
# changes it return False. If the time is over the function returns True. | ||
def checkGpioStaysInState(holdingTime, gpioChannel, gpioHoldingState): | ||
# Get a reference start time (https://docs.python.org/3/library/time.html#time.perf_counter) | ||
startTime = time.perf_counter() | ||
# Continously check if time is not over | ||
while(holdingTime >= (time.perf_counter() - startTime)): | ||
# Return if state does not match holding state | ||
if(gpioHoldingState != GPIO.input(gpioChannel)): | ||
return False | ||
# Else: Wait | ||
return True | ||
|
||
def def_vol0(): | ||
# Actions that call other processes of the phoniebox (Channel Parameter needed by RPi.GPIO) | ||
def shutdown_action(channel): | ||
check_call("./scripts/playout_controls.sh -c=shutdown", shell=True) | ||
def vol0_action(channel): | ||
check_call("./scripts/playout_controls.sh -c=mute", shell=True) | ||
|
||
def def_next(): | ||
def volD_action(channel): | ||
check_call("./scripts/playout_controls.sh -c=volumedown", shell=True) | ||
def volU_action(channel): | ||
check_call("./scripts/playout_controls.sh -c=volumeup", shell=True) | ||
def next_action(channel): | ||
check_call("./scripts/playout_controls.sh -c=playernext", shell=True) | ||
|
||
def def_prev(): | ||
def prev_action(channel): | ||
check_call("./scripts/playout_controls.sh -c=playerprev", shell=True) | ||
|
||
def def_halt(): | ||
def halt_action(channel): | ||
check_call("./scripts/playout_controls.sh -c=playerpause", shell=True) | ||
|
||
def def_recordstart(): | ||
def recordstart_action(channel): | ||
check_call("./scripts/playout_controls.sh -c=recordstart", shell=True) | ||
|
||
def def_recordstop(): | ||
def recordstop_action(channel): | ||
check_call("./scripts/playout_controls.sh -c=recordstop", shell=True) | ||
|
||
def def_recordplaylatest(): | ||
def recordplaylatest_action(channel): | ||
check_call("./scripts/playout_controls.sh -c=recordplaylatest", shell=True) | ||
|
||
shut = Button(3, hold_time=2) | ||
vol0 = Button(13,pull_up=True) | ||
volU = Button(16,pull_up=True,hold_time=0.3,hold_repeat=True) | ||
volD = Button(19,pull_up=True,hold_time=0.3,hold_repeat=True) | ||
next = Button(26,pull_up=True) | ||
prev = Button(20,pull_up=True) | ||
halt = Button(21,pull_up=True) | ||
#reco = Button(6, pull_up=True) # Choose GPIO to fit your hardware | ||
#play = Button(12,pull_up=True) # Choose GPIO to fit your hardware | ||
# Handlers that handle special behavior of the push of a button | ||
# When the shutdown button was held for more than 2 seconds LED flashed and afterwards | ||
def shutdown_handler(channel): | ||
# Detect holding of button | ||
if True == checkGpioStaysInState(shutdownHoldTime, shut, GPIO.LOW): | ||
# Blink LED | ||
for x in range(0, 10): | ||
GPIO.output(pled, x & 1) | ||
sleep(PledBlinkTime) | ||
# Keep LED on until power off | ||
GPIO.output(pled, GPIO.HIGH) | ||
# Shutdown afterwards | ||
shutdown_action(channel) | ||
|
||
# When the Volume Down button was held for more than 0.3 seconds every 0.3 seconds he will lower t$ | ||
def volU_handler(channel): | ||
# Rise volume as requested | ||
volU_action(channel) | ||
# Detect holding of button | ||
while checkGpioStaysInState(volumeHoldTime, volU, GPIO.LOW): | ||
volU_action(channel) | ||
|
||
# When the Volume Up button was held for more than 0.3 seconds every 0.3 seconds he will call a ra$ | ||
def volD_handler(channel): | ||
# Rise volume as requested | ||
volD_action(channel) | ||
# Detect holding of button | ||
while checkGpioStaysInState(volumeHoldTime, volD, GPIO.LOW): | ||
volD_action(channel) | ||
|
||
# Define the used pins of the raspberry board | ||
shut = 3 | ||
vol0 = 13 | ||
volU = 16 | ||
volD = 19 | ||
next = 26 | ||
prev = 20 | ||
halt = 21 | ||
pled = 7 | ||
#reco = | ||
#play = | ||
|
||
# Set GPIO numbering to BCM instead of board numbering | ||
GPIO.setmode(GPIO.BCM) | ||
# Bounce tolerance time for buttons | ||
bouncetime = 500 | ||
volumeHoldTime = 0.3 # Seconds | ||
shutdownHoldTime = 2 # Seconds | ||
PledBlinkTime = 0.3 # Seconds | ||
|
||
# Set up GPIO pins and the power led | ||
GPIO.setup(shut, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||
GPIO.setup(vol0, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||
GPIO.setup(volU, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||
GPIO.setup(volD, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||
GPIO.setup(next, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change it to (#694 ): |
||
GPIO.setup(prev, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||
GPIO.setup(halt, GPIO.IN, pull_up_down=GPIO.PUD_UP) | ||
GPIO.setup(pled, GPIO.OUT) | ||
|
||
# Set standard events for the buttons. Callback functions define the actions of the events (THey are defined above) | ||
GPIO.add_event_detect(shut, GPIO.FALLING, callback=shutdown_handler, bouncetime=bouncetime) | ||
GPIO.add_event_detect(vol0, GPIO.FALLING, callback=vol0_action, bouncetime=bouncetime) | ||
GPIO.add_event_detect(volU, GPIO.FALLING, callback=volU_handler, bouncetime=bouncetime) | ||
GPIO.add_event_detect(volD, GPIO.FALLING, callback=volD_handler, bouncetime=bouncetime) | ||
GPIO.add_event_detect(next, GPIO.FALLING, callback=next_action, bouncetime=bouncetime) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. change next see above comments |
||
GPIO.add_event_detect(prev, GPIO.FALLING, callback=prev_action, bouncetime=bouncetime) | ||
GPIO.add_event_detect(halt, GPIO.FALLING, callback=halt_action, bouncetime=bouncetime) | ||
#reco.when_pressed = recordstart_action | ||
#reco.when_released = recordstop_action | ||
#play.when_pressed = recordplaylatest_action | ||
|
||
shut.when_held = def_shutdown | ||
vol0.when_pressed = def_vol0 | ||
volU.when_pressed = def_volU | ||
#When the Volume Up button was held for more than 0.3 seconds every 0.3 seconds he will call a ra$ | ||
volU.when_held = def_volU | ||
volD.when_pressed = def_volD | ||
#When the Volume Down button was held for more than 0.3 seconds every 0.3 seconds he will lower t$ | ||
volD.when_held = def_volD | ||
next.when_pressed = def_next | ||
prev.when_pressed = def_prev | ||
halt.when_pressed = def_halt | ||
#reco.when_pressed = def_recordstart | ||
#reco.when_released = def_recordstop | ||
#play.when_pressed = def_recordplaylatest | ||
# Switch on power led after boot to indicate state "on" for phoniebox | ||
GPIO.output(pled, GPIO.HIGH) | ||
|
||
pause() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change it to (#694 ):
fwrd = 26