Make it work

remotes/origin/extra-event-options
Phil Howard 2019-10-10 13:49:20 +01:00
parent 2dc1e75d32
commit d64f7ba650
2 changed files with 27 additions and 13 deletions

View File

@ -13,6 +13,7 @@ logger = logging.getLogger(__name__)
class RaspberryGPIOFrontend(pykka.ThreadingActor, core.CoreListener):
def __init__(self, config, core):
super(RaspberryGPIOFrontend, self).__init__()
import RPi.GPIO as GPIO
self.core = core
self.config = config["raspberry-gpio"]
@ -26,13 +27,16 @@ class RaspberryGPIOFrontend(pykka.ThreadingActor, core.CoreListener):
for key in self.config:
if key.startswith("bcm"):
pin = int(key.replace("bcm", ""))
self.pin_settings[pin] = PinConfig().deserialize(
self.config[key]
)
# settings = PinConfig().deserialize(
# self.config[key]
# )
settings = self.config[key]
if settings is None:
continue
pull = GPIO.PUD_UP
edge = GPIO.FALLING
if self.pin_settings[pin].active == 'active_high':
if settings.active == 'active_high':
pull = GPIO.PUD_DOWN
edge = GPIO.RISING
@ -45,7 +49,9 @@ class RaspberryGPIOFrontend(pykka.ThreadingActor, core.CoreListener):
pin,
edge,
callback=self.gpio_event,
bouncetime=self.pin_settings[pin].bouncetime)
bouncetime=settings.bouncetime)
self.pin_settings[pin] = settings
def gpio_event(self, pin):
settings = self.pin_settings[pin]
@ -54,7 +60,7 @@ class RaspberryGPIOFrontend(pykka.ThreadingActor, core.CoreListener):
def dispatch_input(self, event):
handler_name = "handle_{}".format(event)
try:
getattr(self, handler_name)(self)
getattr(self, handler_name)()
except AttributeError:
raise RuntimeError(
"Could not find input handler for event: {}".format(event)

View File

@ -3,7 +3,7 @@ from collections import namedtuple
from mopidy import config
class PinConfig(config.String):
class PinConfig(config.ConfigValue):
tuple_pinconfig = namedtuple("PinConfig",
("event", "active", "bouncetime"))
@ -12,11 +12,18 @@ class PinConfig(config.String):
valid_modes = "active_low", "active_high"
def __init__(self):
config.String.__init__(self, optional=True)
pass
def deserialize(self, value):
value = config.String.deserialize(self, value)
event, active, bouncetime = value.split(',')
if value is None:
return None
value = config.decode(value).strip()
try:
event, active, bouncetime = value.split(',')
except ValueError:
return None
if event not in self.valid_events:
raise ValueError(
@ -42,7 +49,8 @@ class PinConfig(config.String):
return self.tuple_pinconfig(event, active, bouncetime)
def serialize(self, value, display=False):
if value is None:
return ""
value = "{:s},{:s},{:d}".format(
value.event, value.active, value.bouncetime)
value = config.String.serialize(self, value, display)
return value
return config.encode(value)