acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
nRF24_Transmit_Interrupt.ino (4177B)
1 /* 2 RadioLib nRF24 Transmit with Interrupts Example 3 4 This example transmits packets using nRF24 2.4 GHz radio module. 5 Each packet contains up to 32 bytes of data, in the form of: 6 - Arduino String 7 - null-terminated char array (C-string) 8 - arbitrary binary data (byte array) 9 10 Packet delivery is automatically acknowledged by the receiver. 11 12 For default module settings, see the wiki page 13 https://github.com/jgromes/RadioLib/wiki/Default-configuration#nrf24 14 15 For full API reference, see the GitHub Pages 16 https://jgromes.github.io/RadioLib/ 17 */ 18 19 // include the library 20 #include <RadioLib.h> 21 22 // nRF24 has the following connections: 23 // CS pin: 10 24 // IRQ pin: 2 25 // CE pin: 3 26 nRF24 radio = new Module(10, 2, 3); 27 28 // or using RadioShield 29 // https://github.com/jgromes/RadioShield 30 //nRF24 radio = RadioShield.ModuleA; 31 32 // save transmission state between loops 33 int transmissionState = RADIOLIB_ERR_NONE; 34 35 void setup() { 36 Serial.begin(9600); 37 38 // initialize nRF24 with default settings 39 Serial.print(F("[nRF24] Initializing ... ")); 40 int state = radio.begin(); 41 if(state == RADIOLIB_ERR_NONE) { 42 Serial.println(F("success!")); 43 } else { 44 Serial.print(F("failed, code ")); 45 Serial.println(state); 46 while(true); 47 } 48 49 // set transmit address 50 // NOTE: address width in bytes MUST be equal to the 51 // width set in begin() or setAddressWidth() 52 // methods (5 by default) 53 byte addr[] = {0x01, 0x23, 0x45, 0x67, 0x89}; 54 Serial.print(F("[nRF24] Setting transmit pipe ... ")); 55 state = radio.setTransmitPipe(addr); 56 if(state == RADIOLIB_ERR_NONE) { 57 Serial.println(F("success!")); 58 } else { 59 Serial.print(F("failed, code ")); 60 Serial.println(state); 61 while(true); 62 } 63 64 // set the function that will be called 65 // when packet transmission is finished 66 radio.setIrqAction(setFlag); 67 68 // start transmitting the first packet 69 Serial.print(F("[nRF24] Sending first packet ... ")); 70 71 // you can transmit C-string or Arduino string up to 72 // 256 characters long 73 transmissionState = radio.startTransmit("Hello World!"); 74 75 // you can also transmit byte array up to 256 bytes long 76 /* 77 byte byteArr[] = {0x01, 0x23, 0x45, 0x67, 78 0x89, 0xAB, 0xCD, 0xEF}; 79 state = radio.startTransmit(byteArr, 8); 80 */ 81 } 82 83 // flag to indicate that a packet was sent 84 volatile bool transmittedFlag = false; 85 86 // disable interrupt when it's not needed 87 volatile bool enableInterrupt = true; 88 89 // this function is called when a complete packet 90 // is transmitted by the module 91 // IMPORTANT: this function MUST be 'void' type 92 // and MUST NOT have any arguments! 93 #if defined(ESP8266) || defined(ESP32) 94 ICACHE_RAM_ATTR 95 #endif 96 void setFlag(void) { 97 // check if the interrupt is enabled 98 if(!enableInterrupt) { 99 return; 100 } 101 102 // we sent a packet, set the flag 103 transmittedFlag = true; 104 } 105 106 void loop() { 107 // check if the previous transmission finished 108 if(transmittedFlag) { 109 // disable the interrupt service routine while 110 // processing the data 111 enableInterrupt = false; 112 113 // reset flag 114 transmittedFlag = false; 115 116 if (transmissionState == RADIOLIB_ERR_NONE) { 117 // packet was successfully sent 118 Serial.println(F("transmission finished!")); 119 120 // NOTE: when using interrupt-driven transmit method, 121 // it is not possible to automatically measure 122 // transmission data rate using getDataRate() 123 124 } else { 125 Serial.print(F("failed, code ")); 126 Serial.println(transmissionState); 127 128 } 129 130 // wait a second before transmitting again 131 delay(1000); 132 133 // send another one 134 Serial.print(F("[nRF24] Sending another packet ... ")); 135 136 // you can transmit C-string or Arduino string up to 137 // 256 characters long 138 transmissionState = radio.startTransmit("Hello World!"); 139 140 // you can also transmit byte array up to 256 bytes long 141 /* 142 byte byteArr[] = {0x01, 0x23, 0x45, 0x67, 143 0x89, 0xAB, 0xCD, 0xEF}; 144 int state = radio.startTransmit(byteArr, 8); 145 */ 146 147 // we're ready to send more packets, 148 // enable interrupt service routine 149 enableInterrupt = true; 150 } 151 }