Added rotenc tests in frontend.

rotencoder
Martin Felis 2021-03-13 16:38:15 +01:00
parent 3f0a4a33a5
commit 97592cb790
5 changed files with 46 additions and 17 deletions

View File

@ -3,6 +3,8 @@ import logging
import pykka
from mopidy import core
from .rotencoder import RotEncoder
logger = logging.getLogger(__name__)
class RaspberryGPIOFrontend(pykka.ThreadingActor, core.CoreListener):
@ -27,7 +29,6 @@ class RaspberryGPIOFrontend(pykka.ThreadingActor, core.CoreListener):
if settings is None:
continue
pull = GPIO.PUD_UP
edge = GPIO.FALLING
if settings.active == "active_high":
@ -36,15 +37,14 @@ class RaspberryGPIOFrontend(pykka.ThreadingActor, core.CoreListener):
if 'rotenc_id' in settings.options:
edge = GPIO.BOTH
rotenc_id = self.options['rotenc_id']
rotenc_id = settings.options['rotenc_id']
encoder = None
if rotenc_id in self.rot_encoders:
if rotenc_id in self.rot_encoders.keys():
encoder = self.rot_encoders[rotenc_id]
else:
encoder = RotEncoder(rotenc_id)
self.rot_encoders[rotenc_id] = encoder
encoder.add_pin(pin, settings.event)
settings.rot_encoder = encoder
GPIO.setup(pin, GPIO.IN, pull_up_down=pull)
@ -59,11 +59,17 @@ class RaspberryGPIOFrontend(pykka.ThreadingActor, core.CoreListener):
# TODO validate all self.rot_encoders have two pins
def find_pin_rotenc(self, pin):
for encoder in self.rot_encoders.values():
if pin in encoder.pins:
return encoder
def gpio_event(self, pin):
settings = self.pin_settings[pin]
event = settings.event
if settings.rot_encoder:
event = settings.rot_encoder.get_event()
encoder = self.find_pin_rotenc (pin)
if encoder:
event = encoder.get_event()
if event:
self.dispatch_input(event, settings.options)

View File

@ -13,7 +13,7 @@ class ValidList(list):
class PinConfig(config.ConfigValue):
tuple_pinconfig = namedtuple(
"PinConfig", ("event", "active", "bouncetime", "options", "rot_encoder")
"PinConfig", ("event", "active", "bouncetime", "options")
)
valid_events = ValidList(
@ -62,7 +62,7 @@ class PinConfig(config.ConfigValue):
key, value = option.split("=")
options[key] = value
return self.tuple_pinconfig(event, active, bouncetime, options, None)
return self.tuple_pinconfig(event, active, bouncetime, options)
def serialize(self, value, display=False):
if value is None:

View File

@ -7,6 +7,7 @@ class RotEncoder:
self.id = rot_id
self.pins = []
self.events = []
self.state = (None, None)
self.state_map = {
((False, False), (False,True)): 0,
((False, False), (True,False)): 1,

View File

@ -18,6 +18,8 @@ dummy_config = {
"bcm1": deserialize("play_pause,active_low,30"),
"bcm2": deserialize("volume_up,active_high,30"),
"bcm3": deserialize("volume_down,active_high,30"),
"bcm4": deserialize("volume_down,active_high,250,rotenc_id=vol"),
"bcm5": deserialize("volume_up,active_high,250,rotenc_id=vol"),
}
}
@ -196,3 +198,31 @@ def test_frontend_gpio_event():
frontend.gpio_event(3)
stop_mopidy_core()
@mock.patch("RPi.GPIO.input")
def test_frontend_rot_encoder_event(patched_input):
patched_input.return_value = False
frontend = frontend_lib.RaspberryGPIOFrontend(
dummy_config, dummy_mopidy_core()
)
# Check that transition (False, True) -> (False, False) triggers volume_up
encoder = frontend.rot_encoders["vol"]
encoder.state = (False, True)
dispatch_input = mock.Mock()
frontend.dispatch_input = dispatch_input
frontend.gpio_event(4)
assert (dispatch_input.call_args[0][0] == "volume_up")
assert (encoder.state == (False, False))
# Check that we do not submit an event for the invalid transition
# (False, False) -> (False, False)
dispatch_input.reset_mock()
frontend.gpio_event(4)
dispatch_input.assert_not_called()
stop_mopidy_core()

View File

@ -5,14 +5,6 @@ import unittest
from mopidy_raspberry_gpio import RotEncoder
from unittest.mock import patch, MagicMock
MockRPi = MagicMock()
modules = {
"RPi": MockRPi,
"RPi.GPIO": MockRPi.GPIO
}
patcher = patch.dict("sys.modules", modules)
patcher.start()
class RotEncoderTests(unittest.TestCase):
def test_rotenc_init(self):
@ -35,7 +27,7 @@ class RotEncoderTests(unittest.TestCase):
rot_enc = RotEncoder("vol")
rot_enc.add_pin(123, "vol_up")
rot_enc.add_pin(124, "vol_down")
with self.assertRaises(RuntimeError) as cm:
rot_enc.add_pin(124, "vol_down")