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

Lora.cpp (4016B)

      1 #include "Lora.h"
      2 
      3 
      4 SX1262 radio = new Module(RADIO_CS_PIN, RADIO_DIO1_PIN, RADIO_RST_PIN, RADIO_BUSY_PIN);
      5 
      6 
      7 void recvLoop() {
      8     String recv;
      9 
     10     while (true) {
     11         if (radio.available()) {
     12             int state = radio.readData(recv);
     13 
     14             if (state == RADIOLIB_ERR_NONE) {
     15                 playNotificationSound();
     16                 Serial.print(F("[RADIO] Received packet!"));
     17                 Serial.print(F(" Data:"));
     18                 Serial.print(recv);
     19                 Serial.print(F(" RSSI:"));
     20                 Serial.print(radio.getRSSI());
     21                 Serial.print(F(" dBm"));
     22                 Serial.print(F("  SNR:"));
     23                 Serial.print(radio.getSNR());
     24                 Serial.println(F(" dB"));
     25             } else if (state == RADIOLIB_ERR_CRC_MISMATCH) {
     26                 Serial.println(F("CRC error!"));
     27             } else {
     28                 Serial.print(F("Failed, code "));
     29                 Serial.println(state);
     30             }
     31         } else {
     32             Serial.println(F("Radio became unavailable!"));
     33             break;
     34         }
     35     }
     36 }
     37 
     38 
     39 bool setupRadio() {
     40     pinMode(RADIO_CS_PIN, OUTPUT);
     41     digitalWrite(RADIO_CS_PIN, HIGH);
     42 
     43     int state = radio.begin(RADIO_FREQ);
     44     
     45     if (state == RADIOLIB_ERR_NONE) {
     46         Serial.println("Start Radio success!");
     47     } else {
     48         Serial.print("Start Radio failed, code: ");
     49         Serial.println(state);
     50         return false;
     51     }
     52 
     53     if (radio.setFrequency(RADIO_FREQ) == RADIOLIB_ERR_INVALID_FREQUENCY) {
     54         Serial.println(F("Selected frequency is invalid for this module!"));
     55         return false;
     56     }
     57 
     58     if (radio.setBandwidth(125.0) == RADIOLIB_ERR_INVALID_BANDWIDTH) {
     59         Serial.println(F("Selected bandwidth is invalid for this module!"));
     60         return false;
     61     }
     62 
     63     if (radio.setSpreadingFactor(10) == RADIOLIB_ERR_INVALID_SPREADING_FACTOR) {
     64         Serial.println(F("Selected spreading factor is invalid for this module!"));
     65         return false;
     66     }
     67 
     68     if (radio.setCodingRate(6) == RADIOLIB_ERR_INVALID_CODING_RATE) {
     69         Serial.println(F("Selected coding rate is invalid for this module!"));
     70         return false;
     71     }
     72 
     73     if (radio.setSyncWord(0xAB) != RADIOLIB_ERR_NONE) {
     74         Serial.println(F("Unable to set sync word!"));
     75         return false;
     76     }
     77 
     78     // Set output power to 17 dBm (accepted range is -17 - 22 dBm)
     79     if (radio.setOutputPower(17) == RADIOLIB_ERR_INVALID_OUTPUT_POWER) {
     80         Serial.println(F("Selected output power is invalid for this module!"));
     81         return false;
     82     }
     83 
     84     // Set over current protection limit to 140 mA (accepted range is 45 - 140 mA)
     85     if (radio.setCurrentLimit(140) == RADIOLIB_ERR_INVALID_CURRENT_LIMIT) {
     86         Serial.println(F("Selected current limit is invalid for this module!"));
     87         return false;
     88     }
     89 
     90     // Set LoRa preamble length to 15 symbols (accepted range is 0 - 65535)
     91     if (radio.setPreambleLength(15) == RADIOLIB_ERR_INVALID_PREAMBLE_LENGTH) {
     92         Serial.println(F("Selected preamble length is invalid for this module!"));
     93         return false;
     94     }
     95 
     96     if (radio.setCRC(false) == RADIOLIB_ERR_INVALID_CRC_CONFIGURATION) {
     97         Serial.println(F("Selected CRC is invalid for this module!"));
     98         return false;
     99     }
    100 
    101     return true;
    102 }
    103 
    104 
    105 bool transmit() {
    106     int state = radio.transmit("Hello World!");
    107 
    108     if (state == RADIOLIB_ERR_NONE) {
    109         Serial.println(F("Radio transmission successful!"));
    110         Serial.print(F("[SX1262] Datarate:\t"));
    111         Serial.print(radio.getDataRate());
    112         Serial.println(F(" bps"));
    113         return true;
    114     } else if (state == RADIOLIB_ERR_PACKET_TOO_LONG) {
    115         Serial.println(F("Radio packet too long")); // 256 bytes is the maximum packet length
    116         return false;
    117     } else if (state == RADIOLIB_ERR_TX_TIMEOUT) {
    118         Serial.println(F("Radio timeout"));
    119         return false;
    120     } else {
    121         Serial.print(F("Radio error failed, code "));
    122         Serial.println(state);
    123         return false;
    124     }
    125 }