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

SX127x_Transmit_Interrupt.ino (3951B)

      1 /*
      2    RadioLib SX127x 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 SX127x/RFM9x family can also be used.
     12 
     13    For default module settings, see the wiki page
     14    https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---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 // SX1278 has the following connections:
     24 // NSS pin:   10
     25 // DIO0 pin:  2
     26 // RESET pin: 9
     27 // DIO1 pin:  3
     28 SX1278 radio = new Module(10, 2, 9, 3);
     29 
     30 // or using RadioShield
     31 // https://github.com/jgromes/RadioShield
     32 //SX1278 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 SX1278 with default settings
     41   Serial.print(F("[SX1278] 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.setDio0Action(setFlag);
     54 
     55   // start transmitting the first packet
     56   Serial.print(F("[SX1278] 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       // NOTE: when using interrupt-driven transmit method,
    108       //       it is not possible to automatically measure
    109       //       transmission data rate using getDataRate()
    110 
    111     } else {
    112       Serial.print(F("failed, code "));
    113       Serial.println(transmissionState);
    114 
    115     }
    116 
    117     // NOTE: in FSK mode, SX127x will not automatically
    118     //       turn transmitter off after sending a packet
    119     //       set mode to standby to ensure we don't jam others
    120     //radio.standby()
    121 
    122     // wait a second before transmitting again
    123     delay(1000);
    124 
    125     // send another one
    126     Serial.print(F("[SX1278] Sending another packet ... "));
    127 
    128     // you can transmit C-string or Arduino string up to
    129     // 256 characters long
    130     transmissionState = radio.startTransmit("Hello World!");
    131 
    132     // you can also transmit byte array up to 256 bytes long
    133     /*
    134       byte byteArr[] = {0x01, 0x23, 0x45, 0x67,
    135                         0x89, 0xAB, 0xCD, 0xEF};
    136       int state = radio.startTransmit(byteArr, 8);
    137     */
    138 
    139     // we're ready to send more packets,
    140     // enable interrupt service routine
    141     enableInterrupt = true;
    142   }
    143 }