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

Stream_Transmit.ino (4664B)

      1 /*
      2    RadioLib Stream Transmit Example
      3 
      4    This example shows how to transmit data in "Stream" mode.
      5    In this mode, arbitrary length of data may be sent, up to
      6    "infinite" continuous transmission between two devices.
      7 
      8    Caveats:
      9     - CRC of the payload is not supported
     10     - the length of the payload must be known in advance
     11 
     12    Modules that can be used for Stream are:
     13     - SX127x/RFM9x (FSK mode only)
     14     - RF69
     15     - SX1231
     16 
     17    For default module settings, see the wiki page
     18    https://github.com/jgromes/RadioLib/wiki/Default-configuration#sx127xrfm9x---lora-modem
     19 
     20    For full API reference, see the GitHub Pages
     21    https://jgromes.github.io/RadioLib/
     22 */
     23 
     24 // include the library
     25 #include <RadioLib.h>
     26 
     27 // SX1278 has the following connections:
     28 // NSS pin:   10
     29 // DIO0 pin:  2
     30 // RESET pin: 9
     31 // DIO1 pin:  3
     32 SX1278 radio = new Module(10, 2, 9, 3);
     33 
     34 // or using RadioShield
     35 // https://github.com/jgromes/RadioShield
     36 //SX1278 radio = RadioShield.ModuleA;
     37 
     38 // save transmission state between loops
     39 int transmissionState = RADIOLIB_ERR_NONE;
     40 
     41 // this packet is much longer than would normally fit
     42 // into SX1278's internal buffer
     43 String longPacket = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.\
     44  Maecenas at urna ut nunc imperdiet laoreet. Aliquam erat volutpat.\
     45  Etiam mattis mauris vitae posuere tincidunt. In sit amet bibendum nisl,\
     46  a ultrices lorem. Duis hendrerit ultricies condimentum. Phasellus eget nisi\
     47  eget massa aliquam bibendum. Pellentesque ante neque, aliquam non diam non,\
     48  fringilla facilisis ipsum. Morbi in molestie orci. Vestibulum luctus\
     49  venenatis arcu sit amet pellentesque. Nulla posuere sit amet turpis\
     50  id pharetra. Curabitur nec.";
     51 
     52 void setup() {
     53   Serial.begin(9600);
     54 
     55   // initialize SX1278 with default settings
     56   Serial.print(F("[SX1278] Initializing ... "));
     57   int state = radio.beginFSK();
     58 
     59   // when using one of the non-LoRa modules for Stream transmit
     60   // (RF69, CC1101, Si4432 etc.), use the basic begin() method
     61   // int state = radio.begin();
     62 
     63   if (state == RADIOLIB_ERR_NONE) {
     64     Serial.println(F("success!"));
     65   } else {
     66     Serial.print(F("failed, code "));
     67     Serial.println(state);
     68     while (true);
     69   }
     70 
     71   // set the function that will be called
     72   // when transmit buffer is empty
     73   radio.setFifoEmptyAction(fifoAdd);
     74 
     75   // fixed packet length mode is required
     76   radio.fixedPacketLengthMode(0);
     77 
     78   // start transmitting the long packet
     79   Serial.print(F("[SX1278] Sending a very long packet ... "));
     80   transmissionState = radio.startTransmit(longPacket);
     81 }
     82 
     83 // flag to indicate that a packet was sent
     84 volatile bool transmittedFlag = false;
     85 
     86 // disable interrupt when it's not needed
     87 volatile bool enableInterrupt = true;
     88 
     89 // how many bytes are there in total
     90 volatile int totalLength = longPacket.length();
     91 
     92 // counter to keep track of how many bytes still need to be sent
     93 volatile int remLength = totalLength;
     94 
     95 // this function is called when the radio transmit buffer
     96 // is empty and ready to be refilled
     97 // IMPORTANT: this function MUST be 'void' type
     98 //            and MUST NOT have any arguments!
     99 #if defined(ESP8266) || defined(ESP32)
    100   ICACHE_RAM_ATTR
    101 #endif
    102 void fifoAdd(void) {
    103   // check if the interrupt is enabled
    104   if(!enableInterrupt) {
    105     return;
    106   }
    107 
    108   // add more bytes to the transmit buffer
    109   uint8_t* txBuffPtr = (uint8_t*)longPacket.c_str();
    110   transmittedFlag = radio.fifoAdd(txBuffPtr, totalLength, &remLength);
    111 }
    112 
    113 void loop() {
    114   // check if the previous transmission finished
    115   if(transmittedFlag) {
    116     // disable the interrupt service routine while
    117     // processing the data
    118     enableInterrupt = false;
    119 
    120     // reset flag
    121     transmittedFlag = false;
    122 
    123     // reset the counter
    124     remLength = totalLength;
    125 
    126     if (transmissionState == RADIOLIB_ERR_NONE) {
    127       // packet was successfully sent
    128       Serial.println(F("transmission finished!"));
    129 
    130       // NOTE: when using interrupt-driven transmit method,
    131       //       it is not possible to automatically measure
    132       //       transmission data rate using getDataRate()
    133 
    134     } else {
    135       Serial.print(F("failed, code "));
    136       Serial.println(transmissionState);
    137 
    138     }
    139 
    140     // NOTE: in FSK mode, SX127x will not automatically
    141     //       turn transmitter off after sending a packet
    142     //       set mode to standby to ensure we don't jam others
    143     radio.standby();
    144 
    145     // wait a second before transmitting again
    146     delay(1000);
    147 
    148     // send another one
    149     Serial.print(F("[SX1278] Sending another long packet ... "));
    150     transmissionState = radio.startTransmit(longPacket);
    151 
    152     // we're ready to send more packets,
    153     // enable interrupt service routine
    154     enableInterrupt = true;
    155   }
    156 }