Skip to content

Commit

Permalink
settings
Browse files Browse the repository at this point in the history
  • Loading branch information
igagis committed Sep 8, 2024
1 parent 9d4f463 commit 62dbd56
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/bedsidemon/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ using namespace bedsidemon;

application::application(bool window, std::string_view res_path) :
ruisapp::application( //
"ruis-tests",
"bedsidemon"s,
[]() {
// NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers)
ruisapp::window_params wp(r4::vector2<unsigned>(1024, 600));
Expand Down
4 changes: 4 additions & 0 deletions src/bedsidemon/application.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <ruis/util/timer.hpp>
#include <ruisapp/application.hpp>

#include "settings.hpp"

#include "spo2/spo2_sensor.hpp"

namespace bedsidemon {
Expand All @@ -39,6 +41,8 @@ class application : public ruisapp::application
std::shared_ptr<ruis::timer> clock_timer;

public:
bedsidemon::settings_storage settings_storage;

application(bool window, std::string_view res_path);

static application& inst()
Expand Down
70 changes: 70 additions & 0 deletions src/bedsidemon/settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "settings.hpp"

#include <papki/fs_file.hpp>

#include <ruisapp/application.hpp>
#include <filesystem>

using namespace std::string_view_literals;

using namespace bedsidemon;

namespace{
constexpr const auto settings_filename = "settings.tml"sv;

constexpr const auto sweep_speed_key = "sweep_speed_um_per_sec"sv;
}

settings_storage::settings_storage() :
filename(utki::cat(ruisapp::application::inst().directory.config, settings_filename)),
settings_v(read(this->filename))
{}

settings settings_storage::read(std::string_view filename){
papki::fs_file fi(filename);

if(!fi.exists()){
LOG([](auto&o){o << "settings file not found, use default settings" << std::endl;})
return {};
}

settings setts;

auto tml = tml::read(fi);

for(const auto& t : tml){
if(t.value.to_string() == sweep_speed_key){
// TODO:
}
}

return setts;
}

void settings_storage::set(const settings& s){
this->settings_v = s;
this->write();
}

void settings_storage::write(){
settings default_values;

tml::forest tml;

auto add_setting = [&tml](std::string_view key, tml::leaf val){
tml.push_back(tml::tree(key, {val}));
};

if(this->settings_v.sweep_speed_um_per_sec != default_values.sweep_speed_um_per_sec){
add_setting(sweep_speed_key, tml::leaf(this->settings_v.sweep_speed_um_per_sec));
}

if(tml.empty()){
return;
}

std::filesystem::create_directories(ruisapp::application::inst().directory.config);

papki::fs_file fi(filename);
tml::write(tml, fi);
}
32 changes: 32 additions & 0 deletions src/bedsidemon/settings.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include <tml/tree.hpp>

namespace bedsidemon{

struct settings{
uint32_t sweep_speed_um_per_sec = 25000;
};

class settings_storage{
std::string filename;

settings settings_v;

static settings read(std::string_view filename);
void write();

public:
settings_storage();
~settings_storage(){
this->write();// TODO: remove?
}

const settings& get()const noexcept{
return this->settings_v;
}

void set(const settings& s);
};

}

0 comments on commit 62dbd56

Please sign in to comment.