Main, Program Screen basically done, UI Tweaks, backend fixes, start writing testing library

This commit is contained in:
2025-05-08 18:12:26 +02:00
parent 92836fe427
commit e71f9e6d02
13 changed files with 592 additions and 105 deletions

View File

@@ -1,61 +1,100 @@
import lib.com
from lib.com import Com
import lib.decoder
import time
# TODO: Load filters (for comport search)
com = lib.com.Com()
decoder = lib.decoder.Decoder()
class Instructions:
def set_port_override(self, override: str) -> None:
com.set_port_override(override)
def _hook(self, instruction: str, sequence: list[str]) -> bool:
# Class that supports sending instructions to the microcontroller,
# as well as hooking to data stream according to protocol
class Instructions:
def __init__(self, com: Com) -> None:
self._com = com
# Set a port override (to use a specific COM port)
def set_port_override(self, override: str) -> None:
self._com.set_port_override(override)
# Helper method to hook to the data stream according to protocol.
# You can specify the sequence that the program listens to to sync up,
# as an array of strings, that should each be of length one and only contain
# ascii characters
def hook(self, instruction: str, sequence: list[str]) -> bool:
# Add protection: If we cannot establish connection, refuse to run
if not self._com.connect():
return False
# Send instruction to microcontroller to start hooking process
com.send(instruction)
# If instruction is an empty string, do not send instruction
if instruction != "":
self._com.send(instruction)
# Record start time to respond to timeout
start = time.time()
# Check for timeout
# The pointer below points to the element in the array which is the next expected character to be received
pointer = 0
sequence_max = len(sequence) - 1
# Simply the length of the sequence, since it is both cheaper and cleaner to calculate it once
sequence_max = len(sequence)
# Only run for a limited amount of time
while time.time() - start < 5:
if ( decoder.decode_ascii( com.receive(1) ) ) == sequence[pointer]:
# If the decoded ascii character is equal to the next expected character, move pointer right by one
# If not, jump back to start
if (decoder.decode_ascii(self._com.receive(1))) == sequence[pointer]:
pointer += 1
else:
pointer = 0
# If the pointer has reached the end of the sequence, return True, as now the hook was successful
if pointer == sequence_max:
return True
# If we time out, which is the only way in which this code can be reached, return False
return False
def _change_data(self, instruction: str, readback: list[str], data: list[float], readback_length: int) -> None:
# Private helper method to transmit data using the necessary protocols
def _change_data(
self,
instruction: str,
readback: list[str],
data: list[float],
readback_length: int,
) -> None:
# Hook to stream
if self._hook(instruction, readback):
if self.hook(instruction, readback):
# Transmit data
while len(data) > 0:
if com.receive(readback_length) != '':
com.send_float(data.pop(0))
# If we received data back, we can send more data, i.e. from this we know
# the controller has received the data
# If not, we close the connection and create an exception
if self._com.receive(readback_length) != "":
self._com.send_float(data.pop(0))
else:
com.close()
raise Exception('Failed to transmit data. No response from controller')
com.close()
self._com.close()
raise Exception(
"Failed to transmit data. No response from controller"
)
self._com.close()
else:
com.close()
raise ConnectionError('Failed to hook to controller data stream. No fitting response received')
self._com.close()
raise ConnectionError(
"Failed to hook to controller data stream. No fitting response received"
)
# Abstraction of the _change_data method specifically designed to change the entire config
def change_config(self, new_config: list[float]) -> None:
self._change_data('PR', ['\n', 'P', 'R', '\n'], new_config, 3)
try:
self._change_data("PR", ["\n", "P", "R", "\n"], new_config, 3)
except Exception as e:
raise e
# Abstraction of the _change_data method specifically designed to change only the configured temperature
def change_temperature(self, temperatures: list[float]) -> None:
self._change_data('PT', ['\n', 'P', 'T', '\n'], temperatures, 3)
def enable_fastmode(self) -> None:
com.send('FM')
com.close()
def disable_fastmode(self) -> None:
com.send('NM')
com.close()
try:
self._change_data("PT", ["\n", "P", "T", "\n"], temperatures, 3)
except Exception as e:
raise e