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 |
RF69_Receive_Interrupt.ino (3535B)
1 /* 2 RadioLib RF69 Receive with Interrupts Example 3 4 This example listens for FSK transmissions and tries to 5 receive them. Once a packet is received, an interrupt is 6 triggered. 7 8 For default module settings, see the wiki page 9 https://github.com/jgromes/RadioLib/wiki/Default-configuration#rf69sx1231 10 11 For full API reference, see the GitHub Pages 12 https://jgromes.github.io/RadioLib/ 13 */ 14 15 // include the library 16 #include <RadioLib.h> 17 18 // RF69 has the following connections: 19 // CS pin: 10 20 // DIO0 pin: 2 21 // RESET pin: 3 22 RF69 radio = new Module(10, 2, 3); 23 24 // or using RadioShield 25 // https://github.com/jgromes/RadioShield 26 //RF69 radio = RadioShield.ModuleA; 27 28 void setup() { 29 Serial.begin(9600); 30 31 // initialize RF69 with default settings 32 Serial.print(F("[RF69] Initializing ... ")); 33 int state = radio.begin(); 34 if (state == RADIOLIB_ERR_NONE) { 35 Serial.println(F("success!")); 36 } else { 37 Serial.print(F("failed, code ")); 38 Serial.println(state); 39 while (true); 40 } 41 42 // set the function that will be called 43 // when new packet is received 44 radio.setDio0Action(setFlag); 45 46 // start listening for packets 47 Serial.print(F("[RF69] Starting to listen ... ")); 48 state = radio.startReceive(); 49 if (state == RADIOLIB_ERR_NONE) { 50 Serial.println(F("success!")); 51 } else { 52 Serial.print(F("failed, code ")); 53 Serial.println(state); 54 while (true); 55 } 56 57 // if needed, 'listen' mode can be disabled by calling 58 // any of the following methods: 59 // 60 // radio.standby() 61 // radio.sleep() 62 // radio.transmit(); 63 // radio.receive(); 64 // radio.readData(); 65 } 66 67 // flag to indicate that a packet was received 68 volatile bool receivedFlag = false; 69 70 // disable interrupt when it's not needed 71 volatile bool enableInterrupt = true; 72 73 // this function is called when a complete packet 74 // is received by the module 75 // IMPORTANT: this function MUST be 'void' type 76 // and MUST NOT have any arguments! 77 #if defined(ESP8266) || defined(ESP32) 78 ICACHE_RAM_ATTR 79 #endif 80 void setFlag(void) { 81 // check if the interrupt is enabled 82 if(!enableInterrupt) { 83 return; 84 } 85 86 // we got a packet, set the flag 87 receivedFlag = true; 88 } 89 90 void loop() { 91 // check if the flag is set 92 if(receivedFlag) { 93 // disable the interrupt service routine while 94 // processing the data 95 enableInterrupt = false; 96 97 // reset flag 98 receivedFlag = false; 99 100 // you can read received data as an Arduino String 101 String str; 102 int state = radio.readData(str); 103 104 // you can also read received data as byte array 105 /* 106 byte byteArr[8]; 107 int state = radio.readData(byteArr, 8); 108 */ 109 110 if (state == RADIOLIB_ERR_NONE) { 111 // packet was successfully received 112 Serial.println(F("[RF69] Received packet!")); 113 114 // print data of the packet 115 Serial.print(F("[RF69] Data:\t\t")); 116 Serial.println(str); 117 118 // print RSSI (Received Signal Strength Indicator) 119 // of the last received packet 120 Serial.print(F("[RF69] RSSI:\t\t")); 121 Serial.print(radio.getRSSI()); 122 Serial.println(F(" dBm")); 123 124 } else if (state == RADIOLIB_ERR_CRC_MISMATCH) { 125 // packet was received, but is malformed 126 Serial.println(F("CRC error!")); 127 128 } else { 129 // some other error occurred 130 Serial.print(F("failed, code ")); 131 Serial.println(state); 132 133 } 134 135 // put module back to listen mode 136 radio.startReceive(); 137 138 // we're ready to receive more packets, 139 // enable interrupt service routine 140 enableInterrupt = true; 141 } 142 143 }