remotes/origin/python3
Phil Howard 2019-11-21 18:13:31 +00:00
parent ba32cf7df8
commit 87e0d91201
5 changed files with 18 additions and 17 deletions

View File

@ -29,4 +29,5 @@ class Extension(ext.Extension):
def setup(self, registry): def setup(self, registry):
from .frontend import RaspberryGPIOFrontend from .frontend import RaspberryGPIOFrontend
registry.add("frontend", RaspberryGPIOFrontend) registry.add("frontend", RaspberryGPIOFrontend)

View File

@ -12,6 +12,7 @@ class RaspberryGPIOFrontend(pykka.ThreadingActor, core.CoreListener):
def __init__(self, config, core): def __init__(self, config, core):
super().__init__() super().__init__()
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
self.core = core self.core = core
self.config = config["raspberry-gpio"] self.config = config["raspberry-gpio"]
self.pin_settings = {} self.pin_settings = {}
@ -30,20 +31,18 @@ class RaspberryGPIOFrontend(pykka.ThreadingActor, core.CoreListener):
pull = GPIO.PUD_UP pull = GPIO.PUD_UP
edge = GPIO.FALLING edge = GPIO.FALLING
if settings.active == 'active_high': if settings.active == "active_high":
pull = GPIO.PUD_DOWN pull = GPIO.PUD_DOWN
edge = GPIO.RISING edge = GPIO.RISING
GPIO.setup( GPIO.setup(pin, GPIO.IN, pull_up_down=pull)
pin,
GPIO.IN,
pull_up_down=pull)
GPIO.add_event_detect( GPIO.add_event_detect(
pin, pin,
edge, edge,
callback=self.gpio_event, callback=self.gpio_event,
bouncetime=settings.bouncetime) bouncetime=settings.bouncetime,
)
self.pin_settings[pin] = settings self.pin_settings[pin] = settings

View File

@ -4,8 +4,7 @@ from mopidy import config
class PinConfig(config.ConfigValue): class PinConfig(config.ConfigValue):
tuple_pinconfig = namedtuple("PinConfig", tuple_pinconfig = namedtuple("PinConfig", ("event", "active", "bouncetime"))
("event", "active", "bouncetime"))
valid_events = "play_pause", "prev", "next", "volume_up", "volume_down" valid_events = "play_pause", "prev", "next", "volume_up", "volume_down"
@ -21,7 +20,7 @@ class PinConfig(config.ConfigValue):
value = config.decode(value).strip() value = config.decode(value).strip()
try: try:
event, active, bouncetime = value.split(',') event, active, bouncetime = value.split(",")
except ValueError: except ValueError:
return None return None
@ -52,5 +51,6 @@ class PinConfig(config.ConfigValue):
if value is None: if value is None:
return "" return ""
value = "{:s},{:s},{:d}".format( value = "{:s},{:s},{:d}".format(
value.event, value.active, value.bouncetime) value.event, value.active, value.bouncetime
)
return config.encode(value) return config.encode(value)

View File

@ -19,8 +19,8 @@ dummy_config = {
def test_get_frontend_classes(): def test_get_frontend_classes():
sys.modules['RPi'] = mock.Mock() sys.modules["RPi"] = mock.Mock()
sys.modules['RPi.GPIO'] = mock.Mock() sys.modules["RPi.GPIO"] = mock.Mock()
ext = Extension() ext = Extension()
registry = mock.Mock() registry = mock.Mock()
@ -28,14 +28,15 @@ def test_get_frontend_classes():
ext.setup(registry) ext.setup(registry)
registry.add.assert_called_once_with( registry.add.assert_called_once_with(
'frontend', frontend_lib.RaspberryGPIOFrontend) "frontend", frontend_lib.RaspberryGPIOFrontend
)
def test_frontend_handler_dispatch(): def test_frontend_handler_dispatch():
sys.modules['RPi'] = mock.Mock() sys.modules["RPi"] = mock.Mock()
sys.modules['RPi.GPIO'] = mock.Mock() sys.modules["RPi.GPIO"] = mock.Mock()
frontend = frontend_lib.RaspberryGPIOFrontend(dummy_config, mock.Mock()) frontend = frontend_lib.RaspberryGPIOFrontend(dummy_config, mock.Mock())
with pytest.raises(RuntimeError): with pytest.raises(RuntimeError):
frontend.dispatch_input('tomato') frontend.dispatch_input("tomato")