Template version of read_param (#1593)

pull/1620/head
Dean Lee 5 years ago committed by GitHub
parent d00cdf1e0c
commit c9dbaf821c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 76
      selfdrive/ui/ui.cc

@ -4,6 +4,7 @@
#include <unistd.h> #include <unistd.h>
#include <assert.h> #include <assert.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <sstream>
#include <sys/resource.h> #include <sys/resource.h>
#include <czmq.h> #include <czmq.h>
#include "common/util.h" #include "common/util.h"
@ -135,61 +136,28 @@ static void set_do_exit(int sig) {
do_exit = 1; do_exit = 1;
} }
static void read_param_bool(bool* param, const char* param_name, bool persistent_param = false) { template <class T>
static int read_param(T* param, const char *param_name, bool persistent_param = false){
char *s; char *s;
const int result = read_db_value(param_name, &s, NULL, persistent_param); int result = read_db_value(param_name, &s, NULL, persistent_param);
if (result == 0){ if (result == 0){
*param = s[0] == '1'; std::istringstream iss(s);
free(s); iss >> *param;
}
}
static int read_param_float(float* param, const char* param_name, bool persistent_param = false) {
char *s;
const int result = read_db_value(param_name, &s, NULL, persistent_param);
if (result == 0) {
*param = strtod(s, NULL);
free(s);
}
return result;
}
static int read_param_uint64(uint64_t* dest, const char* param_name, bool persistent_param = false) {
char *s;
const int result = read_db_value(param_name, &s, NULL, persistent_param);
if (result == 0) {
*dest = strtoull(s, NULL, 0);
free(s); free(s);
} }
return result; return result;
} }
static void read_param_bool_timeout(bool* param, const char* param_name, int* timeout, bool persistent_param = false) { template <class T>
if (*timeout > 0){ static int read_param_timeout(T* param, const char* param_name, int* timeout, bool persistent_param = false) {
(*timeout)--; int result = -1;
} else {
read_param_bool(param, param_name, persistent_param);
*timeout = 2 * UI_FREQ; // 0.5Hz
}
}
static void read_param_float_timeout(float* param, const char* param_name, int* timeout, bool persistent_param = false) {
if (*timeout > 0){
(*timeout)--;
} else {
read_param_float(param, param_name, persistent_param);
*timeout = 2 * UI_FREQ; // 0.5Hz
}
}
static int read_param_uint64_timeout(uint64_t* dest, const char* param_name, int* timeout, bool persistent_param = false) {
if (*timeout > 0){ if (*timeout > 0){
(*timeout)--; (*timeout)--;
return -1;
} else { } else {
*timeout = 2 * UI_FREQ; // 0.5Hz *timeout = 2 * UI_FREQ; // 0.5Hz
return read_param_uint64(dest, param_name, persistent_param); result = read_param(param, param_name, persistent_param);
} }
return result;
} }
static int write_param_float(float param, const char* param_name, bool persistent_param = false) { static int write_param_float(float param, const char* param_name, bool persistent_param = false) {
@ -277,10 +245,10 @@ static void ui_init_vision(UIState *s, const VisionStreamBufs back_bufs,
0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f,
}}; }};
read_param_float(&s->speed_lim_off, "SpeedLimitOffset"); read_param(&s->speed_lim_off, "SpeedLimitOffset");
read_param_bool(&s->is_metric, "IsMetric"); read_param(&s->is_metric, "IsMetric");
read_param_bool(&s->longitudinal_control, "LongitudinalControl"); read_param(&s->longitudinal_control, "LongitudinalControl");
read_param_bool(&s->limit_set_speed, "LimitSetSpeed"); read_param(&s->limit_set_speed, "LimitSetSpeed");
// Set offsets so params don't get read at the same time // Set offsets so params don't get read at the same time
s->longitudinal_control_timeout = UI_FREQ / 3; s->longitudinal_control_timeout = UI_FREQ / 3;
@ -829,8 +797,8 @@ int main(int argc, char* argv[]) {
const int LEON = is_leon(); const int LEON = is_leon();
float brightness_b, brightness_m; float brightness_b, brightness_m;
int result = read_param_float(&brightness_b, "BRIGHTNESS_B", true); int result = read_param(&brightness_b, "BRIGHTNESS_B", true);
result += read_param_float(&brightness_m, "BRIGHTNESS_M", true); result += read_param(&brightness_m, "BRIGHTNESS_M", true);
if(result != 0){ if(result != 0){
brightness_b = LEON ? 10.0 : 5.0; brightness_b = LEON ? 10.0 : 5.0;
@ -965,11 +933,11 @@ int main(int argc, char* argv[]) {
s->alert_sound = cereal::CarControl::HUDControl::AudibleAlert::NONE; s->alert_sound = cereal::CarControl::HUDControl::AudibleAlert::NONE;
} }
read_param_bool_timeout(&s->is_metric, "IsMetric", &s->is_metric_timeout); read_param_timeout(&s->is_metric, "IsMetric", &s->is_metric_timeout);
read_param_bool_timeout(&s->longitudinal_control, "LongitudinalControl", &s->longitudinal_control_timeout); read_param_timeout(&s->longitudinal_control, "LongitudinalControl", &s->longitudinal_control_timeout);
read_param_bool_timeout(&s->limit_set_speed, "LimitSetSpeed", &s->limit_set_speed_timeout); read_param_timeout(&s->limit_set_speed, "LimitSetSpeed", &s->limit_set_speed_timeout);
read_param_float_timeout(&s->speed_lim_off, "SpeedLimitOffset", &s->limit_set_speed_timeout); read_param_timeout(&s->speed_lim_off, "SpeedLimitOffset", &s->limit_set_speed_timeout);
int param_read = read_param_uint64_timeout(&s->last_athena_ping, "LastAthenaPingTime", &s->last_athena_ping_timeout); int param_read = read_param_timeout(&s->last_athena_ping, "LastAthenaPingTime", &s->last_athena_ping_timeout);
if (param_read != -1) { // Param was updated this loop if (param_read != -1) { // Param was updated this loop
if (param_read != 0) { // Failed to read param if (param_read != 0) { // Failed to read param
s->scene.athenaStatus = NET_DISCONNECTED; s->scene.athenaStatus = NET_DISCONNECTED;

Loading…
Cancel
Save