From 88eeb7e6cacd900c95d4c3b2c167c852df7cc0eb Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Wed, 23 Oct 2024 10:02:43 +0200 Subject: [PATCH] UI UI_can --- src/UI/custom/UI_can.cpp | 60 +++++++++++++++++++++------------------- src/UI/custom/UI_can.hpp | 21 +++++++------- 2 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/UI/custom/UI_can.cpp b/src/UI/custom/UI_can.cpp index f4dca0a..9b680da 100644 --- a/src/UI/custom/UI_can.cpp +++ b/src/UI/custom/UI_can.cpp @@ -1,8 +1,21 @@ #include "UI_can.hpp" -UI_can::UI_can(std::string id, std::string text, Color fg_color, Color bg_color, - std::function on_send) - : UI_item(id, UI_CAN_TYPE, text, 12, fg_color, bg_color), m_on_can_send(on_send) +template T ByteArrayToStdArray(const QByteArray &byteArray) +{ + T stdArray; + + // qDebug() << "std::array size" <(); - can_msg.id = msg.at("id").get(); - can_msg.dlc = msg.at("dlc").get(); - for (size_t i = 0; i < can_msg.dlc; i++) - { - can_msg.data[i] = msg.at("data").at(i).get(); - } + 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); } } -json UI_can::to_json() const +json UI_can::to_json(size_t id) const { - json j = UI_item::to_json(); + json j = UI_item::to_json(id); json can_messages = json::array(); for (const auto &msg : m_can_messages) { json can_msg; - can_msg["timestamp"] = msg.timestamp; - can_msg["id"] = msg.id; - can_msg["dlc"] = msg.dlc; - json data = json::array(); - for (int i = 0; i < msg.dlc; i++) - { - data.push_back(msg.data[i]); - } - can_msg["data"] = data; + 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); } j["can_messages"] = can_messages; return j; } -void UI_can::can_send(can_message_t msg) +void UI_can::can_send(uint32_t id, uint8_t dlc, std::array payload) { - if (m_on_can_send) - { - m_on_can_send(msg); - } + QCanBusFrame frame; + frame.setFrameId(id); + 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 14a6a38..f3c385f 100644 --- a/src/UI/custom/UI_can.hpp +++ b/src/UI/custom/UI_can.hpp @@ -1,27 +1,26 @@ #pragma once #include "UI_item.hpp" -#include +#include #include -#include "CAN_interface.hpp" +#include #define UI_CAN_TYPE std::string("UI_can") class UI_can : public UI_item { public: - UI_can(std::string id, std::string text, Color fg_color, Color bg_color, - std::function on_send = nullptr); + UI_can(std::string text, Color fg_color, Color bg_color); UI_can(const json &j); - void add_can_message(can_message_t msg); + void add_can_message(QCanBusFrame& msg); void from_json(const json &j) override; - json to_json() const override; - - void can_send(can_message_t msg) override; + json to_json(size_t id) const override; + void can_send(uint32_t id, uint8_t dlc, std::array payload) override; + signals: + void on_send(QCanBusFrame msg); private: - std::function m_on_can_send = nullptr; - std::vector m_can_messages; -}; \ No newline at end of file + std::vector m_can_messages; +};