workaround for possible driver bug

This commit is contained in:
Devaev Maxim 2020-09-14 22:59:24 +03:00
parent 6ef53e48c5
commit 123406b2b2

View File

@ -54,7 +54,7 @@ class AioPinsReader: # pylint: disable=too-many-instance-attributes
self, self,
path: str, path: str,
consumer: str, consumer: str,
pins: Dict[int, bool], pins: Dict[int, bool], # (pin, inverted)
notifier: aiotools.AioNotifier, notifier: aiotools.AioNotifier,
) -> None: ) -> None:
@ -63,15 +63,15 @@ class AioPinsReader: # pylint: disable=too-many-instance-attributes
self.__pins = pins self.__pins = pins
self.__notifier = notifier 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.__loop: Optional[asyncio.AbstractEventLoop] = None
self.__thread = threading.Thread(target=self.__run, daemon=True) self.__thread = threading.Thread(target=self.__run, daemon=True)
self.__stop_event = threading.Event()
def get(self, pin: int) -> bool: 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: async def poll(self) -> None:
if not self.__pins: if not self.__pins:
@ -92,17 +92,17 @@ class AioPinsReader: # pylint: disable=too-many-instance-attributes
lines = chip.get_lines(pins) lines = chip.get_lines(pins)
lines.request(self.__consumer, gpiod.LINE_REQ_EV_BOTH_EDGES) 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) lines.event_wait(nsec=1)
self.__state = { self.__state = read_state()
pin: bool(value)
for (pin, value) in zip(pins, lines.get_values())
}
self.__notify() self.__notify()
while not self.__stop_event.is_set(): while not self.__stop_event.is_set():
changed = False
ev_lines = lines.event_wait(1) ev_lines = lines.event_wait(1)
if ev_lines: if ev_lines:
changed = False
for ev_line in ev_lines: for ev_line in ev_lines:
events = ev_line.event_read_multiply() events = ev_line.event_read_multiply()
if events: if events:
@ -110,15 +110,21 @@ class AioPinsReader: # pylint: disable=too-many-instance-attributes
if self.__state[pin] != value: if self.__state[pin] != value:
self.__state[pin] = value self.__state[pin] = value
changed = True changed = True
if changed: else: # Timeout
self.__notify() # 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, bool]: def __parse_event(self, event: gpiod.LineEvent) -> Tuple[int, int]:
pin = event.source.offset() pin = event.source.offset()
if event.type == gpiod.LineEvent.RISING_EDGE: if event.type == gpiod.LineEvent.RISING_EDGE:
return (pin, True) return (pin, 1)
elif event.type == gpiod.LineEvent.FALLING_EDGE: elif event.type == gpiod.LineEvent.FALLING_EDGE:
return (pin, False) return (pin, 0)
raise RuntimeError(f"Invalid event {event} type: {event.type}") raise RuntimeError(f"Invalid event {event} type: {event.type}")
def __notify(self) -> None: def __notify(self) -> None: