Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated examples for callback receive mode #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 27 additions & 14 deletions examples/LoRaDuplexCallback/LoRaDuplexCallback.ino
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ byte destination = 0xFF; // destination to send to
long lastSendTime = 0; // last send time
int interval = 2000; // interval between sends

volatile bool doRead = false; // Flag set by callback to perform read process in main loop

void setup() {
Serial.begin(9600); // initialize serial
while (!Serial);
Expand All @@ -46,6 +48,13 @@ void setup() {
}

void loop() {

// If ISR set the flag, perform read operations
if (doRead) {
readMessage();
doRead = false; // Set flag back to false so next read will happen only after next ISR event
}

if (millis() - lastSendTime > interval) {
String message = "HeLoRa World!"; // send a message
sendMessage(message);
Expand All @@ -56,20 +65,7 @@ void loop() {
}
}

void sendMessage(String outgoing) {
LoRa.beginPacket(); // start packet
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(msgCount); // add message ID
LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket(); // finish packet and send it
msgCount++; // increment message ID
}

void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return

void readMessage() {
// read packet header bytes:
int recipient = LoRa.read(); // recipient address
byte sender = LoRa.read(); // sender address
Expand Down Expand Up @@ -104,3 +100,20 @@ void onReceive(int packetSize) {
Serial.println();
}

void sendMessage(String outgoing) {
LoRa.beginPacket(); // start packet
LoRa.write(destination); // add destination address
LoRa.write(localAddress); // add sender address
LoRa.write(msgCount); // add message ID
LoRa.write(outgoing.length()); // add payload length
LoRa.print(outgoing); // add payload
LoRa.endPacket(); // finish packet and send it
msgCount++; // increment message ID
}

void onReceive(int packetSize) {
if (packetSize == 0) return; // if there's no packet, return

doRead = true; //Set flag to perform read in main loop
}

18 changes: 15 additions & 3 deletions examples/LoRaReceiverCallback/LoRaReceiverCallback.ino
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include <SPI.h>
#include <LoRa.h>

volatile bool doRead = false; // Flag set by callback to perform read process in main loop
volatile int incomingPacketSize;

void setup() {
Serial.begin(9600);
while (!Serial);
Expand All @@ -20,15 +23,19 @@ void setup() {
}

void loop() {
// do nothing
// If ISR set the flag, perform read operations
if (doRead) {
readMessage();
doRead = false; // Set flag back to false so next read will happen only after next ISR event
}
}

void onReceive(int packetSize) {
void readMessage() {
// received a packet
Serial.print("Received packet '");

// read packet
for (int i = 0; i < packetSize; i++) {
for (int i = 0; i < incomingPacketSize; i++) {
Serial.print((char)LoRa.read());
}

Expand All @@ -37,3 +44,8 @@ void onReceive(int packetSize) {
Serial.println(LoRa.packetRssi());
}

void onReceive(int packetSize) {
doRead = true;
incomingPacketSize = packetSize;
}