Skip to content

Commit

Permalink
samples: lora: receive: demo async reception
Browse files Browse the repository at this point in the history
Demonstrate asynchronous reception in the LoRa sample.

Signed-off-by: Jordan Yates <[email protected]>
  • Loading branch information
Jordan Yates authored and carlescufi committed Oct 26, 2021
1 parent 526b3e9 commit 13c516e
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion samples/drivers/lora/receive/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

0 comments on commit 13c516e

Please sign in to comment.