Refactor for better mgmt

pull/15/head
sh123 2020-06-14 21:55:27 +03:00
rodzic 752128197e
commit bec81153ff
4 zmienionych plików z 78 dodań i 59 usunięć

Wyświetl plik

@ -1,12 +1,12 @@
#include <timer.h> #include <timer.h>
#include "WiFi.h" #include "WiFi.h"
#include "loraprs.h" #include "loraprs_service.h"
#define LED_BUILTIN 2 #define LED_BUILTIN 2
#define LED_TOGGLE_PERIOD 1000 #define LED_TOGGLE_PERIOD 1000
LoraPrsConfig cfg; LoraPrs::Config cfg;
LoraPrs loraPrs; LoraPrs::Service loraPrsService;
auto watchdogLedTimer = timer_create_default(); auto watchdogLedTimer = timer_create_default();
@ -57,13 +57,13 @@ void setup() {
while (!Serial); while (!Serial);
initializeConfig(); initializeConfig();
loraPrs.setup(cfg); loraPrsService.setup(cfg);
watchdogLedTimer.every(LED_TOGGLE_PERIOD, toggleWatchdogLed); watchdogLedTimer.every(LED_TOGGLE_PERIOD, toggleWatchdogLed);
} }
void loop() { void loop() {
loraPrs.loop(); loraPrsService.loop();
watchdogLedTimer.tick(); watchdogLedTimer.tick();
} }

43
loraprs_config.h 100644
Wyświetl plik

@ -0,0 +1,43 @@
#ifndef LORAPRS_CONFIG_H
#define LORAPRS_CONFIG_H
#include <Arduino.h>
namespace LoraPrs {
struct Config
{
bool IsClientMode; // true - client mode, false - server mode
long LoraFreq; // lora frequency, e.g. 433.775e6
int LoraBw; // lora bandwidth, e.g. 125e3
byte LoraSf; // lora spreading factor, e.g. 12
byte LoraCodingRate; // lora coding rate, e.g. 7
byte LoraSync; // lora sync word/packet id, 0x3f
byte LoraPower; // lora power level in dbm, 20
int AprsPort; // aprs server port, 14580
String AprsHost; // aprs server hostname, rotate.aprs2.net
String AprsLogin; // aprs callsign to use, e.g. MY0CAL-1
String AprsPass; // aprs login password
String AprsFilter; // aprs filter, see http://www.aprs-is.net/javAPRSFilter.aspx
String AprsRawBeacon; // aprs string for server beacon, e.g. NOCALL-1>APZMDM,WIDE1-1:!0000.00N/00000.00E#LoRA 433.775MHz/BW125/SF12/CR7/0xf3
int AprsRawBeaconPeriodMinutes; // aprs beacon period
String BtName; // bluetooth device name for the client
String WifiSsid; // wifi access point name
String WifiKey; // wifi access point key
bool EnableSignalReport; // true - append signal report on server side for the client to be sent to APRS-IS
bool EnableAutoFreqCorrection; // true - correct own frequency based on received packet frequency deviation
bool EnablePersistentAprsConnection; // true - keep aprs-is connection active all the time
bool EnableRfToIs; // true - enable RF to APRS-IS submission
bool EnableIsToRf; // true - enable APRS-IS to RF submission
bool EnableRepeater; // true - digirepeat incoming packets based on WIDEn-n paths
bool EnableBeacon; // true - send AprsRawBeacon to RF and APRS-IS if EnableRfToIs is true
};
} // LoraPrs
#endif // LORAPRS_CONFIG_H

Wyświetl plik

