Skip to content

Commit

Permalink
singleton uistate
Browse files Browse the repository at this point in the history
  • Loading branch information
deanlee committed Nov 4, 2021
1 parent b7394c6 commit abbbde4
Show file tree
Hide file tree
Showing 14 changed files with 47 additions and 65 deletions.
3 changes: 1 addition & 2 deletions selfdrive/ui/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ if arch in ['x86_64', 'Darwin'] or GetOption('extras'):
replay_lib = qt_env.Library("qt_replay", replay_lib_src, LIBS=base_libs)
replay_libs = [replay_lib, 'avutil', 'avcodec', 'avformat', 'bz2', 'curl', 'swscale'] + qt_libs
qt_env.Program("replay/replay", ["replay/main.cc"], LIBS=replay_libs)

qt_env.Program("watch3", ["watch3.cc"], LIBS=qt_libs + ['common', 'json11'])
qt_env.Program("watch3", ["watch3.cc"], LIBS=qt_libs + ['common', 'json11', 'zmq', 'visionipc', 'messaging'])

if GetOption('test'):
qt_env.Program('replay/tests/test_replay', ['replay/tests/test_runner.cc', 'replay/tests/test_replay.cc'], LIBS=[replay_libs])
7 changes: 2 additions & 5 deletions selfdrive/ui/qt/home.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ HomeWindow::HomeWindow(QWidget* parent) : QWidget(parent) {

sidebar = new Sidebar(this);
main_layout->addWidget(sidebar);
QObject::connect(this, &HomeWindow::update, sidebar, &Sidebar::updateState);
QObject::connect(uiState(), &UIState::uiUpdate, sidebar, &Sidebar::updateState);
QObject::connect(sidebar, &Sidebar::openSettings, this, &HomeWindow::openSettings);

slayout = new QStackedLayout();
Expand All @@ -31,15 +31,13 @@ HomeWindow::HomeWindow(QWidget* parent) : QWidget(parent) {
onroad = new OnroadWindow(this);
slayout->addWidget(onroad);

QObject::connect(this, &HomeWindow::update, onroad, &OnroadWindow::updateStateSignal);
QObject::connect(this, &HomeWindow::offroadTransitionSignal, onroad, &OnroadWindow::offroadTransitionSignal);

driver_view = new DriverViewWindow(this);
connect(driver_view, &DriverViewWindow::done, [=] {
showDriverView(false);
});
slayout->addWidget(driver_view);
setAttribute(Qt::WA_NoSystemBackground);
QObject::connect(uiState(), &UIState::offroadTransition, this, &HomeWindow::offroadTransition);
}

void HomeWindow::showSidebar(bool show) {
Expand All @@ -53,7 +51,6 @@ void HomeWindow::offroadTransition(bool offroad) {
} else {
slayout->setCurrentWidget(onroad);
}
emit offroadTransitionSignal(offroad);
}

void HomeWindow::showDriverView(bool show) {
Expand Down
4 changes: 0 additions & 4 deletions selfdrive/ui/qt/home.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@ class HomeWindow : public QWidget {
void openSettings();
void closeSettings();

// forwarded signals
void update(const UIState &s);
void offroadTransitionSignal(bool offroad);

public slots:
void offroadTransition(bool offroad);
void showDriverView(bool show);
Expand Down
8 changes: 4 additions & 4 deletions selfdrive/ui/qt/maps/map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void MapWindow::initLayers() {
}

void MapWindow::timerUpdate() {
if (!QUIState::ui_state.scene.started) {
if (!uiState()->scene.started) {
return;
}

Expand Down Expand Up @@ -275,7 +275,7 @@ static float get_time_typical(const QGeoRouteSegment &segment) {


void MapWindow::recomputeRoute() {
if (!QUIState::ui_state.scene.started) {
if (!uiState()->scene.started) {
return;
}

Expand Down Expand Up @@ -551,7 +551,7 @@ MapInstructions::MapInstructions(QWidget * parent) : QWidget(parent) {
void MapInstructions::updateDistance(float d) {
QString distance_str;

if (QUIState::ui_state.scene.is_metric) {
if (uiState()->scene.is_metric) {
if (d > 500) {
distance_str.setNum(d / 1000, 'f', 1);
distance_str += " km";
Expand Down Expand Up @@ -788,7 +788,7 @@ void MapETA::updateETA(float s, float s_typical, float d) {
// Distance
QString distance_str;
float num = 0;
if (QUIState::ui_state.scene.is_metric) {
if (uiState()->scene.is_metric) {
num = d / 1000.0;
distance_unit->setText("km");
} else {
Expand Down
4 changes: 2 additions & 2 deletions selfdrive/ui/qt/offroad/settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ DevicePanel::DevicePanel(QWidget* parent) : ListWidget(parent) {

for (auto btn : {dcamBtn, resetCalibBtn, retrainingBtn, regulatoryBtn}) {
if (btn) {
connect(parent, SIGNAL(offroadTransition(bool)), btn, SLOT(setEnabled(bool)));
connect(uiState(), &UIState::offroadTransition, btn, &QPushButton::setEnabled);
addItem(btn);
}
}
Expand Down Expand Up @@ -218,7 +218,7 @@ SoftwarePanel::SoftwarePanel(QWidget* parent) : ListWidget(parent) {
Params().putBool("DoUninstall", true);
}
});
connect(parent, SIGNAL(offroadTransition(bool)), uninstallBtn, SLOT(setEnabled(bool)));
connect(uiState(), &UIState::offroadTransition, uninstallBtn, &QPushButton::setEnabled);

QWidget *widgets[] = {versionLbl, lastUpdateLbl, updateBtn, gitBranchLbl, gitCommitLbl, osVersionLbl, uninstallBtn};
for (QWidget* w : widgets) {
Expand Down
1 change: 0 additions & 1 deletion selfdrive/ui/qt/offroad/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ class SettingsWindow : public QFrame {

signals:
void closeSettings();
void offroadTransition(bool offroad);
void reviewTrainingGuide();
void showDriverView();

Expand Down
12 changes: 6 additions & 6 deletions selfdrive/ui/qt/onroad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ OnroadWindow::OnroadWindow(QWidget *parent) : QWidget(parent) {
alerts->raise();

setAttribute(Qt::WA_OpaquePaintEvent);
QObject::connect(this, &OnroadWindow::updateStateSignal, this, &OnroadWindow::updateState);
QObject::connect(this, &OnroadWindow::offroadTransitionSignal, this, &OnroadWindow::offroadTransition);
QObject::connect(uiState(), &UIState::uiUpdate, this, &OnroadWindow::updateState);
QObject::connect(uiState(), &UIState::offroadTransition, this, &OnroadWindow::offroadTransition);
}

void OnroadWindow::updateState(const UIState &s) {
Expand Down Expand Up @@ -79,7 +79,7 @@ void OnroadWindow::mousePressEvent(QMouseEvent* e) {
void OnroadWindow::offroadTransition(bool offroad) {
#ifdef ENABLE_MAPS
if (!offroad) {
if (map == nullptr && (QUIState::ui_state.has_prime || !MAPBOX_TOKEN.isEmpty())) {
if (map == nullptr && (uiState()->has_prime || !MAPBOX_TOKEN.isEmpty())) {
QMapboxGLSettings settings;

// Valid for 4 weeks since we can't swap tokens on the fly
Expand All @@ -94,7 +94,7 @@ void OnroadWindow::offroadTransition(bool offroad) {

MapWindow * m = new MapWindow(settings);
m->setFixedWidth(width() / 2 - bdr_s);
QObject::connect(this, &OnroadWindow::offroadTransitionSignal, m, &MapWindow::offroadTransition);
QObject::connect(uiState(), &UIState::offroadTransition, m, &MapWindow::offroadTransition);
split->addWidget(m, 0, Qt::AlignRight);
map = m;
}
Expand Down Expand Up @@ -181,14 +181,14 @@ void NvgWindow::initializeGL() {
qInfo() << "OpenGL renderer:" << QString((const char*)glGetString(GL_RENDERER));
qInfo() << "OpenGL language version:" << QString((const char*)glGetString(GL_SHADING_LANGUAGE_VERSION));

ui_nvg_init(&QUIState::ui_state);
ui_nvg_init(uiState());
prev_draw_t = millis_since_boot();
setBackgroundColor(bg_colors[STATUS_DISENGAGED]);
}

void NvgWindow::paintGL() {
CameraViewWidget::paintGL();
ui_draw(&QUIState::ui_state, width(), height());
ui_draw(uiState(), width(), height());

double cur_draw_t = millis_since_boot();
double dt = cur_draw_t - prev_draw_t;
Expand Down
4 changes: 0 additions & 4 deletions selfdrive/ui/qt/onroad.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ class OnroadWindow : public QWidget {
QWidget *map = nullptr;
QHBoxLayout* split;

signals:
void updateStateSignal(const UIState &s);
void offroadTransitionSignal(bool offroad);

private slots:
void offroadTransition(bool offroad);
void updateState(const UIState &s);
Expand Down
2 changes: 1 addition & 1 deletion selfdrive/ui/qt/request_repeater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ RequestRepeater::RequestRepeater(QObject *parent, const QString &requestURL, con
timer = new QTimer(this);
timer->setTimerType(Qt::VeryCoarseTimer);
QObject::connect(timer, &QTimer::timeout, [=]() {
if ((!QUIState::ui_state.scene.started || while_onroad) && QUIState::ui_state.awake && !active()) {
if ((!uiState()->scene.started || while_onroad) && uiState()->awake && !active()) {
sendRequest(requestURL);
}
});
Expand Down
2 changes: 1 addition & 1 deletion selfdrive/ui/qt/widgets/prime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ void SetupWidget::replyFinished(const QString &response) {
if (!json["prime"].toBool()) {
mainLayout->setCurrentWidget(primeAd);
} else {
QUIState::ui_state.has_prime = true;
uiState()->has_prime = true;
mainLayout->setCurrentWidget(primeUser);
}
}
Expand Down
10 changes: 3 additions & 7 deletions selfdrive/ui/qt/window.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,10 @@ MainWindow::MainWindow(QWidget *parent) : QWidget(parent) {
main_layout->addWidget(homeWindow);
QObject::connect(homeWindow, &HomeWindow::openSettings, this, &MainWindow::openSettings);
QObject::connect(homeWindow, &HomeWindow::closeSettings, this, &MainWindow::closeSettings);
QObject::connect(&qs, &QUIState::uiUpdate, homeWindow, &HomeWindow::update);
QObject::connect(&qs, &QUIState::offroadTransition, homeWindow, &HomeWindow::offroadTransition);
QObject::connect(&qs, &QUIState::offroadTransition, homeWindow, &HomeWindow::offroadTransitionSignal);

settingsWindow = new SettingsWindow(this);
main_layout->addWidget(settingsWindow);
QObject::connect(settingsWindow, &SettingsWindow::closeSettings, this, &MainWindow::closeSettings);
QObject::connect(&qs, &QUIState::offroadTransition, settingsWindow, &SettingsWindow::offroadTransition);
QObject::connect(settingsWindow, &SettingsWindow::reviewTrainingGuide, [=]() {
onboardingWindow->showTrainingGuide();
main_layout->setCurrentWidget(onboardingWindow);
Expand All @@ -38,8 +34,8 @@ MainWindow::MainWindow(QWidget *parent) : QWidget(parent) {
}

device.setAwake(true, true);
QObject::connect(&qs, &QUIState::uiUpdate, &device, &Device::update);
QObject::connect(&qs, &QUIState::offroadTransition, [=](bool offroad) {
QObject::connect(uiState(), &UIState::uiUpdate, &device, &Device::update);
QObject::connect(uiState(), &UIState::offroadTransition, [=](bool offroad) {
if (!offroad) {
closeSettings();
}
Expand Down Expand Up @@ -80,7 +76,7 @@ void MainWindow::openSettings() {
void MainWindow::closeSettings() {
main_layout->setCurrentWidget(homeWindow);

if (QUIState::ui_state.scene.started) {
if (uiState()->scene.started) {
emit homeWindow->showSidebar(false);
}
}
Expand Down
1 change: 0 additions & 1 deletion selfdrive/ui/qt/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class MainWindow : public QWidget {
void closeSettings();

Device device;
QUIState qs;

QStackedLayout *main_layout;
HomeWindow *homeWindow;
Expand Down
33 changes: 19 additions & 14 deletions selfdrive/ui/ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,33 +222,33 @@ static void update_status(UIState *s) {
}


QUIState::QUIState(QObject *parent) : QObject(parent) {
ui_state.sm = std::make_unique<SubMaster, const std::initializer_list<const char *>>({
UIState::UIState(QObject *parent) : QObject(parent) {
sm = std::make_unique<SubMaster, const std::initializer_list<const char *>>({
"modelV2", "controlsState", "liveCalibration", "deviceState", "roadCameraState",
"pandaStates", "carParams", "driverMonitoringState", "sensorEvents", "carState", "liveLocationKalman",
});

ui_state.wide_camera = Hardware::TICI() ? Params().getBool("EnableWideCamera") : false;
wide_camera = Hardware::TICI() ? Params().getBool("EnableWideCamera") : false;

// update timer
timer = new QTimer(this);
QObject::connect(timer, &QTimer::timeout, this, &QUIState::update);
QObject::connect(timer, &QTimer::timeout, this, &UIState::update);
timer->start(1000 / UI_FREQ);
}

void QUIState::update() {
update_params(&ui_state);
update_sockets(&ui_state);
update_state(&ui_state);
update_status(&ui_state);
void UIState::update() {
update_params(this);
update_sockets(this);
update_state(this);
update_status(this);

if (ui_state.scene.started != started_prev || ui_state.sm->frame == 1) {
started_prev = ui_state.scene.started;
emit offroadTransition(!ui_state.scene.started);
if (scene.started != started_prev || sm->frame == 1) {
started_prev = scene.started;
emit offroadTransition(!scene.started);
}

watchdog_kick();
emit uiUpdate(ui_state);
emit uiUpdate(*this);
}

Device::Device(QObject *parent) : brightness_filter(BACKLIGHT_OFFROAD, BACKLIGHT_TS, BACKLIGHT_DT), QObject(parent) {
Expand All @@ -259,7 +259,7 @@ void Device::update(const UIState &s) {
updateWakefulness(s);

// TODO: remove from UIState and use signals
QUIState::ui_state.awake = awake;
uiState()->awake = awake;
}

void Device::setAwake(bool on, bool reset) {
Expand Down Expand Up @@ -319,3 +319,8 @@ void Device::updateWakefulness(const UIState &s) {

setAwake(awake_timeout, should_wake);
}

UIState *uiState() {
static UIState ui_state;
return &ui_state;
}
21 changes: 8 additions & 13 deletions selfdrive/ui/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@ typedef struct UIScene {
uint64_t started_frame;
} UIScene;

typedef struct UIState {
class UIState : public QObject {
Q_OBJECT

public:
UIState(QObject* parent = 0);

int fb_w = 0, fb_h = 0;
NVGcontext *vg;

Expand All @@ -129,22 +134,11 @@ typedef struct UIState {
UIStatus status;
UIScene scene = {};

bool awake;
bool awake = true;
bool has_prime = false;

float car_space_transform[6];
bool wide_camera;
} UIState;


class QUIState : public QObject {
Q_OBJECT

public:
QUIState(QObject* parent = 0);

// TODO: get rid of this, only use signal
inline static UIState ui_state = {0};

signals:
void uiUpdate(const UIState &s);
Expand All @@ -158,6 +152,7 @@ private slots:
bool started_prev = true;
};

UIState *uiState();

// device management class

Expand Down

0 comments on commit abbbde4

Please sign in to comment.