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

Si443x_Receive_Interrupt.ino (3408B)

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