diff --git a/API.md b/API.md index 78f054d..b7a0e4f 100644 --- a/API.md +++ b/API.md @@ -188,6 +188,25 @@ LoRa.receive(int size); The `onReceive` callback will be called when a packet is received. + +### onCrcError + +**WARNING**: onCrcError callback uses the interrupt pin on the `dio0`, check `setPins` function! + +#### Register callback + +Register a callback function for when a received packet failed CRC check. + +```arduino +LoRa.onCrcError(onCrcError); + +void onCrcError() { + // ... +} +``` + +The `onCrcError` - function to call when a received packet failed CRC check. + ### Packet RSSI ```arduino diff --git a/keywords.txt b/keywords.txt index 63e0e9a..7bfe14b 100644 --- a/keywords.txt +++ b/keywords.txt @@ -32,6 +32,8 @@ flush KEYWORD2 onReceive KEYWORD2 onTxDone KEYWORD2 +onCrcError KEYWORD2 + receive KEYWORD2 idle KEYWORD2 sleep KEYWORD2 diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 6980f5a..c7a24b5 100644 --- a/src/LoRa.cpp +++ b/src/LoRa.cpp @@ -71,7 +71,8 @@ LoRaClass::LoRaClass() : _packetIndex(0), _implicitHeaderMode(0), _onReceive(NULL), - _onTxDone(NULL) + _onTxDone(NULL), + _onCrcError(NULL) { // overide Stream timeout value setTimeout(0); @@ -385,6 +386,23 @@ void LoRaClass::onTxDone(void(*callback)()) } } +void LoRaClass::onCrcError(void(*callback)()){ + _onCrcError = callback; + + 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 + } +} + void LoRaClass::receive(int size) { @@ -684,6 +702,10 @@ void LoRaClass::handleDio0Rise() _onTxDone(); } } + } else { + if (_onCrcError) { + _onCrcError(); + } } } diff --git a/src/LoRa.h b/src/LoRa.h index c1671c1..c8e1dee 100644 --- a/src/LoRa.h +++ b/src/LoRa.h @@ -58,6 +58,7 @@ class LoRaClass : public Stream { #ifndef ARDUINO_SAMD_MKRWAN1300 void onReceive(void(*callback)(int)); void onTxDone(void(*callback)()); + void onCrcError(void(*callback)()); void receive(int size = 0); #endif @@ -119,6 +120,7 @@ class LoRaClass : public Stream { int _implicitHeaderMode; void (*_onReceive)(int); void (*_onTxDone)(); + void (*_onCrcError)(); }; extern LoRaClass LoRa;