From 7ceb37700d1668b48dac2d95dba0c5eb23a3ec91 Mon Sep 17 00:00:00 2001 From: Ilya Zhuravlev Date: Mon, 10 May 2021 13:56:55 -0400 Subject: [PATCH] matrix_test: don't crash when switching to another device --- src/main/python/matrix_test.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/main/python/matrix_test.py b/src/main/python/matrix_test.py index 2ac22f7..433c101 100644 --- a/src/main/python/matrix_test.py +++ b/src/main/python/matrix_test.py @@ -62,6 +62,10 @@ class MatrixTest(BasicEditor): self.keyboardWidget.updateGeometry() def matrix_poller(self): + if not self.valid(): + self.stop() + return + # Get size for matrix rows = self.keyboard.rows cols = self.keyboard.cols @@ -69,7 +73,12 @@ class MatrixTest(BasicEditor): matrix = [[None] * cols for x in range(rows)] # Get matrix data from keyboard - data = self.keyboard.matrix_poll() + try: + data = self.keyboard.matrix_poll() + except (RuntimeError, ValueError): + self.stop() + return + # Calculate the amount of bytes belong to 1 row, each bit is 1 key, so per 8 keys in a row, # a byte is needed for the row. row_size = math.ceil(cols / 8) @@ -112,7 +121,11 @@ class MatrixTest(BasicEditor): def stop(self): self.timer.stop() - self.keyboard.lock() + try: + self.keyboard.lock() + except (RuntimeError, ValueError): + # if keyboard disappeared, we can't relock it + pass self.startButtonWidget.setText("Start testing") self.polling = False