This commit is contained in:
2026-02-14 14:30:25 +01:00
parent d3c0cb1066
commit ee5c156e56
48 changed files with 549 additions and 510 deletions
+38 -20
View File
@@ -25,17 +25,24 @@ THE SOFTWARE.
#pragma once
#define ITERABLE_ENUM_BEGIN(name) enum class name : int {
#define ITERABLE_ENUM_END ,end, begin = 0 }
#define ITERABLE_ENUM_BEGIN(name) \
enum class name : int \
{
#define ITERABLE_ENUM_END \
, end, begin = 0 \
}
template <class T>
class Iterable
template <class T> class Iterable
{
public:
Iterable(T value = T::begin) : mValue(value) {}
Iterable(const Iterable& other) : mValue(other.mValue) {}
public:
Iterable(T value = T::begin) : mValue(value)
{
}
Iterable(const Iterable &other) : mValue(other.mValue)
{
}
Iterable& operator++()
Iterable &operator++()
{
assert(mValue >= T::begin && mValue < T::end);
mValue = static_cast<T>(static_cast<int>(mValue) + 1);
@@ -49,7 +56,7 @@ public:
return prev;
}
Iterable& operator--()
Iterable &operator--()
{
assert(mValue >= T::end && mValue < T::begin);
mValue = static_cast<T>(static_cast<int>(mValue) - 1);
@@ -83,38 +90,49 @@ public:
return static_cast<T>(static_cast<int>(mValue) - static_cast<int>(i));
}
bool operator >(const T& other) const
bool operator>(const T &other) const
{
return static_cast<int>(mValue) > static_cast<int>(other);
}
bool operator >=(const T& other) const
bool operator>=(const T &other) const
{
return static_cast<int>(mValue) >= static_cast<int>(other);
}
bool operator <(const T& other) const
bool operator<(const T &other) const
{
return static_cast<int>(mValue) < static_cast<int>(other);
}
bool operator <=(const T& other) const
bool operator<=(const T &other) const
{
return static_cast<int>(mValue) <= static_cast<int>(other);
}
bool operator ==(const T& other) const
bool operator==(const T &other) const
{
return static_cast<int>(mValue) == static_cast<int>(other);
}
bool valid() const { return mValue != T::end; }
bool valid() const
{
return mValue != T::end;
}
operator T() const { return mValue; }
operator T&() { return mValue; }
T val() const { return mValue; }
operator T() const
{
return mValue;
}
operator T &()
{
return mValue;
}
T val() const
{
return mValue;
}
private:
private:
T mValue;
};
+7 -10
View File
@@ -1,18 +1,14 @@
#include "better_radiobutton.h"
Better_Radiobutton::Better_Radiobutton(QWidget* parent) :
QWidget{parent},
m_radiobuttons{}
Better_Radiobutton::Better_Radiobutton(QWidget *parent) : QWidget{parent}, m_radiobuttons{}
{
}
Better_Radiobutton::Better_Radiobutton(std::vector<QString> names, QWidget* parent) :
QWidget{parent},
m_v_layout{new QVBoxLayout(this)},
m_radiobuttons{},
m_checked{""}
Better_Radiobutton::Better_Radiobutton(std::vector<QString> names, QWidget *parent)
: QWidget{parent}, m_v_layout{new QVBoxLayout(this)}, m_radiobuttons{}, m_checked{""}
{
for (const auto& name : names) {
for (const auto &name : names)
{
add_radiobutton(name);
}
}
@@ -25,7 +21,8 @@ void Better_Radiobutton::add_radiobutton(QString name)
connect(radiobutton, &QRadioButton::toggled, this, [this, name, radiobutton] {
emit toggled(name);
if (radiobutton->isChecked()) {
if (radiobutton->isChecked())
{
m_checked = name;
emit checked(name);
}
+8 -6
View File
@@ -1,14 +1,15 @@
#ifndef BETTER_RADIOBUTTON_H
#define BETTER_RADIOBUTTON_H
#include <QVBoxLayout>
#include <QRadioButton>
#include <QVBoxLayout>
class Better_Radiobutton : public QWidget {
class Better_Radiobutton : public QWidget
{
Q_OBJECT
public:
explicit Better_Radiobutton(QWidget* parent = nullptr);
explicit Better_Radiobutton(std::vector<QString>names, QWidget* partent = nullptr);
explicit Better_Radiobutton(QWidget *parent = nullptr);
explicit Better_Radiobutton(std::vector<QString> names, QWidget *partent = nullptr);
void add_radiobutton(QString name);
QString isChecked() const;
@@ -16,9 +17,10 @@ class Better_Radiobutton : public QWidget {
signals:
void toggled(QString name);
void checked(QString name);
private:
QVBoxLayout* m_v_layout;
std::vector<QRadioButton*> m_radiobuttons;
QVBoxLayout *m_v_layout;
std::vector<QRadioButton *> m_radiobuttons;
QString m_checked;
};
+26 -22
View File
@@ -1,41 +1,45 @@
#include "can_transceive.h"
#include "ui_can_transceive.h"
Can_Transceive::Can_Transceive(QWidget* parent) : QWidget(parent), ui(new Ui::Can_Transceive) {
Can_Transceive::Can_Transceive(QWidget *parent) : QWidget(parent), ui(new Ui::Can_Transceive)
{
ui->setupUi(this);
can_send_hexspinboxes = {
ui->s_can_send_data0,
ui->s_can_send_data1,
ui->s_can_send_data2,
ui->s_can_send_data3,
ui->s_can_send_data4,
ui->s_can_send_data5,
ui->s_can_send_data6,
ui->s_can_send_data7,
ui->s_can_send_data0, ui->s_can_send_data1, ui->s_can_send_data2, ui->s_can_send_data3,
ui->s_can_send_data4, ui->s_can_send_data5, ui->s_can_send_data6, ui->s_can_send_data7,
};
enable_can_send_data();
connect(ui->c_can_send_dlc, &QComboBox::currentIndexChanged, this, [=, this]{enable_can_send_data();});
connect(ui->b_can_send_send, &QPushButton::clicked, this, [=, this]{emit send_can_message(ui->s_can_send_sid->value(), ui->c_can_send_dlc->currentIndex(), std::array<uint8_t, 8>({(uint8_t)(can_send_hexspinboxes[0]->value()), (uint8_t)(can_send_hexspinboxes[1]->value()),(uint8_t)(can_send_hexspinboxes[2]->value()),(uint8_t)(can_send_hexspinboxes[3]->value()),(uint8_t)(can_send_hexspinboxes[4]->value()),(uint8_t)(can_send_hexspinboxes[5]->value()),(uint8_t)(can_send_hexspinboxes[6]->value()),(uint8_t)(can_send_hexspinboxes[7]->value())}));});
connect(ui->b_clear, &QPushButton::clicked, this, [=, this]{emit can_clear();});
connect(ui->c_can_send_dlc, &QComboBox::currentIndexChanged, this, [=, this] { enable_can_send_data(); });
connect(ui->b_can_send_send, &QPushButton::clicked, this, [=, this] {
emit send_can_message(
ui->s_can_send_sid->value(), ui->c_can_send_dlc->currentIndex(),
std::array<uint8_t, 8>(
{(uint8_t)(can_send_hexspinboxes[0]->value()), (uint8_t)(can_send_hexspinboxes[1]->value()),
(uint8_t)(can_send_hexspinboxes[2]->value()), (uint8_t)(can_send_hexspinboxes[3]->value()),
(uint8_t)(can_send_hexspinboxes[4]->value()), (uint8_t)(can_send_hexspinboxes[5]->value()),
(uint8_t)(can_send_hexspinboxes[6]->value()), (uint8_t)(can_send_hexspinboxes[7]->value())}));
});
connect(ui->b_clear, &QPushButton::clicked, this, [=, this] { emit can_clear(); });
}
Can_Transceive::~Can_Transceive() {
Can_Transceive::~Can_Transceive()
{
delete ui;
}
void Can_Transceive::enable_can_send_data()
{
uint8_t selected = atoi(ui->c_can_send_dlc->currentText().toLocal8Bit());
for (size_t i = 0; i < can_send_hexspinboxes.size(); i++)
{
can_send_hexspinboxes[i]->setEnabled(i < selected);
if (i >= selected)
{
can_send_hexspinboxes[i]->setValue(0);
}
}
uint8_t selected = atoi(ui->c_can_send_dlc->currentText().toLocal8Bit());
for (size_t i = 0; i < can_send_hexspinboxes.size(); i++)
{
can_send_hexspinboxes[i]->setEnabled(i < selected);
if (i >= selected)
{
can_send_hexspinboxes[i]->setValue(0);
}
}
}
void Can_Transceive::clear()
+8 -6
View File
@@ -1,18 +1,20 @@
#ifndef CAN_TRANSCEIVE_H
#define CAN_TRANSCEIVE_H
#include <QWidget>
#include "hex_spinbox.h"
#include <QWidget>
namespace Ui {
namespace Ui
{
class Can_Transceive;
}
class Can_Transceive : public QWidget {
class Can_Transceive : public QWidget
{
Q_OBJECT
public:
explicit Can_Transceive(QWidget* parent = nullptr);
explicit Can_Transceive(QWidget *parent = nullptr);
~Can_Transceive();
void enable_can_send_data();
void clear();
@@ -24,8 +26,8 @@ class Can_Transceive : public QWidget {
void can_clear();
private:
Ui::Can_Transceive* ui;
std::vector<HexSpinBox*> can_send_hexspinboxes;
Ui::Can_Transceive *ui;
std::vector<HexSpinBox *> can_send_hexspinboxes;
};
#endif // CAN_TRANSCEIVE_H
+35 -30
View File
@@ -1,40 +1,42 @@
#include "helpers.h"
void create_dial_needle(QwtDial* dial)
void create_dial_needle(QwtDial *dial)
{
QwtDialSimpleNeedle* needle = new QwtDialSimpleNeedle(
QwtDialSimpleNeedle::Arrow, true, Qt::yellow,
QColor( Qt::gray ).lighter( 130 ) );
QwtDialSimpleNeedle *needle =
new QwtDialSimpleNeedle(QwtDialSimpleNeedle::Arrow, true, Qt::yellow, QColor(Qt::gray).lighter(130));
needle->setWidth(2);
dial->setNeedle( needle );
dial->setNeedle(needle);
}
void set_widget_color(QWidget* widget, QColor color, QColor bg)
void set_widget_color(QWidget *widget, QColor color, QColor bg)
{
QString style = QString("#%1 { background-color: %2; color: %3;}").arg(widget->objectName(), bg.name(), color.name());
QString style =
QString("#%1 { background-color: %2; color: %3;}").arg(widget->objectName(), bg.name(), color.name());
widget->setStyleSheet(style);
}
void set_widget_color(QWidget* widget, QString color, QString bg)
void set_widget_color(QWidget *widget, QString color, QString bg)
{
QString style = QString("#%1 { background-color: %2; color: %3;}").arg(widget->objectName(), bg, color);
widget->setStyleSheet(style);
}
QColor widget_bg_color(QWidget* widget)
QColor widget_bg_color(QWidget *widget)
{
std::string input = widget->styleSheet().toStdString();
QColor background_color;
// Find the position of "background-color:"
// Find the position of "background-color:"
size_t bg_pos = input.find("background-color:");
if (bg_pos != std::string::npos) {
if (bg_pos != std::string::npos)
{
// Find the end position of the background color value
size_t bg_value_start = bg_pos + 17; // Length of "background-color:" is 17
size_t bg_value_end = input.find(";", bg_value_start);
if (bg_value_end != std::string::npos) {
if (bg_value_end != std::string::npos)
{
background_color = QColor(input.substr(bg_value_start, bg_value_end - bg_value_start).c_str());
}
}
@@ -42,43 +44,46 @@ QColor widget_bg_color(QWidget* widget)
return background_color;
}
QColor widget_color(QWidget* widget)
QColor widget_color(QWidget *widget)
{
std::string input = widget->styleSheet().toStdString();
QColor color;
// Find the position of "color:"
// Find the position of "color:"
size_t color_pos = input.find(" color:");
if (color_pos != std::string::npos) {
if (color_pos != std::string::npos)
{
// Find the end position of the text color value
size_t color_value_start = color_pos + 7; // Length of "color:" is 7
size_t color_value_end = input.find(";", color_value_start);
if (color_value_end != std::string::npos) {
if (color_value_end != std::string::npos)
{
color = QColor(input.substr(color_value_start, color_value_end - color_value_start).c_str());
}
}
return color;
}
void reset_widget_color(QWidget* widget)
void reset_widget_color(QWidget *widget)
{
widget->setStyleSheet("");
}
QString network_reply_to_fix(QNetworkReply::NetworkError error)
{
switch (error) {
case QNetworkReply::NetworkError::ConnectionRefusedError:
return "Make sure the server is started ($ systemctl status unocan_server)";
break;
case QNetworkReply::NetworkError::HostNotFoundError:
return "Make sure the simulator is turned on";
break;
case QNetworkReply::NetworkError::InternalServerError:
return "Server had an oopsie, restart the client and if it fails again restart the server";
break;
default:
return "";
break;
switch (error)
{
case QNetworkReply::NetworkError::ConnectionRefusedError:
return "Make sure the server is started ($ systemctl status unocan_server)";
break;
case QNetworkReply::NetworkError::HostNotFoundError:
return "Make sure the simulator is turned on";
break;
case QNetworkReply::NetworkError::InternalServerError:
return "Server had an oopsie, restart the client and if it fails again restart the server";
break;
default:
return "";
break;
}
}
+8 -8
View File
@@ -1,24 +1,24 @@
#ifndef HELPERS_H
#define HELPERS_H
#include <QNetworkReply>
#include <QtWidgets>
#include <qwt_color_map.h>
#include <qwt_dial.h>
#include <qwt_dial_needle.h>
#include <qwt_plot.h>
#include <qwt_plot_curve.h>
#include <qwt_slider.h>
#include <qwt_series_data.h>
#include <qwt_slider.h>
#include <qwt_thermo.h>
#include <QNetworkReply>
void create_dial_needle(QwtDial* dial);
void create_dial_needle(QwtDial *dial);
void set_widget_color(QWidget* widget, QColor color, QColor bg);
void set_widget_color(QWidget* widget, QString color, QString bg);
QColor widget_bg_color(QWidget* widget);
QColor widget_color(QWidget* widget);
void reset_widget_color(QWidget* widget);
void set_widget_color(QWidget *widget, QColor color, QColor bg);
void set_widget_color(QWidget *widget, QString color, QString bg);
QColor widget_bg_color(QWidget *widget);
QColor widget_color(QWidget *widget);
void reset_widget_color(QWidget *widget);
QString network_reply_to_fix(QNetworkReply::NetworkError error);
#endif // HELPERS_H
+1 -5
View File
@@ -1,10 +1,6 @@
#include "hex_spinbox.h"
HexSpinBox::HexSpinBox(QWidget *parent,
bool showPrefix,
const QString &format) :
QSpinBox(parent),
format(format)
HexSpinBox::HexSpinBox(QWidget *parent, bool showPrefix, const QString &format) : QSpinBox(parent), format(format)
{
// Validates hex strings up to 8 chars with or w/out leading "0x" prefix.
// For arbitrary prefix/suffix, the regex could be built dynamically
+2 -4
View File
@@ -1,9 +1,9 @@
#ifndef _HEXSPINBOX_H_
#define _HEXSPINBOX_H_
#include <QSpinBox>
#include <QLineEdit>
#include <QRegularExpressionValidator>
#include <QSpinBox>
// NOTE: Since QSpinBox uses int as the storage type, the effective editing range
// is +/- 0x7FFF FFFF, so it can't handle a full unsigned int.
@@ -13,9 +13,7 @@ class HexSpinBox : public QSpinBox
Q_OBJECT
public:
HexSpinBox(QWidget *parent = nullptr,
bool showPrefix = false,
const QString &format = QStringLiteral("%X"));
HexSpinBox(QWidget *parent = nullptr, bool showPrefix = false, const QString &format = QStringLiteral("%X"));
public slots:
void setShowPrefix(bool show);
+4 -6
View File
@@ -1,7 +1,6 @@
#include "led.h"
Led::Led(QWidget* parrent)
: QPushButton(parrent)
Led::Led(QWidget *parrent) : QPushButton(parrent)
{
set_read_only(this, true);
}
@@ -11,22 +10,21 @@ Led::Led(const QString &text, QWidget *parent)
set_read_only(this, true);
}
Led::Led(const QIcon& icon, const QString &text, QWidget *parent)
Led::Led(const QIcon &icon, const QString &text, QWidget *parent)
{
set_read_only(this, true);
}
Led::~Led()
{
}
bool Led::is_read_only(QWidget* widget)
bool Led::is_read_only(QWidget *widget)
{
return widget->testAttribute(Qt::WA_TransparentForMouseEvents) || (widget->focusPolicy() == Qt::NoFocus);
}
void Led::set_read_only(QWidget* widget, bool read_only)
void Led::set_read_only(QWidget *widget, bool read_only)
{
widget->setAttribute(Qt::WA_TransparentForMouseEvents, read_only);
widget->setFocusPolicy(read_only ? Qt::NoFocus : Qt::StrongFocus);
+6 -4
View File
@@ -3,16 +3,18 @@
#include "QPushButton"
class Led : public QPushButton {
class Led : public QPushButton
{
Q_OBJECT
public:
explicit Led(QWidget *parent = nullptr);
explicit Led(const QString &text, QWidget *parent = nullptr);
Led(const QIcon& icon, const QString &text, QWidget *parent = nullptr);
Led(const QIcon &icon, const QString &text, QWidget *parent = nullptr);
~Led();
private:
bool is_read_only(QWidget* widget);
void set_read_only(QWidget* widget, bool read_only);
bool is_read_only(QWidget *widget);
void set_read_only(QWidget *widget, bool read_only);
};
#endif // LED_H
+4 -3
View File
@@ -1,15 +1,16 @@
#include "mainwindow.h"
#include <QApplication>
#include "spdlog/async.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
#include <QApplication>
void init_logger()
{
spdlog::init_thread_pool(8192, 1);
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
//auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("log-sim_us.txt", 1024 * 1024 * 10, 3);
std::vector<spdlog::sink_ptr> sinks{stdout_sink/*, rotating_sink*/};
// auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("log-sim_us.txt", 1024 * 1024 * 10,
// 3);
std::vector<spdlog::sink_ptr> sinks{stdout_sink /*, rotating_sink*/};
auto logger = std::make_shared<spdlog::async_logger>("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(),
spdlog::async_overflow_policy::block);
spdlog::set_default_logger(logger);
+54 -51
View File
@@ -1,30 +1,28 @@
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "sim-scope_mux_tester.h"
#include "sim-can_debugger.h"
#include "sim-cvs_i10.h"
#include "sim-scope_mux_tester.h"
#include "sim-test.h"
MainWindow::MainWindow(QString host, uint16_t port, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
MainWindow::MainWindow(QString host, uint16_t port, QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow),
m_web_socket(new Web_socket_wrapper(QUrl("ws://" + host + ":" + QString::number(port)), this)),
m_error_dialog(new NetworkError(this)),
m_sims{{"Can_Debugger", new Can_Debugger(m_web_socket, this)},
{"Scope_Mux_Tester", new Scope_Mux_Tester(m_web_socket, this)},
{"CVS_I10", new CVS_I10(m_web_socket, this)},
{"Test_Sim", new Test_Sim(m_web_socket, this)}}
m_error_dialog(new NetworkError(this)), m_sims{{"Can_Debugger", new Can_Debugger(m_web_socket, this)},
{"Scope_Mux_Tester", new Scope_Mux_Tester(m_web_socket, this)},
{"CVS_I10", new CVS_I10(m_web_socket, this)},
{"Test_Sim", new Test_Sim(m_web_socket, this)}}
{
ui->setupUi(this);
this->setWindowTitle("Simulator Selector");
m_error_dialog->set_error("Connection timed out\nCheck if the server is running");
ui->connection->setText("Connected to ws://" + host + ":" + QString::number(port));
//Default sim
//defaultSim("CVS_I10");
// Default sim
// defaultSim("CVS_I10");
connect(ui->pushButton, &QPushButton::clicked, this, [=, this]{open_sim(ui->comboBox->currentText());});
connect(ui->pushButton, &QPushButton::clicked, this, [=, this] { open_sim(ui->comboBox->currentText()); });
connect(this, &MainWindow::quit, this, &QCoreApplication::quit, Qt::QueuedConnection);
}
@@ -37,18 +35,22 @@ MainWindow::~MainWindow()
void MainWindow::setup_cb(void)
{
connect(m_web_socket, &Web_socket_wrapper::on_command_cb, this, [=, this](json& j){on_cmd_cb(j);});
connect(m_web_socket, &Web_socket_wrapper::on_connected, this, [=, this]{m_web_socket->send_command(Web_socket_wrapper::Command::get_simulators);});
connect(m_web_socket, &Web_socket_wrapper::on_closed, this, [=, this]{m_error_dialog->open(); ui->connection->setText("Could not connect to the server");});
connect(m_web_socket, &Web_socket_wrapper::on_command_cb, this, [=, this](json &j) { on_cmd_cb(j); });
connect(m_web_socket, &Web_socket_wrapper::on_connected, this,
[=, this] { m_web_socket->send_command(Web_socket_wrapper::Command::get_simulators); });
connect(m_web_socket, &Web_socket_wrapper::on_closed, this, [=, this] {
m_error_dialog->open();
ui->connection->setText("Could not connect to the server");
});
}
void MainWindow::showEvent( QShowEvent* event )
void MainWindow::showEvent(QShowEvent *event)
{
QWidget::showEvent( event );
QWidget::showEvent(event);
setup_cb();
}
void MainWindow::hideEvent(QHideEvent* event)
void MainWindow::hideEvent(QHideEvent *event)
{
QWidget::hideEvent(event);
disconnect(m_web_socket, nullptr, nullptr, nullptr);
@@ -60,9 +62,9 @@ void MainWindow::defaultSim(QString name)
this->m_default_sim = true;
}
void MainWindow::pase_sim_names(json& sims)
void MainWindow::pase_sim_names(json &sims)
{
for (json& item : sims)
for (json &item : sims)
{
ui->comboBox->addItem(QString::fromStdString(item));
}
@@ -83,7 +85,7 @@ void MainWindow::open_sim(QString sim_name)
m_selected_sim = m_sims.at(sim_name);
this->m_selected_sim_name = sim_name;
}
catch (const std::out_of_range& ex)
catch (const std::out_of_range &ex)
{
SPDLOG_CRITICAL("out_of_range: {}", ex.what());
return;
@@ -100,41 +102,42 @@ void MainWindow::open_sim_window(void)
void MainWindow::check_active_sim(QString name)
{
if (name == "")
{
SPDLOG_DEBUG("Name not set");
m_web_socket->send_command(Web_socket_wrapper::Command::switch_simulator, m_selected_sim_name);
return;
}
SPDLOG_DEBUG("Current sim:", name);
if (name == m_selected_sim_name)
{
SPDLOG_DEBUG("Name set and the same");
open_sim_window();
return;
}
SPDLOG_DEBUG("Name set and wrong");
if (name == "")
{
SPDLOG_DEBUG("Name not set");
m_web_socket->send_command(Web_socket_wrapper::Command::switch_simulator, m_selected_sim_name);
return;
}
SPDLOG_DEBUG("Current sim:", name);
if (name == m_selected_sim_name)
{
SPDLOG_DEBUG("Name set and the same");
open_sim_window();
return;
}
SPDLOG_DEBUG("Name set and wrong");
m_web_socket->send_command(Web_socket_wrapper::Command::switch_simulator, m_selected_sim_name);
}
void MainWindow::on_cmd_cb(json& j)
void MainWindow::on_cmd_cb(json &j)
{
auto response = magic_enum::enum_cast<Web_socket_wrapper::Command>(std::string(j.at("type"))).value_or(Web_socket_wrapper::Command::end);
auto response = magic_enum::enum_cast<Web_socket_wrapper::Command>(std::string(j.at("type")))
.value_or(Web_socket_wrapper::Command::end);
switch (response)
{
case Web_socket_wrapper::Command::get_simulators:
pase_sim_names(j.at("simulators"));
break;
case Web_socket_wrapper::Command::get_active_simulator_name:
check_active_sim(QString::fromStdString(j.at("name")));
break;
case Web_socket_wrapper::Command::switch_simulator:
open_sim_window();
break;
default:
break;
case Web_socket_wrapper::Command::get_simulators:
pase_sim_names(j.at("simulators"));
break;
case Web_socket_wrapper::Command::get_active_simulator_name:
check_active_sim(QString::fromStdString(j.at("name")));
break;
case Web_socket_wrapper::Command::switch_simulator:
open_sim_window();
break;
default:
break;
}
}
+15 -13
View File
@@ -1,13 +1,14 @@
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <map>
#include <QMainWindow>
#include "web_socket_wrapper.h"
#include "networkerror.h"
#include "simulator_base.h"
#include "web_socket_wrapper.h"
#include <QMainWindow>
#include <map>
namespace Ui {
namespace Ui
{
class MainWindow;
}
@@ -20,7 +21,7 @@ class MainWindow : public QMainWindow
~MainWindow();
void defaultSim(QString name);
void pase_sim_names(json& sims);
void pase_sim_names(json &sims);
void open_sim_window(void);
void check_active_sim(QString name);
void open_sim(QString sim_name);
@@ -28,20 +29,21 @@ class MainWindow : public QMainWindow
signals:
void quit(void);
private:
void showEvent(QShowEvent* event);
void hideEvent(QHideEvent* event);
void on_cmd_cb(json& j);
private:
void showEvent(QShowEvent *event);
void hideEvent(QHideEvent *event);
void on_cmd_cb(json &j);
bool m_default_sim = false;
QString m_selected_sim_name;
QMainWindow* m_selected_sim;
Ui::MainWindow* ui;
QMainWindow *m_selected_sim;
Ui::MainWindow *ui;
Web_socket_wrapper *m_web_socket;
NetworkError* m_error_dialog;
NetworkError *m_error_dialog;
std::map<QString, SimulatorBase*> m_sims;
std::map<QString, SimulatorBase *> m_sims;
};
#endif // MAINWINDOW_H
+5 -3
View File
@@ -1,10 +1,11 @@
#include "networkerror.h"
#include "ui_networkerror.h"
NetworkError::NetworkError(QWidget* parent) : QDialog(parent), ui(new Ui::NetworkError) {
NetworkError::NetworkError(QWidget *parent) : QDialog(parent), ui(new Ui::NetworkError)
{
ui->setupUi(this);
connect(ui->exit, &QPushButton::clicked, this, &QCoreApplication::quit, Qt::QueuedConnection);
connect(ui->close, &QPushButton::clicked, this, [=, this]{this->close();});
connect(ui->close, &QPushButton::clicked, this, [=, this] { this->close(); });
this->setWindowTitle("Error");
}
@@ -14,6 +15,7 @@ void NetworkError::set_error(QString error)
ui->Error->setText(error);
}
NetworkError::~NetworkError() {
NetworkError::~NetworkError()
{
delete ui;
}
+6 -4
View File
@@ -3,20 +3,22 @@
#include <QDialog>
namespace Ui {
namespace Ui
{
class NetworkError;
}
class NetworkError : public QDialog {
class NetworkError : public QDialog
{
Q_OBJECT
public:
explicit NetworkError(QWidget* parent = nullptr);
explicit NetworkError(QWidget *parent = nullptr);
void set_error(QString error);
~NetworkError();
private:
Ui::NetworkError* ui;
Ui::NetworkError *ui;
};
#endif // NETWORKERROR_H
+87 -90
View File
@@ -1,36 +1,34 @@
#include "simulator_base.h"
#include "can_transceive.h"
SimulatorBase::SimulatorBase(Web_socket_wrapper* web_socket, QWidget* parent) :
QMainWindow{parent},
m_error_dialog(new NetworkError(this)),
m_timer_update(new QTimer()),
SimulatorBase::SimulatorBase(Web_socket_wrapper *web_socket, QWidget *parent)
: QMainWindow{parent}, m_error_dialog(new NetworkError(this)), m_timer_update(new QTimer()),
m_web_socket(web_socket)
{
m_error_dialog->set_error("Connection timed out\nCheck if the server is running");
m_ui_lookup.reserve(40);
}
QWidget* SimulatorBase::id_to_ui(size_t id)
QWidget *SimulatorBase::id_to_ui(size_t id)
{
return m_ui_lookup.at(id);
}
void SimulatorBase::showEvent( QShowEvent* event )
void SimulatorBase::showEvent(QShowEvent *event)
{
QWidget::showEvent( event );
QWidget::showEvent(event);
SPDLOG_DEBUG("Connecting callbacks");
connect(m_web_socket, &Web_socket_wrapper::on_command_cb, this, [=, this](json& j){on_cmd_cb(j);});
connect(m_web_socket, &Web_socket_wrapper::on_event_cb, this, [=, this](json& j){on_event_cb(j);});
connect(m_web_socket, &Web_socket_wrapper::on_closed, this, [=, this]{m_error_dialog->open();});
connect(m_web_socket, &Web_socket_wrapper::on_command_cb, this, [=, this](json &j) { on_cmd_cb(j); });
connect(m_web_socket, &Web_socket_wrapper::on_event_cb, this, [=, this](json &j) { on_event_cb(j); });
connect(m_web_socket, &Web_socket_wrapper::on_closed, this, [=, this] { m_error_dialog->open(); });
m_web_socket->send_command(Web_socket_wrapper::Command::get_UI_elements);
this->m_timer_update->start(m_refresh_rate);
m_open = true;
}
void SimulatorBase::closeEvent(QCloseEvent* event)
void SimulatorBase::closeEvent(QCloseEvent *event)
{
if (m_open == true)
{
@@ -43,42 +41,44 @@ void SimulatorBase::closeEvent(QCloseEvent* event)
}
}
void SimulatorBase::on_cmd_cb(json& j)
void SimulatorBase::on_cmd_cb(json &j)
{
auto response = magic_enum::enum_cast<Web_socket_wrapper::Command>(std::string(j.at("type"))).value_or(Web_socket_wrapper::Command::end);
auto response = magic_enum::enum_cast<Web_socket_wrapper::Command>(std::string(j.at("type")))
.value_or(Web_socket_wrapper::Command::end);
switch (response)
{
case Web_socket_wrapper::Command::get_UI_elements:
case Web_socket_wrapper::Command::get_UI_element:
UI_item_parser(j);
break;
default:
break;
case Web_socket_wrapper::Command::get_UI_elements:
case Web_socket_wrapper::Command::get_UI_element:
UI_item_parser(j);
break;
default:
break;
}
}
void SimulatorBase::on_event_cb(json& j)
void SimulatorBase::on_event_cb(json &j)
{
auto response = magic_enum::enum_cast<Web_socket_wrapper::Event>(std::string(j.at("type"))).value_or(Web_socket_wrapper::Event::end);
auto response = magic_enum::enum_cast<Web_socket_wrapper::Event>(std::string(j.at("type")))
.value_or(Web_socket_wrapper::Event::end);
switch (response)
{
case Web_socket_wrapper::Event::ui_changed:
UI_item_parser(j);
break;
default:
break;
case Web_socket_wrapper::Event::ui_changed:
UI_item_parser(j);
break;
default:
break;
}
}
void SimulatorBase::push_ui_item(QWidget* item)
void SimulatorBase::push_ui_item(QWidget *item)
{
m_ui_lookup.emplace_back(item);
setup_ui_item(item, m_ui_lookup.size() - 1);
}
void SimulatorBase::UI_item_parser(json& input)
void SimulatorBase::UI_item_parser(json &input)
{
for (auto& uiItem : input["UI_items"])
for (auto &uiItem : input["UI_items"])
{
if (uiItem["type"] == "UI_button")
{
@@ -133,16 +133,16 @@ void SimulatorBase::UI_item_parser(json& input)
}
}
void SimulatorBase::process_ui_label(json& uiItem)
void SimulatorBase::process_ui_label(json &uiItem)
{
QWidget* widget = id_to_ui(uiItem["id"]);
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto label = qobject_cast<QLabel*>(widget);
auto label = qobject_cast<QLabel *>(widget);
if (label == nullptr)
{
SPDLOG_WARN("widget is not of type QLabel");
@@ -157,17 +157,17 @@ void SimulatorBase::process_ui_label(json& uiItem)
}
}
void SimulatorBase::process_ui_slider(json& uiItem)
void SimulatorBase::process_ui_slider(json &uiItem)
{
QLabel* label = id_to_label(uiItem["id"]);
QWidget* widget = id_to_ui(uiItem["id"]);
QLabel *label = id_to_label(uiItem["id"]);
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto slider = qobject_cast<QwtSlider*>(widget);
auto slider = qobject_cast<QwtSlider *>(widget);
if (slider == nullptr)
{
SPDLOG_WARN("widget is not of type QwtSlider");
@@ -197,21 +197,21 @@ void SimulatorBase::process_ui_slider(json& uiItem)
{
slider->setVisible(visible);
}
//TODO: Add color for scale
// TODO: Add color for scale
return;
}
void SimulatorBase::process_ui_dial(json& uiItem)
void SimulatorBase::process_ui_dial(json &uiItem)
{
QLabel* label = id_to_label(uiItem["id"]);
QWidget* widget = id_to_ui(uiItem["id"]);
QLabel *label = id_to_label(uiItem["id"]);
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto dial = qobject_cast<QwtDial*>(widget);
auto dial = qobject_cast<QwtDial *>(widget);
if (dial == nullptr)
{
SPDLOG_WARN("widget is not of type QwtDial");
@@ -231,7 +231,6 @@ void SimulatorBase::process_ui_dial(json& uiItem)
dial->setValue(value);
}
if (dial->isVisible() != visible)
{
dial->setVisible(visible);
@@ -239,17 +238,17 @@ void SimulatorBase::process_ui_dial(json& uiItem)
return;
}
void SimulatorBase::process_ui_thermo(json& uiItem)
void SimulatorBase::process_ui_thermo(json &uiItem)
{
QLabel* label = id_to_label(uiItem["id"]);
QWidget* widget = id_to_ui(uiItem["id"]);
QLabel *label = id_to_label(uiItem["id"]);
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto thermo = qobject_cast<QwtThermo*>(widget);
auto thermo = qobject_cast<QwtThermo *>(widget);
if (thermo == nullptr)
{
SPDLOG_WARN("widget is not of type QwtThermo");
@@ -269,7 +268,6 @@ void SimulatorBase::process_ui_thermo(json& uiItem)
thermo->setValue(value);
}
if (thermo->isVisible() != visible)
{
thermo->setVisible(visible);
@@ -277,21 +275,21 @@ void SimulatorBase::process_ui_thermo(json& uiItem)
return;
}
void SimulatorBase::process_ui_textbox(json& uiItem)
void SimulatorBase::process_ui_textbox(json &uiItem)
{
SPDLOG_CRITICAL("Not implemented");
}
void SimulatorBase::process_ui_combobox(json& uiItem)
void SimulatorBase::process_ui_combobox(json &uiItem)
{
QWidget* widget = id_to_ui(uiItem["id"]);
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto combobox = qobject_cast<QComboBox*>(widget);
auto combobox = qobject_cast<QComboBox *>(widget);
if (combobox == nullptr)
{
SPDLOG_WARN("widget is not of type QComboBox");
@@ -300,7 +298,7 @@ void SimulatorBase::process_ui_combobox(json& uiItem)
QString selected = QString::fromStdString(uiItem["selected"]);
for (json& item : uiItem["options"])
for (json &item : uiItem["options"])
{
if (combobox->findText(QString::fromStdString(item)) == -1)
combobox->addItem(QString::fromStdString(item));
@@ -312,22 +310,22 @@ void SimulatorBase::process_ui_combobox(json& uiItem)
}
}
void SimulatorBase::process_ui_radiobutton(json& uiItem)
void SimulatorBase::process_ui_radiobutton(json &uiItem)
{
SPDLOG_CRITICAL("Not implemented");
}
void SimulatorBase::process_ui_button(json& uiItem)
void SimulatorBase::process_ui_button(json &uiItem)
{
//TODO: fix bgcolor and color
QWidget* widget = id_to_ui(uiItem["id"]);
// TODO: fix bgcolor and color
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto button = qobject_cast<QPushButton*>(widget);
auto button = qobject_cast<QPushButton *>(widget);
if (button == nullptr)
{
SPDLOG_WARN("widget is not of type QPushButton");
@@ -379,16 +377,16 @@ void SimulatorBase::process_ui_button(json& uiItem)
}
}
void SimulatorBase::process_ui_led(json& uiItem)
void SimulatorBase::process_ui_led(json &uiItem)
{
QWidget* widget = id_to_ui(uiItem["id"]);
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto led = qobject_cast<Led*>(widget);
auto led = qobject_cast<Led *>(widget);
if (led == nullptr)
{
SPDLOG_WARN("widget is not of type led");
@@ -434,16 +432,16 @@ void SimulatorBase::process_ui_led(json& uiItem)
}
}
void SimulatorBase::process_ui_can(json& uiItem)
void SimulatorBase::process_ui_can(json &uiItem)
{
QWidget* widget = id_to_ui(uiItem["id"]);
QWidget *widget = id_to_ui(uiItem["id"]);
if (widget == nullptr)
{
SPDLOG_WARN("id_to_ui returned null on {}", QString::number((size_t)uiItem["id"]).toStdString());
return;
}
auto can_ui = qobject_cast<Can_Transceive*>(widget);
auto can_ui = qobject_cast<Can_Transceive *>(widget);
if (can_ui == nullptr)
{
SPDLOG_WARN("widget is not of type led");
@@ -451,19 +449,18 @@ void SimulatorBase::process_ui_can(json& uiItem)
}
can_ui->clear();
for (auto& send_item : uiItem["send_msgs"])
for (auto &send_item : uiItem["send_msgs"])
{
can_ui->add_send_item(send_item["id"], send_item["dlc"], send_item["payload"]);
}
for (auto& recvd_item : uiItem["rcvd_msgs"])
for (auto &recvd_item : uiItem["rcvd_msgs"])
{
can_ui->add_receive_item(recvd_item["id"], recvd_item["dlc"], recvd_item["payload"]);
}
}
void SimulatorBase::setup_ui_item(QWidget* item, size_t index)
void SimulatorBase::setup_ui_item(QWidget *item, size_t index)
{
setup_button(item, index);
setup_combobox(item, index);
@@ -472,55 +469,55 @@ void SimulatorBase::setup_ui_item(QWidget* item, size_t index)
setup_can_ui(item, index);
}
void SimulatorBase::setup_button(QWidget* item, size_t index)
void SimulatorBase::setup_button(QWidget *item, size_t index)
{
QPushButton* button = qobject_cast<QPushButton*>(item);
QPushButton *button = qobject_cast<QPushButton *>(item);
if (button == nullptr)
return;
connect(button, &QPushButton::clicked, this, [=, this]{m_web_socket->send_event(Web_socket_wrapper::Event::clicked, index);});
connect(button, &QPushButton::clicked, this,
[=, this] { m_web_socket->send_event(Web_socket_wrapper::Event::clicked, index); });
}
void SimulatorBase::setup_combobox(QWidget* item, size_t index)
void SimulatorBase::setup_combobox(QWidget *item, size_t index)
{
QComboBox* combobox = qobject_cast<QComboBox*>(item);
QComboBox *combobox = qobject_cast<QComboBox *>(item);
if (combobox == nullptr)
return;
connect(combobox, &QComboBox::currentIndexChanged, this, [=, this]{
m_web_socket->send_event(Web_socket_wrapper::Event::selected, index,
combobox->currentText());});
connect(combobox, &QComboBox::currentIndexChanged, this, [=, this] {
m_web_socket->send_event(Web_socket_wrapper::Event::selected, index, combobox->currentText());
});
}
void SimulatorBase::setup_dial(QWidget* item, size_t index)
void SimulatorBase::setup_dial(QWidget *item, size_t index)
{
QwtDial* dial = qobject_cast<QwtDial*>(item);
QwtDial *dial = qobject_cast<QwtDial *>(item);
if (dial == nullptr)
return;
create_dial_needle(dial);
}
void SimulatorBase::setup_slider(QWidget* item, size_t index)
void SimulatorBase::setup_slider(QWidget *item, size_t index)
{
QwtSlider* slider = qobject_cast<QwtSlider*>(item);
QwtSlider *slider = qobject_cast<QwtSlider *>(item);
if (slider == nullptr)
return;
connect(slider, &QwtSlider::sliderMoved, this, [=, this]{
m_web_socket->send_event(Web_socket_wrapper::Event::value_changed, index,
slider->value());});
connect(slider, &QwtSlider::sliderMoved, this,
[=, this] { m_web_socket->send_event(Web_socket_wrapper::Event::value_changed, index, slider->value()); });
}
void SimulatorBase::setup_can_ui(QWidget* item, size_t index)
void SimulatorBase::setup_can_ui(QWidget *item, size_t index)
{
Can_Transceive* can = qobject_cast<Can_Transceive*>(item);
Can_Transceive *can = qobject_cast<Can_Transceive *>(item);
if (can == nullptr)
return;
connect(can, &Can_Transceive::send_can_message, this, [=, this](uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload){
m_web_socket->send_event(Web_socket_wrapper::Event::can_send, index, id, dlc, payload);
});
connect(can, &Can_Transceive::can_clear, this, [=, this]{
m_web_socket->send_event(Web_socket_wrapper::Event::can_clear, index);
});
connect(can, &Can_Transceive::send_can_message, this,
[=, this](uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload) {
m_web_socket->send_event(Web_socket_wrapper::Event::can_send, index, id, dlc, payload);
});
connect(can, &Can_Transceive::can_clear, this,
[=, this] { m_web_socket->send_event(Web_socket_wrapper::Event::can_clear, index); });
}
+41 -36
View File
@@ -1,64 +1,69 @@
#ifndef SIMULATOR_BASE_H
#define SIMULATOR_BASE_H
#include <QMainWindow>
#include <QWidget>
#include "web_socket_wrapper.h"
#include "networkerror.h"
#include "IterableEnum.hpp"
#include "helpers.h"
#include "led.h"
#include <magic_enum.hpp>
#include "IterableEnum.hpp"
#include "networkerror.h"
#include "spdlog/spdlog.h"
#include "web_socket_wrapper.h"
#include <QMainWindow>
#include <QWidget>
#include <magic_enum.hpp>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
class SimulatorBase : public QMainWindow {
class SimulatorBase : public QMainWindow
{
Q_OBJECT
public:
explicit SimulatorBase(Web_socket_wrapper* api, QWidget* parent = nullptr);
explicit SimulatorBase(Web_socket_wrapper *api, QWidget *parent = nullptr);
void sim_update(void);
QWidget* id_to_ui(size_t id);
virtual QLabel* id_to_label(size_t name) {return nullptr;}
QWidget *id_to_ui(size_t id);
virtual QLabel *id_to_label(size_t name)
{
return nullptr;
}
signals:
void quit();
protected:
void on_event_cb(json& j);
void on_cmd_cb(json& j);
void on_event_cb(json &j);
void on_cmd_cb(json &j);
void push_ui_item(QWidget* item);
void push_ui_item(QWidget *item);
void UI_item_parser(json& input);
virtual void process_ui_label(json& uiItem);
virtual void process_ui_slider(json& uiItem);
virtual void process_ui_dial(json& uiItem);
virtual void process_ui_thermo(json& uiItem);
virtual void process_ui_textbox(json& uiItem);
virtual void process_ui_combobox(json& uiItem);
virtual void process_ui_radiobutton(json& uiItem);
virtual void process_ui_button(json& uiItem);
virtual void process_ui_led(json& uiItem);
virtual void process_ui_can(json& uiItem);
void UI_item_parser(json &input);
virtual void process_ui_label(json &uiItem);
virtual void process_ui_slider(json &uiItem);
virtual void process_ui_dial(json &uiItem);
virtual void process_ui_thermo(json &uiItem);
virtual void process_ui_textbox(json &uiItem);
virtual void process_ui_combobox(json &uiItem);
virtual void process_ui_radiobutton(json &uiItem);
virtual void process_ui_button(json &uiItem);
virtual void process_ui_led(json &uiItem);
virtual void process_ui_can(json &uiItem);
void setup_ui_item(QWidget* item, size_t index);
void setup_button(QWidget* item, size_t index);
void setup_combobox(QWidget* item, size_t index);
void setup_dial(QWidget* item, size_t index);
void setup_slider(QWidget* item, size_t index);
void setup_can_ui(QWidget* item, size_t index);
void setup_ui_item(QWidget *item, size_t index);
void setup_button(QWidget *item, size_t index);
void setup_combobox(QWidget *item, size_t index);
void setup_dial(QWidget *item, size_t index);
void setup_slider(QWidget *item, size_t index);
void setup_can_ui(QWidget *item, size_t index);
void showEvent(QShowEvent* event);
void closeEvent(QCloseEvent* event);
void showEvent(QShowEvent *event);
void closeEvent(QCloseEvent *event);
private:
Web_socket_wrapper* m_web_socket;
Web_socket_wrapper *m_web_socket;
QTimer* m_timer_update;
QTimer *m_timer_update;
uint32_t m_refresh_rate;
NetworkError* m_error_dialog;
NetworkError *m_error_dialog;
bool m_open = false;
std::vector<QWidget*>m_ui_lookup;
std::vector<QWidget *> m_ui_lookup;
};
#endif // SIMULATOR_BASE_H
+5 -4
View File
@@ -1,15 +1,16 @@
#include "sim-can_debugger.h"
#include "ui_sim-can_debugger.h"
Can_Debugger::Can_Debugger(Web_socket_wrapper* web_socket, QWidget *parent) :
SimulatorBase(web_socket, parent),
ui(new Ui::Can_Debugger) {
Can_Debugger::Can_Debugger(Web_socket_wrapper *web_socket, QWidget *parent)
: SimulatorBase(web_socket, parent), ui(new Ui::Can_Debugger)
{
ui->setupUi(this);
this->setWindowTitle("CAN Debugger");
this->push_ui_item(ui->can_transceive);
}
Can_Debugger::~Can_Debugger() {
Can_Debugger::~Can_Debugger()
{
delete ui;
}
+7 -5
View File
@@ -1,22 +1,24 @@
#ifndef SIM_CAN_DEBUGGER_H
#define SIM_CAN_DEBUGGER_H
#include <QMainWindow>
#include "simulator_base.h"
#include <QMainWindow>
namespace Ui {
namespace Ui
{
class Can_Debugger;
}
class Can_Debugger : public SimulatorBase {
class Can_Debugger : public SimulatorBase
{
Q_OBJECT
public:
explicit Can_Debugger(Web_socket_wrapper* web_socket, QWidget *parent = nullptr);
explicit Can_Debugger(Web_socket_wrapper *web_socket, QWidget *parent = nullptr);
~Can_Debugger();
private:
Ui::Can_Debugger* ui;
Ui::Can_Debugger *ui;
};
#endif // SIM_CAN_DEBUGGER_H
+6 -6
View File
@@ -1,9 +1,8 @@
#include "sim-test.h"
#include "ui_sim-test.h"
Test_Sim::Test_Sim(Web_socket_wrapper* web_socket, QWidget* parent) :
SimulatorBase(web_socket, parent),
ui(new Ui::Test_Sim)
Test_Sim::Test_Sim(Web_socket_wrapper *web_socket, QWidget *parent)
: SimulatorBase(web_socket, parent), ui(new Ui::Test_Sim)
{
ui->setupUi(this);
this->setWindowTitle("Test Sim");
@@ -18,13 +17,14 @@ Test_Sim::Test_Sim(Web_socket_wrapper* web_socket, QWidget* parent) :
push_ui_item(ui->led);
}
Test_Sim::~Test_Sim() {
Test_Sim::~Test_Sim()
{
delete ui;
}
QLabel* Test_Sim::id_to_label(size_t id)
QLabel *Test_Sim::id_to_label(size_t id)
{
QWidget* widget = id_to_ui(id);
QWidget *widget = id_to_ui(id);
if (widget == ui->slider)
return ui->slider_label;
if (widget == ui->thermo)
+8 -6
View File
@@ -1,25 +1,27 @@
#ifndef SIM_TEST_H
#define SIM_TEST_H
#include <QMainWindow>
#include "simulator_base.h"
#include "web_socket_wrapper.h"
#include <QMainWindow>
namespace Ui {
namespace Ui
{
class Test_Sim;
}
class Test_Sim : public SimulatorBase {
class Test_Sim : public SimulatorBase
{
Q_OBJECT
public:
explicit Test_Sim(Web_socket_wrapper* web_socket, QWidget* parent = nullptr);
explicit Test_Sim(Web_socket_wrapper *web_socket, QWidget *parent = nullptr);
~Test_Sim();
QLabel* id_to_label(size_t name) override;
QLabel *id_to_label(size_t name) override;
private:
Ui::Test_Sim* ui;
Ui::Test_Sim *ui;
};
#endif // SIM_TEST_H
+72 -75
View File
@@ -1,17 +1,15 @@
#include "web_socket_wrapper.h"
#include "spdlog/spdlog.h"
#include "magic_enum.hpp"
#include "spdlog/spdlog.h"
Web_socket_wrapper::Web_socket_wrapper(const QUrl &url, QObject *parent) :
QObject(parent),
m_web_socket(new QWebSocket("", QWebSocketProtocol::VersionLatest, this)),
m_ping_timer(new QTimer(this)),
m_pong_timer(new QTimer(this))
Web_socket_wrapper::Web_socket_wrapper(const QUrl &url, QObject *parent)
: QObject(parent), m_web_socket(new QWebSocket("", QWebSocketProtocol::VersionLatest, this)),
m_ping_timer(new QTimer(this)), m_pong_timer(new QTimer(this))
{
connect(m_web_socket, &QWebSocket::connected, this, &Web_socket_wrapper::m_on_connected);
connect(m_web_socket, &QWebSocket::disconnected, this, &Web_socket_wrapper::on_closed);
connect(m_web_socket, &QWebSocket::pong, this, &Web_socket_wrapper::m_on_pong);
connect(m_ping_timer, &QTimer::timeout, this, [=, this]{
connect(m_ping_timer, &QTimer::timeout, this, [=, this] {
m_web_socket->ping();
m_pong_timer->start(500);
});
@@ -31,25 +29,25 @@ void Web_socket_wrapper::send_command(Command command, size_t id)
json j;
switch (command)
{
case Command::get_UI_element:
j["command"]["type"] = magic_enum::enum_name(command).data();
j["command"]["id"] = id;
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
case Command::get_UI_elements:
j["command"]["type"] = magic_enum::enum_name(command).data();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
case Command::get_active_simulator_name:
j["command"]["type"] = magic_enum::enum_name(command).data();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
case Command::get_simulators:
j["command"]["type"] = magic_enum::enum_name(command).data();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
case Command::get_UI_element:
j["command"]["type"] = magic_enum::enum_name(command).data();
j["command"]["id"] = id;
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
case Command::get_UI_elements:
j["command"]["type"] = magic_enum::enum_name(command).data();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
case Command::get_active_simulator_name:
j["command"]["type"] = magic_enum::enum_name(command).data();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
case Command::get_simulators:
j["command"]["type"] = magic_enum::enum_name(command).data();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
}
}
@@ -58,13 +56,13 @@ void Web_socket_wrapper::send_command(Command command, QString id)
json j;
switch (command)
{
case Command::switch_simulator:
j["command"]["type"] = magic_enum::enum_name(command).data();
j["command"]["name"] = id.toStdString();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
case Command::switch_simulator:
j["command"]["type"] = magic_enum::enum_name(command).data();
j["command"]["name"] = id.toStdString();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
}
}
@@ -73,14 +71,14 @@ void Web_socket_wrapper::send_event(Event event, size_t id)
json j;
switch (event)
{
case Event::clicked:
case Event::can_clear:
j["event"]["type"] = magic_enum::enum_name(event).data();
j["event"]["id"] = id;
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
case Event::clicked:
case Event::can_clear:
j["event"]["type"] = magic_enum::enum_name(event).data();
j["event"]["id"] = id;
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
}
}
@@ -89,14 +87,14 @@ void Web_socket_wrapper::send_event(Event event, size_t id, double val)
json j;
switch (event)
{
case Event::value_changed:
j["event"]["type"] = magic_enum::enum_name(event).data();
j["event"]["id"] = id;
j["event"]["value"] = val;
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
case Event::value_changed:
j["event"]["type"] = magic_enum::enum_name(event).data();
j["event"]["id"] = id;
j["event"]["value"] = val;
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
}
}
@@ -105,20 +103,20 @@ void Web_socket_wrapper::send_event(Event event, size_t id, QString val)
json j;
switch (event)
{
case Event::text_changed:
j["event"]["type"] = magic_enum::enum_name(event).data();
j["event"]["id"] = id;
j["event"]["text"] = val.toStdString();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
case Event::selected:
j["event"]["type"] = magic_enum::enum_name(event).data();
j["event"]["id"] = id;
j["event"]["selected"] = val.toStdString();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
case Event::text_changed:
j["event"]["type"] = magic_enum::enum_name(event).data();
j["event"]["id"] = id;
j["event"]["text"] = val.toStdString();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
case Event::selected:
j["event"]["type"] = magic_enum::enum_name(event).data();
j["event"]["id"] = id;
j["event"]["selected"] = val.toStdString();
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
}
}
@@ -127,16 +125,16 @@ void Web_socket_wrapper::send_event(Event event, size_t id, uint32_t sid, uint8_
json j;
switch (event)
{
case Event::can_send:
j["event"]["type"] = magic_enum::enum_name(event).data();
j["event"]["id"] = id;
j["event"]["sid"] = sid;
j["event"]["dlc"] = dlc;
j["event"]["payload"] = payload;
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
case Event::can_send:
j["event"]["type"] = magic_enum::enum_name(event).data();
j["event"]["id"] = id;
j["event"]["sid"] = sid;
j["event"]["dlc"] = dlc;
j["event"]["payload"] = payload;
m_web_socket->sendTextMessage(QString(j.dump().data()));
break;
default:
break;
}
}
@@ -149,8 +147,7 @@ void Web_socket_wrapper::close()
void Web_socket_wrapper::m_on_connected()
{
m_connected = true;
connect(m_web_socket, &QWebSocket::textMessageReceived,
this, &Web_socket_wrapper::m_on_received);
connect(m_web_socket, &QWebSocket::textMessageReceived, this, &Web_socket_wrapper::m_on_received);
m_ping_timer->start(1000);
+7 -6
View File
@@ -1,8 +1,8 @@
#ifndef WEB_SOCKET_WRAPPER_H
#define WEB_SOCKET_WRAPPER_H
#include <QtWebSockets/QWebSocket>
#include <QTimer>
#include <QtWebSockets/QWebSocket>
#include <nlohmann/json.hpp>
using json = nlohmann::json;
@@ -33,6 +33,7 @@ class Web_socket_wrapper : public QObject
end,
begin = 0
};
public:
explicit Web_socket_wrapper(const QUrl &url, QObject *parent = nullptr);
~Web_socket_wrapper();
@@ -47,8 +48,8 @@ class Web_socket_wrapper : public QObject
Q_SIGNALS:
void on_connected();
void on_closed();
void on_command_cb(json& data);
void on_event_cb(json& data);
void on_command_cb(json &data);
void on_event_cb(json &data);
private Q_SLOTS:
void m_on_connected();
@@ -58,9 +59,9 @@ class Web_socket_wrapper : public QObject
private:
bool m_connected = false;
QWebSocket* m_web_socket;
QTimer* m_ping_timer;
QTimer* m_pong_timer;
QWebSocket *m_web_socket;
QTimer *m_ping_timer;
QTimer *m_pong_timer;
};
#endif // WEB_SOCKET_WRAPPER_H