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
+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
{
+9 -8
View File
@@ -13,8 +13,9 @@ void init_logger()
{
spdlog::init_thread_pool(8192, 1);
auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
//auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("log-sim_us.txt", 1024 * 1024 * 10, 3);
std::vector<spdlog::sink_ptr> sinks{stdout_sink/*, rotating_sink*/};
// auto rotating_sink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("log-sim_us.txt", 1024 * 1024 * 10,
// 3);
std::vector<spdlog::sink_ptr> sinks{stdout_sink /*, rotating_sink*/};
auto logger = std::make_shared<spdlog::async_logger>("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(),
spdlog::async_overflow_policy::block);
spdlog::set_default_logger(logger);
@@ -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);
+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"
+7 -6
View File
@@ -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()
+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;
+5 -4
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()
@@ -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)
{
+15 -13
View File
@@ -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;
};