From 2a47178499c434f371f8c034d8624692d550aab1 Mon Sep 17 00:00:00 2001 From: Alexander Braml Date: Sun, 10 Dec 2023 06:54:03 +0100 Subject: [PATCH] Refactor UARTSwitchMatrix to extend abstract SwitchMatrix --- .idea/.gitignore | 8 - .idea/inspectionProfiles/Project_Default.xml | 207 ------------------ .idea/misc.xml | 2 +- .../device_base/DeviceConfigs.py | 1 + .../devices/UARTSwitchMatrix.py | 65 +++--- 5 files changed, 31 insertions(+), 252 deletions(-) delete mode 100644 .idea/.gitignore delete mode 100644 .idea/inspectionProfiles/Project_Default.xml diff --git a/.idea/.gitignore b/.idea/.gitignore deleted file mode 100644 index 13566b8..0000000 --- a/.idea/.gitignore +++ /dev/null @@ -1,8 +0,0 @@ -# Default ignored files -/shelf/ -/workspace.xml -# Editor-based HTTP Client requests -/httpRequests/ -# Datasource local storage ignored files -/dataSources/ -/dataSources.local.xml diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml deleted file mode 100644 index 5c52ceb..0000000 --- a/.idea/inspectionProfiles/Project_Default.xml +++ /dev/null @@ -1,207 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 4f3d149..4b6dcd3 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,7 +1,7 @@ - \ No newline at end of file diff --git a/py_instrument_control_lib/device_base/DeviceConfigs.py b/py_instrument_control_lib/device_base/DeviceConfigs.py index a73c08c..a8d87a9 100644 --- a/py_instrument_control_lib/device_base/DeviceConfigs.py +++ b/py_instrument_control_lib/device_base/DeviceConfigs.py @@ -19,3 +19,4 @@ class SerialDeviceConfig(DeviceConfig): interface: str baud_rate: int = 9600 timeout: int = 2 + number_of_connection_attempts: int = 3 diff --git a/py_instrument_control_lib/devices/UARTSwitchMatrix.py b/py_instrument_control_lib/devices/UARTSwitchMatrix.py index 7ad9a22..bc701f3 100644 --- a/py_instrument_control_lib/devices/UARTSwitchMatrix.py +++ b/py_instrument_control_lib/devices/UARTSwitchMatrix.py @@ -2,11 +2,12 @@ import serial -from py_instrument_control_lib.device_base.Device import Device from py_instrument_control_lib.device_base.DeviceConfigs import SerialDeviceConfig +from py_instrument_control_lib.device_types.AbstractSwitchMatrix import AbstractSwitchMatrix -class UARTSwitchMatrix(Device): +class UARTSwitchMatrix(AbstractSwitchMatrix): + _config: SerialDeviceConfig def __init__(self, config: SerialDeviceConfig): super().__init__(config) @@ -16,61 +17,55 @@ def connect(self) -> None: self.serial_port.flush() self.__connect_to_arduino() - def set_row_col(self, row: int, col: int) -> None: - """ - Send r: and c: to the Arduino nano - Args: - :param row: row to set, valid values are 0 - 12 - :param col: column to set, valid values are 0 - 12 - """ - self.set_row(row) - self.set_column(col) - - def set_row(self, row_idx, timeout_in_secs=2): + def set_row(self, row): """ Sends r: to the Arduino nano Args: - row_idx: row to set valid values are 0 - 12. 12 means no row is selected. - timeout_in_secs: Timeout in seconds to wait for the acknowledgement. + row: row to set valid values are 0 - 12. 12 means no row is selected. """ - self.execute(f'r: {row_idx}\n') - return self.__wait_for_response(timeout_in_secs) + self.execute(f'r: {row}\n') + return self.__wait_for_response() - def set_column(self, column_idx, timeout_in_secs=2): + def set_col(self, col): """ Sends c: to the Arduino nano Args: - column_idx: column to set valid values are 0 - 12. 12 means no column is selected. - timeout_in_secs: Timeout in seconds to wait for the acknowledgement. + col: column to set valid values are 0 - 12. 12 means no column is selected. """ - self.execute(f'c: {column_idx}\n') - return self.__wait_for_response(timeout_in_secs) + self.execute(f'c: {col}\n') + return self.__wait_for_response() - def __connect_to_arduino(self, number_of_attempts=3): + def set_row_col(self, row: int, col: int) -> None: """ - Sends a connection request to the Arduino Nano as long as the connection is established. + Send r: and c: to the Arduino nano Args: - :param number_of_attempts + :param row: row to set, valid values are 0 - 12 + :param col: column to set, valid values are 0 - 12 + """ + self.set_row(row) + self.set_col(col) + + def __connect_to_arduino(self): + """ + Sends a connection request to the Arduino Nano as long as the connection is established. """ - for i in range(number_of_attempts): + for i in range(self._config.number_of_connection_attempts): if self.__send_connect_message(): return True else: print("Timeout when trying to connect") - def __send_connect_message(self, timeout_in_secs=2): + def __send_connect_message(self): """ Sends the connect message to the Arduino Nano. - Args: - timeout_in_secs: Timeout in seconds to wait for the acknowledgement. """ self.execute('connect\n') - return self.__wait_for_response(timeout_in_secs) + return self.__wait_for_response() - def __wait_for_response(self, timeout_in_secs): + def __wait_for_response(self): time.sleep(0.4) start_ts = time.time() - while time.time() < start_ts + timeout_in_secs: + while time.time() < start_ts + self._config.timeout: ack = self.serial_port.readline() ack = ack.decode('ascii') if 'ack' in ack: @@ -90,12 +85,10 @@ def query(self, query: str) -> str: def disconnect(self) -> None: self.__send_disconnect_message() - def __send_disconnect_message(self, timeout_in_secs=2): + def __send_disconnect_message(self): """ Sends the disconnect message to the Arduino Nano. - Args: - timeout_in_secs: Timeout in seconds to wait for the acknowledgement. """ self.execute('disconnect\n') - self.__wait_for_response(timeout_in_secs) + self.__wait_for_response() self.serial_port.close()