Skip to content

Commit 096382b

Browse files
icyqwqlbuque
authored andcommitted
lib/module: Support callback for LoRa.
Signed-off-by: icyqwq <[email protected]>
1 parent a7725ec commit 096382b

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

m5stack/libs/module/lora.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from lora import SX1278
99
from lora import SX1276
1010
from lora import RxPacket
11-
from micropython import const
11+
from micropython import const, schedule
1212

1313

1414
class LoraModule:
@@ -25,10 +25,20 @@ class LoraModule:
2525
2626
example: |
2727
from module import LoraModule
28-
lora = LoraModule()
28+
lora = LoraModule(pin_irq=35, pin_rst=13) # basic
29+
lora = LoraModule(pin_irq=35, pin_rst=25) # core2
30+
lora = LoraModule(pin_irq=10, pin_rst=5) # cores3
2931
lora.send("Hello, LoRa!")
32+
3033
print(lora.recv())
3134
35+
def callback(received_data):
36+
global lora
37+
print(received_data)
38+
lora.start_recv()
39+
lora.set_irq_callback(callback)
40+
lora.start_recv()
41+
3242
"""
3343

3444
"""
@@ -49,10 +59,10 @@ def __init__(
4959
pin_rst=25,
5060
freq_band=LORA_433,
5161
sf=8,
52-
bw: str = "500",
62+
bw: str = "125",
5363
coding_rate=8,
5464
preamble_len=12,
55-
output_power=0,
65+
output_power=20,
5666
):
5767
"""
5868
note: Initialize the LoRa module.
@@ -107,6 +117,8 @@ def __init__(
107117
lora_cfg=lora_cfg,
108118
)
109119

120+
self.irq_callback = None
121+
110122
def _validate_range(self, value, min, max):
111123
if value < min or value > max:
112124
raise ValueError(f"Value {value} out of range {min} to {max}")
@@ -151,3 +163,43 @@ def recv(self, timeout_ms=None, rx_length=0xFF, rx_packet: RxPacket = None) -> R
151163
note: Returns None on timeout, or an `RxPacket` instance with the packet on success.
152164
"""
153165
return self.modem.recv(timeout_ms, rx_length, rx_packet)
166+
167+
def start_recv(self):
168+
"""
169+
note: Start receiving data once, trigger an interrupt when data is received.
170+
"""
171+
self.modem.start_recv(continuous=True)
172+
173+
def set_irq_callback(self, callback):
174+
"""
175+
note: Set the IRQ callback function.
176+
177+
params:
178+
callback:
179+
note: The callback function. The function should accept one argument, which is the received data.
180+
"""
181+
182+
def _irq_callback():
183+
if self.irq_callback:
184+
schedule(self.irq_callback, self.modem.poll_recv())
185+
186+
self.irq_callback = callback
187+
self.modem.set_irq_callback(_irq_callback)
188+
189+
def standby(self):
190+
"""
191+
note: Set the modem to standby mode.
192+
"""
193+
self.modem.standby()
194+
195+
def sleep(self):
196+
"""
197+
note: Set the modem to sleep mode.
198+
"""
199+
self.modem.sleep()
200+
201+
def irq_triggered(self) -> bool:
202+
"""
203+
note: Check if the IRQ has been triggered.
204+
"""
205+
return self.modem.irq_triggered()

0 commit comments

Comments
 (0)