diff --git a/examples/LoRaDuplexCallback/LoRaDuplexCallback.ino b/examples/LoRaDuplexCallback/LoRaDuplexCallback.ino index 2385188..1893137 100644 --- a/examples/LoRaDuplexCallback/LoRaDuplexCallback.ino +++ b/examples/LoRaDuplexCallback/LoRaDuplexCallback.ino @@ -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); @@ -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); @@ -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 @@ -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 +} + diff --git a/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino b/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino index 1920c5d..99a9f5e 100644 --- a/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino +++ b/examples/LoRaReceiverCallback/LoRaReceiverCallback.ino @@ -1,6 +1,9 @@ #include #include +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); @@ -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()); } @@ -37,3 +44,8 @@ void onReceive(int packetSize) { Serial.println(LoRa.packetRssi()); } +void onReceive(int packetSize) { + doRead = true; + incomingPacketSize = packetSize; +} +