primer refactoring

This commit is contained in:
richonguzman 2023-06-03 22:11:02 -04:00
parent a9e522e068
commit 642f3638c4
5 changed files with 158 additions and 113 deletions

View File

@ -15,8 +15,9 @@
#include "display.h"
#include "pins.h"
#include "power_management.h"
#include "lora_utils.h"
#define VERSION "2023.06.02"
#define VERSION "2023.06.03"
logging::Logger logger;
@ -30,10 +31,6 @@ OneButton userButton = OneButton(BUTTON_PIN, true, true);
HardwareSerial neo6m_gps(1);
TinyGPSPlus gps;
void validateConfigFile();
void setup_lora();
void setup_gps();
char *ax25_base91enc(char *s, uint8_t n, uint32_t v);
String createDateString(time_t t);
String createTimeString(time_t t);
@ -90,29 +87,6 @@ void setup_gps() {
neo6m_gps.begin(9600, SERIAL_8N1, GPS_TX, GPS_RX);
}
void setup_lora() {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", "Set SPI pins!");
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", "Set LoRa pins!");
LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);
long freq = Config.loramodule.frequency;
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", "frequency: %d", freq);
if (!LoRa.begin(freq)) {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "LoRa", "Starting LoRa failed!");
show_display("ERROR", "Starting LoRa failed!");
while (true) {
}
}
LoRa.setSpreadingFactor(Config.loramodule.spreadingFactor);
LoRa.setSignalBandwidth(Config.loramodule.signalBandwidth);
LoRa.setCodingRate4(Config.loramodule.codingRate4);
LoRa.enableCrc();
LoRa.setTxPower(Config.loramodule.power);
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", "LoRa init done!");
}
void loadMessagesFromMemory() {
File fileToRead;
noMessageWarning = false;
@ -144,15 +118,6 @@ void deleteFile() {
SPIFFS.remove("/aprsMessages.txt");
}
void sendNewLoraPacket(String newPacket) {
LoRa.beginPacket();
LoRa.write('<');
LoRa.write(0xFF);
LoRa.write(0x01);
LoRa.write((const uint8_t *)newPacket.c_str(), newPacket.length());
LoRa.endPacket();
}
void sendMessage(String station, String textMessage) {
String messageToSend;
for(int i = station.length(); i < 9; i++) {
@ -167,7 +132,7 @@ void sendMessage(String station, String textMessage) {
} else {
show_display("MSG Tx >>", "", messageToSend, 1000);
}
sendNewLoraPacket(messageToSend);
LoRaUtils::sendNewPacket(messageToSend);
}
static void ButtonSinglePress() {
@ -249,7 +214,7 @@ static void ButtonDoublePress() {
void startingStatus() {
delay(2000);
sendNewLoraPacket(currentBeacon->callsign + ">" + Config.destination + "," + Config.path + ":>" + Config.defaultStatus);
LoRaUtils::sendNewPacket(currentBeacon->callsign + ">" + Config.destination + "," + Config.path + ":>" + Config.defaultStatus);
statusAfterBootState = false;
}
@ -714,10 +679,10 @@ void setup() {
setup_display();
show_display(" LoRa APRS", "", " Richonguzman", " -- CD2RXU --", "", " " VERSION, 4000);
validateConfigFile();
Config.validateConfigFile(currentBeacon->callsign);
loadNumMessages();
setup_gps();
setup_lora();
LoRaUtils::setup();
WiFi.mode(WIFI_OFF);
btStop();
@ -942,7 +907,7 @@ void loop() {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_DEBUG, "Loop", "%s", data.c_str());
show_display("<<< TX >>>", "", data);
sendNewLoraPacket(data);
LoRaUtils::sendNewPacket(data);
if (currentBeacon->smartBeaconState) {
lastTxLat = gps.location.lat();
@ -1074,14 +1039,6 @@ void loop() {
}
/// FUNCTIONS ///
void validateConfigFile() {
if (currentBeacon->callsign == "NOCALL-7") {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "Config", "Change your settings in 'data/tracker_config.json' and upload it via 'Upload File System image'");
show_display("ERROR", "Change your settings", "'tracker_config.json'", "upload it via --> ", "'Upload File System image'");
while (true) {}
}
}
char *ax25_base91enc(char *s, uint8_t n, uint32_t v) {
/* Creates a Base-91 representation of the value in v in the string */
/* pointed to by s, n-characters long. String length should be n+1. */

75
src/configuration.cpp Normal file
View File

@ -0,0 +1,75 @@
#include "configuration.h"
#include "logger.h"
#include "display.h"
extern logging::Logger logger;
Configuration::Configuration(const String &filePath) {
_filePath = filePath;
if (!SPIFFS.begin(false)) {
Serial.println("SPIFFS Mount Failed");
return;
}
readFile(SPIFFS, _filePath.c_str());
}
void Configuration::readFile(fs::FS &fs, const char *fileName) {
StaticJsonDocument<2048> data;
File configFile = fs.open(fileName, "r");
DeserializationError error = deserializeJson(data, configFile);
if (error) {
Serial.println("Failed to read file, using default configuration");
}
JsonArray BeaconsArray = data["beacons"];
for (int i = 0; i < BeaconsArray.size(); i++) {
Beacon bcn;
bcn.callsign = BeaconsArray[i]["callsign"].as<String>();
bcn.symbol = BeaconsArray[i]["symbol"].as<String>();
bcn.comment = BeaconsArray[i]["comment"].as<String>();
bcn.smartBeaconState = BeaconsArray[i]["smart_beacon"]["active"].as<bool>();
bcn.slowRate = BeaconsArray[i]["smart_beacon"]["slowRate"].as<int>();
bcn.slowSpeed = BeaconsArray[i]["smart_beacon"]["slowSpeed"].as<int>();
bcn.fastRate = BeaconsArray[i]["smart_beacon"]["fastRate"].as<int>();
bcn.fastSpeed = BeaconsArray[i]["smart_beacon"]["fastSpeed"].as<int>();
bcn.minTxDist = BeaconsArray[i]["smart_beacon"]["minTxDist"].as<int>();
bcn.minDeltaBeacon = BeaconsArray[i]["smart_beacon"]["minDeltaBeacon"].as<int>();
bcn.turnMinDeg = BeaconsArray[i]["smart_beacon"]["turnMinDeg"].as<int>();
bcn.turnSlope = BeaconsArray[i]["smart_beacon"]["turnSlope"].as<int>();
beacons.push_back(bcn);
}
loramodule.frequency = data["lora"]["frequency"].as<long>();
loramodule.spreadingFactor = data["lora"]["spreadingFactor"].as<int>();
loramodule.signalBandwidth = data["lora"]["signalBandwidth"].as<long>();
loramodule.codingRate4 = data["lora"]["codingRate4"].as<int>();
loramodule.power = data["lora"]["power"].as<int>();
displayEcoMode = data["other"]["displayEcoMode"].as<bool>();
displayTimeout = data["other"]["displayTimeout"].as<int>();
destination = data["other"]["destination"].as<String>();
path = data["other"]["path"].as<String>();
overlay = data["other"]["overlay"].as<String>();
nonSmartBeaconRate = data["other"]["nonSmartBeaconRate"].as<int>();
listeningTrackerTime = data["other"]["listeningTrackerTime"].as<int>();
maxDistanceToTracker = data["other"]["maxDistanceToTracker"].as<int>();
defaultStatusAfterBoot = data["other"]["defaultStatusAfterBoot"].as<bool>();
defaultStatus = data["other"]["defaultStatus"].as<String>();
standingUpdateTime = data["other"]["standingUpdateTime"].as<int>();
sendAltitude = data["other"]["sendAltitude"].as<bool>();
configFile.close();
}
void Configuration::validateConfigFile(String currentBeaconCallsign) {
if (currentBeaconCallsign == "NOCALL-7") {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "Config", "Change your settings in 'data/tracker_config.json' and upload it via 'Upload File System image'");
show_display("ERROR", "Change your settings", "'tracker_config.json'", "upload it via --> ", "'Upload File System image'");
while (true) {
delay(1000);
}
}
}

View File

@ -32,7 +32,6 @@ public:
int power;
};
class Configuration {
public:
@ -49,74 +48,16 @@ public:
int maxDistanceToTracker;
bool defaultStatusAfterBoot;
String defaultStatus;
bool ecoMode;
bool standingUpdate;
int standingUpdateTime;
bool sendAltitude;
Configuration(String &filePath) {
_filePath = filePath;
if (!SPIFFS.begin(false)) {
Serial.println("SPIFFS Mount Failed");
return;
}
readFile(SPIFFS, _filePath.c_str());
}
Configuration(const String &filePath);
void validateConfigFile(String currentBeaconCallsign);
private:
Configuration() {}; // Hide default constructor
void readFile(fs::FS &fs, const char *fileName) ;
String _filePath;
void readFile(fs::FS &fs, const char *fileName) {
StaticJsonDocument<2048> data;
File configFile = fs.open(fileName, "r");
DeserializationError error = deserializeJson(data, configFile);
if (error) {
Serial.println("Failed to read file, using default configuration");
}
JsonArray BeaconsArray = data["beacons"];
for (int i = 0; i < BeaconsArray.size(); i++) {
Beacon bcn;
bcn.callsign = BeaconsArray[i]["callsign"].as<String>();
bcn.symbol = BeaconsArray[i]["symbol"].as<String>();
bcn.comment = BeaconsArray[i]["comment"].as<String>();
bcn.smartBeaconState = BeaconsArray[i]["smart_beacon"]["active"].as<bool>();
bcn.slowRate = BeaconsArray[i]["smart_beacon"]["slowRate"].as<int>();
bcn.slowSpeed = BeaconsArray[i]["smart_beacon"]["slowSpeed"].as<int>();
bcn.fastRate = BeaconsArray[i]["smart_beacon"]["fastRate"].as<int>();
bcn.fastSpeed = BeaconsArray[i]["smart_beacon"]["fastSpeed"].as<int>();
bcn.minTxDist = BeaconsArray[i]["smart_beacon"]["minTxDist"].as<int>();
bcn.minDeltaBeacon = BeaconsArray[i]["smart_beacon"]["minDeltaBeacon"].as<int>();
bcn.turnMinDeg = BeaconsArray[i]["smart_beacon"]["turnMinDeg"].as<int>();
bcn.turnSlope = BeaconsArray[i]["smart_beacon"]["turnSlope"].as<int>();
beacons.push_back(bcn);
}
loramodule.frequency = data["lora"]["frequency"].as<long>();
loramodule.spreadingFactor = data["lora"]["spreadingFactor"].as<int>();
loramodule.signalBandwidth = data["lora"]["signalBandwidth"].as<long>();
loramodule.codingRate4 = data["lora"]["codingRate4"].as<int>();
loramodule.power = data["lora"]["power"].as<int>();
displayEcoMode = data["other"]["displayEcoMode"].as<bool>();
displayTimeout = data["other"]["displayTimeout"].as<int>();
destination = data["other"]["destination"].as<String>();
path = data["other"]["path"].as<String>();
overlay = data["other"]["overlay"].as<String>();
nonSmartBeaconRate = data["other"]["nonSmartBeaconRate"].as<int>();
listeningTrackerTime = data["other"]["listeningTrackerTime"].as<int>();
maxDistanceToTracker = data["other"]["maxDistanceToTracker"].as<int>();
defaultStatusAfterBoot = data["other"]["defaultStatusAfterBoot"].as<bool>();
defaultStatus = data["other"]["defaultStatus"].as<String>();
ecoMode = data["other"]["ecoMode "].as<bool>();
standingUpdateTime = data["other"]["standingUpdateTime"].as<int>();
sendAltitude = data["other"]["sendAltitude"].as<bool>();
configFile.close();
}
};
#endif

61
src/lora_utils.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <LoRa.h>
#include <logger.h>
#include "configuration.h"
#include "display.h"
extern logging::Logger logger;
extern Configuration Config;
namespace LoRaUtils {
void setup() {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", "Set SPI pins!");
SPI.begin(LORA_SCK, LORA_MISO, LORA_MOSI, LORA_CS);
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", "Set LoRa pins!");
LoRa.setPins(LORA_CS, LORA_RST, LORA_IRQ);
long freq = Config.loramodule.frequency;
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", "frequency: %d", freq);
if (!LoRa.begin(freq)) {
logger.log(logging::LoggerLevel::LOGGER_LEVEL_ERROR, "LoRa", "Starting LoRa failed!");
show_display("ERROR", "Starting LoRa failed!");
//displayUtils::show("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);
logger.log(logging::LoggerLevel::LOGGER_LEVEL_INFO, "LoRa", "LoRa init done!");
}
void sendNewPacket(const String &newPacket) {
LoRa.beginPacket();
LoRa.write('<');
LoRa.write(0xFF);
LoRa.write(0x01);
LoRa.write((const uint8_t *)newPacket.c_str(), newPacket.length());
LoRa.endPacket();
}
/*String receivePacket() {
String loraPacket;
int packetSize = LoRa.parsePacket(); // Listening for LoRa Packets
if (packetSize) {
while (LoRa.available()) {
int inChar = LoRa.read();
loraPacket += (char)inChar;
}
}
return loraPacket;
}*/
}

11
src/lora_utils.h Normal file
View File

@ -0,0 +1,11 @@
#ifndef LORA_UTILS_H_
#define LORA_UTILS_H_
namespace LoRaUtils {
void setup();
void sendNewPacket(const String &newPacket);
//String receivePacket();
}
#endif