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_Receive.ino (3719B)

      1 /*
      2    RadioLib Stream Receive Example
      3 
      4    This example shows how to receive 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 void setup() {
     39   Serial.begin(9600);
     40 
     41   // initialize SX1278 with default settings
     42   Serial.print(F("[SX1278] Initializing ... "));
     43   int state = radio.beginFSK();
     44   
     45   // when using one of the non-LoRa modules for Stream transmit
     46   // (RF69, CC1101, Si4432 etc.), use the basic begin() method
     47   // int state = radio.begin();
     48   
     49   if (state == RADIOLIB_ERR_NONE) {
     50     Serial.println(F("success!"));
     51   } else {
     52     Serial.print(F("failed, code "));
     53     Serial.println(state);
     54     while (true);
     55   }
     56 
     57   // set the function that will be called
     58   // when receive buffer is full
     59   radio.setFifoFullAction(fifoGet);
     60 
     61   // fixed packet length mode is required
     62   radio.fixedPacketLengthMode(0);
     63 
     64   // start listening for packets
     65   Serial.print(F("[SX1278] Starting to listen ... "));
     66   state = radio.startReceive();
     67   if (state == RADIOLIB_ERR_NONE) {
     68     Serial.println(F("success!"));
     69   } else {
     70     Serial.print(F("failed, code "));
     71     Serial.println(state);
     72     while (true);
     73   }
     74 
     75   // if needed, 'listen' mode can be disabled by calling
     76   // any of the following methods:
     77   //
     78   // radio.standby()
     79   // radio.sleep()
     80   // radio.transmit();
     81   // radio.receive();
     82   // radio.readData();
     83   // radio.scanChannel();
     84 }
     85 
     86 // flag to indicate that a packet was received
     87 volatile bool receivedFlag = false;
     88 
     89 // disable interrupt when it's not needed
     90 volatile bool enableInterrupt = true;
     91 
     92 // how many bytes are there in total
     93 const int totalLength = 512;
     94 
     95 // counter to keep track of how many bytes have been received so far
     96 volatile int receivedLength = 0;
     97 
     98 // buffer to save the received data into
     99 volatile uint8_t rxBuffer[totalLength + 1];
    100 
    101 // this function is called when the radio receive buffer
    102 // is full and ready to be read
    103 // IMPORTANT: this function MUST be 'void' type
    104 //            and MUST NOT have any arguments!
    105 #if defined(ESP8266) || defined(ESP32)
    106   ICACHE_RAM_ATTR
    107 #endif
    108 void fifoGet(void) {
    109   // check if the interrupt is enabled
    110   if(!enableInterrupt) {
    111     return;
    112   }
    113 
    114   // set the flag when we receive the full packet
    115   receivedFlag = radio.fifoGet(rxBuffer, totalLength, &receivedLength);
    116 }
    117 
    118 void loop() {
    119   // check if the flag is set
    120   if(receivedFlag) {
    121     // disable the interrupt service routine while
    122     // processing the data
    123     enableInterrupt = false;
    124   
    125     // packet was successfully received
    126     Serial.println(F("[SX1278] Received packet!"));
    127 
    128     // print data of the packet
    129     Serial.print(F("[SX1278] Data:\t\t"));
    130     Serial.println((char*)rxBuffer);
    131       
    132     // reset flag
    133     receivedFlag = false;
    134     receivedLength = 0;
    135     
    136     // put module back to listen mode
    137     radio.startReceive();
    138 
    139     // we're ready to receive more packets,
    140     // enable interrupt service routine
    141     enableInterrupt = true;
    142   }
    143 
    144 }