diff --git a/src/LoRa.cpp b/src/LoRa.cpp index 6980f5a..bafa0e2 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), + _onError(NULL) { // overide Stream timeout value setTimeout(0); @@ -385,6 +386,23 @@ void LoRaClass::onTxDone(void(*callback)()) } } +void LoRaClass::onError(void(*callback)()){ + _onError = 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 (_onError) { + _onError(); + } } } diff --git a/src/LoRa.h b/src/LoRa.h index c1671c1..bdb65ac 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 onError(void(*callback)()); void receive(int size = 0); #endif @@ -119,6 +120,7 @@ class LoRaClass : public Stream { int _implicitHeaderMode; void (*_onReceive)(int); void (*_onTxDone)(); + void (*_onError)(); }; extern LoRaClass LoRa;