From 68d4627e99f62e723007a02fa489851ebe22c7e6 Mon Sep 17 00:00:00 2001 From: "Charles J. Cliffe" Date: Mon, 13 Apr 2015 19:58:34 -0400 Subject: [PATCH] Basic configuration file init and ppm value save/load test --- CMakeLists.txt | 2 + src/AppConfig.cpp | 100 ++++++++++++++++++++++++++++++++++++++++++++++ src/AppConfig.h | 21 ++++++++++ src/CubicSDR.cpp | 19 ++++++++- src/CubicSDR.h | 4 ++ 5 files changed, 145 insertions(+), 1 deletion(-) create mode 100644 src/AppConfig.cpp create mode 100644 src/AppConfig.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 2eb316e..cd4d4dd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -226,6 +226,7 @@ ENDIF (APPLE) SET (cubicsdr_sources src/CubicSDR.cpp src/AppFrame.cpp + src/AppConfig.cpp src/sdr/SDRThread.cpp src/sdr/SDRPostThread.cpp src/demod/DemodulatorPreThread.cpp @@ -267,6 +268,7 @@ SET (cubicsdr_headers src/CubicSDRDefs.h src/CubicSDR.h src/AppFrame.h + src/AppConfig.h src/sdr/SDRThread.h src/sdr/SDRPostThread.h src/demod/DemodulatorPreThread.h diff --git a/src/AppConfig.cpp b/src/AppConfig.cpp new file mode 100644 index 0000000..f676393 --- /dev/null +++ b/src/AppConfig.cpp @@ -0,0 +1,100 @@ +#include "AppConfig.h" + +std::string AppConfig::getConfigDir() { + + std::string dataDir = wxStandardPaths::Get().GetUserDataDir().ToStdString(); + + bool mkStatus = false; + + if (!wxDir::Exists(dataDir)) { + mkStatus = wxDir::Make(dataDir); + } else { + mkStatus = true; + } + + if (!mkStatus) { + std::cout << "Warning, unable to initialize user data directory." << std::endl; + } + + return dataDir; +} + +void AppConfig::setPPM(std::string device_serial, int ppm) { + device_ppm[device_serial] = ppm; +} + +int AppConfig::getPPM(std::string device_serial) { + if (device_ppm.find(device_serial) == device_ppm.end()) { + return 0; + } + return device_ppm[device_serial]; +} + +bool AppConfig::save() { + DataTree cfg; + + cfg.rootNode()->setName("cubicsdr_config"); + DataNode *ppm_data = cfg.rootNode()->newChild("ppm"); + + std::map::iterator device_ppm_i; + for (device_ppm_i = device_ppm.begin(); device_ppm_i != device_ppm.end(); device_ppm_i++) { + DataNode *ppm_ent = ppm_data->newChild("device"); + ppm_ent->newChild("id")->element()->set(device_ppm_i->first); + ppm_ent->newChild("value")->element()->set((int)device_ppm_i->second); + } + + std::string cfgFileDir = getConfigDir(); + + wxFileName cfgFile = wxFileName(cfgFileDir, "cubicsdr.xml"); + std::string cfgFileName = cfgFile.GetFullPath(wxPATH_NATIVE).ToStdString(); + + if (!cfg.SaveToFileXML(cfgFileName)) { + std::cout << "Error saving :: configuration file '" << cfgFileName << "' is not writable!" << std::endl; + return false; + } + + return true; +} + +bool AppConfig::load() { + DataTree cfg; + std::string cfgFileDir = getConfigDir(); + + wxFileName cfgFile = wxFileName(cfgFileDir, "cubicsdr.xml"); + std::string cfgFileName = cfgFile.GetFullPath(wxPATH_NATIVE).ToStdString(); + + if (!cfgFile.Exists()) { + return true; + } + + if (cfgFile.IsFileReadable()) { + cfg.LoadFromFileXML(cfgFileName); + } else { + std::cout << "Error loading:: configuration file '" << cfgFileName << "' is not readable!" << std::endl; + return false; + } + + if (cfg.rootNode()->hasAnother("ppm")) { + device_ppm.clear(); + + DataNode *ppm_data = cfg.rootNode()->getNext("ppm"); + + while (ppm_data->hasAnother("device")) { + DataNode *ppm_ent = ppm_data->getNext("device"); + + if (ppm_ent->hasAnother("id") && ppm_ent->hasAnother("value")) { + std::string deviceId(*ppm_ent->getNext("id")); + int ppmValue = *ppm_ent->getNext("value"); + setPPM(deviceId, ppmValue); + std::cout << "Loaded PPM for device '" << deviceId << "' at " << ppmValue << "ppm" << std::endl; + } + } + } + + return true; +} + +bool AppConfig::reset() { + + return true; +} diff --git a/src/AppConfig.h b/src/AppConfig.h new file mode 100644 index 0000000..d2cba42 --- /dev/null +++ b/src/AppConfig.h @@ -0,0 +1,21 @@ +#pragma once + +#include +#include +#include + +#include "DataTree.h" + +class AppConfig { +public: + + std::string getConfigDir(); + + void setPPM(std::string device_serial, int ppm); + int getPPM(std::string device_serial); + bool save(); + bool load(); + bool reset(); +private: + std::map device_ppm; +}; diff --git a/src/CubicSDR.cpp b/src/CubicSDR.cpp index 0c50fe9..4ed2979 100644 --- a/src/CubicSDR.cpp +++ b/src/CubicSDR.cpp @@ -34,8 +34,20 @@ bool CubicSDR::OnInit() { chdir(path); #endif - if (!wxApp::OnInit()) + if (!wxApp::OnInit()) { return false; + } + + wxApp:SetAppName("cubicsdr"); + + config.setPPM("RTLBlah :: 00000001",11); + config.setPPM("RTLBlah :: 00000002",12); + config.save(); + config.load(); + + std::cout << "test1: " << config.getPPM("RTLBlah :: 00000001") << std::endl; + std::cout << "test2: " << config.getPPM("RTLBlah :: 00000002") << std::endl; + std::cout << "test3: " << config.getPPM("foo") << std::endl; frequency = DEFAULT_FREQ; offset = 0; @@ -221,3 +233,8 @@ void CubicSDR::setDevice(int deviceId) { int CubicSDR::getDevice() { return sdrThread->getDeviceId(); } + + +AppConfig *CubicSDR::getConfig() { + return &config; +} diff --git a/src/CubicSDR.h b/src/CubicSDR.h index d841ad7..aead935 100644 --- a/src/CubicSDR.h +++ b/src/CubicSDR.h @@ -14,6 +14,7 @@ #include "SDRPostThread.h" #include "AudioThread.h" #include "DemodulatorMgr.h" +#include "AppConfig.h" #define NUM_DEMODULATORS 1 @@ -49,7 +50,10 @@ public: void bindDemodulator(DemodulatorInstance *demod); void removeDemodulator(DemodulatorInstance *demod); + AppConfig *getConfig(); + private: + AppConfig config; PrimaryGLContext *m_glContext; std::vector devs;