mirror of
https://github.com/sandeepmistry/arduino-LoRa.git
synced 2025-04-19 13:02:14 +03:00
Merge pull request #320 from ricaun/pr/10
Looks good, thanks a lot, this feature is a great addition
This commit is contained in:
commit
26640bc6b7
24
API.md
24
API.md
@ -38,6 +38,10 @@ To save further pins one could connect the reset pin of the MCU with reset pin o
|
||||
|
||||
* `reset` - set to `-1` to omit this pin
|
||||
|
||||
#### Pin dio0 interrupt callbacks
|
||||
|
||||
The dio0 pin can be used for transmission finish callback and/or receiving callback, check `onTxDone` and `onReceive`.
|
||||
|
||||
### Set SPI interface
|
||||
|
||||
Override the default SPI interface used by the library. **Must** be called before `LoRa.begin()`.
|
||||
@ -117,6 +121,24 @@ LoRa.endPacket(async);
|
||||
|
||||
Returns `1` on success, `0` on failure.
|
||||
|
||||
### Tx Done
|
||||
|
||||
**WARNING**: TxDone callback uses the interrupt pin on the `dio0` check `setPins` function!
|
||||
|
||||
### Register callback
|
||||
|
||||
Register a callback function for when a packet transmission finish.
|
||||
|
||||
```arduino
|
||||
LoRa.onTxDone(onTxDone);
|
||||
|
||||
void onTxDone() {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
* `onTxDone` - function to call when a packet transmission finish.
|
||||
|
||||
## Receiving data
|
||||
|
||||
### Parsing packet
|
||||
@ -136,7 +158,7 @@ Returns the packet size in bytes or `0` if no packet was received.
|
||||
|
||||
### Continuous receive mode
|
||||
|
||||
**WARNING**: Not supported on the Arduino MKR WAN 1300 board!
|
||||
**WARNING**: Receive callback uses the interrupt pin on the `dio0`, check `setPins` function!
|
||||
|
||||
#### Register callback
|
||||
|
||||
|
@ -0,0 +1,50 @@
|
||||
#include <SPI.h>
|
||||
#include <LoRa.h>
|
||||
|
||||
int counter = 0;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
while (!Serial);
|
||||
|
||||
Serial.println("LoRa Sender non-blocking Callback");
|
||||
|
||||
if (!LoRa.begin(915E6)) {
|
||||
Serial.println("Starting LoRa failed!");
|
||||
while (1);
|
||||
}
|
||||
|
||||
LoRa.onTxDone(onTxDone);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (runEvery(1000)) { // repeat every 1000 millis
|
||||
|
||||
Serial.print("Sending packet non-blocking: ");
|
||||
Serial.println(counter);
|
||||
|
||||
// send in async / non-blocking mode
|
||||
LoRa.beginPacket();
|
||||
LoRa.print("hello ");
|
||||
LoRa.print(counter);
|
||||
LoRa.endPacket(true); // true = async / non-blocking mode
|
||||
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
void onTxDone() {
|
||||
Serial.println("TxDone");
|
||||
}
|
||||
|
||||
boolean runEvery(unsigned long interval)
|
||||
{
|
||||
static unsigned long previousMillis = 0;
|
||||
unsigned long currentMillis = millis();
|
||||
if (currentMillis - previousMillis >= interval)
|
||||
{
|
||||
previousMillis = currentMillis;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
@ -53,6 +53,7 @@ void setup() {
|
||||
Serial.println();
|
||||
|
||||
LoRa.onReceive(onReceive);
|
||||
LoRa.onTxDone(onTxDone);
|
||||
LoRa_rxMode();
|
||||
}
|
||||
|
||||
@ -83,8 +84,7 @@ void LoRa_sendMessage(String message) {
|
||||
LoRa_txMode(); // set tx mode
|
||||
LoRa.beginPacket(); // start packet
|
||||
LoRa.print(message); // add payload
|
||||
LoRa.endPacket(); // finish packet and send it
|
||||
LoRa_rxMode(); // set rx mode
|
||||
LoRa.endPacket(true); // finish packet and send it
|
||||
}
|
||||
|
||||
void onReceive(int packetSize) {
|
||||
@ -96,7 +96,11 @@ void onReceive(int packetSize) {
|
||||
|
||||
Serial.print("Gateway Receive: ");
|
||||
Serial.println(message);
|
||||
}
|
||||
|
||||
void onTxDone() {
|
||||
Serial.println("TxDone");
|
||||
LoRa_rxMode();
|
||||
}
|
||||
|
||||
boolean runEvery(unsigned long interval)
|
||||
|
@ -53,6 +53,7 @@ void setup() {
|
||||
Serial.println();
|
||||
|
||||
LoRa.onReceive(onReceive);
|
||||
LoRa.onTxDone(onTxDone);
|
||||
LoRa_rxMode();
|
||||
}
|
||||
|
||||
@ -83,8 +84,7 @@ void LoRa_sendMessage(String message) {
|
||||
LoRa_txMode(); // set tx mode
|
||||
LoRa.beginPacket(); // start packet
|
||||
LoRa.print(message); // add payload
|
||||
LoRa.endPacket(); // finish packet and send it
|
||||
LoRa_rxMode(); // set rx mode
|
||||
LoRa.endPacket(true); // finish packet and send it
|
||||
}
|
||||
|
||||
void onReceive(int packetSize) {
|
||||
@ -96,7 +96,11 @@ void onReceive(int packetSize) {
|
||||
|
||||
Serial.print("Node Receive: ");
|
||||
Serial.println(message);
|
||||
}
|
||||
|
||||
void onTxDone() {
|
||||
Serial.println("TxDone");
|
||||
LoRa_rxMode();
|
||||
}
|
||||
|
||||
boolean runEvery(unsigned long interval)
|
||||
|
@ -31,6 +31,7 @@ peek KEYWORD2
|
||||
flush KEYWORD2
|
||||
|
||||
onReceive KEYWORD2
|
||||
onTxDone KEYWORD2
|
||||
receive KEYWORD2
|
||||
idle KEYWORD2
|
||||
sleep KEYWORD2
|
||||
|
44
src/LoRa.cpp
44
src/LoRa.cpp
@ -70,7 +70,8 @@ LoRaClass::LoRaClass() :
|
||||
_frequency(0),
|
||||
_packetIndex(0),
|
||||
_implicitHeaderMode(0),
|
||||
_onReceive(NULL)
|
||||
_onReceive(NULL),
|
||||
_onTxDone(NULL)
|
||||
{
|
||||
// overide Stream timeout value
|
||||
setTimeout(0);
|
||||
@ -177,13 +178,14 @@ int LoRaClass::beginPacket(int implicitHeader)
|
||||
|
||||
int LoRaClass::endPacket(bool async)
|
||||
{
|
||||
|
||||
if ((async) && (_onTxDone))
|
||||
writeRegister(REG_DIO_MAPPING_1, 0x40); // DIO0 => TXDONE
|
||||
|
||||
// put in TX mode
|
||||
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);
|
||||
|
||||
if (async) {
|
||||
// grace time is required for the radio
|
||||
delayMicroseconds(150);
|
||||
} else {
|
||||
if (!async) {
|
||||
// wait for TX done
|
||||
while ((readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) {
|
||||
yield();
|
||||
@ -353,8 +355,24 @@ void LoRaClass::onReceive(void(*callback)(int))
|
||||
|
||||
if (callback) {
|
||||
pinMode(_dio0, INPUT);
|
||||
#ifdef SPI_HAS_NOTUSINGINTERRUPT
|
||||
SPI.usingInterrupt(digitalPinToInterrupt(_dio0));
|
||||
#endif
|
||||
attachInterrupt(digitalPinToInterrupt(_dio0), LoRaClass::onDio0Rise, RISING);
|
||||
} else {
|
||||
detachInterrupt(digitalPinToInterrupt(_dio0));
|
||||
#ifdef SPI_HAS_NOTUSINGINTERRUPT
|
||||
SPI.notUsingInterrupt(digitalPinToInterrupt(_dio0));
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
writeRegister(REG_DIO_MAPPING_1, 0x00);
|
||||
void LoRaClass::onTxDone(void(*callback)())
|
||||
{
|
||||
_onTxDone = callback;
|
||||
|
||||
if (callback) {
|
||||
pinMode(_dio0, INPUT);
|
||||
#ifdef SPI_HAS_NOTUSINGINTERRUPT
|
||||
SPI.usingInterrupt(digitalPinToInterrupt(_dio0));
|
||||
#endif
|
||||
@ -369,6 +387,9 @@ void LoRaClass::onReceive(void(*callback)(int))
|
||||
|
||||
void LoRaClass::receive(int size)
|
||||
{
|
||||
|
||||
writeRegister(REG_DIO_MAPPING_1, 0x00); // DIO0 => RXDONE
|
||||
|
||||
if (size > 0) {
|
||||
implicitHeaderMode();
|
||||
|
||||
@ -640,6 +661,8 @@ void LoRaClass::handleDio0Rise()
|
||||
writeRegister(REG_IRQ_FLAGS, irqFlags);
|
||||
|
||||
if ((irqFlags & IRQ_PAYLOAD_CRC_ERROR_MASK) == 0) {
|
||||
|
||||
if ((irqFlags & IRQ_RX_DONE_MASK) != 0) {
|
||||
// received a packet
|
||||
_packetIndex = 0;
|
||||
|
||||
@ -652,6 +675,15 @@ void LoRaClass::handleDio0Rise()
|
||||
if (_onReceive) {
|
||||
_onReceive(packetLength);
|
||||
}
|
||||
|
||||
// reset FIFO address
|
||||
writeRegister(REG_FIFO_ADDR_PTR, 0);
|
||||
}
|
||||
else if ((irqFlags & IRQ_TX_DONE_MASK) != 0) {
|
||||
if (_onTxDone) {
|
||||
_onTxDone();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -57,6 +57,7 @@ public:
|
||||
|
||||
#ifndef ARDUINO_SAMD_MKRWAN1300
|
||||
void onReceive(void(*callback)(int));
|
||||
void onTxDone(void(*callback)());
|
||||
|
||||
void receive(int size = 0);
|
||||
#endif
|
||||
@ -117,6 +118,7 @@ private:
|
||||
int _packetIndex;
|
||||
int _implicitHeaderMode;
|
||||
void (*_onReceive)(int);
|
||||
void (*_onTxDone)();
|
||||
};
|
||||
|
||||
extern LoRaClass LoRa;
|
||||
|
Loading…
x
Reference in New Issue
Block a user