Skip to content

Commit 460e9e6

Browse files
authored
class level memoryview for _cmd buf
1 parent fbf7e12 commit 460e9e6

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

micropython/lora/lora-sx126x/lora/sx126x.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,8 @@ def __init__(
152152
dio3_tcxo_start_time_us if dio3_tcxo_millivolts else 0
153153
)
154154

155-
self._buf = bytearray(9) # shared buffer for commands
155+
self._buf = bytearray(9) # shared buffer for commands, access through _buf_view
156+
self._buf_view = memoryview(self._buf)
156157

157158
# These settings are kept in the object (as can't read them back from the modem)
158159
self._output_power = 14
@@ -704,11 +705,11 @@ def _cmd(self, fmt, *write_args, n_read=0, write_buf=None, read_buf=None):
704705
# have happened well before _cmd() is called again.
705706
self._wait_not_busy(self._busy_timeout)
706707

707-
# Pack write_args into _buf and wrap a memoryview of the correct length around it
708+
# Pack write_args into slice of _buf_view memoryview of correct length
708709
wrlen = struct.calcsize(fmt)
709-
assert n_read + wrlen <= len(self._buf) # if this fails, make _buf bigger!
710-
struct.pack_into(fmt, self._buf, 0, *write_args)
711-
buf = memoryview(self._buf)[: (wrlen + n_read)]
710+
assert n_read + wrlen <= len(self._buf_view) # if this fails, make _buf bigger!
711+
struct.pack_into(fmt, self._buf_view, 0, *write_args)
712+
buf = self._buf_view[: (wrlen + n_read)]
712713

713714
if _DEBUG:
714715
print(">>> {}".format(buf[:wrlen].hex()))
@@ -723,7 +724,7 @@ def _cmd(self, fmt, *write_args, n_read=0, write_buf=None, read_buf=None):
723724
self._cs(1)
724725

725726
if n_read > 0:
726-
res = memoryview(buf)[wrlen : (wrlen + n_read)] # noqa: E203
727+
res = self._buf_view[wrlen : (wrlen + n_read)] # noqa: E203
727728
if _DEBUG:
728729
print("<<< {}".format(res.hex()))
729730
return res

0 commit comments

Comments
 (0)