From e2413a3c25f376806b07bddf9afa28e993ffafd6 Mon Sep 17 00:00:00 2001 From: Kyle Brown Date: Sat, 2 Mar 2019 15:14:11 -0800 Subject: [PATCH] Still dirty, but moved the problem to a better place. Still unrealistic to hit --- kmk/firmware.py | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/kmk/firmware.py b/kmk/firmware.py index 390fbba..20a057e 100644 --- a/kmk/firmware.py +++ b/kmk/firmware.py @@ -155,22 +155,12 @@ class Firmware: :param update: ''' if update is not None: - # TODO Sort why this is needed when mashing keys on split half - # This is a dirty hack to prevent crashes in unrealistic cases - try: - self._state.matrix_changed( - update[0], - update[1], - update[2], - ) - except IndexError: - # If buffer get's corrupted, reset the master half. - # Alternative would be flush the contents and release all keys - import microcontroller - microcontroller.reset() - def _flush_buffer(self): - self.uart.read() + self._state.matrix_changed( + update[0], + update[1], + update[2], + ) def _send_to_master(self, update): if self.split_master_left: @@ -182,7 +172,13 @@ class Firmware: def _receive_from_slave(self): if self.uart is not None and self.uart.in_waiting > 0: + if self.uart.in_waiting >= 60: + # This is a dirty hack to prevent crashes in unrealistic cases + import microcontroller + microcontroller.reset() + update = bytearray(self.uart.read(3)) + # Built in debug mode switch if update == b'DEB': print(self.uart.readline()) @@ -216,7 +212,7 @@ class Firmware: def init_uart(self, pin, timeout=20): if self._master_half(): - return busio.UART(tx=None, rx=pin, timeout=timeout) + return busio.UART(tx=None, rx=pin, timeout=timeout,) else: return busio.UART(tx=pin, rx=None, timeout=timeout)