mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
UI
UI_can
This commit is contained in:
+32
-28
@@ -1,8 +1,21 @@
|
|||||||
#include "UI_can.hpp"
|
#include "UI_can.hpp"
|
||||||
|
|
||||||
UI_can::UI_can(std::string id, std::string text, Color fg_color, Color bg_color,
|
template <class T> T ByteArrayToStdArray(const QByteArray &byteArray)
|
||||||
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)
|
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);
|
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);
|
m_can_messages.push_back(msg);
|
||||||
}
|
}
|
||||||
@@ -22,44 +35,35 @@ void UI_can::from_json(const json &j)
|
|||||||
m_can_messages.clear();
|
m_can_messages.clear();
|
||||||
for (const auto &msg : j.at("can_messages"))
|
for (const auto &msg : j.at("can_messages"))
|
||||||
{
|
{
|
||||||
can_message_t can_msg;
|
QCanBusFrame can_msg;
|
||||||
can_msg.timestamp = msg.at("timestamp").get<time_t>();
|
can_msg.setFrameId(msg.at("id"));
|
||||||
can_msg.id = msg.at("id").get<uint16_t>();
|
can_msg.setPayload(QByteArray(msg.at("bytes"), msg.at("dlc")));
|
||||||
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>();
|
|
||||||
}
|
|
||||||
m_can_messages.push_back(can_msg);
|
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();
|
json can_messages = json::array();
|
||||||
for (const auto &msg : m_can_messages)
|
for (const auto &msg : m_can_messages)
|
||||||
{
|
{
|
||||||
json can_msg;
|
json can_msg;
|
||||||
can_msg["timestamp"] = msg.timestamp;
|
can_msg["id"] = (uint32_t)msg.frameId();
|
||||||
can_msg["id"] = msg.id;
|
can_msg["dlc"] = msg.payload().size();
|
||||||
can_msg["dlc"] = msg.dlc;
|
can_msg["bytes"] = ByteArrayToStdArray<std::array<uint8_t, 8>>(msg.payload());
|
||||||
json data = json::array();
|
|
||||||
for (int i = 0; i < msg.dlc; i++)
|
|
||||||
{
|
|
||||||
data.push_back(msg.data[i]);
|
|
||||||
}
|
|
||||||
can_msg["data"] = data;
|
|
||||||
can_messages.push_back(can_msg);
|
can_messages.push_back(can_msg);
|
||||||
}
|
}
|
||||||
j["can_messages"] = can_messages;
|
j["can_messages"] = can_messages;
|
||||||
return j;
|
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)
|
QCanBusFrame frame;
|
||||||
{
|
frame.setFrameId(id);
|
||||||
m_on_can_send(msg);
|
frame.setPayload(QByteArray::fromRawData((const char *)(payload.data()), dlc));
|
||||||
}
|
|
||||||
|
emit on_send(frame);
|
||||||
}
|
}
|
||||||
+10
-11
@@ -1,27 +1,26 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "UI_item.hpp"
|
#include "UI_item.hpp"
|
||||||
#include <ctime>
|
#include <QCanBusFrame>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include "CAN_interface.hpp"
|
#include <ctime>
|
||||||
|
|
||||||
#define UI_CAN_TYPE std::string("UI_can")
|
#define UI_CAN_TYPE std::string("UI_can")
|
||||||
|
|
||||||
class UI_can : public UI_item
|
class UI_can : public UI_item
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UI_can(std::string id, std::string text, Color fg_color, Color bg_color,
|
UI_can(std::string text, Color fg_color, Color bg_color);
|
||||||
std::function<void(can_message_t msg)> on_send = nullptr);
|
|
||||||
UI_can(const json &j);
|
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;
|
void from_json(const json &j) override;
|
||||||
json to_json() const override;
|
json to_json(size_t id) const override;
|
||||||
|
|
||||||
void can_send(can_message_t msg) override;
|
|
||||||
|
|
||||||
|
void can_send(uint32_t id, uint8_t dlc, std::array<uint8_t, 8> payload) override;
|
||||||
|
signals:
|
||||||
|
void on_send(QCanBusFrame msg);
|
||||||
private:
|
private:
|
||||||
std::function<void(can_message_t msg)> m_on_can_send = nullptr;
|
std::vector<QCanBusFrame> m_can_messages;
|
||||||
std::vector<can_message_t> m_can_messages;
|
};
|
||||||
};
|
|
||||||
|
|||||||
Reference in New Issue
Block a user