Files
remote-ui/UI/ui_can.cpp
T
sani7 2a76e13305 Removes JSON constructor from UI elements
This fixes the error: clang:optin.cplusplus.VirtualCall
When virtual methods are called during construction and destruction the polymorphism is restricted to the class that’s being constructed or destructed because the more derived contexts are either not yet initialized or already destructed.
2026-02-14 14:32:16 +01:00

112 lines
2.8 KiB
C++

#include "ui_can.hpp"
template <class T> T ByteArrayToStdArray(const QByteArray &byteArray)
{
T stdArray;
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(Color fg_color, Color bg_color, QObject *parent) : UI_item(UI_CAN_TYPE, "", fg_color, bg_color, parent)
{
setup_item(false, true, true);
}
UI_can::UI_can(QObject *parent) : UI_item(UI_CAN_TYPE, parent)
{
setup_item(false, true, true);
}
void UI_can::add_send_message(QCanBusFrame msg)
{
CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(),
ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload()));
m_can_send_messages.push_back(frame);
emit ui_changed();
}
void UI_can::add_received_message(QCanBusFrame msg)
{
CanFrame frame(msg.frameId(), (uint8_t)msg.payload().size(),
ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload()));
m_can_received_messages.push_back(frame);
emit ui_changed();
}
void UI_can::clear_send_buffer()
{
m_can_send_messages.clear();
emit ui_changed();
}
void UI_can::clear_receive_buffer()
{
m_can_received_messages.clear();
emit ui_changed();
}
void UI_can::can_clear()
{
clear_send_buffer();
clear_receive_buffer();
}
void UI_can::from_json(const json &j)
{
UI_item::from_json(j);
m_can_send_messages.clear();
m_can_received_messages.clear();
for (const auto &msg : j.at("send_msgs"))
{
CanFrame can_msg(msg.at("id"), msg.at("dlc"), msg.at("payload"));
m_can_send_messages.push_back(can_msg);
}
for (const auto &msg : j.at("rcvd_msgs"))
{
CanFrame can_msg(msg.at("id"), msg.at("dlc"), msg.at("payload"));
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_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.m_SID;
can_msg["dlc"] = msg.m_DLC;
can_msg["payload"] = msg.m_payload;
can_send_messages.push_back(can_msg);
}
j["send_msgs"] = can_send_messages;
for (const auto &msg : m_can_received_messages)
{
json can_msg;
can_msg["id"] = (uint32_t)msg.m_SID;
can_msg["dlc"] = msg.m_DLC;
can_msg["payload"] = msg.m_payload;
can_received_messages.push_back(can_msg);
}
j["rcvd_msgs"] = can_received_messages;
return j;
}
void UI_can::can_send(uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload)
{
QCanBusFrame frame;
frame.setFrameId(id);
frame.setPayload(QByteArray::fromRawData((const char *)(payload.data()), dlc));
emit send(frame);
}