mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
Add can ui element
This commit is contained in:
@@ -1 +1,15 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <ctime>
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
typedef struct can_message_s
|
||||||
|
{
|
||||||
|
time_t timestamp;
|
||||||
|
uint16_t id;
|
||||||
|
uint8_t dlc;
|
||||||
|
uint8_t data[8];
|
||||||
|
|
||||||
|
can_message_s() : timestamp(0), id(0), dlc(0), data{0}
|
||||||
|
{
|
||||||
|
}
|
||||||
|
} can_message_t;
|
||||||
@@ -18,6 +18,8 @@ target_compile_options(UI PRIVATE $<$<CONFIG:Debug>:
|
|||||||
-Wall -Wextra -pedantic-errors -Wconversion -Wsign-conversion
|
-Wall -Wextra -pedantic-errors -Wconversion -Wsign-conversion
|
||||||
>)
|
>)
|
||||||
|
|
||||||
|
file(GLOB_RECURSE CUSTOM_UI_SOURCES "custom/*.cpp")
|
||||||
|
|
||||||
target_sources(UI
|
target_sources(UI
|
||||||
PRIVATE
|
PRIVATE
|
||||||
${CMAKE_CURRENT_LIST_DIR}/UI_item.cpp
|
${CMAKE_CURRENT_LIST_DIR}/UI_item.cpp
|
||||||
@@ -42,6 +44,8 @@ target_sources(UI
|
|||||||
${CMAKE_CURRENT_LIST_DIR}/UI_thermo.hpp
|
${CMAKE_CURRENT_LIST_DIR}/UI_thermo.hpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/UI_x_boxes.cpp
|
${CMAKE_CURRENT_LIST_DIR}/UI_x_boxes.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/UI_x_boxes.hpp
|
${CMAKE_CURRENT_LIST_DIR}/UI_x_boxes.hpp
|
||||||
|
|
||||||
|
${CUSTOM_UI_SOURCES}
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(UI
|
target_include_directories(UI
|
||||||
@@ -49,6 +53,7 @@ target_include_directories(UI
|
|||||||
${CMAKE_CURRENT_LIST_DIR}
|
${CMAKE_CURRENT_LIST_DIR}
|
||||||
PRIVATE
|
PRIVATE
|
||||||
${PROJECT_DIR}/external/**
|
${PROJECT_DIR}/external/**
|
||||||
|
${PROJECT_DIR}/src/CAN/
|
||||||
${CMAKE_BINARY_DIR}/**
|
${CMAKE_BINARY_DIR}/**
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -161,3 +161,8 @@ void UI_item::set_value(double value)
|
|||||||
{
|
{
|
||||||
UNUSED(value);
|
UNUSED(value);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void UI_item::can_send(can_message_t msg)
|
||||||
|
{
|
||||||
|
UNUSED(msg);
|
||||||
|
};
|
||||||
@@ -49,6 +49,7 @@ class UI_item : public QObject
|
|||||||
virtual void click();
|
virtual void click();
|
||||||
virtual void set_selected(std::string selected);
|
virtual void set_selected(std::string selected);
|
||||||
virtual void set_value(double value);
|
virtual void set_value(double value);
|
||||||
|
virtual void can_send(can_message_t msg);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void value_changed();
|
void value_changed();
|
||||||
|
|||||||
@@ -0,0 +1,65 @@
|
|||||||
|
#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)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
m_can_messages.push_back(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
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"))
|
||||||
|
{
|
||||||
|
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>();
|
||||||
|
}
|
||||||
|
m_can_messages.push_back(can_msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
json UI_can::to_json() const
|
||||||
|
{
|
||||||
|
json j = UI_item::to_json();
|
||||||
|
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_messages.push_back(can_msg);
|
||||||
|
}
|
||||||
|
j["can_messages"] = can_messages;
|
||||||
|
return j;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UI_can::can_send(can_message_t msg)
|
||||||
|
{
|
||||||
|
if (m_on_can_send)
|
||||||
|
{
|
||||||
|
m_on_can_send(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "UI_item.hpp"
|
||||||
|
#include <ctime>
|
||||||
|
#include <chrono>
|
||||||
|
#include "CAN_interface.hpp"
|
||||||
|
|
||||||
|
#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(const json &j);
|
||||||
|
|
||||||
|
void add_can_message(can_message_t msg);
|
||||||
|
|
||||||
|
void from_json(const json &j) override;
|
||||||
|
json to_json() const override;
|
||||||
|
|
||||||
|
void can_send(can_message_t msg) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::function<void(can_message_t msg)> m_on_can_send = nullptr;
|
||||||
|
std::vector<can_message_t> m_can_messages;
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user