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
+29 -11
View File
@@ -25,15 +25,22 @@ 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) {}
Iterable(T value = T::begin) : mValue(value)
{
}
Iterable(const Iterable &other) : mValue(other.mValue)
{
}
Iterable &operator++()
{
@@ -83,7 +90,6 @@ public:
return static_cast<T>(static_cast<int>(mValue) - static_cast<int>(i));
}
bool operator>(const T &other) const
{
return static_cast<int>(mValue) > static_cast<int>(other);
@@ -109,11 +115,23 @@ public:
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:
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);
}
+4 -2
View File
@@ -1,10 +1,11 @@
#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);
@@ -16,6 +17,7 @@ class Better_Radiobutton : public QWidget {
signals:
void toggled(QString name);
void checked(QString name);
private:
QVBoxLayout *m_v_layout;
std::vector<QRadioButton *> m_radiobuttons;
+15 -11
View File
@@ -1,27 +1,31 @@
#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_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;
}
+5 -3
View File
@@ -1,14 +1,16 @@
#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:
+14 -9
View File
@@ -2,16 +2,16 @@
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);
}
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);
}
@@ -30,11 +30,13 @@ QColor widget_bg_color(QWidget* widget)
// 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());
}
}
@@ -49,11 +51,13 @@ QColor widget_color(QWidget* widget)
// 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());
}
}
@@ -67,7 +71,8 @@ void reset_widget_color(QWidget* widget)
QString network_reply_to_fix(QNetworkReply::NetworkError error)
{
switch (error) {
switch (error)
{
case QNetworkReply::NetworkError::ConnectionRefusedError:
return "Make sure the server is started ($ systemctl status unocan_server)";
break;
+2 -2
View File
@@ -1,16 +1,16 @@
#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);
+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);
+1 -3
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);
}
@@ -18,7 +17,6 @@ Led::Led(const QIcon& icon, const QString &text, QWidget *parent)
Led::~Led()
{
}
bool Led::is_read_only(QWidget *widget)
+3 -1
View File
@@ -3,13 +3,15 @@
#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();
private:
bool is_read_only(QWidget *widget);
void set_read_only(QWidget *widget, bool read_only);
+3 -2
View File
@@ -1,14 +1,15 @@
#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);
// 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);
+12 -9
View File
@@ -1,17 +1,15 @@
#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)},
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)}}
@@ -38,8 +36,12 @@ 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_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)
@@ -122,7 +124,8 @@ void MainWindow::check_active_sim(QString name)
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:
+6 -4
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;
}
@@ -28,6 +29,7 @@ class MainWindow : public QMainWindow
signals:
void quit(void);
private:
void showEvent(QShowEvent *event);
void hideEvent(QHideEvent *event);
+4 -2
View File
@@ -1,7 +1,8 @@
#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(); });
@@ -14,6 +15,7 @@ void NetworkError::set_error(QString error)
ui->Error->setText(error);
}
NetworkError::~NetworkError() {
NetworkError::~NetworkError()
{
delete ui;
}
+4 -2
View File
@@ -3,11 +3,13 @@
#include <QDialog>
namespace Ui {
namespace Ui
{
class NetworkError;
}
class NetworkError : public QDialog {
class NetworkError : public QDialog
{
Q_OBJECT
public:
+16 -19
View File
@@ -1,10 +1,8 @@
#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");
@@ -45,7 +43,8 @@ void SimulatorBase::closeEvent(QCloseEvent* event)
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:
@@ -59,7 +58,8 @@ void SimulatorBase::on_cmd_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:
@@ -231,7 +231,6 @@ void SimulatorBase::process_ui_dial(json& uiItem)
dial->setValue(value);
}
if (dial->isVisible() != visible)
{
dial->setVisible(visible);
@@ -269,7 +268,6 @@ void SimulatorBase::process_ui_thermo(json& uiItem)
thermo->setValue(value);
}
if (thermo->isVisible() != visible)
{
thermo->setVisible(visible);
@@ -460,7 +458,6 @@ void SimulatorBase::process_ui_can(json& uiItem)
{
can_ui->add_receive_item(recvd_item["id"], recvd_item["dlc"], recvd_item["payload"]);
}
}
void SimulatorBase::setup_ui_item(QWidget *item, size_t index)
@@ -477,7 +474,8 @@ void SimulatorBase::setup_button(QWidget* item, size_t index)
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)
@@ -487,8 +485,8 @@ void SimulatorBase::setup_combobox(QWidget* item, size_t index)
return;
connect(combobox, &QComboBox::currentIndexChanged, this, [=, this] {
m_web_socket->send_event(Web_socket_wrapper::Event::selected, index,
combobox->currentText());});
m_web_socket->send_event(Web_socket_wrapper::Event::selected, index, combobox->currentText());
});
}
void SimulatorBase::setup_dial(QWidget *item, size_t index)
@@ -506,9 +504,8 @@ void SimulatorBase::setup_slider(QWidget* item, size_t index)
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)
@@ -517,10 +514,10 @@ void SimulatorBase::setup_can_ui(QWidget* item, size_t index)
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){
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::can_clear, this,
[=, this] { m_web_socket->send_event(Web_socket_wrapper::Event::can_clear, index); });
}
+13 -8
View File
@@ -1,28 +1,33 @@
#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);
void sim_update(void);
QWidget *id_to_ui(size_t id);
virtual QLabel* id_to_label(size_t name) {return nullptr;}
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);
+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;
}
+5 -3
View File
@@ -1,14 +1,16 @@
#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:
+4 -4
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,7 +17,8 @@ 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;
}
+5 -3
View File
@@ -1,15 +1,17 @@
#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:
+5 -8
View File
@@ -1,12 +1,10 @@
#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);
@@ -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);
+2 -1
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();
+1 -1
+10 -8
View File
@@ -1,11 +1,11 @@
#include "CAN_interface.hpp"
#include "spdlog/spdlog.h"
#include "magic_enum.hpp"
#include "spdlog/spdlog.h"
CAN_Interface::CAN_Interface(QString dev, QObject *parent)
: QObject(parent)
CAN_Interface::CAN_Interface(QString dev, QObject *parent) : QObject(parent)
{
if (!QCanBus::instance()->plugins().contains(QStringLiteral("socketcan")))
{
if (!QCanBus::instance()->plugins().contains(QStringLiteral("socketcan"))) {
SPDLOG_CRITICAL("Runtime error: CAN Interface: socketcan plugin not found");
throw std::runtime_error("CAN Interface: socketcan plugin not found");
}
@@ -18,8 +18,8 @@ CAN_Interface::CAN_Interface(QString dev, QObject *parent)
throw std::runtime_error("CAN Interface: " + errorString.toStdString());
}
connect(m_canDevice, &QCanBusDevice::errorOccurred,
this, [=, this](QCanBusDevice::CanBusError error){processErrors(error);});
connect(m_canDevice, &QCanBusDevice::errorOccurred, this,
[=, this](QCanBusDevice::CanBusError error) { processErrors(error); });
m_canDevice->connectDevice();
connect(m_canDevice, &QCanBusDevice::framesReceived, this, [this] {
@@ -44,13 +44,15 @@ void CAN_Interface::send_frame(const QCanBusFrame frame)
void CAN_Interface::processErrors(QCanBusDevice::CanBusError error) const
{
switch (error) {
switch (error)
{
case QCanBusDevice::ReadError:
case QCanBusDevice::WriteError:
case QCanBusDevice::ConnectionError:
case QCanBusDevice::ConfigurationError:
case QCanBusDevice::UnknownError:
SPDLOG_CRITICAL("CAN Interface: {} {}", magic_enum::enum_name<QCanBusDevice::CanBusError>(error), m_canDevice->errorString().toStdString());
SPDLOG_CRITICAL("CAN Interface: {} {}", magic_enum::enum_name<QCanBusDevice::CanBusError>(error),
m_canDevice->errorString().toStdString());
break;
default:
break;
+1 -1
View File
@@ -1,8 +1,8 @@
#pragma once
#include <QObject>
#include <QCanBus>
#include <QCanBusDevice>
#include <QCanBusFrame>
#include <QObject>
class CAN_Interface : public QObject
{
+2 -1
View File
@@ -13,7 +13,8 @@ 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);
// 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);
+6 -8
View File
@@ -31,7 +31,8 @@ json Simulators::command_parser(json command)
case Command::get_UI_element:
SPDLOG_INFO("Command {}: {}", type, (size_t)command.at("id"));
j["response"]["type"] = type;
j["response"]["UI_item"] = invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id"));
j["response"]["UI_item"] =
invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(command.at("id"));
break;
case Command::get_UI_elements:
SPDLOG_INFO("Command {}", type);
@@ -91,23 +92,20 @@ void Simulators::event_handler(json event)
}
case Event::text_changed: {
SPDLOG_DEBUG("Event {}: {} {}", type, (size_t)event.at("id"), std::string(event.at("text")));
invoke_active_simulator()
->get_UI_item(event.at("id"))
->set_text(std::string(event.at("text")));
invoke_active_simulator()->get_UI_item(event.at("id"))->set_text(std::string(event.at("text")));
break;
}
case Event::selected: {
SPDLOG_DEBUG("Event {}: {} {}", type, (size_t)event.at("id"), std::string(event.at("selected")));
invoke_active_simulator()
->get_UI_item(event.at("id"))
->set_selected(std::string(event.at("selected")));
invoke_active_simulator()->get_UI_item(event.at("id"))->set_selected(std::string(event.at("selected")));
break;
}
case Event::can_send: {
SPDLOG_DEBUG("Event {}: {}", type, (size_t)event.at("id"));
invoke_active_simulator()
->get_UI_item(event.at("id"))
->can_send((uint32_t)event.at("sid"), (uint8_t)event.at("dlc"), (std::array<uint8_t, 8>)event.at("payload"));
->can_send((uint32_t)event.at("sid"), (uint8_t)event.at("dlc"),
(std::array<uint8_t, 8>)event.at("payload"));
break;
}
case Event::can_clear: {
+1
View File
@@ -3,6 +3,7 @@
#include <memory>
#include <mutex>
#include "CAN_wrapper.hpp"
#include "simulator_base.hpp"
#include "websocket.hpp"
+6 -5
View File
@@ -1,8 +1,7 @@
#include "can_debugger.hpp"
Can_Debugger::Can_Debugger(CAN_Wrapper* can_wrapper, QObject* parent) :
Simulator_base("Can_Debugger", can_wrapper, std::chrono::milliseconds(100), parent),
m_wrapper(can_wrapper),
Can_Debugger::Can_Debugger(CAN_Wrapper *can_wrapper, QObject *parent)
: Simulator_base("Can_Debugger", can_wrapper, std::chrono::milliseconds(100), parent), m_wrapper(can_wrapper),
m_can_ui(Color::Black, Color::White)
{
this->add_UI_item(&this->m_can_ui);
@@ -17,8 +16,10 @@ void Can_Debugger::timer()
void Can_Debugger::run_at_startup()
{
connect(m_wrapper, &CAN_Wrapper::on_can_message, this, [=, this](QCanBusFrame frame){this->m_can_ui.add_received_message(frame);});
connect(m_wrapper, &CAN_Wrapper::on_can_send, this, [=, this](QCanBusFrame frame){this->m_can_ui.add_send_message(frame);});
connect(m_wrapper, &CAN_Wrapper::on_can_message, this,
[=, this](QCanBusFrame frame) { this->m_can_ui.add_received_message(frame); });
connect(m_wrapper, &CAN_Wrapper::on_can_send, this,
[=, this](QCanBusFrame frame) { this->m_can_ui.add_send_message(frame); });
}
void Can_Debugger::run_at_stop()
+3 -2
View File
@@ -1,10 +1,11 @@
#pragma once
#include "simulator_base.hpp"
class Can_Debugger : public Simulator_base {
class Can_Debugger : public Simulator_base
{
Q_OBJECT
public:
Can_Debugger();
explicit Can_Debugger(QObject* parent = nullptr);
void timer() override;
void run_at_startup() override;
void run_at_stop() override;
+2 -1
View File
@@ -28,7 +28,8 @@ m_led("Led", Color(Color::White), Color(Color::Black), Color(Color::Red))
this->add_UI_item(&this->m_checkbox);
QObject::connect(&m_button, &UI_button::on_click, this, [=, this] { button_clicked(&m_button); });
QObject::connect(&m_slider, &UI_slider::on_change, this, [=, this](double value) { slider_changed(&m_slider, value); });
QObject::connect(&m_slider, &UI_slider::on_change, this,
[=, this](double value) { slider_changed(&m_slider, value); });
}
void Test_Sim::timer()
+3 -1
View File
@@ -1,12 +1,14 @@
#pragma once
#include "simulator_base.hpp"
class Test_Sim : public Simulator_base {
class Test_Sim : public Simulator_base
{
Q_OBJECT
public:
Test_Sim(QObject* parent = nullptr);
void timer() override;
void run_at_startup() override;
private:
void button_clicked(UI_button *id);
void slider_changed(UI_slider *id, double value);
-1
View File
@@ -7,7 +7,6 @@ UI_checkbox::UI_checkbox(QObject *parent) : UI_item(UI_CHECKBOX_TYPE, parent), m
UI_checkbox::UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_CHECKBOX_TYPE, text, 0, fg_color, bg_color, parent), m_selected(false)
{
}
UI_checkbox::UI_checkbox(const json &j, QObject *parent) : UI_item(UI_CHECKBOX_TYPE, parent), m_selected()
+1 -2
View File
@@ -9,8 +9,7 @@ class UI_checkbox : public UI_item
Q_OBJECT
public:
UI_checkbox(QObject *parent = nullptr);
UI_checkbox(std::string text, Color fg_color, Color bg_color,
QObject *parent = nullptr);
UI_checkbox(std::string text, Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_checkbox(const json &j, QObject *parent = nullptr);
void toggle_selected();
+2 -2
View File
@@ -4,8 +4,8 @@ UI_combobox::UI_combobox(QObject *parent) : UI_x_boxes(UI_COMBOBOX_TYPE, parent)
{
}
UI_combobox::UI_combobox(Color fg_color, Color bg_color,
std::vector<std::string> options, size_t selected, QObject *parent)
UI_combobox::UI_combobox(Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected,
QObject *parent)
: UI_x_boxes(UI_COMBOBOX_TYPE, "", fg_color, bg_color, options, parent), m_selected(selected)
{
}
+2 -2
View File
@@ -11,8 +11,8 @@ class UI_combobox : public UI_x_boxes
Q_OBJECT
public:
UI_combobox(QObject *parent = nullptr);
UI_combobox(Color fg_color, Color bg_color, std::vector<std::string> options,
size_t selected = 0, QObject *parent = nullptr);
UI_combobox(Color fg_color, Color bg_color, std::vector<std::string> options, size_t selected = 0,
QObject *parent = nullptr);
UI_combobox(const json &j, QObject *parent = nullptr);
void set_selected(size_t selected);
+5 -7
View File
@@ -1,20 +1,18 @@
#include "UI_dial.hpp"
UI_dial::UI_dial(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent)
UI_dial::UI_dial(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max,
double value, QObject *parent)
: UI_item(UI_DIAL_TYPE, text, 12, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit)
{
}
UI_dial::UI_dial(std::string text, Color fg_color, Color bg_color, double min, double max,
double value, QObject *parent)
UI_dial::UI_dial(std::string text, Color fg_color, Color bg_color, double min, double max, double value,
QObject *parent)
: UI_dial(text, "", fg_color, bg_color, min, max, value, parent)
{
}
UI_dial::UI_dial(const json &j, QObject *parent)
: UI_item(UI_DIAL_TYPE, parent)
UI_dial::UI_dial(const json &j, QObject *parent) : UI_item(UI_DIAL_TYPE, parent)
{
from_json(j);
}
+1 -3
View File
@@ -1,7 +1,6 @@
#include "UI_item.hpp"
UI_item::UI_item(std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color,
QObject *parent)
UI_item::UI_item(std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color, QObject *parent)
: QObject(parent), m_id((size_t)-1), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color),
m_bg_color(bg_color), enabled(true), visible(true)
{
@@ -171,5 +170,4 @@ void UI_item::can_send(uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload)
void UI_item::can_clear()
{
}
+3 -3
View File
@@ -6,8 +6,8 @@
#include <string>
#include <thread>
#include <QObject>
#include <QCanBusFrame>
#include <QObject>
using json = nlohmann::json;
#define UNUSED(x) (void)(x)
@@ -17,8 +17,8 @@ class UI_item : public QObject
Q_OBJECT
public:
explicit UI_item(std::string type, QObject *parent = nullptr);
explicit UI_item(std::string type, std::string text, uint8_t text_size, Color fg_color,
Color bg_color, QObject *parent = nullptr);
explicit UI_item(std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color,
QObject *parent = nullptr);
explicit UI_item(const json &j, QObject *parent = nullptr);
~UI_item();
+1 -2
View File
@@ -8,8 +8,7 @@ class UI_led : public UI_item
{
Q_OBJECT
public:
UI_led(std::string text, Color fg_color, Color bg_color, Color led_color,
QObject *parent = nullptr);
UI_led(std::string text, Color fg_color, Color bg_color, Color led_color, QObject *parent = nullptr);
UI_led(const json &j, QObject *parent = nullptr);
Color led_color() const;
+2 -2
View File
@@ -2,7 +2,8 @@
UI_slider::UI_slider(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max,
double value, QObject *parent)
: UI_item(UI_SLIDER_TYPE, text, 12, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit)
: UI_item(UI_SLIDER_TYPE, text, 12, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value),
m_unit(unit)
{
}
@@ -10,7 +11,6 @@ UI_slider::UI_slider(std::string text, Color fg_color, Color bg_color, double mi
QObject *parent)
: UI_slider(text, "", fg_color, bg_color, min, max, value, parent)
{
}
UI_slider::UI_slider(const json &j, QObject *parent) : UI_item(UI_SLIDER_TYPE, parent)
+2 -1
View File
@@ -2,7 +2,8 @@
UI_thermo::UI_thermo(std::string text, std::string unit, Color fg_color, Color bg_color, double min, double max,
double value, QObject *parent)
: UI_item(UI_THERMO_TYPE, text, 12, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value), m_unit(unit)
: UI_item(UI_THERMO_TYPE, text, 12, fg_color, bg_color, parent), m_min(min), m_max(max), m_value(value),
m_unit(unit)
{
}
+2 -2
View File
@@ -7,8 +7,8 @@ class UI_x_boxes : public UI_item
Q_OBJECT
public:
UI_x_boxes(std::string type, QObject *parent = nullptr);
UI_x_boxes(std::string type, std::string text, Color fg_color, Color bg_color,
std::vector<std::string> options, QObject *parent = nullptr);
UI_x_boxes(std::string type, std::string text, Color fg_color, Color bg_color, std::vector<std::string> options,
QObject *parent = nullptr);
void from_json(const json &j) override;
json to_json(size_t id) const override;
+4 -2
View File
@@ -24,14 +24,16 @@ UI_can::UI_can(const json &j, QObject *parent) : UI_item(UI_CAN_TYPE, parent)
void UI_can::add_send_message(QCanBusFrame msg)
{
CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(), ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload()));
CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(),
ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload()));
m_can_send_messages.push_back(frame);
emit value_changed();
}
void UI_can::add_received_message(QCanBusFrame msg)
{
CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(), ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload()));
CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(),
ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload()));
m_can_received_messages.push_back(frame);
emit value_changed();
}
+2 -3
View File
@@ -8,10 +8,8 @@
class CanFrame
{
public:
CanFrame(uint32_t sid, uint8_t dlc, std::array<uint8_t, 8> payload)
: m_SID(sid), m_DLC(dlc), m_payload(payload)
CanFrame(uint32_t sid, uint8_t dlc, std::array<uint8_t, 8> payload) : m_SID(sid), m_DLC(dlc), m_payload(payload)
{
}
uint32_t m_SID;
uint8_t m_DLC;
@@ -37,6 +35,7 @@ class UI_can : public UI_item
void can_clear() override;
signals:
void on_send(QCanBusFrame msg);
private:
std::vector<CanFrame> m_can_send_messages;
std::vector<CanFrame> m_can_received_messages;