Basic configuration file init and ppm value save/load test

This commit is contained in:
Charles J. Cliffe 2015-04-13 19:58:34 -04:00
parent 4f2b9d93c3
commit 68d4627e99
5 changed files with 145 additions and 1 deletions

View File

@ -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

100
src/AppConfig.cpp Normal file
View File

@ -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<std::string, int>::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;
}

21
src/AppConfig.h Normal file
View File

@ -0,0 +1,21 @@
#pragma once
#include <wx/stdpaths.h>
#include <wx/dir.h>
#include <wx/filename.h>
#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<std::string, int> device_ppm;
};

View File

@ -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;
}

View File

@ -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<SDRDeviceInfo *> devs;