From ec7c31a00e3a3a8e89a74bd4cf4a12698928fe67 Mon Sep 17 00:00:00 2001 From: mchilli Date: Wed, 18 Sep 2024 12:33:31 +0200 Subject: [PATCH] load serial payload directly as json, to prevent memory error --- circuitpython-8.x/code.py | 18 ++++-------------- circuitpython-9.x/code.py | 18 ++++-------------- 2 files changed, 8 insertions(+), 28 deletions(-) diff --git a/circuitpython-8.x/code.py b/circuitpython-8.x/code.py index 503e4e9..86397d2 100644 --- a/circuitpython-8.x/code.py +++ b/circuitpython-8.x/code.py @@ -108,7 +108,6 @@ def __init__(self) -> None: self.readonly = storage.getmount('/').readonly self.serial_data = usb_cdc.data - self.serial_buffer = "" self.serial_last_state = False self.macroStack = [self._init_macros()] @@ -339,19 +338,17 @@ def _update_encoder_macros(self) -> None: {}).get("decreased") ) - def _handle_serial_data(self, payload: str) -> dict: + def _handle_serial_data(self, payload: object) -> dict: """ handle the data comming over the serial connection Args: - payload (str): the data, as json string + payload (object): the data Returns: dict: response, sended over the serial connection """ response = {} try: - payload = json.loads(payload) - if 'command' not in payload.keys(): response['ERR'] = 'Wrong payload: %s' % payload return response @@ -463,15 +460,8 @@ def start(self) -> None: if self.serial_data.connected: if self.serial_data.in_waiting > 0: - while self.serial_data.in_waiting: - chunk = self.serial_data.read( - self.serial_data.in_waiting) - self.serial_buffer += chunk.decode("utf-8") - - if self.serial_buffer.endswith("\n"): - self._send_serial_data( - self._handle_serial_data(self.serial_buffer[:-1])) - self.serial_buffer = "" + self._send_serial_data( + self._handle_serial_data(json.load(self.serial_data))) # get key events, so no inputs will be stored during connection # self.macropad.keys.events.get() diff --git a/circuitpython-9.x/code.py b/circuitpython-9.x/code.py index 503e4e9..86397d2 100644 --- a/circuitpython-9.x/code.py +++ b/circuitpython-9.x/code.py @@ -108,7 +108,6 @@ def __init__(self) -> None: self.readonly = storage.getmount('/').readonly self.serial_data = usb_cdc.data - self.serial_buffer = "" self.serial_last_state = False self.macroStack = [self._init_macros()] @@ -339,19 +338,17 @@ def _update_encoder_macros(self) -> None: {}).get("decreased") ) - def _handle_serial_data(self, payload: str) -> dict: + def _handle_serial_data(self, payload: object) -> dict: """ handle the data comming over the serial connection Args: - payload (str): the data, as json string + payload (object): the data Returns: dict: response, sended over the serial connection """ response = {} try: - payload = json.loads(payload) - if 'command' not in payload.keys(): response['ERR'] = 'Wrong payload: %s' % payload return response @@ -463,15 +460,8 @@ def start(self) -> None: if self.serial_data.connected: if self.serial_data.in_waiting > 0: - while self.serial_data.in_waiting: - chunk = self.serial_data.read( - self.serial_data.in_waiting) - self.serial_buffer += chunk.decode("utf-8") - - if self.serial_buffer.endswith("\n"): - self._send_serial_data( - self._handle_serial_data(self.serial_buffer[:-1])) - self.serial_buffer = "" + self._send_serial_data( + self._handle_serial_data(json.load(self.serial_data))) # get key events, so no inputs will be stored during connection # self.macropad.keys.events.get()