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):
from .frontend import RaspberryGPIOFrontend
registry.add("frontend", RaspberryGPIOFrontend)

View File

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

View File

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

View File

@ -1,3 +1,3 @@
from setuptools import setup
setup()
setup()

View File

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