Skip to content

Commit

Permalink
ui update
Browse files Browse the repository at this point in the history
  • Loading branch information
rav4kumar committed Nov 11, 2020
1 parent 894c7b2 commit b825ecf
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 1 deletion.
69 changes: 68 additions & 1 deletion selfdrive/ui/android/ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdlib.h>
#include <math.h>
#include <sys/resource.h>
#include <iostream>

#include <algorithm>

Expand Down Expand Up @@ -29,6 +30,61 @@ static void ui_set_brightness(UIState *s, int brightness) {
}
}

static void send_df(UIState *s, int status) {
MessageBuilder msg;
auto dfStatus = msg.initEvent().initDynamicFollowButton();
dfStatus.setStatus(status);
s->pm->send("dynamicFollowButton", msg);
}

static void send_ml(UIState *s, bool enabled) {
MessageBuilder msg;
auto mlStatus = msg.initEvent().initModelLongButton();
mlStatus.setEnabled(enabled);
s->pm->send("modelLongButton", msg);
}

static bool handle_df_touch(UIState *s, int touch_x, int touch_y) {
//dfButton manager
int padding = 40;
if ((1660 - padding <= touch_x) && (855 - padding <= touch_y)) {
s->scene.dfButtonStatus++;
if (s->scene.dfButtonStatus > 3) { s->scene.dfButtonStatus = 0; }
send_df(s, s->scene.dfButtonStatus);
printf("df button: %d\n", s->scene.dfButtonStatus);
return true;
}
return false;
}

static bool handle_ml_touch(UIState *s, int touch_x, int touch_y) {
//mlButton manager
int padding = 40;
int btn_w = 500;
int btn_h = 138;
int xs[2] = {1920 / 2 - btn_w / 2, 1920 / 2 + btn_w / 2};
int y_top = 915 - btn_h / 2;
if (xs[0] <= touch_x + padding && touch_x - padding <= xs[1] && y_top - padding <= touch_y) {
s->scene.mlButtonEnabled = !s->scene.mlButtonEnabled;
send_ml(s, s->scene.mlButtonEnabled);
printf("ml button: %d\n", s->scene.mlButtonEnabled);
return true;
}
return false;
}

static bool handle_SA_touched(UIState *s, int touch_x, int touch_y) {
if (s->active_app == cereal::UiLayoutState::App::NONE) { // if onroad (not settings or home)
if ((s->awake && s->vision_connected && s->status != STATUS_OFFROAD) || s->ui_debug) { // if car started or debug mode
if (handle_ml_touch(s, touch_x, touch_y)) {
s->scene.uilayout_sidebarcollapsed = true; // collapse sidebar when tapping any SA button
return true; // only allow one button to be pressed at a time
}
}
}
return false;
}

