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

SX128x_Transmit_Interrupt.ino (3563B)

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