160 lines
5.0 KiB
C++
160 lines
5.0 KiB
C++
#include "globalhelper.hh"
|
|
#include "qdir.h"
|
|
#include "qsettings.h"
|
|
#include "qstandardpaths.h"
|
|
#include <array>
|
|
#include <QDir>
|
|
#include <QStandardPaths>
|
|
#include <QSettings>
|
|
#include <qt6/QtCore/qcontainerfwd.h>
|
|
#include <algorithm>
|
|
#include <vector>
|
|
|
|
const QString IPC_CONFIG_BASEDIR = QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation) + "/tactile";
|
|
|
|
const QString IPC_CONFIG = "sensor_ipc_config.ini";
|
|
|
|
const QString APP_VERSION = "0.2.0";
|
|
const QString PROFILES_GROUP = "profile";
|
|
|
|
namespace {
|
|
|
|
QString tactileTypeToString(Tactile_TYPE type) {
|
|
switch (type) {
|
|
case Tactile_TYPE::PiezoresistiveA:
|
|
return "压阻A型";
|
|
case Tactile_TYPE::PiezoresistiveB:
|
|
return "压阻B型";
|
|
case Tactile_TYPE::Hall:
|
|
return "霍尔型";
|
|
}
|
|
return "霍尔型";
|
|
}
|
|
|
|
Tactile_TYPE tactileTypeFromString(const QString& type) {
|
|
if (type == "压阻A型") {
|
|
return Tactile_TYPE::PiezoresistiveA;
|
|
}
|
|
if (type == "压阻B型") {
|
|
return Tactile_TYPE::PiezoresistiveB;
|
|
}
|
|
return Tactile_TYPE::Hall;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
GlobalHelper& GlobalHelper::instance() {
|
|
static GlobalHelper instance;
|
|
return instance;
|
|
}
|
|
|
|
GlobalHelper::GlobalHelper() {
|
|
QDir().mkpath(IPC_CONFIG_BASEDIR);
|
|
// qDebug() << "QDir: " << IPC_CONFIG_BASEDIR;
|
|
// ConfigProfile cfg{
|
|
// .name = "default",
|
|
// .type = Tactile_TYPE::Hall,
|
|
// .matrix_width = 3,
|
|
// .matrix_height = 4,
|
|
// .range_left = 200,
|
|
// .range_right = 300,
|
|
// .baud_rate = 115200
|
|
// };
|
|
// save_profile(cfg, 0);
|
|
load_profiles();
|
|
}
|
|
void GlobalHelper::load_profiles() {
|
|
QString str_ipc_config_filename = IPC_CONFIG_BASEDIR + "/" + IPC_CONFIG;
|
|
QSettings settings(str_ipc_config_filename, QSettings::IniFormat);
|
|
|
|
config_vec.clear();
|
|
|
|
settings.beginGroup(PROFILES_GROUP);
|
|
const auto profile_names = settings.childGroups();
|
|
for (const auto& profile_group_name : profile_names) {
|
|
settings.beginGroup(profile_group_name);
|
|
|
|
ConfigProfile profile;
|
|
profile.name = settings.value("profile_name", profile_group_name).toString();
|
|
profile.type = tactileTypeFromString(settings.value("type").toString());
|
|
profile.matrix_width = settings.value("matrix_width", 0).toInt();
|
|
profile.matrix_height = settings.value("matrix_height", 0).toInt();
|
|
profile.range_left = settings.value("range_left", 0).toInt();
|
|
profile.range_right = settings.value("range_right", 0).toInt();
|
|
profile.baud_rate = settings.value("baud_rate", 0).toInt();
|
|
|
|
config_vec.push_back(profile);
|
|
|
|
settings.endGroup();
|
|
}
|
|
settings.endGroup();
|
|
qDebug() << "profiles: " << config_vec.size();
|
|
}
|
|
|
|
void GlobalHelper::reload_profiles() {
|
|
load_profiles();
|
|
}
|
|
|
|
void GlobalHelper::save_profile(const ConfigProfile& profile, int is_default) {
|
|
QString str_ipc_config_filename = IPC_CONFIG_BASEDIR + "/" + IPC_CONFIG;
|
|
QSettings settings(str_ipc_config_filename, QSettings::IniFormat);
|
|
settings.beginGroup(PROFILES_GROUP);
|
|
settings.beginGroup(profile.name);
|
|
settings.setValue("profile_name", profile.name);
|
|
settings.setValue("type", tactileTypeToString(profile.type));
|
|
settings.setValue("matrix_width", profile.matrix_width);
|
|
settings.setValue("matrix_height", profile.matrix_height);
|
|
settings.setValue("range_left", profile.range_left);
|
|
settings.setValue("range_right", profile.range_right);
|
|
settings.setValue("baud_rate", profile.baud_rate);
|
|
settings.setValue("is_default", is_default);
|
|
settings.endGroup();
|
|
settings.endGroup();
|
|
}
|
|
|
|
bool GlobalHelper::add_new_profile(QString name, Tactile_TYPE type, int width, int height, int rl, int rr, int baud) {
|
|
ConfigProfile cfg{
|
|
.name = name,
|
|
.type = type,
|
|
.matrix_width = width,
|
|
.matrix_height = height,
|
|
.range_left = rl,
|
|
.range_right = rr,
|
|
.baud_rate = baud
|
|
};
|
|
|
|
return add_new_profile(cfg);
|
|
}
|
|
|
|
bool GlobalHelper::add_new_profile(const ConfigProfile& profile) {
|
|
auto item_find = std::find_if(config_vec.begin(), config_vec.end(), [profile](const ConfigProfile& p) {
|
|
return p.name == profile.name;
|
|
});
|
|
|
|
if (item_find == config_vec.end()) {
|
|
save_profile(profile);
|
|
config_vec.push_back(profile);
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void GlobalHelper::remove_profile(const QString& name) {
|
|
auto item_find = std::find_if(config_vec.begin(), config_vec.end(), [name](const ConfigProfile& p){
|
|
return p.name == name;
|
|
});
|
|
|
|
if (item_find != config_vec.end()) {
|
|
config_vec.erase(item_find);
|
|
QString str_ipc_config_filename = IPC_CONFIG_BASEDIR + "/" + IPC_CONFIG;
|
|
QSettings settings(str_ipc_config_filename, QSettings::IniFormat);
|
|
settings.beginGroup(PROFILES_GROUP);
|
|
settings.remove(name);
|
|
settings.endGroup();
|
|
}
|
|
}
|
|
|
|
std::vector<ConfigProfile>& GlobalHelper::get_all_profile() {
|
|
return config_vec;
|
|
}
|