first pass at turning SoundslabInput into a class
This commit is contained in:
parent
7e42594e13
commit
d0c6cf06e5
|
@ -5,20 +5,22 @@ import time
|
||||||
import musicpd
|
import musicpd
|
||||||
from math import floor
|
from math import floor
|
||||||
|
|
||||||
|
class SoundslabInputHandler:
|
||||||
|
def __init__(self):
|
||||||
# connect to mpd
|
# connect to mpd
|
||||||
player_client = musicpd.MPDClient()
|
self.player_client = musicpd.MPDClient()
|
||||||
player_client.connect()
|
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)
|
||||||
|
@ -27,85 +29,8 @@ GPIO.setmode(GPIO.BCM)
|
||||||
# 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
|
|
||||||
def NE():
|
|
||||||
# play/pause toggle
|
|
||||||
global player_client
|
|
||||||
status = player_client.status()
|
|
||||||
if status['state'] == "play":
|
|
||||||
print("pausing")
|
|
||||||
player_client.pause(1)
|
|
||||||
elif status['state'] == "pause":
|
|
||||||
print("resuming")
|
|
||||||
player_client.pause(0)
|
|
||||||
else: # state == "stop"
|
|
||||||
print("playing")
|
|
||||||
player_client.play()
|
|
||||||
|
|
||||||
def SW():
|
|
||||||
# previous track
|
|
||||||
global player_client
|
|
||||||
status = player_client.status()
|
|
||||||
if float(status['elapsed']) > 5.0:
|
|
||||||
# restart track
|
|
||||||
print("restart current track")
|
|
||||||
player_client.seekcur(0)
|
|
||||||
else:
|
|
||||||
# go to previous track
|
|
||||||
print("previous track")
|
|
||||||
player_client.previous()
|
|
||||||
|
|
||||||
def NW():
|
|
||||||
# open menu
|
|
||||||
global display
|
|
||||||
print("toggling menu")
|
|
||||||
if display.show_menu is True:
|
|
||||||
display.show_menu = False
|
|
||||||
else:
|
|
||||||
display.show_menu = True
|
|
||||||
|
|
||||||
def SE():
|
|
||||||
# next track
|
|
||||||
global player_client
|
|
||||||
print("next track")
|
|
||||||
player_client.next()
|
|
||||||
|
|
||||||
def UP():
|
|
||||||
# volume up 10%
|
|
||||||
global player_client
|
|
||||||
print("vol up")
|
|
||||||
status = player_client.status()
|
|
||||||
if int(status['volume']) < 100:
|
|
||||||
player_client.setvol(int(status['volume']) + 10)
|
|
||||||
|
|
||||||
def DN():
|
|
||||||
# volume down 10%
|
|
||||||
global player_client
|
|
||||||
print("vol down")
|
|
||||||
status = player_client.status()
|
|
||||||
if int(status['volume']) > 0:
|
|
||||||
player_client.setvol(int(status['volume']) - 10)
|
|
||||||
|
|
||||||
def LF():
|
|
||||||
# seek back 10s
|
|
||||||
global player_client
|
|
||||||
print("seek -10s")
|
|
||||||
status = player_client.status()
|
|
||||||
player_client.seekcur(floor(float(status['elapsed']) - 10.0))
|
|
||||||
|
|
||||||
def RT():
|
|
||||||
# seek forward 10s
|
|
||||||
global player_client
|
|
||||||
print("seek +10s")
|
|
||||||
status = player_client.status()
|
|
||||||
player_client.seekcur(floor(float(status['elapsed']) + 10.0))
|
|
||||||
|
|
||||||
def SELECT():
|
|
||||||
# display lock/unlock
|
|
||||||
display.toggleDisplayOnOff()
|
|
||||||
|
|
||||||
# This is a dictionary of the combination of pins and labels
|
# This is a dictionary of the combination of pins and labels
|
||||||
LABELLED_BUTTONS = {
|
self.LABELLED_BUTTONS = {
|
||||||
5: NW,
|
5: NW,
|
||||||
6: SW,
|
6: SW,
|
||||||
16: NE,
|
16: NE,
|
||||||
|
@ -117,37 +42,109 @@ LABELLED_BUTTONS = {
|
||||||
20: RT
|
20: RT
|
||||||
}
|
}
|
||||||
|
|
||||||
# "handle_button" will be called every time a button is pressed
|
|
||||||
# It receives one argument: the associated input pin.
|
|
||||||
def handle_button(pin):
|
|
||||||
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
|
# Loop through out buttons and attach the "handle_button" function to each
|
||||||
# We're watching the "FALLING" edge (transition from 3.3V to Ground) and
|
# We're watching the "FALLING" edge (transition from 3.3V to Ground) and
|
||||||
# picking a generous bouncetime of 150ms to smooth out button presses.
|
# picking a generous bouncetime of 150ms to smooth out button presses.
|
||||||
for pin in BUTTONS:
|
for pin in self.BUTTONS:
|
||||||
GPIO.add_event_detect(pin, GPIO.FALLING, handle_button, bouncetime=150)
|
GPIO.add_event_detect(pin, GPIO.FALLING, self.handle_button, bouncetime=150)
|
||||||
|
|
||||||
|
# some button handlers
|
||||||
|
def NE(self):
|
||||||
|
# play/pause toggle
|
||||||
|
status = self.player_client.status()
|
||||||
|
if status['state'] == "play":
|
||||||
|
print("pausing")
|
||||||
|
player_client.pause(1)
|
||||||
|
elif status['state'] == "pause":
|
||||||
|
print("resuming")
|
||||||
|
player_client.pause(0)
|
||||||
|
else: # state == "stop"
|
||||||
|
print("playing")
|
||||||
|
player_client.play()
|
||||||
|
|
||||||
|
def SW(self):
|
||||||
|
# previous track
|
||||||
|
status = player_client.status()
|
||||||
|
if float(status['elapsed']) > 5.0:
|
||||||
|
# restart track
|
||||||
|
print("restart current track")
|
||||||
|
self.player_client.seekcur(0)
|
||||||
|
else:
|
||||||
|
# go to previous track
|
||||||
|
print("previous track")
|
||||||
|
self.player_client.previous()
|
||||||
|
|
||||||
|
def NW(self):
|
||||||
|
# open menu
|
||||||
|
print("toggling menu")
|
||||||
|
if self.display.show_menu is True:
|
||||||
|
self.display.show_menu = False
|
||||||
|
else:
|
||||||
|
self.display.show_menu = True
|
||||||
|
|
||||||
|
def SE(self):
|
||||||
|
# next track
|
||||||
|
print("next track")
|
||||||
|
self.player_client.next()
|
||||||
|
|
||||||
|
def UP(self):
|
||||||
|
# volume up 10%
|
||||||
|
print("vol up")
|
||||||
|
status = self.player_client.status()
|
||||||
|
if int(status['volume']) < 100:
|
||||||
|
self.player_client.setvol(int(status['volume']) + 10)
|
||||||
|
|
||||||
|
def DN(self):
|
||||||
|
# volume down 10%
|
||||||
|
print("vol down")
|
||||||
|
status = self.player_client.status()
|
||||||
|
if int(status['volume']) > 0:
|
||||||
|
self.player_client.setvol(int(status['volume']) - 10)
|
||||||
|
|
||||||
|
def LF(self):
|
||||||
|
# seek back 10s
|
||||||
|
print("seek -10s")
|
||||||
|
status = self.player_client.status()
|
||||||
|
self.player_client.seekcur(floor(float(status['elapsed']) - 10.0))
|
||||||
|
|
||||||
|
def RT(self):
|
||||||
|
# seek forward 10s
|
||||||
|
print("seek +10s")
|
||||||
|
status = self.player_client.status()
|
||||||
|
self.player_client.seekcur(floor(float(status['elapsed']) + 10.0))
|
||||||
|
|
||||||
|
def SELECT(self):
|
||||||
|
# display lock/unlock
|
||||||
|
self.display.toggleDisplayOnOff()
|
||||||
|
|
||||||
|
# "handle_button" will be called every time a button is pressed
|
||||||
|
# It receives one argument: the associated input pin.
|
||||||
|
def handle_button(self, pin):
|
||||||
|
label = self.LABELS[self.BUTTONS.index(pin)]
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue