first pass at turning SoundslabInput into a class

This commit is contained in:
Sundog Jones 2020-08-02 16:47:56 -07:00
parent 7e42594e13
commit d0c6cf06e5
1 changed files with 112 additions and 115 deletions

View File

@ -5,149 +5,146 @@ import time
import musicpd import musicpd
from math import floor from math import floor
# connect to mpd class SoundslabInputHandler:
player_client = musicpd.MPDClient() def __init__(self):
player_client.connect() # connect to mpd
self.player_client = musicpd.MPDClient()
self.player_client.connect()
# initialize connection to display # initialize connection to display
display = SoundslabDisplay(player_client) self.display = SoundslabDisplay(player_client)
# The buttons on Pirate Audio are connected to pins 5, 6, 16 and 24 # The buttons on Pirate Audio are connected to pins 5, 6, 16 and 24
# Boards prior to 23 January 2020 used 5, 6, 16 and 20 # Boards prior to 23 January 2020 used 5, 6, 16 and 20
# try changing 24 to 20 if your Y button doesn't work. # try changing 24 to 20 if your Y button doesn't work.
BUTTONS = [5, 6, 16, 24, 17, 27, 22, 23, 20] self.BUTTONS = [5, 6, 16, 24, 17, 27, 22, 23, 20]
# These correspond to buttons A, B, X and Y then the five-way switch positions # These correspond to buttons A, B, X and Y then the five-way switch positions
LABELS = ['NW', 'SW', 'NE', 'SE', 'UP', 'SELECT', 'LF', 'DN', 'RT'] self.LABELS = ['NW', 'SW', 'NE', 'SE', 'UP', 'SELECT', 'LF', 'DN', 'RT']
# Set up RPi.GPIO with the "BCM" numbering scheme # Set up RPi.GPIO with the "BCM" numbering scheme
GPIO.setmode(GPIO.BCM) GPIO.setmode(GPIO.BCM)
# Buttons connect to ground when pressed, so we should set them up # Buttons connect to ground when pressed, so we should set them up
# with a "PULL UP", which weakly pulls the input signal to 3.3V. # with a "PULL UP", which weakly pulls the input signal to 3.3V.
GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(BUTTONS, GPIO.IN, pull_up_down=GPIO.PUD_UP)
# some button handlers # This is a dictionary of the combination of pins and labels
def NE(): self.LABELLED_BUTTONS = {
# play/pause toggle 5: NW,
global player_client 6: SW,
status = player_client.status() 16: NE,
if status['state'] == "play": 24: SE,
print("pausing") 17: UP,
player_client.pause(1) 27: SELECT,
elif status['state'] == "pause": 22: LF,
print("resuming") 23: DN,
player_client.pause(0) 20: RT
else: # state == "stop" }
print("playing")
player_client.play()
def SW(): # Loop through out buttons and attach the "handle_button" function to each
# previous track # We're watching the "FALLING" edge (transition from 3.3V to Ground) and
global player_client # picking a generous bouncetime of 150ms to smooth out button presses.
status = player_client.status() for pin in self.BUTTONS:
if float(status['elapsed']) > 5.0: GPIO.add_event_detect(pin, GPIO.FALLING, self.handle_button, bouncetime=150)
# restart track
print("restart current track")
player_client.seekcur(0)
else:
# go to previous track
print("previous track")
player_client.previous()
def NW(): # some button handlers
# open menu def NE(self):
global display # play/pause toggle
print("toggling menu") status = self.player_client.status()
if display.show_menu is True: if status['state'] == "play":
display.show_menu = False print("pausing")
else: player_client.pause(1)
display.show_menu = True elif status['state'] == "pause":
print("resuming")
player_client.pause(0)
else: # state == "stop"
print("playing")
player_client.play()
def SE(): def SW(self):
# next track # previous track
global player_client status = player_client.status()
print("next track") if float(status['elapsed']) > 5.0:
player_client.next() # restart track
print("restart current track")
self.player_client.seekcur(0)
else:
# go to previous track
print("previous track")
self.player_client.previous()
def UP(): def NW(self):
# volume up 10% # open menu
global player_client print("toggling menu")
print("vol up") if self.display.show_menu is True:
status = player_client.status() self.display.show_menu = False
if int(status['volume']) < 100: else:
player_client.setvol(int(status['volume']) + 10) self.display.show_menu = True
def DN(): def SE(self):
# volume down 10% # next track
global player_client print("next track")
print("vol down") self.player_client.next()
status = player_client.status()
if int(status['volume']) > 0:
player_client.setvol(int(status['volume']) - 10)
def LF(): def UP(self):
# seek back 10s # volume up 10%
global player_client print("vol up")
print("seek -10s") status = self.player_client.status()
status = player_client.status() if int(status['volume']) < 100:
player_client.seekcur(floor(float(status['elapsed']) - 10.0)) self.player_client.setvol(int(status['volume']) + 10)
def RT(): def DN(self):
# seek forward 10s # volume down 10%
global player_client print("vol down")
print("seek +10s") status = self.player_client.status()
status = player_client.status() if int(status['volume']) > 0:
player_client.seekcur(floor(float(status['elapsed']) + 10.0)) self.player_client.setvol(int(status['volume']) - 10)
def SELECT(): def LF(self):
# display lock/unlock # seek back 10s
display.toggleDisplayOnOff() print("seek -10s")
status = self.player_client.status()
self.player_client.seekcur(floor(float(status['elapsed']) - 10.0))
# This is a dictionary of the combination of pins and labels def RT(self):
LABELLED_BUTTONS = { # seek forward 10s
5: NW, print("seek +10s")
6: SW, status = self.player_client.status()
16: NE, self.player_client.seekcur(floor(float(status['elapsed']) + 10.0))
24: SE,
17: UP,
27: SELECT,
22: LF,
23: DN,
20: RT
}
# "handle_button" will be called every time a button is pressed def SELECT(self):
# It receives one argument: the associated input pin. # display lock/unlock
def handle_button(pin): self.display.toggleDisplayOnOff()
label = LABELS[BUTTONS.index(pin)]
print("Button press detected on pin: {} label: {}".format(pin, label))
func = LABELLED_BUTTONS.get(pin)
func()
# Loop through out buttons and attach the "handle_button" function to each # "handle_button" will be called every time a button is pressed
# We're watching the "FALLING" edge (transition from 3.3V to Ground) and # It receives one argument: the associated input pin.
# picking a generous bouncetime of 150ms to smooth out button presses. def handle_button(self, pin):
for pin in BUTTONS: label = self.LABELS[self.BUTTONS.index(pin)]
GPIO.add_event_detect(pin, GPIO.FALLING, handle_button, bouncetime=150) print("Button press detected on pin: {} label: {}".format(pin, label))
func = self.LABELLED_BUTTONS.get(pin)
func()
# for now, we'll just go ahead and keep kicking things off from here
input_handler = SoundslabInputHandler()
while True: while True:
if display.show_menu is True: if input_handler.display.show_menu is True:
# test menu # test menu
display.updateMenu({ input_handler.display.updateMenu({
"has_previous": False, "has_previous": False,
"has_next": True, "has_next": True,
"selected": 0, "selected": 0,
"rows": ["Turn repeat on", "Turn shuffle on", "View queue >"] "rows": ["Turn repeat on", "Turn shuffle on", "View queue >"]
}) })
else: else:
display.updateMenu() input_handler.display.updateMenu()
# test album art # test album art
display.updateAlbumArt() input_handler.display.updateAlbumArt()
# test setting up the overlay # test setting up the overlay
display.updateOverlay() input_handler.display.updateOverlay()
# test pushing the update to the display # test pushing the update to the display
display.updateDisplay() input_handler.display.updateDisplay()
# wait before continuing # wait before continuing
time.sleep(1.0) time.sleep(0.75)