From e7f13d99658fc9b07f4bffb99e02926d3acb04ab Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Thu, 30 Jan 2025 11:08:46 +0100 Subject: [PATCH] CAN debuger --- src/UI/CMakeLists.txt | 3 +- src/UI/UI.hpp | 4 +- src/UI/custom/UI_can.cpp | 64 ++++++++++++++++++++++++------- src/UI/custom/UI_can.hpp | 13 +++++-- tools/simulators/can_debugger.cpp | 22 ++++++++--- tools/simulators/can_debugger.hpp | 5 ++- 6 files changed, 86 insertions(+), 25 deletions(-) diff --git a/src/UI/CMakeLists.txt b/src/UI/CMakeLists.txt index 5c7f06c..db10a56 100644 --- a/src/UI/CMakeLists.txt +++ b/src/UI/CMakeLists.txt @@ -18,7 +18,7 @@ target_compile_options(UI PRIVATE $<$: -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/ diff --git a/src/UI/UI.hpp b/src/UI/UI.hpp index a2dff43..098094d 100644 --- a/src/UI/UI.hpp +++ b/src/UI/UI.hpp @@ -8,4 +8,6 @@ #include "UI_led.hpp" #include "UI_slider.hpp" #include "UI_textbox.hpp" -#include "UI_thermo.hpp" \ No newline at end of file +#include "UI_thermo.hpp" + +#include "custom/UI_can.hpp" diff --git a/src/UI/custom/UI_can.cpp b/src/UI/custom/UI_can.cpp index 9b680da..65c4b69 100644 --- a/src/UI/custom/UI_can.cpp +++ b/src/UI/custom/UI_can.cpp @@ -14,48 +14,86 @@ template 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>(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>(msg.payload()); + + can_received_messages.push_back(can_msg); + } + j["can_received_messages"] = can_received_messages; return j; } @@ -66,4 +104,4 @@ void UI_can::can_send(uint32_t id, uint8_t dlc, std::array payload) frame.setPayload(QByteArray::fromRawData((const char *)(payload.data()), dlc)); emit on_send(frame); -} \ No newline at end of file +} diff --git a/src/UI/custom/UI_can.hpp b/src/UI/custom/UI_can.hpp index e9f7ea5..94cbeca 100644 --- a/src/UI/custom/UI_can.hpp +++ b/src/UI/custom/UI_can.hpp @@ -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 m_can_messages; + std::vector m_can_send_messages; + std::vector m_can_received_messages; }; diff --git a/tools/simulators/can_debugger.cpp b/tools/simulators/can_debugger.cpp index 1efbb90..cb80abf 100644 --- a/tools/simulators/can_debugger.cpp +++ b/tools/simulators/can_debugger.cpp @@ -1,10 +1,16 @@ #include "can_debugger.hpp" -#include -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); } diff --git a/tools/simulators/can_debugger.hpp b/tools/simulators/can_debugger.hpp index 9b074ae..c836c0c 100644 --- a/tools/simulators/can_debugger.hpp +++ b/tools/simulators/can_debugger.hpp @@ -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; };