@ -1,13 +1,15 @@
#include "loraprs.h" #include "loraprs_service.h"
LoraPrs::LoraPrs() namespace LoraPrs {
Service::Service()
: serialBt_() : serialBt_()
, kissState_(KissState::Void) , kissState_(KissState::Void)
, kissCmd_(KissCmd::NoCmd) , kissCmd_(KissCmd::NoCmd)
{ {
} }
void LoraPrs::setup(const LoraPrsConfig &conf) void Service::setup(const Config &conf)
{ {
previousBeaconMs_ = 0; previousBeaconMs_ = 0;
@ -52,7 +54,7 @@ void LoraPrs::setup(const LoraPrsConfig &conf)
} }
} }
void LoraPrs::setupWifi(const String &wifiName, const String &wifiKey) void Service::setupWifi(const String &wifiName, const String &wifiKey)
{ {
if (!isClient_) { if (!isClient_) {
Serial.print("WIFI connecting to " + wifiName); Serial.print("WIFI connecting to " + wifiName);
@ -70,7 +72,7 @@ void LoraPrs::setupWifi(const String &wifiName, const String &wifiKey)
} }
} }
void LoraPrs::reconnectWifi() void Service::reconnectWifi()
{ {
Serial.print("WIFI re-connecting..."); Serial.print("WIFI re-connecting...");
@ -83,7 +85,7 @@ void LoraPrs::reconnectWifi()
Serial.println("ok"); Serial.println("ok");
} }
bool LoraPrs::reconnectAprsis() bool Service::reconnectAprsis()
{ {
Serial.print("APRSIS connecting..."); Serial.print("APRSIS connecting...");
@ -97,7 +99,7 @@ bool LoraPrs::reconnectAprsis()
return true; return true;
} }
void LoraPrs::setupLora(int loraFreq, int bw, byte sf, byte cr, byte pwr, byte sync) void Service::setupLora(int loraFreq, int bw, byte sf, byte cr, byte pwr, byte sync)
{ {
Serial.print("LoRa init..."); Serial.print("LoRa init...");
@ -117,7 +119,7 @@ void LoraPrs::setupLora(int loraFreq, int bw, byte sf, byte cr, byte pwr, byte s
Serial.println("ok"); Serial.println("ok");
} }
void LoraPrs::setupBt(const String &btName) void Service::setupBt(const String &btName)
{ {
Serial.print("BT init " + btName + "..."); Serial.print("BT init " + btName + "...");
@ -130,7 +132,7 @@ void LoraPrs::setupBt(const String &btName)
} }
} }
void LoraPrs::loop() void Service::loop()
{ {
if (needsWifi() && WiFi.status() != WL_CONNECTED) { if (needsWifi() && WiFi.status() != WL_CONNECTED) {
reconnectWifi(); reconnectWifi();
@ -153,7 +155,7 @@ void LoraPrs::loop()
delay(10); delay(10);
} }
void LoraPrs::sendBeacon() void Service::sendBeacon()
{ {
long currentMs = millis(); long currentMs = millis();
@ -172,7 +174,7 @@ void LoraPrs::sendBeacon()
previousBeaconMs_ = currentMs; previousBeaconMs_ = currentMs;
} }
} }
void LoraPrs::sendToAprsis(String aprsMessage) void Service::sendToAprsis(String aprsMessage)
{ {
if (needsWifi() && WiFi.status() != WL_CONNECTED) { if (needsWifi() && WiFi.status() != WL_CONNECTED) {
reconnectWifi(); reconnectWifi();
@ -187,7 +189,7 @@ void LoraPrs::sendToAprsis(String aprsMessage)
} }
} }
void LoraPrs::onAprsisDataAvailable() void Service::onAprsisDataAvailable()
{ {
String aprsisData; String aprsisData;
@ -210,7 +212,7 @@ void LoraPrs::onAprsisDataAvailable()
} }
} }
bool LoraPrs::sendToLora(const AX25::Payload &payload) bool Service::sendToLora(const AX25::Payload &payload)
{ {
byte buf[512]; byte buf[512];
int bytesWritten = payload.ToBinary(buf, sizeof(buf)); int bytesWritten = payload.ToBinary(buf, sizeof(buf));
@ -225,7 +227,7 @@ bool LoraPrs::sendToLora(const AX25::Payload &payload)
return true; return true;
} }
void LoraPrs::onLoraDataAvailable(int packetSize) void Service::onLoraDataAvailable(int packetSize)
{ {
int rxBufIndex = 0; int rxBufIndex = 0;
byte rxBuf[packetSize]; byte rxBuf[packetSize];
@ -292,13 +294,13 @@ void LoraPrs::onLoraDataAvailable(int packetSize)
delay(50); delay(50);
} }
void LoraPrs::kissResetState() void Service::kissResetState()
{ {
kissCmd_ = KissCmd::NoCmd; kissCmd_ = KissCmd::NoCmd;
kissState_ = KissState::Void; kissState_ = KissState::Void;
} }
void LoraPrs::onBtDataAvailable() void Service::onBtDataAvailable()
{ {
while (serialBt_.available()) { while (serialBt_.available()) {
byte txByte = serialBt_.read(); byte txByte = serialBt_.read();
@ -355,3 +357,5 @@ void LoraPrs::onBtDataAvailable()
} }
delay(20); delay(20);
} }
} // LoraPrs

Wyświetl plik

@ -1,5 +1,5 @@
#ifndef LORAPRS_H #ifndef LORAPRS_SEVICE_H
#define LORAPRS_H #define LORAPRS_SERVICE_H
#include <Arduino.h> #include <Arduino.h>
#include <SPI.h> #include <SPI.h>
@ -8,46 +8,16 @@
#include "BluetoothSerial.h" #include "BluetoothSerial.h"
#include "ax25_payload.h" #include "ax25_payload.h"
#include "loraprs_config.h"
struct LoraPrsConfig namespace LoraPrs {
{
bool IsClientMode;
long LoraFreq;
int LoraBw;
byte LoraSf;
byte LoraCodingRate;
byte LoraSync;
byte LoraPower;
int AprsPort; class Service
String AprsHost;
String AprsLogin;
String AprsPass;
String AprsFilter;
String AprsRawBeacon;
int AprsRawBeaconPeriodMinutes;
String BtName;
String WifiSsid;
String WifiKey;
bool EnableSignalReport;
bool EnableAutoFreqCorrection;
bool EnablePersistentAprsConnection;
bool EnableRfToIs;
bool EnableIsToRf;
bool EnableRepeater;
bool EnableBeacon;
};
class LoraPrs
{ {
public: public:
LoraPrs(); Service();
void setup(const LoraPrsConfig &conf); void setup(const Config &conf);
void loop(); void loop();
private: private:
@ -132,4 +102,6 @@ private:
WiFiClient aprsisConn_; WiFiClient aprsisConn_;
}; };
#endif // LORAPRS_H } // LoraPrs
#endif // LORAPRS_SERVICE_H