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

CC1101_Transmit_Interrupt.ino (3742B)

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