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

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