Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onError callback #329

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ flush KEYWORD2

onReceive KEYWORD2
onTxDone KEYWORD2
onCrcError KEYWORD2

receive KEYWORD2
idle KEYWORD2
sleep KEYWORD2
Expand Down
24 changes: 23 additions & 1 deletion src/LoRa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ LoRaClass::LoRaClass() :
_packetIndex(0),
_implicitHeaderMode(0),
_onReceive(NULL),
_onTxDone(NULL)
_onTxDone(NULL),
_onCrcError(NULL)
{
// overide Stream timeout value
setTimeout(0);
Expand Down Expand Up @@ -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)
{

Expand Down Expand Up @@ -684,6 +702,10 @@ void LoRaClass::handleDio0Rise()
_onTxDone();
}
}
} else {
if (_onCrcError) {
_onCrcError();
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/LoRa.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -119,6 +120,7 @@ class LoRaClass : public Stream {
int _implicitHeaderMode;
void (*_onReceive)(int);
void (*_onTxDone)();
void (*_onCrcError)();
};

extern LoRaClass LoRa;
Expand Down