Skip to content

Commit 60d1370

Browse files
mahollidpgeorge
authored andcommitted
lora-sx126x: Change to class-level memoryview for _cmd buf.
Currently, the LoRa SX126x driver dynamically creates at least one, sometimes two, memoryview objects with each call to `_cmd`. This commit simply provides the class with a long-lived memoryview object for `_cmd` to easily slice as necessary. Unlike the SX127x chips, Semtech unfortunately designed the SX126x modems to be more command-centric (as opposed to directly setting registers). Given the amount `_cmd` is called during normal device operation, even a minor improvement here should have a decent impact. Basic TX and RX tests pass on hardware. Signed-off-by: Max Holliday <[email protected]>
1 parent fbf7e12 commit 60d1370

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ 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_view = memoryview(bytearray(9)) # shared buffer for commands
156156

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

707-
# Pack write_args into _buf and wrap a memoryview of the correct length around it
707+
# Pack write_args into slice of _buf_view memoryview of correct length
708708
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)]
709+
assert n_read + wrlen <= len(self._buf_view) # if this fails, make _buf bigger!
710+
struct.pack_into(fmt, self._buf_view, 0, *write_args)
711+
buf = self._buf_view[: (wrlen + n_read)]
712712

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

725725
if n_read > 0:
726-
res = memoryview(buf)[wrlen : (wrlen + n_read)] # noqa: E203
726+
res = self._buf_view[wrlen : (wrlen + n_read)] # noqa: E203
727727
if _DEBUG:
728728
print("<<< {}".format(res.hex()))
729729
return res
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
metadata(version="0.1.2")
1+
metadata(version="0.1.3")
22
require("lora")
33
package("lora")

0 commit comments

Comments
 (0)