LoRa_APRS_iGate/src/lora_utils.cpp

79 lines
2 KiB
C++
Raw Normal View History

2023-06-04 16:10:39 +02:00
#include <LoRa.h>
2023-06-06 17:21:59 +02:00
#include "configuration.h"
2023-06-04 16:10:39 +02:00
#include "display.h"
2023-06-08 23:09:05 +02:00
extern Configuration Config;
extern int stationMode;
2023-06-04 16:10:39 +02:00
2023-06-07 23:25:50 +02:00
namespace LoRa_Utils {
2023-06-04 16:10:39 +02:00
void setup() {
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);
2023-06-08 23:09:05 +02:00
long freq;
if (stationMode == 1 || stationMode == 2) {
freq = Config.loramodule.iGateFreq;
} else {
freq = Config.loramodule.digirepeaterTxFreq;
}
if (!LoRa.begin(freq)) {
2023-06-04 16:10:39 +02:00
Serial.println("Starting LoRa failed!");
show_display("ERROR", "Starting LoRa failed!");
while (true) {
delay(1000);
}
}
LoRa.setSpreadingFactor(Config.loramodule.spreadingFactor);
LoRa.setSignalBandwidth(Config.loramodule.signalBandwidth);
LoRa.setCodingRate4(Config.loramodule.codingRate4);
LoRa.enableCrc();
LoRa.setTxPower(Config.loramodule.power);
Serial.println("LoRa init done!\n");
}
void sendNewPacket(const String &typeOfMessage, const String &newPacket) {
LoRa.beginPacket();
LoRa.write('<');
if (typeOfMessage == "APRS") {
LoRa.write(0xFF);
} else if (typeOfMessage == "LoRa") {
LoRa.write(0xF8);
}
LoRa.write(0x01);
LoRa.write((const uint8_t *)newPacket.c_str(), newPacket.length());
LoRa.endPacket();
2023-06-06 19:14:47 +02:00
Serial.print("---> LoRa Packet Tx : ");
2023-06-04 16:10:39 +02:00
Serial.println(newPacket);
}
2023-06-06 20:37:47 +02:00
String generatePacket(String aprsisPacket) {
String firstPart, messagePart;
aprsisPacket.trim();
firstPart = aprsisPacket.substring(0, aprsisPacket.indexOf(","));
messagePart = aprsisPacket.substring(aprsisPacket.indexOf("::")+2);
return firstPart + ",TCPIP," + Config.callsign + "::" + messagePart;
}
2023-06-07 23:25:50 +02:00
String receivePacket() {
String loraPacket = "";
2023-06-04 16:10:39 +02:00
int packetSize = LoRa.parsePacket(); // Listening for LoRa Packets
if (packetSize) {
while (LoRa.available()) {
int inChar = LoRa.read();
loraPacket += (char)inChar;
}
}
return loraPacket;
2023-06-07 23:25:50 +02:00
}
2023-06-04 16:10:39 +02:00
2023-06-08 01:34:18 +02:00
void changeFreqTx() {
2023-06-08 02:55:38 +02:00
delay(500);
2023-06-08 23:09:05 +02:00
LoRa.setFrequency(Config.loramodule.digirepeaterTxFreq);
2023-06-08 01:34:18 +02:00
}
void changeFreqRx() {
2023-06-08 02:55:38 +02:00
delay(500);
2023-06-08 23:09:05 +02:00
LoRa.setFrequency(Config.loramodule.digirepeaterRxFreq);
2023-06-08 01:34:18 +02:00
}
2023-06-04 16:10:39 +02:00
}