UI_can
This commit is contained in:
2024-12-30 12:08:13 +01:00
parent 6f72617371
commit 88eeb7e6ca
2 changed files with 42 additions and 39 deletions
+32 -28
View File
@@ -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<void(can_message_t msg)> on_send)
: UI_item(id, UI_CAN_TYPE, text, 12, fg_color, bg_color), m_on_can_send(on_send)
template <class T> T ByteArrayToStdArray(const QByteArray &byteArray)
{
T stdArray;
// qDebug() << "std::array size" <<stdArray.size();
for (unsigned int i = 0; i < stdArray.size() && i < (unsigned int)byteArray.size(); i++)
{
stdArray[i] = (unsigned char)byteArray.at(i);
}
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)
{
}
@@ -11,7 +24,7 @@ UI_can::UI_can(const json &j) : UI_item(UI_CAN_TYPE)
from_json(j);
}
void UI_can::add_can_message(can_message_t msg)
void UI_can::add_can_message(QCanBusFrame& msg)
{
m_can_messages.push_back(msg);
}
@@ -22,44 +35,35 @@ void UI_can::from_json(const json &j)
m_can_messages.clear();
for (const auto &msg : j.at("can_messages"))
{
can_message_t can_msg;
can_msg.timestamp = msg.at("timestamp").get<time_t>();
can_msg.id = msg.at("id").get<uint16_t>();
can_msg.dlc = msg.at("dlc").get<uint8_t>();
for (size_t i = 0; i < can_msg.dlc; i++)
{
can_msg.data[i] = msg.at("data").at(i).get<uint8_t>();
}
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<std::array<uint8_t, 8>>(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<uint8_t, 8> 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);
}
+10 -11
View File
@@ -1,27 +1,26 @@
#pragma once
#include "UI_item.hpp"
#include <ctime>
#include <QCanBusFrame>
#include <chrono>
#include "CAN_interface.hpp"
#include <ctime>
#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<void(can_message_t msg)> 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<uint8_t, 8> payload) override;
signals:
void on_send(QCanBusFrame msg);
private:
std::function<void(can_message_t msg)> m_on_can_send = nullptr;
std::vector<can_message_t> m_can_messages;
};
std::vector<QCanBusFrame> m_can_messages;
};