From 011e90ce298f66820779eb14e47f92171a9e0107 Mon Sep 17 00:00:00 2001 From: Martin Clausen Date: Sun, 25 Nov 2018 19:48:50 +0100 Subject: [PATCH 1/5] fix sample systemd configuration for rotary-encoder.py --- .../phoniebox-rotary-encoder.service.stretch-default.sample | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 From ac97c2644994843c473fc7f386d5d1876045cdac Mon Sep 17 00:00:00 2001 From: Martin Clausen Date: Sun, 25 Nov 2018 19:53:14 +0100 Subject: [PATCH 2/5] activate pullups for bare rotary encoders, reduce debounce time remove switch functionality from encoder program, since is covered by gpio-buttons.py added second rotary encoder channel for tracks adjusted GPIO pins --- scripts/ky040.py | 27 +++------------------------ scripts/rotary-encoder.py | 29 +++++++++++++++++++---------- 2 files changed, 22 insertions(+), 34 deletions(-) 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..8fb77a9f2 100755 --- a/scripts/rotary-encoder.py +++ b/scripts/rotary-encoder.py @@ -40,33 +40,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 = 27 + DATAPINVol = 17 + + 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() From e03e4c773ab429bba3b498046edfd95179ef0619 Mon Sep 17 00:00:00 2001 From: Martin Clausen Date: Sun, 25 Nov 2018 20:05:20 +0100 Subject: [PATCH 3/5] make GPIO selection compatible with directly via I2S connected DACs disable GPIOs which functions are covered by rotary encoders --- misc/sampleconfigs/gpio-buttons.py.sample | 29 +++++++++-------------- 1 file changed, 11 insertions(+), 18 deletions(-) diff --git a/misc/sampleconfigs/gpio-buttons.py.sample b/misc/sampleconfigs/gpio-buttons.py.sample index 92267782d..5ed2b02d5 100644 --- a/misc/sampleconfigs/gpio-buttons.py.sample +++ b/misc/sampleconfigs/gpio-buttons.py.sample @@ -3,9 +3,6 @@ from gpiozero import Button from signal import pause from subprocess import check_call -# 2018-10-31 -# Added the function on holding volume + - buttons to change the volume in 0.3s interval -# # 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 @@ -38,23 +35,19 @@ def def_halt(): check_call("./scripts/playout_controls.sh -c=playerpause", 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) +#vol0 = Button(13,pull_up=True) +#volU = Button(16,pull_up=True) +#volD = Button(19,pull_up=True) +#next = Button(26,pull_up=True) +#prev = Button(20,pull_up=True) +halt = Button(24,pull_up=True) 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 +#vol0.when_pressed = def_vol0 +#volU.when_pressed = def_volU +#volD.when_pressed = def_volD +#next.when_pressed = def_next +#prev.when_pressed = def_prev halt.when_pressed = def_halt pause() From 1d1f77fb3b1676e502bf6376b345926d4b817195 Mon Sep 17 00:00:00 2001 From: Martin Clausen Date: Tue, 27 Nov 2018 22:11:13 +0100 Subject: [PATCH 4/5] Extended comments on code and how to use Fix voltage on RPi side for encoder KY040 connection Provide separate example of encoder compatible gpio button script Revert "make GPIO selection compatible with directly via I2S connected DACs" This reverts commit e03e4c773ab429bba3b498046edfd95179ef0619. --- .../gpio-buttons.py.rotaryencoder.sample | 31 ++++++++++++++++++ misc/sampleconfigs/gpio-buttons.py.sample | 32 ++++++++++++------- scripts/rotary-encoder.py | 17 ++++++---- 3 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 misc/sampleconfigs/gpio-buttons.py.rotaryencoder.sample 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 5ed2b02d5..28f8bb2af 100644 --- a/misc/sampleconfigs/gpio-buttons.py.sample +++ b/misc/sampleconfigs/gpio-buttons.py.sample @@ -3,6 +3,12 @@ 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 +# # 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 @@ -35,19 +41,23 @@ def def_halt(): check_call("./scripts/playout_controls.sh -c=playerpause", shell=True) shut = Button(3, hold_time=2) -#vol0 = Button(13,pull_up=True) -#volU = Button(16,pull_up=True) -#volD = Button(19,pull_up=True) -#next = Button(26,pull_up=True) -#prev = Button(20,pull_up=True) -halt = Button(24,pull_up=True) +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) shut.when_held = def_shutdown -#vol0.when_pressed = def_vol0 -#volU.when_pressed = def_volU -#volD.when_pressed = def_volD -#next.when_pressed = def_next -#prev.when_pressed = def_prev +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 pause() diff --git a/scripts/rotary-encoder.py b/scripts/rotary-encoder.py index 8fb77a9f2..a853d0a70 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 27 | # | | | | | -# | DT |------)----o----------| GPIO 6 | +# | DT |------)----o----------| GPIO 17 | # | | | | | | -# | SW |------)----)----------| GPIO 13 | +# | SW |------)----)----------| GPIO 3 | # | | | | | | -# | + |------)----)----------| 5V | +# | + |------)----)----------| 3.3V | # | | | | | | # | GND |------)----)----------| GND | # | | | | | | From 517b99ebf7c1eada5ab97e8afe6b15cd60e42c79 Mon Sep 17 00:00:00 2001 From: Martin Clausen Date: Mon, 10 Dec 2018 21:45:53 +0100 Subject: [PATCH 5/5] align with new pin out --- scripts/rotary-encoder.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/rotary-encoder.py b/scripts/rotary-encoder.py index a853d0a70..0a2c31d47 100755 --- a/scripts/rotary-encoder.py +++ b/scripts/rotary-encoder.py @@ -17,9 +17,9 @@ # # .---------------. .---------------. # | | | | -# | CLK |------o---------------| GPIO 27 | +# | CLK |------o---------------| GPIO 5 | # | | | | | -# | DT |------)----o----------| GPIO 17 | +# | DT |------)----o----------| GPIO 6 | # | | | | | | # | SW |------)----)----------| GPIO 3 | # | | | | | | @@ -60,8 +60,8 @@ def rotaryChangeCCWTrack(): if __name__ == "__main__": - CLOCKPINVol = 27 - DATAPINVol = 17 + CLOCKPINVol = 5 + DATAPINVol = 6 CLOCKPINTrack = 22 DATAPINTrack = 23