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_Transmit_Interrupt.ino (3523B)

      1 /*
      2    RadioLib Si443x Transmit with Interrupts Example
      3 
      4    This example transmits packets using Si4432 FSK radio module.
      5    Each packet contains up to 64 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    Other modules from Si443x/RFM2x family can also be used.
     11 
     12    For default module settings, see the wiki page
     13    https://github.com/jgromes/RadioLib/wiki/Default-configuration#si443xrfm2x
     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 // Si4432 has the following connections:
     23 // nSEL pin:  10
     24 // nIRQ pin:  2
     25 // SDN pin:   9
     26 Si4432 radio = new Module(10, 2, 9);
     27 
     28 // or using RadioShield
     29 // https://github.com/jgromes/RadioShield
     30 //Si4432 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 Si4432 with default settings
     39   Serial.print(F("[Si4432] 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 the function that will be called
     50   // when packet transmission is finished
     51   radio.setIrqAction(setFlag);
     52 
     53   // start transmitting the first packet
     54   Serial.print(F("[Si4432] Sending first packet ... "));
     55 
     56   // you can transmit C-string or Arduino string up to
     57   // 64 characters long
     58   transmissionState = radio.startTransmit("Hello World!");
     59 
     60   // you can also transmit byte array up to 64 bytes long
     61   /*
     62     byte byteArr[] = {0x01, 0x23, 0x45, 0x67,
     63                       0x89, 0xAB, 0xCD, 0xEF};
     64     state = radio.startTransmit(byteArr, 8);
     65   */
     66 }
     67 
     68 // flag to indicate that a packet was sent
     69 volatile bool transmittedFlag = false;
     70 
     71 // disable interrupt when it's not needed
     72 volatile bool enableInterrupt = true;
     73 
     74 // this function is called when a complete packet
     75 // is transmitted by the module
     76 // IMPORTANT: this function MUST be 'void' type
     77 //            and MUST NOT have any arguments!
     78 #if defined(ESP8266) || defined(ESP32)
     79   ICACHE_RAM_ATTR
     80 #endif
     81 void setFlag(void) {
     82   // check if the interrupt is enabled
     83   if(!enableInterrupt) {
     84     return;
     85   }
     86 
     87   // we sent a packet, set the flag
     88   transmittedFlag = true;
     89 }
     90 
     91 void loop() {
     92   // check if the previous transmission finished
     93   if(transmittedFlag) {
     94     // disable the interrupt service routine while
     95     // processing the data
     96     enableInterrupt = false;
     97 
     98     // reset flag
     99     transmittedFlag = false;
    100 
    101     if (transmissionState == RADIOLIB_ERR_NONE) {
    102       // packet was successfully sent
    103       Serial.println(F("transmission finished!"));
    104 
    105     } else {
    106       Serial.print(F("failed, code "));
    107       Serial.println(transmissionState);
    108 
    109     }
    110 
    111     // wait a second before transmitting again
    112     delay(1000);
    113 
    114     // send another one
    115     Serial.print(F("[Si4432] Sending another packet ... "));
    116 
    117     // you can transmit C-string or Arduino string up to
    118     // 256 characters long
    119     transmissionState = radio.startTransmit("Hello World!");
    120 
    121     // you can also transmit byte array up to 64 bytes long
    122     /*
    123       byte byteArr[] = {0x01, 0x23, 0x45, 0x67,
    124                         0x89, 0xAB, 0xCD, 0xEF};
    125       int state = radio.startTransmit(byteArr, 8);
    126     */
    127 
    128     // we're ready to send more packets,
    129     // enable interrupt service routine
    130     enableInterrupt = true;
    131   }
    132 }