diff options
author | Devaev Maxim <[email protected]> | 2020-09-14 22:59:24 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2020-09-14 22:59:24 +0300 |
commit | 123406b2b2012e84c7e30cfa999ee921d05a7c56 (patch) | |
tree | 2573826d196337c424b17274753c71352fa797ce /kvmd/aiogp.py | |
parent | 6ef53e48c56af680e6efbfd1002a2bb144d2043f (diff) |
workaround for possible driver bug
Diffstat (limited to 'kvmd/aiogp.py')
-rw-r--r-- | kvmd/aiogp.py | 36 |
1 files changed, 21 insertions, 15 deletions
diff --git a/kvmd/aiogp.py b/kvmd/aiogp.py index 7dfe4fa9..3b50aaac 100644 --- a/kvmd/aiogp.py +++ b/kvmd/aiogp.py @@ -54,7 +54,7 @@ class AioPinsReader: # pylint: disable=too-many-instance-attributes self, path: str, consumer: str, - pins: Dict[int, bool], + pins: Dict[int, bool], # (pin, inverted) notifier: aiotools.AioNotifier, ) -> None: @@ -63,15 +63,15 @@ class AioPinsReader: # pylint: disable=too-many-instance-attributes self.__pins = pins self.__notifier = notifier - self.__state = dict.fromkeys(pins, False) + self.__state = dict.fromkeys(pins, 0) - self.__stop_event = threading.Event() self.__loop: Optional[asyncio.AbstractEventLoop] = None self.__thread = threading.Thread(target=self.__run, daemon=True) + self.__stop_event = threading.Event() def get(self, pin: int) -> bool: - return (self.__state[pin] ^ self.__pins[pin]) + return (bool(self.__state[pin]) ^ self.__pins[pin]) async def poll(self) -> None: if not self.__pins: @@ -92,17 +92,17 @@ class AioPinsReader: # pylint: disable=too-many-instance-attributes lines = chip.get_lines(pins) lines.request(self.__consumer, gpiod.LINE_REQ_EV_BOTH_EDGES) + def read_state() -> Dict[int, int]: + return dict(zip(pins, lines.get_values())) + lines.event_wait(nsec=1) - self.__state = { - pin: bool(value) - for (pin, value) in zip(pins, lines.get_values()) - } + self.__state = read_state() self.__notify() while not self.__stop_event.is_set(): + changed = False ev_lines = lines.event_wait(1) if ev_lines: - changed = False for ev_line in ev_lines: events = ev_line.event_read_multiply() if events: @@ -110,15 +110,21 @@ class AioPinsReader: # pylint: disable=too-many-instance-attributes if self.__state[pin] != value: self.__state[pin] = value changed = True - if changed: - self.__notify() - - def __parse_event(self, event: gpiod.LineEvent) -> Tuple[int, bool]: + else: # Timeout + # Ensure state to avoid driver bugs + state = read_state() + if self.__state != state: + self.__state = state + changed = True + if changed: + self.__notify() + + def __parse_event(self, event: gpiod.LineEvent) -> Tuple[int, int]: pin = event.source.offset() if event.type == gpiod.LineEvent.RISING_EDGE: - return (pin, True) + return (pin, 1) elif event.type == gpiod.LineEvent.FALLING_EDGE: - return (pin, False) + return (pin, 0) raise RuntimeError(f"Invalid event {event} type: {event.type}") def __notify(self) -> None: |