static void handle_display_state(UIState *s, bool user_input) {

static int awake_timeout = 0;
Expand Down Expand Up @@ -120,6 +176,7 @@ int main(int argc, char* argv[]) {
UIState uistate = {};
UIState *s = &uistate;
ui_init(s);
sa_init(s, true);
s->sound = &sound;

TouchState touch = {0};
Expand All @@ -146,10 +203,17 @@ int main(int argc, char* argv[]) {
const int MAX_VOLUME = LEON ? 15 : 12;
s->sound->setVolume(MIN_VOLUME);

bool last_started = s->started;
while (!do_exit) {
if (!s->started) {
usleep(50 * 1000);
}

if (s->started && !last_started) {
sa_init(s, false); // reset ml button and regrab params
}
last_started = s->started;

double u1 = millis_since_boot();

ui_update(s);
Expand All @@ -158,8 +222,11 @@ int main(int argc, char* argv[]) {
int touch_x = -1, touch_y = -1;
int touched = touch_poll(&touch, &touch_x, &touch_y, 0);
if (touched == 1) {
if (s->ui_debug) { printf("touched x: %d, y: %d\n", touch_x, touch_y); }
handle_sidebar_touch(s, touch_x, touch_y);
handle_vision_touch(s, touch_x, touch_y);
if (!handle_SA_touched(s, touch_x, touch_y)) { // if SA button not touched
handle_vision_touch(s, touch_x, touch_y);
}
}

// Don't waste resources on drawing in case screen is off
Expand Down
54 changes: 54 additions & 0 deletions selfdrive/ui/paint.cc
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,58 @@ static void ui_draw_driver_view(UIState *s) {

//dev ui

static void ui_draw_df_button(UIState *s) {
int btn_status = s->scene.dfButtonStatus;
int btn_w = 150;
int btn_h = 150;
int y_padding = 50;
int btn_x = 1920 - btn_w;
int btn_y = 1080 - btn_h - y_padding;
int btn_colors[4][3] = {{4, 67, 137}, {36, 168, 188}, {252, 255, 75}, {55, 184, 104}};

nvgBeginPath(s->vg);
nvgRoundedRect(s->vg, btn_x-110, btn_y-45, btn_w, btn_h, 100);
nvgStrokeColor(s->vg, nvgRGBA(btn_colors[btn_status][0], btn_colors[btn_status][1], btn_colors[btn_status][2], 255));
nvgStrokeWidth(s->vg, 11);
nvgStroke(s->vg);

nvgFillColor(s->vg, nvgRGBA(255, 255, 255, 255));
nvgFontSize(s->vg, 80);
nvgText(s->vg, btn_x - 38, btn_y + 30, "DF", NULL);

nvgFillColor(s->vg, nvgRGBA(255, 255, 255, 255));
nvgFontSize(s->vg, 45);
nvgText(s->vg, btn_x - 34, btn_y + 50 + 15, "profile", NULL);
}

static void ui_draw_ml_button(UIState *s) {
int btn_w = 500;
int btn_h = 138;
int x = 1920 / 2;
int y = 915;
int btn_x = x - btn_w / 2;
int btn_y = y - btn_h / 2;

nvgBeginPath(s->vg);
nvgRoundedRect(s->vg, btn_x, btn_y, btn_w, btn_h, 25);
if (s->scene.mlButtonEnabled) { // change outline color based on status of button
nvgStrokeColor(s->vg, nvgRGBA(55, 184, 104, 255));
} else {
nvgStrokeColor(s->vg, nvgRGBA(184, 55, 55, 255));
}
nvgStrokeWidth(s->vg, 12);
nvgStroke(s->vg);

nvgBeginPath(s->vg); // dark background for readability
nvgRoundedRect(s->vg, btn_x, btn_y, btn_w, btn_h, 25);
nvgFillColor(s->vg, nvgRGBA(75, 75, 75, 75));
nvgFill(s->vg);

nvgFillColor(s->vg, nvgRGBA(255, 255, 255, 255));
nvgFontSize(s->vg, 65);
nvgText(s->vg, x, y + btn_h / 8, "Toggle Model Long", NULL);
}

static void ui_draw_vision_header(UIState *s) {
const Rect &viz_rect = s->scene.viz_rect;
NVGpaint gradient = nvgLinearGradient(s->vg, viz_rect.x,
Expand Down Expand Up @@ -769,6 +821,8 @@ void ui_draw_bbui(UIState *s) {
static void ui_draw_vision_footer(UIState *s) {
ui_draw_vision_face(s);
ui_draw_bbui(s);
ui_draw_ml_button(s);
ui_draw_df_button(s);
//ui_draw_vision_brake(s);
}

Expand Down
29 changes: 29 additions & 0 deletions selfdrive/ui/ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <math.h>
#include <poll.h>
#include <sys/mman.h>
#include "json11.hpp"
#include <fstream>

#include "common/util.h"
#include "common/swaglog.h"
Expand All @@ -15,6 +17,8 @@
#include "ui.hpp"
#include "paint.hpp"

std::map<std::string, int> DF_TO_IDX = {{"close", 0}, {"normal", 1}, {"far", 2}, {"auto", 3}};

extern volatile sig_atomic_t do_exit;

int write_param_float(float param, const char* param_name, bool persistent_param) {
Expand All @@ -23,6 +27,31 @@ int write_param_float(float param, const char* param_name, bool persistent_param
return Params(persistent_param).write_db_value(param_name, s, size < sizeof(s) ? size : sizeof(s));
}

void sa_init(UIState *s, bool full_init) {
if (full_init) {
s->pm = new PubMaster({"dynamicFollowButton", "modelLongButton"});
}

s->ui_debug = false; // change to true while debugging

// stock additions todo: run opparams first (in main()?) to ensure json values exist
std::ifstream op_params_file("/data/op_params.json");
std::string op_params_content((std::istreambuf_iterator<char>(op_params_file)),
(std::istreambuf_iterator<char>()));

std::string err;
auto json = json11::Json::parse(op_params_content, err);
if (!json.is_null() && err.empty()) {
printf("successfully parsed opParams json\n");
s->scene.dfButtonStatus = DF_TO_IDX[json["dynamic_follow"].string_value()];
// printf("dfButtonStatus: %d\n", s->scene.dfButtonStatus);
} else { // error parsing json
printf("ERROR PARSING OPPARAMS JSON!\n");
s->scene.dfButtonStatus = 0;
}
s->scene.mlButtonEnabled = false; // state isn't saved yet
}

void ui_init(UIState *s) {
s->sm = new SubMaster({"model", "controlsState", "uiLayoutState", "liveCalibration", "radarState", "thermal",
"health", "carParams", "ubloxGnss", "driverState", "dMonitoringState", "sensorEvents", "carState"});
Expand Down
6 changes: 6 additions & 0 deletions selfdrive/ui/ui.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ static std::map<UIStatus, NVGcolor> bg_colors = {

typedef struct UIScene {

int dfButtonStatus;
bool mlButtonEnabled;

float mpc_x[50];
float mpc_y[50];

Expand Down Expand Up @@ -185,6 +188,7 @@ typedef struct UIState {
int img_network[6];

SubMaster *sm;
PubMaster *pm;

Sound *sound;
UIStatus status;
Expand Down Expand Up @@ -214,6 +218,7 @@ typedef struct UIState {
bool ignition;
bool is_metric;
bool longitudinal_control;
bool ui_debug;
uint64_t last_athena_ping;
uint64_t started_frame;

Expand All @@ -227,6 +232,7 @@ typedef struct UIState {
} UIState;

void ui_init(UIState *s);
void sa_init(UIState *s, bool full_init);
void ui_update(UIState *s);

int write_param_float(float param, const char* param_name, bool persistent_param = false);
Expand Down

0 comments on commit b825ecf

Please sign in to comment.