mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-08-21 08:14:00 +08:00
Params: python-like interface (#20506)
* rebase master * delete outdated test_params.c * putBool & more robust getBool * putBool(SshEnabled) old-commit-hash: 98e55996f67aaaf715c941d417ae4d3b0187f388
This commit is contained in:
@@ -134,12 +134,12 @@ void RequestRepeater::requestFinished(){
|
||||
if (reply->error() == QNetworkReply::NoError) {
|
||||
// save to cache
|
||||
if (!cache_key.isEmpty()) {
|
||||
Params().write_db_value(cache_key.toStdString(), response.toStdString());
|
||||
Params().put(cache_key.toStdString(), response.toStdString());
|
||||
}
|
||||
emit receivedResponse(response);
|
||||
} else {
|
||||
if (!cache_key.isEmpty()) {
|
||||
Params().delete_db_value(cache_key.toStdString());
|
||||
Params().remove(cache_key.toStdString());
|
||||
}
|
||||
emit failedResponse(reply->errorString());
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ HomeWindow::HomeWindow(QWidget* parent) : QWidget(parent) {
|
||||
void HomeWindow::mousePressEvent(QMouseEvent* e) {
|
||||
UIState* ui_state = &glWindow->ui_state;
|
||||
if (GLWindow::ui_state.scene.driver_view) {
|
||||
Params().write_db_value("IsDriverViewEnabled", "0", 1);
|
||||
Params().putBool("IsDriverViewEnabled", false);
|
||||
GLWindow::ui_state.scene.driver_view = false;
|
||||
return;
|
||||
}
|
||||
@@ -88,7 +88,7 @@ OffroadHome::OffroadHome(QWidget* parent) : QWidget(parent) {
|
||||
QObject::connect(alert_notification, SIGNAL(released()), this, SLOT(openAlerts()));
|
||||
header_layout->addWidget(alert_notification, 0, Qt::AlignHCenter | Qt::AlignRight);
|
||||
|
||||
std::string brand = Params().read_db_bool("Passive") ? "dashcam" : "openpilot";
|
||||
std::string brand = Params().getBool("Passive") ? "dashcam" : "openpilot";
|
||||
QLabel* version = new QLabel(QString::fromStdString(brand + " v" + Params().get("Version")));
|
||||
version->setStyleSheet(R"(font-size: 55px;)");
|
||||
header_layout->addWidget(version, 0, Qt::AlignHCenter | Qt::AlignRight);
|
||||
@@ -230,12 +230,8 @@ GLWindow::GLWindow(QWidget* parent) : brightness_filter(BACKLIGHT_OFFROAD, BACKL
|
||||
backlight_timer = new QTimer(this);
|
||||
QObject::connect(backlight_timer, SIGNAL(timeout()), this, SLOT(backlightUpdate()));
|
||||
|
||||
int result = read_param(&brightness_b, "BRIGHTNESS_B", true);
|
||||
result += read_param(&brightness_m, "BRIGHTNESS_M", true);
|
||||
if (result != 0) {
|
||||
brightness_b = 10.0;
|
||||
brightness_m = 0.1;
|
||||
}
|
||||
brightness_b = Params().get<float>("BRIGHTNESS_B").value_or(10.0);
|
||||
brightness_m = Params().get<float>("BRIGHTNESS_M").value_or(0.1);
|
||||
}
|
||||
|
||||
GLWindow::~GLWindow() {
|
||||
|
||||
@@ -127,13 +127,13 @@ OnboardingWindow::OnboardingWindow(QWidget *parent) : QStackedWidget(parent) {
|
||||
addWidget(terms);
|
||||
|
||||
connect(terms, &TermsPage::acceptedTerms, [=](){
|
||||
Params().write_db_value("HasAcceptedTerms", current_terms_version);
|
||||
Params().put("HasAcceptedTerms", current_terms_version);
|
||||
updateActiveScreen();
|
||||
});
|
||||
|
||||
TrainingGuide* tr = new TrainingGuide(this);
|
||||
connect(tr, &TrainingGuide::completedTraining, [=](){
|
||||
Params().write_db_value("CompletedTrainingVersion", current_training_version);
|
||||
Params().put("CompletedTrainingVersion", current_training_version);
|
||||
updateActiveScreen();
|
||||
});
|
||||
addWidget(tr);
|
||||
|
||||
@@ -61,7 +61,7 @@ QWidget * toggles_panel() {
|
||||
"In this mode openpilot will ignore lanelines and just drive how it thinks a human would.",
|
||||
"../assets/offroad/icon_road.png"));
|
||||
|
||||
bool record_lock = Params().read_db_bool("RecordFrontLock");
|
||||
bool record_lock = Params().getBool("RecordFrontLock");
|
||||
record_toggle->setEnabled(!record_lock);
|
||||
|
||||
QWidget *widget = new QWidget;
|
||||
@@ -87,29 +87,29 @@ DevicePanel::DevicePanel(QWidget* parent) : QWidget(parent) {
|
||||
offroad_btns.append(new ButtonControl("Driver Camera", "PREVIEW",
|
||||
"Preview the driver facing camera to help optimize device mounting position for best driver monitoring experience. (vehicle must be off)",
|
||||
[=]() {
|
||||
Params().write_db_value("IsDriverViewEnabled", "1", 1);
|
||||
Params().putBool("IsDriverViewEnabled", true);
|
||||
GLWindow::ui_state.scene.driver_view = true; }
|
||||
));
|
||||
|
||||
offroad_btns.append(new ButtonControl("Reset Calibration", "RESET",
|
||||
"openpilot requires the device to be mounted within 4° left or right and within 5° up or down. openpilot is continuously calibrating, resetting is rarely required.", [=]() {
|
||||
if (ConfirmationDialog::confirm("Are you sure you want to reset calibration?")) {
|
||||
Params().delete_db_value("CalibrationParams");
|
||||
Params().remove("CalibrationParams");
|
||||
}
|
||||
}));
|
||||
|
||||
offroad_btns.append(new ButtonControl("Review Training Guide", "REVIEW",
|
||||
"Review the rules, features, and limitations of openpilot", [=]() {
|
||||
if (ConfirmationDialog::confirm("Are you sure you want to review the training guide?")) {
|
||||
Params().delete_db_value("CompletedTrainingVersion");
|
||||
Params().remove("CompletedTrainingVersion");
|
||||
emit reviewTrainingGuide();
|
||||
}
|
||||
}));
|
||||
|
||||
QString brand = params.read_db_bool("Passive") ? "dashcam" : "openpilot";
|
||||
QString brand = params.getBool("Passive") ? "dashcam" : "openpilot";
|
||||
offroad_btns.append(new ButtonControl("Uninstall " + brand, "UNINSTALL", "", [=]() {
|
||||
if (ConfirmationDialog::confirm("Are you sure you want to uninstall?")) {
|
||||
Params().write_db_value("DoUninstall", "1");
|
||||
Params().putBool("DoUninstall", true);
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -161,7 +161,7 @@ DeveloperPanel::DeveloperPanel(QWidget* parent) : QFrame(parent) {
|
||||
|
||||
void DeveloperPanel::showEvent(QShowEvent *event) {
|
||||
Params params = Params();
|
||||
std::string brand = params.read_db_bool("Passive") ? "dashcam" : "openpilot";
|
||||
std::string brand = params.getBool("Passive") ? "dashcam" : "openpilot";
|
||||
QList<QPair<QString, std::string>> dev_params = {
|
||||
{"Version", brand + " v" + params.get("Version", false).substr(0, 14)},
|
||||
{"Git Branch", params.get("GitBranch", false)},
|
||||
|
||||
@@ -109,12 +109,11 @@ class ParamControl : public ToggleControl {
|
||||
public:
|
||||
ParamControl(const QString ¶m, const QString &title, const QString &desc, const QString &icon, QWidget *parent = nullptr) : ToggleControl(title, desc, icon, parent) {
|
||||
// set initial state from param
|
||||
if (Params().read_db_bool(param.toStdString().c_str())) {
|
||||
if (Params().getBool(param.toStdString().c_str())) {
|
||||
toggle.togglePosition();
|
||||
}
|
||||
QObject::connect(this, &ToggleControl::toggleFlipped, [=](int state) {
|
||||
char value = state ? '1' : '0';
|
||||
Params().write_db_value(param.toStdString().c_str(), &value, 1);
|
||||
Params().putBool(param.toStdString().c_str(), (bool)state);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
@@ -36,7 +36,7 @@ void DriveStats::parseResponse(QString response) {
|
||||
labels.hours->setText(QString::number((int)(obj["minutes"].toDouble() / 60)));
|
||||
};
|
||||
|
||||
bool metric = Params().read_db_bool("IsMetric");
|
||||
bool metric = Params().getBool("IsMetric");
|
||||
QJsonObject json = doc.object();
|
||||
update(json["all"].toObject(), all_, metric);
|
||||
update(json["week"].toObject(), week_, metric);
|
||||
@@ -51,7 +51,7 @@ DriveStats::DriveStats(QWidget* parent) : QWidget(parent) {
|
||||
gl->addLayout(build_stat_layout(&labels.hours, "HOURS"), row, 2, 3, 1);
|
||||
};
|
||||
|
||||
const char* distance_unit = Params().read_db_bool("IsMetric") ? "KM" : "MILES";
|
||||
const char* distance_unit = Params().getBool("IsMetric") ? "KM" : "MILES";
|
||||
QGridLayout* gl = new QGridLayout();
|
||||
gl->setMargin(0);
|
||||
gl->addWidget(new QLabel("ALL TIME"), 0, 0, 1, 3);
|
||||
|
||||
@@ -80,9 +80,9 @@ void OffroadAlert::refresh() {
|
||||
|
||||
void OffroadAlert::updateAlerts() {
|
||||
alertCount = 0;
|
||||
updateAvailable = params.read_db_bool("UpdateAvailable");
|
||||
updateAvailable = params.getBool("UpdateAvailable");
|
||||
for (const auto& [key, label] : alerts) {
|
||||
auto bytes = params.read_db_bytes(key.c_str());
|
||||
auto bytes = params.get(key.c_str());
|
||||
if (bytes.size()) {
|
||||
QJsonDocument doc_par = QJsonDocument::fromJson(QByteArray(bytes.data(), bytes.size()));
|
||||
QJsonObject obj = doc_par.object();
|
||||
|
||||
@@ -34,8 +34,8 @@ SshControl::SshControl() : AbstractControl("SSH Keys", "Warning: This grants SSH
|
||||
getUserKeys(username);
|
||||
}
|
||||
} else {
|
||||
Params().delete_db_value("GithubUsername");
|
||||
Params().delete_db_value("GithubSshKeys");
|
||||
Params().remove("GithubUsername");
|
||||
Params().remove("GithubSshKeys");
|
||||
refresh();
|
||||
}
|
||||
});
|
||||
@@ -89,8 +89,8 @@ void SshControl::parseResponse(){
|
||||
networkTimer->stop();
|
||||
QString response = reply->readAll();
|
||||
if (reply->error() == QNetworkReply::NoError && response.length()) {
|
||||
Params().write_db_value("GithubUsername", username.toStdString());
|
||||
Params().write_db_value("GithubSshKeys", response.toStdString());
|
||||
Params().put("GithubUsername", username.toStdString());
|
||||
Params().put("GithubSshKeys", response.toStdString());
|
||||
} else if(reply->error() == QNetworkReply::NoError){
|
||||
err = "Username '" + username + "' has no keys on GitHub";
|
||||
} else {
|
||||
|
||||
+6
-14
@@ -10,13 +10,6 @@
|
||||
#include "ui.hpp"
|
||||
#include "paint.hpp"
|
||||
|
||||
|
||||
int write_param_float(float param, const char* param_name, bool persistent_param) {
|
||||
char s[16];
|
||||
int size = snprintf(s, sizeof(s), "%f", param);
|
||||
return Params(persistent_param).write_db_value(param_name, s, size < sizeof(s) ? size : sizeof(s));
|
||||
}
|
||||
|
||||
// Projects a point in car to space to the corresponding point in full frame
|
||||
// image space.
|
||||
static bool calib_frame_to_full_frame(const UIState *s, float in_x, float in_y, float in_z, vertex_data *out) {
|
||||
@@ -279,14 +272,13 @@ static void update_alert(UIState *s) {
|
||||
static void update_params(UIState *s) {
|
||||
const uint64_t frame = s->sm->frame;
|
||||
UIScene &scene = s->scene;
|
||||
|
||||
Params params;
|
||||
if (frame % (5*UI_FREQ) == 0) {
|
||||
read_param(&scene.is_metric, "IsMetric");
|
||||
scene.is_metric = params.getBool("IsMetric");
|
||||
} else if (frame % (6*UI_FREQ) == 0) {
|
||||
scene.athenaStatus = NET_DISCONNECTED;
|
||||
uint64_t last_ping = 0;
|
||||
if (read_param(&last_ping, "LastAthenaPingTime") == 0) {
|
||||
scene.athenaStatus = nanos_since_boot() - last_ping < 70e9 ? NET_CONNECTED : NET_ERROR;
|
||||
if (auto last_ping = params.get<float>("LastAthenaPingTime"); last_ping) {
|
||||
scene.athenaStatus = nanos_since_boot() - *last_ping < 70e9 ? NET_CONNECTED : NET_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -329,8 +321,8 @@ static void update_status(UIState *s) {
|
||||
s->status = STATUS_DISENGAGED;
|
||||
s->scene.started_frame = s->sm->frame;
|
||||
|
||||
read_param(&s->scene.is_rhd, "IsRHD");
|
||||
read_param(&s->scene.end_to_end, "EndToEndToggle");
|
||||
s->scene.is_rhd = Params().getBool("IsRHD");
|
||||
s->scene.end_to_end = Params().getBool("EndToEndToggle");
|
||||
s->sidebar_collapsed = true;
|
||||
s->scene.alert_size = cereal::ControlsState::AlertSize::NONE;
|
||||
s->vipc_client = s->scene.driver_view ? s->vipc_client_front : s->vipc_client_rear;
|
||||
|
||||
@@ -171,28 +171,3 @@ typedef struct UIState {
|
||||
|
||||
void ui_init(UIState *s);
|
||||
void ui_update(UIState *s);
|
||||
|
||||
int write_param_float(float param, const char* param_name, bool persistent_param = false);
|
||||
template <class T>
|
||||
int read_param(T* param, const char *param_name, bool persistent_param = false){
|
||||
T param_orig = *param;
|
||||
char *value;
|
||||
size_t sz;
|
||||
|
||||
int result = Params(persistent_param).read_db_value(param_name, &value, &sz);
|
||||
if (result == 0){
|
||||
std::string s = std::string(value, sz); // value is not null terminated
|
||||
free(value);
|
||||
|
||||
// Parse result
|
||||
std::istringstream iss(s);
|
||||
iss >> *param;
|
||||
|
||||
// Restore original value if parsing failed
|
||||
if (iss.fail()) {
|
||||
*param = param_orig;
|
||||
result = -1;
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user