samples: lora: receive: demo async reception
Demonstrate asynchronous reception in the LoRa sample. Signed-off-by: Jordan Yates <jordan.yates@data61.csiro.au>
This commit is contained in:
parent
526b3e9605
commit
13c516e04b
1 changed files with 26 additions and 1 deletions
|
@ -20,6 +20,24 @@ BUILD_ASSERT(DT_NODE_HAS_STATUS(DEFAULT_RADIO_NODE, okay),
|
|||
#include <logging/log.h>
|
||||
LOG_MODULE_REGISTER(lora_receive);
|
||||
|
||||
void lora_receive_cb(const struct device *dev, uint8_t *data, uint16_t size,
|
||||
int16_t rssi, int8_t snr)
|
||||
{
|
||||
static int cnt;
|
||||
|
||||
ARG_UNUSED(dev);
|
||||
ARG_UNUSED(size);
|
||||
|
||||
LOG_INF("Received data: %s (RSSI:%ddBm, SNR:%ddBm)",
|
||||
log_strdup(data), rssi, snr);
|
||||
|
||||
/* Stop receiving after 10 packets */
|
||||
if (++cnt == 10) {
|
||||
LOG_INF("Stopping packet receptions");
|
||||
lora_recv_async(dev, NULL);
|
||||
}
|
||||
}
|
||||
|
||||
void main(void)
|
||||
{
|
||||
const struct device *lora_dev = DEVICE_DT_GET(DEFAULT_RADIO_NODE);
|
||||
|
@ -48,7 +66,9 @@ void main(void)
|
|||
return;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
/* Receive 4 packets synchronously */
|
||||
LOG_INF("Synchronous reception");
|
||||
for (int i = 0; i < 4; i++) {
|
||||
/* Block until data arrives */
|
||||
len = lora_recv(lora_dev, data, MAX_DATA_LEN, K_FOREVER,
|
||||
&rssi, &snr);
|
||||
|
@ -60,4 +80,9 @@ void main(void)
|
|||
LOG_INF("Received data: %s (RSSI:%ddBm, SNR:%ddBm)",
|
||||
log_strdup(data), rssi, snr);
|
||||
}
|
||||
|
||||
/* Enable asynchronous reception */
|
||||
LOG_INF("Asynchronous reception");
|
||||
lora_recv_async(lora_dev, lora_receive_cb);
|
||||
k_sleep(K_FOREVER);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue