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

SX126x_Transmit_Interrupt.ino (3817B)

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