Skip to content

Commit

Permalink
load serial payload directly as json, to prevent memory error
Browse files Browse the repository at this point in the history
  • Loading branch information
mchilli committed Sep 18, 2024
1 parent 92e671d commit ec7c31a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 28 deletions.
18 changes: 4 additions & 14 deletions circuitpython-8.x/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
18 changes: 4 additions & 14 deletions circuitpython-9.x/code.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit ec7c31a

Please sign in to comment.