diff --git a/client/IterableEnum.hpp b/client/IterableEnum.hpp index 963277c..8fa4b70 100644 --- a/client/IterableEnum.hpp +++ b/client/IterableEnum.hpp @@ -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 Iterable +template 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(static_cast(mValue) + 1); @@ -49,7 +56,7 @@ public: return prev; } - Iterable& operator--() + Iterable &operator--() { assert(mValue >= T::end && mValue < T::begin); mValue = static_cast(static_cast(mValue) - 1); @@ -83,38 +90,49 @@ public: return static_cast(static_cast(mValue) - static_cast(i)); } - - bool operator >(const T& other) const + bool operator>(const T &other) const { return static_cast(mValue) > static_cast(other); } - bool operator >=(const T& other) const + bool operator>=(const T &other) const { return static_cast(mValue) >= static_cast(other); } - bool operator <(const T& other) const + bool operator<(const T &other) const { return static_cast(mValue) < static_cast(other); } - bool operator <=(const T& other) const + bool operator<=(const T &other) const { return static_cast(mValue) <= static_cast(other); } - bool operator ==(const T& other) const + bool operator==(const T &other) const { return static_cast(mValue) == static_cast(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; }; diff --git a/client/better_radiobutton.cpp b/client/better_radiobutton.cpp index d6339f0..1e37a6c 100644 --- a/client/better_radiobutton.cpp +++ b/client/better_radiobutton.cpp @@ -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 names, QWidget* parent) : - QWidget{parent}, - m_v_layout{new QVBoxLayout(this)}, - m_radiobuttons{}, - m_checked{""} +Better_Radiobutton::Better_Radiobutton(std::vector 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); } diff --git a/client/better_radiobutton.h b/client/better_radiobutton.h index 90b3076..9ff9ac7 100644 --- a/client/better_radiobutton.h +++ b/client/better_radiobutton.h @@ -1,14 +1,15 @@ #ifndef BETTER_RADIOBUTTON_H #define BETTER_RADIOBUTTON_H -#include #include +#include -class Better_Radiobutton : public QWidget { +class Better_Radiobutton : public QWidget +{ Q_OBJECT public: - explicit Better_Radiobutton(QWidget* parent = nullptr); - explicit Better_Radiobutton(std::vectornames, QWidget* partent = nullptr); + explicit Better_Radiobutton(QWidget *parent = nullptr); + explicit Better_Radiobutton(std::vector 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 m_radiobuttons; + QVBoxLayout *m_v_layout; + std::vector m_radiobuttons; QString m_checked; }; diff --git a/client/can_transceive.cpp b/client/can_transceive.cpp index 614d6f0..34cef57 100644 --- a/client/can_transceive.cpp +++ b/client/can_transceive.cpp @@ -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)(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)(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() diff --git a/client/can_transceive.h b/client/can_transceive.h index f629242..318d2f0 100644 --- a/client/can_transceive.h +++ b/client/can_transceive.h @@ -1,18 +1,20 @@ #ifndef CAN_TRANSCEIVE_H #define CAN_TRANSCEIVE_H -#include #include "hex_spinbox.h" +#include -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 can_send_hexspinboxes; + Ui::Can_Transceive *ui; + std::vector can_send_hexspinboxes; }; #endif // CAN_TRANSCEIVE_H diff --git a/client/helpers.cpp b/client/helpers.cpp index 0aacc6b..022567c 100644 --- a/client/helpers.cpp +++ b/client/helpers.cpp @@ -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; } } diff --git a/client/helpers.h b/client/helpers.h index 230ff5a..39ee5a9 100644 --- a/client/helpers.h +++ b/client/helpers.h @@ -1,24 +1,24 @@ #ifndef HELPERS_H #define HELPERS_H +#include #include #include #include #include #include #include -#include #include +#include #include -#include -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 diff --git a/client/hex_spinbox.cpp b/client/hex_spinbox.cpp index 2982f1e..6ad9521 100644 --- a/client/hex_spinbox.cpp +++ b/client/hex_spinbox.cpp @@ -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 diff --git a/client/hex_spinbox.h b/client/hex_spinbox.h index aeb1d4d..69e8931 100644 --- a/client/hex_spinbox.h +++ b/client/hex_spinbox.h @@ -1,9 +1,9 @@ #ifndef _HEXSPINBOX_H_ #define _HEXSPINBOX_H_ -#include #include #include +#include // 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); diff --git a/client/led.cpp b/client/led.cpp index e83a882..3ef56d2 100644 --- a/client/led.cpp +++ b/client/led.cpp @@ -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); diff --git a/client/led.h b/client/led.h index adf970e..a3294d4 100644 --- a/client/led.h +++ b/client/led.h @@ -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 diff --git a/client/main.cpp b/client/main.cpp index 823c43e..4e0fafe 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -1,15 +1,16 @@ #include "mainwindow.h" -#include #include "spdlog/async.h" #include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/spdlog.h" +#include void init_logger() { spdlog::init_thread_pool(8192, 1); auto stdout_sink = std::make_shared(); - //auto rotating_sink = std::make_shared("log-sim_us.txt", 1024 * 1024 * 10, 3); - std::vector sinks{stdout_sink/*, rotating_sink*/}; + // auto rotating_sink = std::make_shared("log-sim_us.txt", 1024 * 1024 * 10, + // 3); + std::vector sinks{stdout_sink /*, rotating_sink*/}; auto logger = std::make_shared("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); spdlog::set_default_logger(logger); diff --git a/client/mainwindow.cpp b/client/mainwindow.cpp index fa41caa..b378a55 100644 --- a/client/mainwindow.cpp +++ b/client/mainwindow.cpp @@ -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(std::string(j.at("type"))).value_or(Web_socket_wrapper::Command::end); + auto response = magic_enum::enum_cast(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; } } diff --git a/client/mainwindow.h b/client/mainwindow.h index 7d5c475..7845056 100644 --- a/client/mainwindow.h +++ b/client/mainwindow.h @@ -1,13 +1,14 @@ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include -#include -#include "web_socket_wrapper.h" #include "networkerror.h" #include "simulator_base.h" +#include "web_socket_wrapper.h" +#include +#include -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 m_sims; + std::map m_sims; }; #endif // MAINWINDOW_H diff --git a/client/networkerror.cpp b/client/networkerror.cpp index 55c5e54..a6f7f87 100644 --- a/client/networkerror.cpp +++ b/client/networkerror.cpp @@ -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; } diff --git a/client/networkerror.h b/client/networkerror.h index 08edfb5..8aa7b2e 100644 --- a/client/networkerror.h +++ b/client/networkerror.h @@ -3,20 +3,22 @@ #include -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 diff --git a/client/simulator_base.cpp b/client/simulator_base.cpp index 869b2e7..ef9dfe3 100644 --- a/client/simulator_base.cpp +++ b/client/simulator_base.cpp @@ -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(std::string(j.at("type"))).value_or(Web_socket_wrapper::Command::end); + auto response = magic_enum::enum_cast(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(std::string(j.at("type"))).value_or(Web_socket_wrapper::Event::end); + auto response = magic_enum::enum_cast(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(widget); + auto label = qobject_cast(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(widget); + auto slider = qobject_cast(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(widget); + auto dial = qobject_cast(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(widget); + auto thermo = qobject_cast(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(widget); + auto combobox = qobject_cast(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(widget); + auto button = qobject_cast(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(widget); + auto led = qobject_cast(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(widget); + auto can_ui = qobject_cast(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(item); + QPushButton *button = qobject_cast(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(item); + QComboBox *combobox = qobject_cast(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(item); + QwtDial *dial = qobject_cast(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(item); + QwtSlider *slider = qobject_cast(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(item); + Can_Transceive *can = qobject_cast(item); if (can == nullptr) return; - connect(can, &Can_Transceive::send_can_message, this, [=, this](uint32_t id, uint8_t dlc, std::array 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 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); }); } diff --git a/client/simulator_base.h b/client/simulator_base.h index 8ea23bc..c71de9d 100644 --- a/client/simulator_base.h +++ b/client/simulator_base.h @@ -1,64 +1,69 @@ #ifndef SIMULATOR_BASE_H #define SIMULATOR_BASE_H -#include -#include -#include "web_socket_wrapper.h" -#include "networkerror.h" +#include "IterableEnum.hpp" #include "helpers.h" #include "led.h" -#include -#include "IterableEnum.hpp" +#include "networkerror.h" #include "spdlog/spdlog.h" +#include "web_socket_wrapper.h" +#include +#include +#include #include 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::vectorm_ui_lookup; + std::vector m_ui_lookup; }; #endif // SIMULATOR_BASE_H diff --git a/client/simulators/sim-can_debugger.cpp b/client/simulators/sim-can_debugger.cpp index 9698bd8..7e03623 100644 --- a/client/simulators/sim-can_debugger.cpp +++ b/client/simulators/sim-can_debugger.cpp @@ -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; } diff --git a/client/simulators/sim-can_debugger.h b/client/simulators/sim-can_debugger.h index 71c91e8..aa61d0f 100644 --- a/client/simulators/sim-can_debugger.h +++ b/client/simulators/sim-can_debugger.h @@ -1,22 +1,24 @@ #ifndef SIM_CAN_DEBUGGER_H #define SIM_CAN_DEBUGGER_H -#include #include "simulator_base.h" +#include -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 diff --git a/client/simulators/sim-test.cpp b/client/simulators/sim-test.cpp index be0a15d..798c6eb 100644 --- a/client/simulators/sim-test.cpp +++ b/client/simulators/sim-test.cpp @@ -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) diff --git a/client/simulators/sim-test.h b/client/simulators/sim-test.h index d3db76c..c7104a2 100644 --- a/client/simulators/sim-test.h +++ b/client/simulators/sim-test.h @@ -1,25 +1,27 @@ #ifndef SIM_TEST_H #define SIM_TEST_H -#include #include "simulator_base.h" #include "web_socket_wrapper.h" +#include -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 diff --git a/client/web_socket_wrapper.cpp b/client/web_socket_wrapper.cpp index 6292ce3..6d412bc 100644 --- a/client/web_socket_wrapper.cpp +++ b/client/web_socket_wrapper.cpp @@ -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); diff --git a/client/web_socket_wrapper.h b/client/web_socket_wrapper.h index b7f86b4..296dd83 100644 --- a/client/web_socket_wrapper.h +++ b/client/web_socket_wrapper.h @@ -1,8 +1,8 @@ #ifndef WEB_SOCKET_WRAPPER_H #define WEB_SOCKET_WRAPPER_H -#include #include +#include #include 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 diff --git a/external/color b/external/color index adc825c..c1bc9f3 160000 --- a/external/color +++ b/external/color @@ -1 +1 @@ -Subproject commit adc825ce650114cda15e71d113074e37f7228566 +Subproject commit c1bc9f3c203b96d9e6dddfe92e5b4a8756fa4b44 diff --git a/external/cpp-templates b/external/cpp-templates index 4ddbc6d..e0ccbd6 160000 --- a/external/cpp-templates +++ b/external/cpp-templates @@ -1 +1 @@ -Subproject commit 4ddbc6da937bafb655eba44749e8fc8ca15d4136 +Subproject commit e0ccbd6a67c67cf4aec8e2fbd733f8f3ba7fd2a9 diff --git a/server/CAN/CAN_interface.cpp b/server/CAN/CAN_interface.cpp index 9b06d87..8aac267 100644 --- a/server/CAN/CAN_interface.cpp +++ b/server/CAN/CAN_interface.cpp @@ -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(error), m_canDevice->errorString().toStdString()); + SPDLOG_CRITICAL("CAN Interface: {} {}", magic_enum::enum_name(error), + m_canDevice->errorString().toStdString()); break; default: break; diff --git a/server/CAN/CAN_interface.hpp b/server/CAN/CAN_interface.hpp index df9dc98..6d88354 100644 --- a/server/CAN/CAN_interface.hpp +++ b/server/CAN/CAN_interface.hpp @@ -1,8 +1,8 @@ #pragma once -#include #include #include #include +#include class CAN_Interface : public QObject { diff --git a/server/main.cpp b/server/main.cpp index 5b17afb..67756ac 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -13,8 +13,9 @@ void init_logger() { spdlog::init_thread_pool(8192, 1); auto stdout_sink = std::make_shared(); - //auto rotating_sink = std::make_shared("log-sim_us.txt", 1024 * 1024 * 10, 3); - std::vector sinks{stdout_sink/*, rotating_sink*/}; + // auto rotating_sink = std::make_shared("log-sim_us.txt", 1024 * 1024 * 10, + // 3); + std::vector sinks{stdout_sink /*, rotating_sink*/}; auto logger = std::make_shared("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); spdlog::set_default_logger(logger); @@ -32,13 +33,13 @@ int main(int argc, char *argv[]) parser.addHelpOption(); QCommandLineOption port_option(QStringList() << "p" - << "port", - QCoreApplication::translate("main", "Port for echoserver [default: 9002]."), - QCoreApplication::translate("main", "port"), QLatin1String("9002")); + << "port", + QCoreApplication::translate("main", "Port for echoserver [default: 9002]."), + QCoreApplication::translate("main", "port"), QLatin1String("9002")); QCommandLineOption can_port_option(QStringList() << "c" - << "can", - QCoreApplication::translate("main", "CAN device [default: can0]."), - QCoreApplication::translate("main", "can device"), QLatin1String("can0")); + << "can", + QCoreApplication::translate("main", "CAN device [default: can0]."), + QCoreApplication::translate("main", "can device"), QLatin1String("can0")); parser.addOption(port_option); parser.addOption(can_port_option); parser.process(a); diff --git a/server/message_parser.cpp b/server/message_parser.cpp index 500c22a..e39cf1c 100644 --- a/server/message_parser.cpp +++ b/server/message_parser.cpp @@ -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)event.at("payload")); + ->can_send((uint32_t)event.at("sid"), (uint8_t)event.at("dlc"), + (std::array)event.at("payload")); break; } case Event::can_clear: { diff --git a/server/simulators.hpp b/server/simulators.hpp index 060716c..bff2b8e 100644 --- a/server/simulators.hpp +++ b/server/simulators.hpp @@ -3,6 +3,7 @@ #include #include +#include "CAN_wrapper.hpp" #include "simulator_base.hpp" #include "websocket.hpp" diff --git a/server/simulators/can_debugger.cpp b/server/simulators/can_debugger.cpp index 66fac87..d1d3c8d 100644 --- a/server/simulators/can_debugger.cpp +++ b/server/simulators/can_debugger.cpp @@ -1,13 +1,12 @@ #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); - connect(&m_can_ui, &UI_can::on_send, this, [=, this](QCanBusFrame frame){m_wrapper->send_can_message(frame);}); + connect(&m_can_ui, &UI_can::on_send, this, [=, this](QCanBusFrame frame) { m_wrapper->send_can_message(frame); }); } void Can_Debugger::timer() @@ -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() diff --git a/server/simulators/can_debugger.hpp b/server/simulators/can_debugger.hpp index 1d1db4a..e3c3f52 100644 --- a/server/simulators/can_debugger.hpp +++ b/server/simulators/can_debugger.hpp @@ -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; diff --git a/server/simulators/test_sim.cpp b/server/simulators/test_sim.cpp index a5e462d..2157f43 100644 --- a/server/simulators/test_sim.cpp +++ b/server/simulators/test_sim.cpp @@ -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() @@ -47,20 +48,20 @@ void Test_Sim::run_at_startup() SPDLOG_INFO("Test_Sim started"); } -void Test_Sim::button_clicked(UI_button* id) +void Test_Sim::button_clicked(UI_button *id) { if (id == &this->m_button) { bool state = this->m_button.bg_color() == Color(Color::Red) ? true : false; this->m_button.set_bg_color(state ? Color(Color::Green) : Color(Color::Red)); this->m_button.set_text(state ? "On" : "Off"); - + this->m_led.set_led_color(state ? Color(Color::Green) : Color(Color::Red)); return; } } -void Test_Sim::slider_changed(UI_slider* id, double value) +void Test_Sim::slider_changed(UI_slider *id, double value) { if (id == &this->m_slider) { diff --git a/server/simulators/test_sim.hpp b/server/simulators/test_sim.hpp index a6e98a3..a620b95 100644 --- a/server/simulators/test_sim.hpp +++ b/server/simulators/test_sim.hpp @@ -1,23 +1,25 @@ #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); - private: - UI_button m_button; - UI_combobox m_combobox; - UI_label m_label; - UI_checkbox m_checkbox; - UI_slider m_slider; - UI_dial m_dial; - UI_thermo m_thermo; - UI_led m_led; + private: + void button_clicked(UI_button *id); + void slider_changed(UI_slider *id, double value); + + private: + UI_button m_button; + UI_combobox m_combobox; + UI_label m_label; + UI_checkbox m_checkbox; + UI_slider m_slider; + UI_dial m_dial; + UI_thermo m_thermo; + UI_led m_led; }; diff --git a/src/UI/UI_checkbox.cpp b/src/UI/UI_checkbox.cpp index 02d069b..98db114 100644 --- a/src/UI/UI_checkbox.cpp +++ b/src/UI/UI_checkbox.cpp @@ -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() diff --git a/src/UI/UI_checkbox.hpp b/src/UI/UI_checkbox.hpp index a125e7c..dc129f9 100644 --- a/src/UI/UI_checkbox.hpp +++ b/src/UI/UI_checkbox.hpp @@ -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(); diff --git a/src/UI/UI_combobox.cpp b/src/UI/UI_combobox.cpp index a4c5f7d..1cd8f5b 100644 --- a/src/UI/UI_combobox.cpp +++ b/src/UI/UI_combobox.cpp @@ -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 options, size_t selected, QObject *parent) +UI_combobox::UI_combobox(Color fg_color, Color bg_color, std::vector options, size_t selected, + QObject *parent) : UI_x_boxes(UI_COMBOBOX_TYPE, "", fg_color, bg_color, options, parent), m_selected(selected) { } diff --git a/src/UI/UI_combobox.hpp b/src/UI/UI_combobox.hpp index d2851b6..2524ca6 100644 --- a/src/UI/UI_combobox.hpp +++ b/src/UI/UI_combobox.hpp @@ -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 options, - size_t selected = 0, QObject *parent = nullptr); + UI_combobox(Color fg_color, Color bg_color, std::vector options, size_t selected = 0, + QObject *parent = nullptr); UI_combobox(const json &j, QObject *parent = nullptr); void set_selected(size_t selected); diff --git a/src/UI/UI_dial.cpp b/src/UI/UI_dial.cpp index 54417bc..4640610 100644 --- a/src/UI/UI_dial.cpp +++ b/src/UI/UI_dial.cpp @@ -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); } diff --git a/src/UI/UI_item.cpp b/src/UI/UI_item.cpp index 1d15401..8ee0e96 100644 --- a/src/UI/UI_item.cpp +++ b/src/UI/UI_item.cpp @@ -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 payload) void UI_item::can_clear() { - } diff --git a/src/UI/UI_item.hpp b/src/UI/UI_item.hpp index 000f163..1d8a534 100644 --- a/src/UI/UI_item.hpp +++ b/src/UI/UI_item.hpp @@ -6,8 +6,8 @@ #include #include -#include #include +#include 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(); diff --git a/src/UI/UI_led.hpp b/src/UI/UI_led.hpp index 5161f50..2f1a8ee 100644 --- a/src/UI/UI_led.hpp +++ b/src/UI/UI_led.hpp @@ -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; diff --git a/src/UI/UI_slider.cpp b/src/UI/UI_slider.cpp index e565b58..5b3d35e 100644 --- a/src/UI/UI_slider.cpp +++ b/src/UI/UI_slider.cpp @@ -2,17 +2,17 @@ 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) { } UI_slider::UI_slider(std::string text, Color fg_color, Color bg_color, double min, double max, double value, - QObject *parent) - : UI_slider(text, "", fg_color, bg_color, min, max, value, parent) + 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) { from_json(j); diff --git a/src/UI/UI_thermo.cpp b/src/UI/UI_thermo.cpp index 8b149d0..ded7288 100644 --- a/src/UI/UI_thermo.cpp +++ b/src/UI/UI_thermo.cpp @@ -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) { } diff --git a/src/UI/UI_x_boxes.hpp b/src/UI/UI_x_boxes.hpp index 071cf12..a49a850 100644 --- a/src/UI/UI_x_boxes.hpp +++ b/src/UI/UI_x_boxes.hpp @@ -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 options, QObject *parent = nullptr); + UI_x_boxes(std::string type, std::string text, Color fg_color, Color bg_color, std::vector options, + QObject *parent = nullptr); void from_json(const json &j) override; json to_json(size_t id) const override; diff --git a/src/UI/custom/UI_can.cpp b/src/UI/custom/UI_can.cpp index 1f701bf..2e4cff7 100644 --- a/src/UI/custom/UI_can.cpp +++ b/src/UI/custom/UI_can.cpp @@ -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>(msg.payload())); + CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(), + ByteArrayToStdArray>(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>(msg.payload())); + CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(), + ByteArrayToStdArray>(msg.payload())); m_can_received_messages.push_back(frame); emit value_changed(); } diff --git a/src/UI/custom/UI_can.hpp b/src/UI/custom/UI_can.hpp index a537240..77043ea 100644 --- a/src/UI/custom/UI_can.hpp +++ b/src/UI/custom/UI_can.hpp @@ -8,10 +8,8 @@ class CanFrame { public: - CanFrame(uint32_t sid, uint8_t dlc, std::array payload) - : m_SID(sid), m_DLC(dlc), m_payload(payload) + CanFrame(uint32_t sid, uint8_t dlc, std::array 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 m_can_send_messages; std::vector m_can_received_messages;