Skip to content

Commit ec7c31a

Browse files
committed
load serial payload directly as json, to prevent memory error
1 parent 92e671d commit ec7c31a

File tree

2 files changed

+8
-28
lines changed

2 files changed

+8
-28
lines changed

circuitpython-8.x/code.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ def __init__(self) -> None:
108108

109109
self.readonly = storage.getmount('/').readonly
110110
self.serial_data = usb_cdc.data
111-
self.serial_buffer = ""
112111
self.serial_last_state = False
113112

114113
self.macroStack = [self._init_macros()]
@@ -339,19 +338,17 @@ def _update_encoder_macros(self) -> None:
339338
{}).get("decreased")
340339
)
341340

342-
def _handle_serial_data(self, payload: str) -> dict:
341+
def _handle_serial_data(self, payload: object) -> dict:
343342
""" handle the data comming over the serial connection
344343
345344
Args:
346-
payload (str): the data, as json string
345+
payload (object): the data
347346
348347
Returns:
349348
dict: response, sended over the serial connection
350349
"""
351350
response = {}
352351
try:
353-
payload = json.loads(payload)
354-
355352
if 'command' not in payload.keys():
356353
response['ERR'] = 'Wrong payload: %s' % payload
357354
return response
@@ -463,15 +460,8 @@ def start(self) -> None:
463460

464461
if self.serial_data.connected:
465462
if self.serial_data.in_waiting > 0:
466-
while self.serial_data.in_waiting:
467-
chunk = self.serial_data.read(
468-
self.serial_data.in_waiting)
469-
self.serial_buffer += chunk.decode("utf-8")
470-
471-
if self.serial_buffer.endswith("\n"):
472-
self._send_serial_data(
473-
self._handle_serial_data(self.serial_buffer[:-1]))
474-
self.serial_buffer = ""
463+
self._send_serial_data(
464+
self._handle_serial_data(json.load(self.serial_data)))
475465

476466
# get key events, so no inputs will be stored during connection
477467
# self.macropad.keys.events.get()

circuitpython-9.x/code.py

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ def __init__(self) -> None:
108108

109109
self.readonly = storage.getmount('/').readonly
110110
self.serial_data = usb_cdc.data
111-
self.serial_buffer = ""
112111
self.serial_last_state = False
113112

114113
self.macroStack = [self._init_macros()]
@@ -339,19 +338,17 @@ def _update_encoder_macros(self) -> None:
339338
{}).get("decreased")
340339
)
341340

342-
def _handle_serial_data(self, payload: str) -> dict:
341+
def _handle_serial_data(self, payload: object) -> dict:
343342
""" handle the data comming over the serial connection
344343
345344
Args:
346-
payload (str): the data, as json string
345+
payload (object): the data
347346
348347
Returns:
349348
dict: response, sended over the serial connection
350349
"""
351350
response = {}
352351
try:
353-
payload = json.loads(payload)
354-
355352
if 'command' not in payload.keys():
356353
response['ERR'] = 'Wrong payload: %s' % payload
357354
return response
@@ -463,15 +460,8 @@ def start(self) -> None:
463460

464461
if self.serial_data.connected:
465462
if self.serial_data.in_waiting > 0:
466-
while self.serial_data.in_waiting:
467-
chunk = self.serial_data.read(
468-
self.serial_data.in_waiting)
469-
self.serial_buffer += chunk.decode("utf-8")
470-
471-
if self.serial_buffer.endswith("\n"):
472-
self._send_serial_data(
473-
self._handle_serial_data(self.serial_buffer[:-1]))
474-
self.serial_buffer = ""
463+
self._send_serial_data(
464+
self._handle_serial_data(json.load(self.serial_data)))
475465

476466
# get key events, so no inputs will be stored during connection
477467
# self.macropad.keys.events.get()

0 commit comments

Comments
 (0)