diff --git a/misc/sampleconfigs/gpio-buttons.py.rotaryencoder.sample b/misc/sampleconfigs/gpio-buttons.py.rotaryencoder.sample new file mode 100644 index 000000000..bae02dd9c --- /dev/null +++ b/misc/sampleconfigs/gpio-buttons.py.rotaryencoder.sample @@ -0,0 +1,31 @@ +#!/usr/bin/python3 +from gpiozero import Button +from signal import pause +from subprocess import check_call + +# This script is compatible with any I2S DAC e.g. from Hifiberry, Justboom, ES9023, PCM5102A +# Only Buttons that have dissimilar functions as the two encoder are enabled + +# 2018-10-15 +# this script has the `pull_up=True` for all pins. See the following link for additional info: +# https://github.com/MiczFlor/RPi-Jukebox-RFID/issues/259#issuecomment-430007446 +# +# 2017-12-12 +# This script was copied from the following RPi forum post: +# https://forum-raspberrypi.de/forum/thread/13144-projekt-jukebox4kids-jukebox-fuer-kinder/?postID=312257#post312257 +# 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_halt(): + check_call("./scripts/playout_controls.sh -c=playerpause", shell=True) + +shut = Button(3, hold_time=2) +halt = Button(24,pull_up=True) + +shut.when_held = def_shutdown +halt.when_pressed = def_halt + +pause() diff --git a/misc/sampleconfigs/gpio-buttons.py.sample b/misc/sampleconfigs/gpio-buttons.py.sample index 92267782d..28f8bb2af 100644 --- a/misc/sampleconfigs/gpio-buttons.py.sample +++ b/misc/sampleconfigs/gpio-buttons.py.sample @@ -3,6 +3,9 @@ from gpiozero import Button from signal import pause from subprocess import check_call +# 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 + # 2018-10-31 # Added the function on holding volume + - buttons to change the volume in 0.3s interval # diff --git a/misc/sampleconfigs/phoniebox-rotary-encoder.service.stretch-default.sample b/misc/sampleconfigs/phoniebox-rotary-encoder.service.stretch-default.sample index 25a4b7f21..dded1e6ff 100644 --- a/misc/sampleconfigs/phoniebox-rotary-encoder.service.stretch-default.sample +++ b/misc/sampleconfigs/phoniebox-rotary-encoder.service.stretch-default.sample @@ -5,7 +5,9 @@ After=network.target iptables.service firewalld.service [Service] Restart=always WorkingDirectory=/home/pi/RPi-Jukebox-RFID -ExecStart=/home/pi/RPi-Jukebox-RFID/scripts/rotary-encoder.py +ExecStart=/usr/bin/python2 /home/pi/RPi-Jukebox-RFID/scripts/rotary-encoder.py +User=pi +Group=pi [Install] WantedBy=multi-user.target diff --git a/scripts/ky040.py b/scripts/ky040.py index cafd6d299..e4ebc6970 100755 --- a/scripts/ky040.py +++ b/scripts/ky040.py @@ -10,37 +10,24 @@ class KY040: - def __init__(self, arg_clockPin, arg_dataPin, arg_switchPin=None, arg_rotaryCallbackCW=None, arg_rotaryCallbackCCW=None, arg_switchCallback=None, arg_rotaryBouncetime=100, arg_switchBouncetime=200): + def __init__(self, arg_clockPin, arg_dataPin, arg_rotaryCallbackCW=None, arg_rotaryCallbackCCW=None, arg_rotaryBouncetime=100, arg_switchBouncetime=100): # persist values self.clockPin = arg_clockPin self.dataPin = arg_dataPin - self.switchPin = arg_switchPin self.rotaryCallbackCW = arg_rotaryCallbackCW self.rotaryCallbackCCW = arg_rotaryCallbackCCW - self.switchCallback = arg_switchCallback self.rotaryBouncetime = arg_rotaryBouncetime - self.switchBouncetime = arg_switchBouncetime # setup pins - # data and clock have pullups at the PCB - GPIO.setup(self.clockPin, GPIO.IN) - GPIO.setup(self.dataPin, GPIO.IN) - - if None != self.switchPin: - GPIO.setup(self.switchPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) + GPIO.setup(self.clockPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) + GPIO.setup(self.dataPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) def start(self): GPIO.add_event_detect(self.clockPin, GPIO.FALLING, callback=self._clockCallback, bouncetime=self.rotaryBouncetime) - if None != self.switchPin: - GPIO.add_event_detect(self.switchPin, GPIO.FALLING, callback=self._switchCallback, bouncetime=self.switchBouncetime) - def stop(self): GPIO.remove_event_detect(self.clockPin) - if None != self.switchPin: - GPIO.remove_event_detect(self.switchPin) - def _clockCallback(self, pin): if GPIO.input(self.clockPin) == 0: data = GPIO.input(self.dataPin) @@ -48,11 +35,3 @@ def _clockCallback(self, pin): self.rotaryCallbackCCW() else: self.rotaryCallbackCW() - - def _switchCallback(self, pin): - if None == self.switchPin: - return - - if GPIO.input(self.switchPin) == 0: - self.switchCallback() - diff --git a/scripts/rotary-encoder.py b/scripts/rotary-encoder.py index 68230c919..0a2c31d47 100755 --- a/scripts/rotary-encoder.py +++ b/scripts/rotary-encoder.py @@ -1,24 +1,29 @@ #!/usr/bin/python3 -# rotary volume knob +# rotary volume and track knob +# This script is compatible with any I2S DAC e.g. from Hifiberry, Justboom, ES9023, PCM5102A +# Please combine with corresponding gpio button script, which handels the button functionality of the encoder +# RPi-Jukebox-RFID/misc/sampleconfigs/gpio-buttons.py.rotaryencoder.sample + # these files belong all together: # RPi-Jukebox-RFID/scripts/rotary-encoder.py # RPi-Jukebox-RFID/scripts/ky040.py # RPi-Jukebox-RFID/misc/sampleconfigs/phoniebox-rotary-encoder.service.stretch-default.sample +# RPi-Jukebox-RFID/misc/sampleconfigs/gpio-buttons.py.rotaryencoder.sample # See wiki for more info: https://github.com/MiczFlor/RPi-Jukebox-RFID/wiki # -# circuit diagram +# circuit diagram for one of two possible encoders (volume), use GPIOs from code below for the tracks # (capacitors are optionally) # # .---------------. .---------------. # | | | | -# | CLK |------o---------------| GPIO 5 | +# | CLK |------o---------------| GPIO 5 | # | | | | | -# | DT |------)----o----------| GPIO 6 | +# | DT |------)----o----------| GPIO 6 | # | | | | | | -# | SW |------)----)----------| GPIO 13 | +# | SW |------)----)----------| GPIO 3 | # | | | | | | -# | + |------)----)----------| 5V | +# | + |------)----)----------| 3.3V | # | | | | | | # | GND |------)----)----------| GND | # | | | | | | @@ -40,33 +45,42 @@ from subprocess import check_call -def rotaryChangeCW(): +def rotaryChangeCWVol(): check_call("./scripts/playout_controls.sh -c=volumeup", shell=True) -def rotaryChangeCCW(): +def rotaryChangeCCWVol(): check_call("./scripts/playout_controls.sh -c=volumedown", shell=True) -def switchPressed(dummy): - check_call("./scripts/playout_controls.sh -c=mute", shell=True) +def rotaryChangeCWTrack(): + check_call("./scripts/playout_controls.sh -c=playernext", shell=True) + +def rotaryChangeCCWTrack(): + check_call("./scripts/playout_controls.sh -c=playerprev", shell=True) if __name__ == "__main__": - CLOCKPIN = 5 - DATAPIN = 6 - SWITCHPIN = 13 + CLOCKPINVol = 5 + DATAPINVol = 6 + + CLOCKPINTrack = 22 + DATAPINTrack = 23 GPIO.setmode(GPIO.BCM) - ky040 = KY040(CLOCKPIN, DATAPIN, SWITCHPIN, rotaryChangeCW, rotaryChangeCCW, switchPressed) + ky040Vol = KY040(CLOCKPINVol, DATAPINVol, rotaryChangeCWVol, rotaryChangeCCWVol) + + ky040Track = KY040(CLOCKPINTrack, DATAPINTrack, rotaryChangeCWTrack, rotaryChangeCCWTrack) - ky040.start() + ky040Vol.start() + ky040Track.start() try: while True: time.sleep(0.2) finally: - ky040.stop() + ky040Vol.stop() + ky040Track.stop() GPIO.cleanup()