Skip to content
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

Enhanced Encoder support and new GPIO Pinout to enchance compatibility with I2S DAC #350

Merged
merged 6 commits into from
Dec 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions misc/sampleconfigs/gpio-buttons.py.rotaryencoder.sample
Original file line number Diff line number Diff line change
@@ -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()
3 changes: 3 additions & 0 deletions misc/sampleconfigs/gpio-buttons.py.sample
Original file line number Diff line number Diff line change
Expand Up @@ -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
#
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
27 changes: 3 additions & 24 deletions scripts/ky040.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,28 @@

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)
if data == 1:
self.rotaryCallbackCCW()
else:
self.rotaryCallbackCW()

def _switchCallback(self, pin):
if None == self.switchPin:
return

if GPIO.input(self.switchPin) == 0:
self.switchCallback()

46 changes: 30 additions & 16 deletions scripts/rotary-encoder.py
Original file line number Diff line number Diff line change
@@ -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 |
# | | | | | |
Expand All @@ -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()


Expand Down