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