mirror of
https://github.com/sandeepmistry/arduino-LoRa.git
synced 2025-04-19 13:02:14 +03:00
OnTxDone
Add callback onTxDone
This commit is contained in:
parent
ed97efd1ed
commit
e641518510
18
API.md
18
API.md
@ -117,6 +117,24 @@ LoRa.endPacket(async);
|
|||||||
|
|
||||||
Returns `1` on success, `0` on failure.
|
Returns `1` on success, `0` on failure.
|
||||||
|
|
||||||
|
### Tx Done
|
||||||
|
|
||||||
|
**WARNING**: Not supported on the Arduino MKR WAN 1300 board!
|
||||||
|
|
||||||
|
### 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
|
## Receiving data
|
||||||
|
|
||||||
### Parsing packet
|
### Parsing packet
|
||||||
|
@ -31,6 +31,7 @@ peek KEYWORD2
|
|||||||
flush KEYWORD2
|
flush KEYWORD2
|
||||||
|
|
||||||
onReceive KEYWORD2
|
onReceive KEYWORD2
|
||||||
|
onTxDone KEYWORD2
|
||||||
receive KEYWORD2
|
receive KEYWORD2
|
||||||
idle KEYWORD2
|
idle KEYWORD2
|
||||||
sleep KEYWORD2
|
sleep KEYWORD2
|
||||||
|
44
src/LoRa.cpp
44
src/LoRa.cpp
@ -70,7 +70,8 @@ LoRaClass::LoRaClass() :
|
|||||||
_frequency(0),
|
_frequency(0),
|
||||||
_packetIndex(0),
|
_packetIndex(0),
|
||||||
_implicitHeaderMode(0),
|
_implicitHeaderMode(0),
|
||||||
_onReceive(NULL)
|
_onReceive(NULL),
|
||||||
|
_onTxDone(NULL)
|
||||||
{
|
{
|
||||||
// overide Stream timeout value
|
// overide Stream timeout value
|
||||||
setTimeout(0);
|
setTimeout(0);
|
||||||
@ -177,13 +178,14 @@ int LoRaClass::beginPacket(int implicitHeader)
|
|||||||
|
|
||||||
int LoRaClass::endPacket(bool async)
|
int LoRaClass::endPacket(bool async)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
if ((async) && (_onTxDone))
|
||||||
|
writeRegister(REG_DIO_MAPPING_1, 0x40); // DIO0 => TXDONE
|
||||||
|
|
||||||
// put in TX mode
|
// put in TX mode
|
||||||
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);
|
writeRegister(REG_OP_MODE, MODE_LONG_RANGE_MODE | MODE_TX);
|
||||||
|
|
||||||
if (async) {
|
if (!async) {
|
||||||
// grace time is required for the radio
|
|
||||||
delayMicroseconds(150);
|
|
||||||
} else {
|
|
||||||
// wait for TX done
|
// wait for TX done
|
||||||
while ((readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) {
|
while ((readRegister(REG_IRQ_FLAGS) & IRQ_TX_DONE_MASK) == 0) {
|
||||||
yield();
|
yield();
|
||||||
@ -353,8 +355,24 @@ void LoRaClass::onReceive(void(*callback)(int))
|
|||||||
|
|
||||||
if (callback) {
|
if (callback) {
|
||||||
pinMode(_dio0, INPUT);
|
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
|
#ifdef SPI_HAS_NOTUSINGINTERRUPT
|
||||||
SPI.usingInterrupt(digitalPinToInterrupt(_dio0));
|
SPI.usingInterrupt(digitalPinToInterrupt(_dio0));
|
||||||
#endif
|
#endif
|
||||||
@ -369,6 +387,9 @@ void LoRaClass::onReceive(void(*callback)(int))
|
|||||||
|
|
||||||
void LoRaClass::receive(int size)
|
void LoRaClass::receive(int size)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
writeRegister(REG_DIO_MAPPING_1, 0x00); // DIO0 => RXDONE
|
||||||
|
|
||||||
if (size > 0) {
|
if (size > 0) {
|
||||||
implicitHeaderMode();
|
implicitHeaderMode();
|
||||||
|
|
||||||
@ -640,6 +661,8 @@ void LoRaClass::handleDio0Rise()
|
|||||||
writeRegister(REG_IRQ_FLAGS, irqFlags);
|
writeRegister(REG_IRQ_FLAGS, irqFlags);
|
||||||
|
|
||||||
if ((irqFlags & IRQ_PAYLOAD_CRC_ERROR_MASK) == 0) {
|
if ((irqFlags & IRQ_PAYLOAD_CRC_ERROR_MASK) == 0) {
|
||||||
|
|
||||||
|
if ((irqFlags & IRQ_RX_DONE_MASK) != 0) {
|
||||||
// received a packet
|
// received a packet
|
||||||
_packetIndex = 0;
|
_packetIndex = 0;
|
||||||
|
|
||||||
@ -652,6 +675,15 @@ void LoRaClass::handleDio0Rise()
|
|||||||
if (_onReceive) {
|
if (_onReceive) {
|
||||||
_onReceive(packetLength);
|
_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
|
#ifndef ARDUINO_SAMD_MKRWAN1300
|
||||||
void onReceive(void(*callback)(int));
|
void onReceive(void(*callback)(int));
|
||||||
|
void onTxDone(void(*callback)());
|
||||||
|
|
||||||
void receive(int size = 0);
|
void receive(int size = 0);
|
||||||
#endif
|
#endif
|
||||||
@ -117,6 +118,7 @@ private:
|
|||||||
int _packetIndex;
|
int _packetIndex;
|
||||||
int _implicitHeaderMode;
|
int _implicitHeaderMode;
|
||||||
void (*_onReceive)(int);
|
void (*_onReceive)(int);
|
||||||
|
void (*_onTxDone)();
|
||||||
};
|
};
|
||||||
|
|
||||||
extern LoRaClass LoRa;
|
extern LoRaClass LoRa;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user