Skip to content

Commit ce95c6e

Browse files
icyqwqlbuque
authored andcommitted
libs/module: Optmize send method.
Signed-off-by: icyqwq <[email protected]>
1 parent 096382b commit ce95c6e

File tree

3 files changed

+85
-10
lines changed

3 files changed

+85
-10
lines changed

docs/en/module/lora.rst

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
LoraModule
33
==========
44

5-
.. include:: ../refs/module.loramodule.ref
5+
.. include:: ../refs/module.lora.ref
66

77
The LoRa433_V1.1 Module is part of the M5Stack stackable module series. It is a LoRa communication module that operates at a 433MHz frequency and utilizes the Ra-02 module (SX1278 chip) solution.
88

@@ -16,10 +16,19 @@ Micropython Example::
1616
import M5
1717
from M5 import *
1818
from module import LoraModule
19-
lora = LoraModule()
19+
lora = LoraModule(pin_irq=35, pin_rst=13) # basic
20+
lora = LoraModule(pin_irq=35, pin_rst=25) # core2
21+
lora = LoraModule(pin_irq=10, pin_rst=5) # cores3
2022
lora.send("Hello, LoRa!")
23+
2124
print(lora.recv())
2225
26+
def callback(received_data):
27+
global lora
28+
print(received_data)
29+
lora.start_recv()
30+
lora.set_irq_callback(callback)
31+
lora.start_recv()
2332

2433
UIFLOW2 Example:
2534

@@ -35,7 +44,7 @@ Constructors
3544

3645
.. class:: LoraModule(pin_cs, pin_irq, pin_rst, freq_band, sf, bw, coding_rate, preamble_len, output_power)
3746

38-
Initialize the LoRa module.
47+
Initialize the LoRa module.
3948

4049
:param int pin_cs: Chip select pin
4150
:param int pin_irq: Interrupt pin
@@ -59,6 +68,8 @@ Methods
5968

6069
Send a data packet.
6170

71+
:return (int): The return value is the timestamp when transmission completed, as a&#x60;time.ticks_ms()&#x60; result. It will be more accurate if the modem was initialized to use interrupts.
72+
6273
:param packet: The data packet to send.
6374
:param tx_at_ms: Time to transmit the packet in milliseconds. For precise timing of sent packets, there is an optional &#x60;tx_at_ms&#x60; argument which is a timestamp (as a &#x60;time.ticks_ms()&#x60; value). If set, the packet will be sent as close as possible to this timestamp and the function will block until that time arrives
6475

@@ -70,6 +81,8 @@ Methods
7081

7182
Receive a data packet.
7283

84+
:return (RxPacket): Returns None on timeout, or an &#x60;RxPacket&#x60; instance with the packet on success.
85+
7386
:param timeout_ms: Optional, sets a receive timeout in milliseconds. If None (default value), then the function will block indefinitely until a packet is received.
7487
:param int rx_length: Necessary to set if &#x60;implicit_header&#x60; is set to &#x60;True&#x60; (see above). This is the length of the packet to receive. Ignored in the default LoRa explicit header mode, where the received radio header includes the length.
7588
:param RxPacket rx_packet: Optional, this can be an &#x60;RxPacket&#x60; object previously received from the modem. If the newly received packet has the same length, this object is reused and returned to save an allocation. If the newly received packet has a different length, a new &#x60;RxPacket&#x60; object is allocated and returned instead.
@@ -78,6 +91,57 @@ Methods
7891

7992
|recv.svg|
8093

94+
.. method:: LoraModule.start_recv()
95+
96+
Start receiving data once, trigger an interrupt when data is received.
97+
98+
99+
100+
UIFLOW2:
101+
102+
|start_recv.svg|
103+
104+
.. method:: LoraModule.set_irq_callback(callback)
105+
106+
Set the IRQ callback function.
107+
108+
109+
:param callback: The callback function. The function should accept one argument, which is the received data.
110+
111+
UIFLOW2:
112+
113+
|set_irq_callback.svg|
114+
115+
.. method:: LoraModule.standby()
116+
117+
Set the modem to standby mode.
118+
119+
120+
121+
UIFLOW2:
122+
123+
|standby.svg|
124+
125+
.. method:: LoraModule.sleep()
126+
127+
Set the modem to sleep mode.
128+
129+
130+
131+
UIFLOW2:
132+
133+
|sleep.svg|
134+
135+
.. method:: LoraModule.irq_triggered()
136+
137+
Check if the IRQ has been triggered.
138+
139+
140+
141+
UIFLOW2:
142+
143+
|irq_triggered.svg|
144+
81145

82146

83147
Constants
@@ -88,9 +152,9 @@ Constants
88152

89153
Select the LoRa frequency band.
90154

91-
155+
92156
.. data:: LoraModule.BANDWIDTHS
93157

94158
Valid bandwidth
95159

96-
160+

docs/en/refs/module.lora.ref

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44
:height: 200px
55
:width: 200px
66

7-
.. |init.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/loramodule/init.svg
8-
.. |_validate_range.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/loramodule/_validate_range.svg
9-
.. |send.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/loramodule/send.svg
10-
.. |recv.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/loramodule/recv.svg
7+
.. |init.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/lora/init.svg
8+
.. |send.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/lora/send.svg
9+
.. |recv.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/lora/recv.svg
10+
.. |start_recv.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/lora/start_recv.svg
11+
.. |set_irq_callback.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/lora/set_irq_callback.svg
12+
.. |standby.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/lora/standby.svg
13+
.. |sleep.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/lora/sleep.svg
14+
.. |irq_triggered.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/lora/irq_triggered.svg
1115

12-
.. |example.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/loramodule/example.svg
16+
.. |example.svg| image:: https://static-cdn.m5stack.com/mpy_docs/module/lora/example.svg
1317

m5stack/libs/module/lora.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ def send(self, packet, tx_at_ms=None) -> int:
140140
return:
141141
note: The return value is the timestamp when transmission completed, as a`time.ticks_ms()` result. It will be more accurate if the modem was initialized to use interrupts.
142142
"""
143+
if isinstance(packet, str):
144+
packet = bytes(packet, "utf-8")
145+
elif isinstance(packet, list | tuple):
146+
packet = bytes(packet)
147+
elif isinstance(packet, int):
148+
packet = bytes([packet])
149+
143150
return self.modem.send(packet, tx_at_ms)
144151

145152
def recv(self, timeout_ms=None, rx_length=0xFF, rx_packet: RxPacket = None) -> RxPacket:

0 commit comments

Comments
 (0)