From c9dbaf821c5654019dec0ac814c56ed54c3cacf3 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Tue, 2 Jun 2020 05:30:53 +0800 Subject: [PATCH] Template version of read_param (#1593) --- selfdrive/ui/ui.cc | 78 ++++++++++++++-------------------------------- 1 file changed, 23 insertions(+), 55 deletions(-) diff --git a/selfdrive/ui/ui.cc b/selfdrive/ui/ui.cc index e70197b268..31f6c17179 100644 --- a/selfdrive/ui/ui.cc +++ b/selfdrive/ui/ui.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include "common/util.h" @@ -135,61 +136,28 @@ static void set_do_exit(int sig) { do_exit = 1; } -static void read_param_bool(bool* param, const char* param_name, bool persistent_param = false) { +template +static int read_param(T* 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 = s[0] == '1'; - free(s); - } -} - -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); + int result = read_db_value(param_name, &s, NULL, persistent_param); + if (result == 0){ + std::istringstream iss(s); + iss >> *param; 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); - } - return result; -} - -static void read_param_bool_timeout(bool* param, const char* param_name, int* timeout, bool persistent_param = false) { - if (*timeout > 0){ - (*timeout)--; - } 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) { +template +static int read_param_timeout(T* param, const char* param_name, int* timeout, bool persistent_param = false) { + int result = -1; if (*timeout > 0){ (*timeout)--; } else { - read_param_float(param, param_name, persistent_param); *timeout = 2 * UI_FREQ; // 0.5Hz + result = read_param(param, param_name, persistent_param); } -} - -static int read_param_uint64_timeout(uint64_t* dest, const char* param_name, int* timeout, bool persistent_param = false) { - if (*timeout > 0){ - (*timeout)--; - return -1; - } else { - *timeout = 2 * UI_FREQ; // 0.5Hz - return read_param_uint64(dest, param_name, persistent_param); - } + return result; } 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, }}; - read_param_float(&s->speed_lim_off, "SpeedLimitOffset"); - read_param_bool(&s->is_metric, "IsMetric"); - read_param_bool(&s->longitudinal_control, "LongitudinalControl"); - read_param_bool(&s->limit_set_speed, "LimitSetSpeed"); + read_param(&s->speed_lim_off, "SpeedLimitOffset"); + read_param(&s->is_metric, "IsMetric"); + read_param(&s->longitudinal_control, "LongitudinalControl"); + read_param(&s->limit_set_speed, "LimitSetSpeed"); // Set offsets so params don't get read at the same time s->longitudinal_control_timeout = UI_FREQ / 3; @@ -829,8 +797,8 @@ int main(int argc, char* argv[]) { const int LEON = is_leon(); float brightness_b, brightness_m; - int result = read_param_float(&brightness_b, "BRIGHTNESS_B", true); - result += read_param_float(&brightness_m, "BRIGHTNESS_M", true); + int result = read_param(&brightness_b, "BRIGHTNESS_B", true); + result += read_param(&brightness_m, "BRIGHTNESS_M", true); if(result != 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; } - read_param_bool_timeout(&s->is_metric, "IsMetric", &s->is_metric_timeout); - read_param_bool_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_float_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); + read_param_timeout(&s->is_metric, "IsMetric", &s->is_metric_timeout); + read_param_timeout(&s->longitudinal_control, "LongitudinalControl", &s->longitudinal_control_timeout); + read_param_timeout(&s->limit_set_speed, "LimitSetSpeed", &s->limit_set_speed_timeout); + read_param_timeout(&s->speed_lim_off, "SpeedLimitOffset", &s->limit_set_speed_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 != 0) { // Failed to read param s->scene.athenaStatus = NET_DISCONNECTED;