Update RadioLib

This commit is contained in:
lewisxhe
2024-09-19 11:12:36 +08:00
parent 210367a171
commit 5794daa8aa
301 changed files with 115546 additions and 12703 deletions
@@ -1,19 +1,23 @@
/*
RadioLib nRF24 Receive Example
RadioLib nRF24 Blocking Receive Example
This example listens for FSK transmissions using nRF24 2.4 GHz radio module.
To successfully receive data, the following settings have to be the same
on both transmitter and receiver:
- carrier frequency
- data rate
- transmit pipe on transmitter must match receive pipe
on receiver
This example listens for FSK transmissions using nRF24 2.4 GHz radio module.
To successfully receive data, the following settings have to be the same
on both transmitter and receiver:
- carrier frequency
- data rate
- transmit pipe on transmitter must match receive pipe on receiver
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#nrf24
Using blocking receive is not recommended, as it will lead
to significant amount of timeouts, inefficient use of processor
time and can some miss packets!
Instead, interrupt receive is recommended.
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
For default module settings, see the wiki page
https://github.com/jgromes/RadioLib/wiki/Default-configuration#nrf24
For full API reference, see the GitHub Pages
https://jgromes.github.io/RadioLib/
*/
// include the library
@@ -40,7 +44,7 @@ void setup() {
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
while (true) { delay(10); }
}
// set receive pipe 0 address
@@ -55,7 +59,7 @@ void setup() {
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
while (true) { delay(10); }
}
}
@@ -41,7 +41,7 @@ void setup() {
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
while (true) { delay(10); }
}
// set receive pipe 0 address
@@ -56,12 +56,12 @@ void setup() {
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
while (true) { delay(10); }
}
// set the function that will be called
// when new packet is received
radio.setIrqAction(setFlag);
radio.setPacketReceivedAction(setFlag);
// start listening
Serial.print(F("[nRF24] Starting to listen ... "));
@@ -71,7 +71,7 @@ void setup() {
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while (true);
while (true) { delay(10); }
}
// if needed, 'listen' mode can be disabled by calling
@@ -87,9 +87,6 @@ void setup() {
// flag to indicate that a packet was received
volatile bool receivedFlag = false;
// disable interrupt when it's not needed
volatile bool enableInterrupt = true;
// this function is called when a complete packet
// is received by the module
// IMPORTANT: this function MUST be 'void' type
@@ -98,11 +95,6 @@ volatile bool enableInterrupt = true;
ICACHE_RAM_ATTR
#endif
void setFlag(void) {
// check if the interrupt is enabled
if(!enableInterrupt) {
return;
}
// we got a packet, set the flag
receivedFlag = true;
}
@@ -110,10 +102,6 @@ void setFlag(void) {
void loop() {
// check if the flag is set
if(receivedFlag) {
// disable the interrupt service routine while
// processing the data
enableInterrupt = false;
// reset flag
receivedFlag = false;
@@ -124,7 +112,8 @@ void loop() {
// you can also read received data as byte array
/*
byte byteArr[8];
int state = radio.readData(byteArr, 8);
int numBytes = radio.getPacketLength();
int state = radio.readData(byteArr, numBytes);
*/
if (state == RADIOLIB_ERR_NONE) {
@@ -144,10 +133,5 @@ void loop() {
// put module back to listen mode
radio.startReceive();
// we're ready to receive more packets,
// enable interrupt service routine
enableInterrupt = true;
}
}
@@ -40,7 +40,7 @@ void setup() {
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
while (true) { delay(10); }
}
// set transmit address
@@ -55,16 +55,20 @@ void setup() {
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
while (true) { delay(10); }
}
}
// counter to keep track of transmitted packets
int count = 0;
void loop() {
Serial.print(F("[nRF24] Transmitting packet ... "));
// you can transmit C-string or Arduino string up to
// 32 characters long
int state = radio.transmit("Hello World!");
String str = "Hello World! #" + String(count++);
int state = radio.transmit(str);
if (state == RADIOLIB_ERR_NONE) {
// the packet was successfully transmitted
@@ -43,7 +43,7 @@ void setup() {
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
while (true) { delay(10); }
}
// set transmit address
@@ -58,12 +58,12 @@ void setup() {
} else {
Serial.print(F("failed, code "));
Serial.println(state);
while(true);
while (true) { delay(10); }
}
// set the function that will be called
// when packet transmission is finished
radio.setIrqAction(setFlag);
radio.setPacketSentAction(setFlag);
// start transmitting the first packet
Serial.print(F("[nRF24] Sending first packet ... "));
@@ -83,9 +83,6 @@ void setup() {
// flag to indicate that a packet was sent
volatile bool transmittedFlag = false;
// disable interrupt when it's not needed
volatile bool enableInterrupt = true;
// this function is called when a complete packet
// is transmitted by the module
// IMPORTANT: this function MUST be 'void' type
@@ -94,22 +91,16 @@ volatile bool enableInterrupt = true;
ICACHE_RAM_ATTR
#endif
void setFlag(void) {
// check if the interrupt is enabled
if(!enableInterrupt) {
return;
}
// we sent a packet, set the flag
transmittedFlag = true;
}
// counter to keep track of transmitted packets
int count = 0;
void loop() {
// check if the previous transmission finished
if(transmittedFlag) {
// disable the interrupt service routine while
// processing the data
enableInterrupt = false;
// reset flag
transmittedFlag = false;
@@ -127,6 +118,11 @@ void loop() {
}
// clean up after transmission is finished
// this will ensure transmitter is disabled,
// RF switch is powered down etc.
radio.finishTransmit();
// wait a second before transmitting again
delay(1000);
@@ -134,8 +130,9 @@ void loop() {
Serial.print(F("[nRF24] Sending another packet ... "));
// you can transmit C-string or Arduino string up to
// 256 characters long
transmissionState = radio.startTransmit("Hello World!");
// 32 characters long
String str = "Hello World! #" + String(count++);
transmissionState = radio.startTransmit(str);
// you can also transmit byte array up to 256 bytes long
/*
@@ -143,9 +140,5 @@ void loop() {
0x89, 0xAB, 0xCD, 0xEF};
int state = radio.startTransmit(byteArr, 8);
*/
// we're ready to send more packets,
// enable interrupt service routine
enableInterrupt = true;
}
}