From 5fdfb765de997198a1a61fd8e4b67ce8d29eeb28 Mon Sep 17 00:00:00 2001 From: Sander Speetjens Date: Sat, 7 Sep 2024 15:46:31 +0200 Subject: [PATCH] Add can ui element --- src/CAN/CAN_interface.hpp | 16 +++++++++- src/UI/CMakeLists.txt | 5 +++ src/UI/UI_item.cpp | 5 +++ src/UI/UI_item.hpp | 1 + src/UI/custom/UI_can.cpp | 65 +++++++++++++++++++++++++++++++++++++++ src/UI/custom/UI_can.hpp | 27 ++++++++++++++++ 6 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 src/UI/custom/UI_can.cpp create mode 100644 src/UI/custom/UI_can.hpp diff --git a/src/CAN/CAN_interface.hpp b/src/CAN/CAN_interface.hpp index 7b9637e..236ba5d 100644 --- a/src/CAN/CAN_interface.hpp +++ b/src/CAN/CAN_interface.hpp @@ -1 +1,15 @@ -#pragma once \ No newline at end of file +#pragma once +#include +#include + +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; \ No newline at end of file diff --git a/src/UI/CMakeLists.txt b/src/UI/CMakeLists.txt index f0611e5..09f2d7b 100644 --- a/src/UI/CMakeLists.txt +++ b/src/UI/CMakeLists.txt @@ -18,6 +18,8 @@ target_compile_options(UI PRIVATE $<$: -Wall -Wextra -pedantic-errors -Wconversion -Wsign-conversion >) +file(GLOB_RECURSE CUSTOM_UI_SOURCES "custom/*.cpp") + target_sources(UI PRIVATE ${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_x_boxes.cpp ${CMAKE_CURRENT_LIST_DIR}/UI_x_boxes.hpp + + ${CUSTOM_UI_SOURCES} ) target_include_directories(UI @@ -49,6 +53,7 @@ target_include_directories(UI ${CMAKE_CURRENT_LIST_DIR} PRIVATE ${PROJECT_DIR}/external/** + ${PROJECT_DIR}/src/CAN/ ${CMAKE_BINARY_DIR}/** ) diff --git a/src/UI/UI_item.cpp b/src/UI/UI_item.cpp index 1d783fd..404e7d1 100644 --- a/src/UI/UI_item.cpp +++ b/src/UI/UI_item.cpp @@ -161,3 +161,8 @@ void UI_item::set_value(double value) { UNUSED(value); }; + +void UI_item::can_send(can_message_t msg) +{ + UNUSED(msg); +}; \ No newline at end of file diff --git a/src/UI/UI_item.hpp b/src/UI/UI_item.hpp index f7f35ef..7fa43a2 100644 --- a/src/UI/UI_item.hpp +++ b/src/UI/UI_item.hpp @@ -49,6 +49,7 @@ class UI_item : public QObject virtual void click(); virtual void set_selected(std::string selected); virtual void set_value(double value); + virtual void can_send(can_message_t msg); signals: void value_changed(); diff --git a/src/UI/custom/UI_can.cpp b/src/UI/custom/UI_can.cpp new file mode 100644 index 0000000..f4dca0a --- /dev/null +++ b/src/UI/custom/UI_can.cpp @@ -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 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(); + 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(); + } + 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); + } +} \ No newline at end of file diff --git a/src/UI/custom/UI_can.hpp b/src/UI/custom/UI_can.hpp new file mode 100644 index 0000000..14a6a38 --- /dev/null +++ b/src/UI/custom/UI_can.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "UI_item.hpp" +#include +#include +#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 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 m_on_can_send = nullptr; + std::vector m_can_messages; +}; \ No newline at end of file