CAN debuger

This commit is contained in:
2026-02-01 18:42:24 +01:00
parent 10604e4b03
commit e7f13d9965
6 changed files with 86 additions and 25 deletions
+2 -1
View File
@@ -18,7 +18,7 @@ target_compile_options(UI PRIVATE $<$<CONFIG:Debug>:
-Wall -Wextra -pedantic-errors -Wconversion -Wsign-conversion
>)
file(GLOB_RECURSE CUSTOM_UI_SOURCES "custom/*.cpp")
file(GLOB_RECURSE CUSTOM_UI_SOURCES "custom/*.?pp")
target_sources(UI
PRIVATE
@@ -51,6 +51,7 @@ target_sources(UI
target_include_directories(UI
PUBLIC
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/custom/**
PRIVATE
${PROJECT_DIR}/external/**
${PROJECT_DIR}/src/CAN/
+2
View File
@@ -9,3 +9,5 @@
#include "UI_slider.hpp"
#include "UI_textbox.hpp"
#include "UI_thermo.hpp"
#include "custom/UI_can.hpp"
+50 -12
View File
@@ -14,48 +14,86 @@ template <class T> T ByteArrayToStdArray(const QByteArray &byteArray)
return stdArray;
}
UI_can::UI_can(std::string text, Color fg_color, Color bg_color)
: UI_item(UI_CAN_TYPE, text, 12, fg_color, bg_color)
UI_can::UI_can(Color fg_color, Color bg_color, QObject *parent)
: UI_item(UI_CAN_TYPE, "", 12, fg_color, bg_color, parent)
{
}
UI_can::UI_can(const json &j) : UI_item(UI_CAN_TYPE)
UI_can::UI_can(const json &j, QObject *parent) : UI_item(UI_CAN_TYPE, parent)
{
from_json(j);
}
void UI_can::add_can_message(QCanBusFrame& msg)
void UI_can::add_send_message(QCanBusFrame& msg)
{
m_can_messages.push_back(msg);
m_can_send_messages.push_back(msg);
emit value_changed();
}
void UI_can::add_received_message(QCanBusFrame& msg)
{
m_can_received_messages.push_back(msg);
emit value_changed();
}
void UI_can::clear_send_buffer()
{
m_can_send_messages.clear();
emit value_changed();
}
void UI_can::clear_receive_buffer()
{
m_can_received_messages.clear();
emit value_changed();
}
void UI_can::from_json(const json &j)
{
UI_item::from_json(j);
m_can_messages.clear();
for (const auto &msg : j.at("can_messages"))
m_can_send_messages.clear();
m_can_received_messages.clear();
for (const auto &msg : j.at("can_send_messages"))
{
QCanBusFrame can_msg;
can_msg.setFrameId(msg.at("id"));
can_msg.setPayload(QByteArray(msg.at("bytes"), msg.at("dlc")));
m_can_messages.push_back(can_msg);
m_can_send_messages.push_back(can_msg);
}
for (const auto &msg : j.at("can_received_messages"))
{
QCanBusFrame can_msg;
can_msg.setFrameId(msg.at("id"));
can_msg.setPayload(QByteArray(msg.at("bytes"), msg.at("dlc")));
m_can_received_messages.push_back(can_msg);
}
}
json UI_can::to_json(size_t id) const
{
json j = UI_item::to_json(id);
json can_messages = json::array();
for (const auto &msg : m_can_messages)
json can_send_messages = json::array();
json can_received_messages = json::array();
for (const auto &msg : m_can_send_messages)
{
json can_msg;
can_msg["id"] = (uint32_t)msg.frameId();
can_msg["dlc"] = msg.payload().size();
can_msg["bytes"] = ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload());
can_messages.push_back(can_msg);
can_send_messages.push_back(can_msg);
}
j["can_messages"] = can_messages;
j["can_send_messages"] = can_send_messages;
for (const auto &msg : m_can_received_messages)
{
json can_msg;
can_msg["id"] = (uint32_t)msg.frameId();
can_msg["dlc"] = msg.payload().size();
can_msg["bytes"] = ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload());
can_received_messages.push_back(can_msg);
}
j["can_received_messages"] = can_received_messages;
return j;
}
+9 -4
View File
@@ -7,11 +7,15 @@
class UI_can : public UI_item
{
Q_OBJECT
public:
UI_can(std::string text, Color fg_color, Color bg_color);
UI_can(const json &j);
UI_can(Color fg_color, Color bg_color, QObject *parent = nullptr);
UI_can(const json &j, QObject *parent = nullptr);
void add_can_message(QCanBusFrame& msg);
void add_send_message(QCanBusFrame& msg);
void add_received_message(QCanBusFrame& msg);
void clear_send_buffer();
void clear_receive_buffer();
void from_json(const json &j) override;
json to_json(size_t id) const override;
@@ -20,5 +24,6 @@ class UI_can : public UI_item
signals:
void on_send(QCanBusFrame msg);
private:
std::vector<QCanBusFrame> m_can_messages;
std::vector<QCanBusFrame> m_can_send_messages;
std::vector<QCanBusFrame> m_can_received_messages;
};
+17 -5
View File
@@ -1,10 +1,16 @@
#include "can_debugger.hpp"
#include <iostream>
Can_Debugger::Can_Debugger() :
Simulator_base("Can_Debugger")
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),
m_clear_can_list("Clear list", Color::Black, Color::White)
{
//this->add_UI_item(&this->);
this->add_UI_item(&this->m_can_ui);
this->add_UI_item(&this->m_clear_can_list);
connect(&m_clear_can_list, &UI_button::on_click, this, [=, this]{this->m_can_ui.clear_send_buffer(); this->m_can_ui.clear_receive_buffer();});
connect(&m_can_ui, &UI_can::on_send, this, [=, this](QCanBusFrame frame){m_wrapper->send_can_message(frame);});
}
void Can_Debugger::timer()
@@ -14,5 +20,11 @@ void Can_Debugger::timer()
void Can_Debugger::run_at_startup()
{
// 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);});
}
void Can_Debugger::run_at_stop()
{
disconnect(m_wrapper, nullptr, nullptr, nullptr);
}
+4 -1
View File
@@ -7,6 +7,9 @@ class Can_Debugger : public Simulator_base {
Can_Debugger();
void timer() override;
void run_at_startup() override;
void run_at_stop() override;
private:
CAN_Wrapper* m_wrapper;
UI_can m_can_ui;
UI_button m_clear_can_list;
};