From 21a2c16607af9e17623423b102dc858a0cb88e8e Mon Sep 17 00:00:00 2001 From: Sani7 Date: Thu, 29 Aug 2024 15:00:27 +0200 Subject: [PATCH] Lint --- src/UI/UI_button.cpp | 9 ++- src/UI/UI_button.hpp | 20 +++--- src/UI/UI_checkbox.cpp | 14 ++-- src/UI/UI_checkbox.hpp | 32 +++++---- src/UI/UI_combobox.cpp | 12 ++-- src/UI/UI_combobox.hpp | 32 +++++---- src/UI/UI_dial.cpp | 5 +- src/UI/UI_dial.hpp | 27 +++---- src/UI/UI_item.cpp | 12 ++-- src/UI/UI_item.hpp | 23 +++--- src/UI/UI_label.cpp | 7 +- src/UI/UI_label.hpp | 8 +-- src/UI/UI_led.cpp | 13 ++-- src/UI/UI_led.hpp | 11 +-- src/UI/UI_slider.cpp | 9 ++- src/UI/UI_slider.hpp | 30 ++++---- src/UI/UI_textbox.cpp | 9 ++- src/UI/UI_textbox.hpp | 14 ++-- src/UI/UI_thermo.cpp | 14 ++-- src/UI/UI_thermo.hpp | 41 +++++------ src/UI/UI_x_boxes.cpp | 8 +-- src/UI/UI_x_boxes.hpp | 16 +++-- tests/Test.hpp | 2 +- tools/main.cpp | 41 ++++++----- tools/message_parser.cpp | 122 +++++++++++++++---------------- tools/simulator_base.cpp | 42 +++++++---- tools/simulator_base.hpp | 150 ++++++++++++++++++++------------------- tools/simulators.cpp | 18 ++--- tools/simulators.hpp | 34 ++++----- tools/websocket.cpp | 61 ++++++++-------- tools/websocket.hpp | 30 +++++--- 31 files changed, 457 insertions(+), 409 deletions(-) diff --git a/src/UI/UI_button.cpp b/src/UI/UI_button.cpp index 168bde9..db222df 100644 --- a/src/UI/UI_button.cpp +++ b/src/UI/UI_button.cpp @@ -1,19 +1,18 @@ #include "UI_button.hpp" -UI_button::UI_button(std::string id, std::string text, Color fg_color, Color bg_color, std::function on_click) +UI_button::UI_button(std::string id, std::string text, Color fg_color, Color bg_color, + std::function on_click) : UI_item(id, UI_BUTTON_TYPE, text, 12, fg_color, bg_color) { this->m_on_click = on_click; } -UI_button::UI_button(const json& j) - : UI_item(UI_BUTTON_TYPE), - m_on_click(nullptr) +UI_button::UI_button(const json &j) : UI_item(UI_BUTTON_TYPE), m_on_click(nullptr) { from_json(j); } -void UI_button::from_json(const json& j) +void UI_button::from_json(const json &j) { UI_item::from_json(j); } diff --git a/src/UI/UI_button.hpp b/src/UI/UI_button.hpp index 4ba100a..a55c2ef 100644 --- a/src/UI/UI_button.hpp +++ b/src/UI/UI_button.hpp @@ -6,14 +6,16 @@ class UI_button : public UI_item { - public: - UI_button(std::string id, std::string text, Color fg_color, Color bg_color, std::function on_click = nullptr); - UI_button(const json& j); - - void from_json(const json& j) override; - json to_json() const override; + public: + UI_button(std::string id, std::string text, Color fg_color, Color bg_color, + std::function on_click = nullptr); + UI_button(const json &j); - void click() const override; - private: - std::function m_on_click = nullptr; + void from_json(const json &j) override; + json to_json() const override; + + void click() const override; + + private: + std::function m_on_click = nullptr; }; \ No newline at end of file diff --git a/src/UI/UI_checkbox.cpp b/src/UI/UI_checkbox.cpp index 3556ea2..fdfcfe8 100644 --- a/src/UI/UI_checkbox.cpp +++ b/src/UI/UI_checkbox.cpp @@ -1,11 +1,12 @@ #include "UI_checkbox.hpp" -UI_checkbox::UI_checkbox() - : UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr) +UI_checkbox::UI_checkbox() : UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr) { } -UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector options, std::function)> on_change) +UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, + std::vector options, + std::function)> on_change) : UI_x_boxes(id, UI_CHECKBOX_TYPE, text, fg_color, bg_color, options), m_selected(), m_on_change(on_change) { for (size_t i = 0; i < m_options.size(); i++) @@ -14,8 +15,7 @@ UI_checkbox::UI_checkbox(std::string id, std::string text, Color fg_color, Color } } -UI_checkbox::UI_checkbox(const json& j) : -UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr) +UI_checkbox::UI_checkbox(const json &j) : UI_x_boxes(UI_CHECKBOX_TYPE), m_selected(), m_on_change(nullptr) { from_json(j); } @@ -61,10 +61,10 @@ std::vector UI_checkbox::selected_text() const return selected_text; } -void UI_checkbox::from_json(const json& j) +void UI_checkbox::from_json(const json &j) { UI_x_boxes::from_json(j); - + if (j.contains("selected")) { m_selected.clear(); diff --git a/src/UI/UI_checkbox.hpp b/src/UI/UI_checkbox.hpp index 2d55380..07461f3 100644 --- a/src/UI/UI_checkbox.hpp +++ b/src/UI/UI_checkbox.hpp @@ -1,26 +1,28 @@ #pragma once -#include #include "UI_item.hpp" #include "UI_x_boxes.hpp" +#include #define UI_CHECKBOX_TYPE std::string("UI_checkbox") class UI_checkbox : public UI_x_boxes { - public: - UI_checkbox(); - UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector options, std::function)> on_change = nullptr); - UI_checkbox(const json& j); + public: + UI_checkbox(); + UI_checkbox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector options, + std::function)> on_change = nullptr); + UI_checkbox(const json &j); - void toggle_selected(size_t selected); - void toggle_selected(std::string selected); - std::vector selected() const; - std::vector selected_text() const; - - void from_json(const json& j) override; - json to_json() const override; - private: - std::vector m_selected; + void toggle_selected(size_t selected); + void toggle_selected(std::string selected); + std::vector selected() const; + std::vector selected_text() const; - std::function)> m_on_change; + void from_json(const json &j) override; + json to_json() const override; + + private: + std::vector m_selected; + + std::function)> m_on_change; }; \ No newline at end of file diff --git a/src/UI/UI_combobox.cpp b/src/UI/UI_combobox.cpp index f51122f..b8ac84e 100644 --- a/src/UI/UI_combobox.cpp +++ b/src/UI/UI_combobox.cpp @@ -1,17 +1,17 @@ #include "UI_combobox.hpp" -UI_combobox::UI_combobox() - : UI_x_boxes(UI_COMBOBOX_TYPE) +UI_combobox::UI_combobox() : UI_x_boxes(UI_COMBOBOX_TYPE) { } -UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector options, size_t selected, std::function on_change) +UI_combobox::UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, + std::vector options, size_t selected, + std::function on_change) : UI_x_boxes(id, UI_COMBOBOX_TYPE, text, fg_color, bg_color, options), m_selected(selected), m_on_change(on_change) { } -UI_combobox::UI_combobox(const json& j) -: UI_x_boxes(UI_COMBOBOX_TYPE), m_on_change(nullptr) +UI_combobox::UI_combobox(const json &j) : UI_x_boxes(UI_COMBOBOX_TYPE), m_on_change(nullptr) { from_json(j); } @@ -49,7 +49,7 @@ std::string UI_combobox::selected_text() const return m_options[m_selected]; } -void UI_combobox::from_json(const json& j) +void UI_combobox::from_json(const json &j) { UI_x_boxes::from_json(j); std::string selected_str = j.at("selected"); diff --git a/src/UI/UI_combobox.hpp b/src/UI/UI_combobox.hpp index 6c07683..bab1c55 100644 --- a/src/UI/UI_combobox.hpp +++ b/src/UI/UI_combobox.hpp @@ -1,27 +1,29 @@ #pragma once -#include #include "UI_item.hpp" #include "UI_x_boxes.hpp" +#include #define UI_COMBOBOX_TYPE std::string("UI_combobox") class UI_combobox : public UI_x_boxes { - public: - UI_combobox(); - UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector options, size_t selected = 0, std::function on_change = nullptr); - UI_combobox(const json& j); + public: + UI_combobox(); + UI_combobox(std::string id, std::string text, Color fg_color, Color bg_color, std::vector options, + size_t selected = 0, std::function on_change = nullptr); + UI_combobox(const json &j); - void set_selected(size_t selected); - void set_selected(std::string selected) override; - size_t selected() const; - std::string selected_text() const; - - void from_json(const json& j) override; - json to_json() const override; - private: - size_t m_selected; + void set_selected(size_t selected); + void set_selected(std::string selected) override; + size_t selected() const; + std::string selected_text() const; - std::function m_on_change; + void from_json(const json &j) override; + json to_json() const override; + + private: + size_t m_selected; + + std::function m_on_change; }; \ No newline at end of file diff --git a/src/UI/UI_dial.cpp b/src/UI/UI_dial.cpp index 6f42b4b..b0f2726 100644 --- a/src/UI/UI_dial.cpp +++ b/src/UI/UI_dial.cpp @@ -5,8 +5,7 @@ UI_dial::UI_dial(std::string id, std::string text, Color fg_color, Color bg_colo { } -UI_dial::UI_dial(const json& j) -: UI_item(UI_DIAL_TYPE) +UI_dial::UI_dial(const json &j) : UI_item(UI_DIAL_TYPE) { from_json(j); } @@ -36,7 +35,7 @@ double UI_dial::max() const return m_max; } -void UI_dial::from_json(const json& j) +void UI_dial::from_json(const json &j) { UI_item::from_json(j); diff --git a/src/UI/UI_dial.hpp b/src/UI/UI_dial.hpp index 36a5950..33e468d 100644 --- a/src/UI/UI_dial.hpp +++ b/src/UI/UI_dial.hpp @@ -5,20 +5,21 @@ class UI_dial : public UI_item { - public: - UI_dial(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value); - UI_dial(const json& j); + public: + UI_dial(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value); + UI_dial(const json &j); - void set_value(double value) override; - double value() const; + void set_value(double value) override; + double value() const; - double min() const; - double max() const; + double min() const; + double max() const; - void from_json(const json& j) override; - json to_json() const override; - private: - double m_min; - double m_max; - double m_value; + void from_json(const json &j) override; + json to_json() const override; + + private: + double m_min; + double m_max; + double m_value; }; \ No newline at end of file diff --git a/src/UI/UI_item.cpp b/src/UI/UI_item.cpp index ca465a0..c209fbf 100644 --- a/src/UI/UI_item.cpp +++ b/src/UI/UI_item.cpp @@ -1,16 +1,18 @@ #include "UI_item.hpp" -UI_item::UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color) - : m_id(id), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color), m_bg_color(bg_color), enabled(true), visible(true) +UI_item::UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color) + : m_id(id), m_type(type), m_text(text), m_text_size(text_size), m_fg_color(fg_color), m_bg_color(bg_color), + enabled(true), visible(true) { } UI_item::UI_item(std::string type) - : m_id(""), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Black), m_bg_color(Color::White), enabled(true), visible(true) + : m_id(""), m_type(type), m_text(""), m_text_size(12), m_fg_color(Color::Black), m_bg_color(Color::White), + enabled(true), visible(true) { } -UI_item::UI_item(const json& j) +UI_item::UI_item(const json &j) { from_json(j); } @@ -106,7 +108,7 @@ bool UI_item::is_visible() const return visible; } -void UI_item::from_json(const json& j) +void UI_item::from_json(const json &j) { if (!is_type(j)) return; diff --git a/src/UI/UI_item.hpp b/src/UI/UI_item.hpp index 8c3894e..6695cda 100644 --- a/src/UI/UI_item.hpp +++ b/src/UI/UI_item.hpp @@ -1,19 +1,20 @@ #pragma once -#include -#include +#include "color.hpp" #include +#include #include #include -#include "color.hpp" +#include using json = nlohmann::json; #define UNUSED(x) (void)(x) -class UI_item { +class UI_item +{ public: UI_item(std::string type); UI_item(std::string id, std::string type, std::string text, uint8_t text_size, Color fg_color, Color bg_color); - UI_item(const json& j); + UI_item(const json &j); std::string id() const; std::string type() const; @@ -37,13 +38,19 @@ class UI_item { void set_visible(bool visible); bool is_visible() const; - virtual void from_json(const json& j); + virtual void from_json(const json &j); virtual json to_json() const; virtual void click() const {}; - virtual void set_selected(std::string selected) {UNUSED(selected);}; - virtual void set_value(double value) {UNUSED(value);}; + virtual void set_selected(std::string selected) + { + UNUSED(selected); + }; + virtual void set_value(double value) + { + UNUSED(value); + }; protected: std::string m_id; diff --git a/src/UI/UI_label.cpp b/src/UI/UI_label.cpp index 082c84e..514b85c 100644 --- a/src/UI/UI_label.cpp +++ b/src/UI/UI_label.cpp @@ -5,18 +5,17 @@ UI_label::UI_label(std::string id, std::string text, Color fg_color, Color bg_co { } -UI_label::UI_label(const json& j) - : UI_item(UI_LABEL_TYPE) +UI_label::UI_label(const json &j) : UI_item(UI_LABEL_TYPE) { from_json(j); } -void UI_label::from_json(const json& j) +void UI_label::from_json(const json &j) { UI_item::from_json(j); } json UI_label::to_json() const { - return UI_item::to_json(); + return UI_item::to_json(); } \ No newline at end of file diff --git a/src/UI/UI_label.hpp b/src/UI/UI_label.hpp index 44614b1..3e3b400 100644 --- a/src/UI/UI_label.hpp +++ b/src/UI/UI_label.hpp @@ -1,15 +1,15 @@ #pragma once -#include #include "UI_item.hpp" +#include #define UI_LABEL_TYPE std::string("UI_label") class UI_label : public UI_item { -public: + public: UI_label(std::string id, std::string text, Color fg_color, Color bg_color); - UI_label(const json& j); + UI_label(const json &j); - void from_json(const json& j) override; + void from_json(const json &j) override; json to_json() const override; }; \ No newline at end of file diff --git a/src/UI/UI_led.cpp b/src/UI/UI_led.cpp index 4a9ac89..7917e0a 100644 --- a/src/UI/UI_led.cpp +++ b/src/UI/UI_led.cpp @@ -1,14 +1,11 @@ #include "UI_led.hpp" -UI_led::UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color) : -UI_item(id, UI_LED_TYPE, text, 12, fg_color, bg_color), -m_led_color(led_color) +UI_led::UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color) + : UI_item(id, UI_LED_TYPE, text, 12, fg_color, bg_color), m_led_color(led_color) { - } -UI_led::UI_led(const json& j) : -UI_item(UI_LED_TYPE) +UI_led::UI_led(const json &j) : UI_item(UI_LED_TYPE) { from_json(j); } @@ -23,7 +20,7 @@ void UI_led::set_led_color(Color led_color) m_led_color = led_color; } -void UI_led::from_json(const json& j) +void UI_led::from_json(const json &j) { UI_item::from_json(j); std::string color; @@ -35,5 +32,5 @@ json UI_led::to_json() const { json j = UI_item::to_json(); j["led_color"] = m_led_color.to_hex(); - return j; + return j; } \ No newline at end of file diff --git a/src/UI/UI_led.hpp b/src/UI/UI_led.hpp index 0e5dfd2..3f52884 100644 --- a/src/UI/UI_led.hpp +++ b/src/UI/UI_led.hpp @@ -1,20 +1,21 @@ #pragma once -#include #include "UI_item.hpp" +#include #define UI_LED_TYPE std::string("UI_led") class UI_led : public UI_item { -public: + public: UI_led(std::string id, std::string text, Color fg_color, Color bg_color, Color led_color); - UI_led(const json& j); + UI_led(const json &j); Color led_color() const; void set_led_color(Color led_color); - void from_json(const json& j) override; + void from_json(const json &j) override; json to_json() const override; -private: + + private: Color m_led_color; }; \ No newline at end of file diff --git a/src/UI/UI_slider.cpp b/src/UI/UI_slider.cpp index fb748e7..78b7280 100644 --- a/src/UI/UI_slider.cpp +++ b/src/UI/UI_slider.cpp @@ -1,14 +1,13 @@ #include "UI_slider.hpp" -UI_slider::UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, std::function on_change) +UI_slider::UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, + double value, std::function on_change) : UI_item(id, UI_SLIDER_TYPE, text, 12, fg_color, bg_color), m_min(min), m_max(max), m_value(value) { this->m_on_change = on_change; } -UI_slider::UI_slider(const json& j) -: UI_item(UI_SLIDER_TYPE), -m_on_change(nullptr) +UI_slider::UI_slider(const json &j) : UI_item(UI_SLIDER_TYPE), m_on_change(nullptr) { from_json(j); } @@ -42,7 +41,7 @@ double UI_slider::max() const return m_max; } -void UI_slider::from_json(const json& j) +void UI_slider::from_json(const json &j) { UI_item::from_json(j); diff --git a/src/UI/UI_slider.hpp b/src/UI/UI_slider.hpp index 7cbd816..2266bb8 100644 --- a/src/UI/UI_slider.hpp +++ b/src/UI/UI_slider.hpp @@ -5,22 +5,24 @@ class UI_slider : public UI_item { - public: - UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, std::function on_change = nullptr); - UI_slider(const json& j); + public: + UI_slider(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value, + std::function on_change = nullptr); + UI_slider(const json &j); - void set_value(double value) override; - double value() const; + void set_value(double value) override; + double value() const; - double min() const; - double max() const; + double min() const; + double max() const; - void from_json(const json& j) override; - json to_json() const override; - private: - double m_min; - double m_max; - double m_value; + void from_json(const json &j) override; + json to_json() const override; - std::function m_on_change; + private: + double m_min; + double m_max; + double m_value; + + std::function m_on_change; }; \ No newline at end of file diff --git a/src/UI/UI_textbox.cpp b/src/UI/UI_textbox.cpp index c981dc7..e64f1b3 100644 --- a/src/UI/UI_textbox.cpp +++ b/src/UI/UI_textbox.cpp @@ -1,13 +1,12 @@ #include "UI_textbox.hpp" -UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, std::function on_change) +UI_textbox::UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, + std::function on_change) : UI_item(id, UI_TEXTBOX_TYPE, "", 0, fg_color, bg_color), m_hint(hint), m_on_change(on_change) { } -UI_textbox::UI_textbox(const json& j) - : UI_item(UI_TEXTBOX_TYPE), - m_on_change(nullptr) +UI_textbox::UI_textbox(const json &j) : UI_item(UI_TEXTBOX_TYPE), m_on_change(nullptr) { from_json(j); } @@ -33,7 +32,7 @@ std::string UI_textbox::hint() const return m_hint; } -void UI_textbox::from_json(const json& j) +void UI_textbox::from_json(const json &j) { UI_item::from_json(j); m_hint = j.at("hint"); diff --git a/src/UI/UI_textbox.hpp b/src/UI/UI_textbox.hpp index cbb7ac3..39eefb3 100644 --- a/src/UI/UI_textbox.hpp +++ b/src/UI/UI_textbox.hpp @@ -1,23 +1,25 @@ #pragma once -#include #include "UI_item.hpp" +#include #define UI_TEXTBOX_TYPE std::string("UI_textbox") class UI_textbox : public UI_item { -public: - UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, std::function on_change = nullptr); - UI_textbox(const json& j); + public: + UI_textbox(std::string id, std::string hint, Color fg_color, Color bg_color, + std::function on_change = nullptr); + UI_textbox(const json &j); void set_text(std::string text) override; void set_hint(std::string hint); std::string hint() const; - void from_json(const json& j) override; + void from_json(const json &j) override; json to_json() const override; -private: + + private: std::string m_hint; std::function m_on_change; }; \ No newline at end of file diff --git a/src/UI/UI_thermo.cpp b/src/UI/UI_thermo.cpp index fe7a059..99e92cc 100644 --- a/src/UI/UI_thermo.cpp +++ b/src/UI/UI_thermo.cpp @@ -1,12 +1,12 @@ #include "UI_thermo.hpp" -UI_thermo::UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value) +UI_thermo::UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, + double value) : UI_item(id, UI_THERMO_TYPE, text, 12, fg_color, bg_color), m_min(min), m_max(max), m_value(value) { } -UI_thermo::UI_thermo(const json& j) -: UI_item(UI_THERMO_TYPE) +UI_thermo::UI_thermo(const json &j) : UI_item(UI_THERMO_TYPE) { from_json(j); } @@ -50,7 +50,7 @@ void UI_thermo::add_color_stop(double value, Color color) { if (value <= 0 || value >= 1) return; - + this->m_color_map[value] = color; } @@ -80,7 +80,7 @@ void UI_thermo::remove_color_stop_normalized(double value) this->m_color_map.erase(normalized); } -void UI_thermo::from_json(const json& j) +void UI_thermo::from_json(const json &j) { UI_item::from_json(j); @@ -88,7 +88,7 @@ void UI_thermo::from_json(const json& j) this->m_max = j.at("max"); this->m_value = j.at("value"); this->m_color_map.clear(); - for (auto& i : j["color_map"]) + for (auto &i : j["color_map"]) { double k = i.at("key"); std::string c = i.at("value"); @@ -103,7 +103,7 @@ json UI_thermo::to_json() const j["max"] = this->m_max; j["value"] = this->m_value; j["color_map"] = json::array(); - for (auto& [key, value] : this->m_color_map) + for (auto &[key, value] : this->m_color_map) { json item = json::object(); item["key"] = key; diff --git a/src/UI/UI_thermo.hpp b/src/UI/UI_thermo.hpp index a255771..64c9a6a 100644 --- a/src/UI/UI_thermo.hpp +++ b/src/UI/UI_thermo.hpp @@ -6,28 +6,29 @@ // TODO: Add color scale class UI_thermo : public UI_item { - public: - UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value); - UI_thermo(const json& j); + public: + UI_thermo(std::string id, std::string text, Color fg_color, Color bg_color, double min, double max, double value); + UI_thermo(const json &j); - void set_value(double value) override; - double value() const; + void set_value(double value) override; + double value() const; - double min() const; - double max() const; + double min() const; + double max() const; - void set_start_color(Color color); - void set_end_color(Color color); - void add_color_stop(double value, Color color); - void add_color_stop_normalized(double value, Color color); - void remove_color_stop(double value); - void remove_color_stop_normalized(double value); + void set_start_color(Color color); + void set_end_color(Color color); + void add_color_stop(double value, Color color); + void add_color_stop_normalized(double value, Color color); + void remove_color_stop(double value); + void remove_color_stop_normalized(double value); - void from_json(const json& j) override; - json to_json() const override; - private: - double m_min; - double m_max; - double m_value; - std::unordered_map m_color_map; + void from_json(const json &j) override; + json to_json() const override; + + private: + double m_min; + double m_max; + double m_value; + std::unordered_map m_color_map; }; \ No newline at end of file diff --git a/src/UI/UI_x_boxes.cpp b/src/UI/UI_x_boxes.cpp index 4244c4d..55ee526 100644 --- a/src/UI/UI_x_boxes.cpp +++ b/src/UI/UI_x_boxes.cpp @@ -1,16 +1,16 @@ #include "UI_x_boxes.hpp" -UI_x_boxes::UI_x_boxes(std::string type) - : UI_item(type), m_options() +UI_x_boxes::UI_x_boxes(std::string type) : UI_item(type), m_options() { } -UI_x_boxes::UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, std::vector options) +UI_x_boxes::UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, + std::vector options) : UI_item(id, type, text, 12, fg_color, bg_color), m_options(options) { } -void UI_x_boxes::from_json(const json& j) +void UI_x_boxes::from_json(const json &j) { UI_item::from_json(j); if (j.contains("options")) diff --git a/src/UI/UI_x_boxes.hpp b/src/UI/UI_x_boxes.hpp index ccf24c6..6c72819 100644 --- a/src/UI/UI_x_boxes.hpp +++ b/src/UI/UI_x_boxes.hpp @@ -4,12 +4,14 @@ class UI_x_boxes : public UI_item { - public: - UI_x_boxes(std::string type); - UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, std::vector options); + public: + UI_x_boxes(std::string type); + UI_x_boxes(std::string id, std::string type, std::string text, Color fg_color, Color bg_color, + std::vector options); - void from_json(const json& j) override; - json to_json() const override; - protected: - std::vector m_options; + void from_json(const json &j) override; + json to_json() const override; + + protected: + std::vector m_options; }; \ No newline at end of file diff --git a/tests/Test.hpp b/tests/Test.hpp index 01c9ace..9393577 100644 --- a/tests/Test.hpp +++ b/tests/Test.hpp @@ -1,5 +1,5 @@ -#include "gtest/gtest.h" #include "UI.hpp" +#include "gtest/gtest.h" #include #define UNUSED(x) (void)(x) \ No newline at end of file diff --git a/tools/main.cpp b/tools/main.cpp index f75f586..079a3b6 100644 --- a/tools/main.cpp +++ b/tools/main.cpp @@ -3,32 +3,36 @@ #include #include -#include "spdlog/spdlog.h" #include "spdlog/async.h" -#include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/sinks/rotating_file_sink.h" +#include "spdlog/sinks/stdout_color_sinks.h" +#include "spdlog/spdlog.h" +#include "message_parser.hpp" #include "simulators.hpp" #include "websocket.hpp" -#include "message_parser.hpp" class Spdlog_buffer : public std::stringbuf { -public: - Spdlog_buffer(std::function log_function) : log_function(log_function) {} - int sync() { + public: + Spdlog_buffer(std::function log_function) : log_function(log_function) + { + } + int sync() + { std::string str = this->str(); str.pop_back(); log_function(str); this->str(""); return 0; } -private: - std::function log_function; + + private: + std::function log_function; }; -Simulators* g_simulators; -Websocket* g_web_socket; +Simulators *g_simulators; +Websocket *g_web_socket; Spdlog_buffer g_info_buffer([](std::string message) { spdlog::info(message); }); Spdlog_buffer g_error_buffer([](std::string message) { spdlog::error(message); }); std::ostream g_out(&g_info_buffer); @@ -52,10 +56,11 @@ void int_handler(int s) void init_logger() { spdlog::init_thread_pool(8192, 1); - auto stdout_sink = std::make_shared(); - auto rotating_sink = std::make_shared("log-sim_us.txt", 1024*1024*10, 3); - std::vector sinks {stdout_sink, rotating_sink}; - auto logger = std::make_shared("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); + auto stdout_sink = std::make_shared(); + auto rotating_sink = std::make_shared("log-sim_us.txt", 1024 * 1024 * 10, 3); + std::vector sinks{stdout_sink, rotating_sink}; + auto logger = std::make_shared("sim_us", sinks.begin(), sinks.end(), spdlog::thread_pool(), + spdlog::async_overflow_policy::block); spdlog::set_default_logger(logger); spdlog::set_pattern("[%H:%M:%S %z] [%n] [%^---%L---%$] [thread %t] %v"); spdlog::flush_every(std::chrono::seconds(3)); @@ -80,14 +85,16 @@ int main(void) spdlog::info("Starting server"); Simulators simulators; g_simulators = &simulators; - - Websocket web_socket(9002, &g_out, &g_err, [](std::string message) { return message_parser(message); }, []() { return g_simulators->changed_UI_items().dump(); }); + + Websocket web_socket( + 9002, &g_out, &g_err, [](std::string message) { return message_parser(message); }, + []() { return g_simulators->changed_UI_items().dump(); }); g_web_socket = &web_socket; // Start the message processor and broadcast processor in separate threads std::thread message_processor_thread(std::bind(&Websocket::process_messages, &web_socket)); std::thread broadcast_processor_thred(std::bind(&Websocket::process_broadcast, &web_socket)); - + // Run the asio loop with the main thread web_socket.run(); diff --git a/tools/message_parser.cpp b/tools/message_parser.cpp index 8cfbc38..8f2f72f 100644 --- a/tools/message_parser.cpp +++ b/tools/message_parser.cpp @@ -1,8 +1,8 @@ #include "message_parser.hpp" -#include #include +#include -extern Simulators* g_simulators; +extern Simulators *g_simulators; std::string message_parser(std::string message) { @@ -31,36 +31,37 @@ json command_parser(json command) Command cmd = magic_enum::enum_cast(type).value_or(Command::end); switch (cmd) { - case Command::get_UI_element: - spdlog::info("Command {}: {}", type, std::string(command.at("id"))); - j["response"]["type"] = type; - j["response"]["UI_item"] = g_simulators->invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(); - break; - case Command::get_UI_elements: - spdlog::info("Command {}", type); - j["response"]["type"] = type; - j["response"]["UI_items"] = g_simulators->invoke_active_simulator()->get_UI_items()["UI_items"]; - break; - case Command::switch_simulator: - spdlog::info("Command {} to {}", type, std::string(command.at("name"))); - g_simulators->switch_simulator(command.at("name")); - j["response"]["type"] = type; - break; - case Command::get_active_simulator_name: - spdlog::info("Command {}", type); - j["response"]["type"] = type; - j["response"]["name"] = g_simulators->active_simulator_name(); - break; - case Command::get_simulators: - spdlog::info("Command {}", type); - j["response"]["type"] = type; - j["response"]["simulators"] = g_simulators->list_simulators(); - break; - default: - break; + case Command::get_UI_element: + spdlog::info("Command {}: {}", type, std::string(command.at("id"))); + j["response"]["type"] = type; + j["response"]["UI_item"] = + g_simulators->invoke_active_simulator()->get_UI_item(command.at("id"))->to_json(); + break; + case Command::get_UI_elements: + spdlog::info("Command {}", type); + j["response"]["type"] = type; + j["response"]["UI_items"] = g_simulators->invoke_active_simulator()->get_UI_items()["UI_items"]; + break; + case Command::switch_simulator: + spdlog::info("Command {} to {}", type, std::string(command.at("name"))); + g_simulators->switch_simulator(command.at("name")); + j["response"]["type"] = type; + break; + case Command::get_active_simulator_name: + spdlog::info("Command {}", type); + j["response"]["type"] = type; + j["response"]["name"] = g_simulators->active_simulator_name(); + break; + case Command::get_simulators: + spdlog::info("Command {}", type); + j["response"]["type"] = type; + j["response"]["simulators"] = g_simulators->list_simulators(); + break; + default: + break; } } - catch(const json::exception& e) + catch (const json::exception &e) { spdlog::error(e.what()); } @@ -80,39 +81,40 @@ void event_handler(json event) return; } - switch(e) + switch (e) { - case Event::clicked: - { - spdlog::debug("Event {}: {}", type, std::string(event.at("id"))); - g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->click(); - break; - } - case Event::value_changed: - { - spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"])); - g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_value((double)event.at("value")); - break; - } - case Event::text_changed: - { - spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("text"))); - g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_text(std::string(event.at("text"))); - break; - } - case Event::selected: - { - spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("selected"))); - g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->set_selected(std::string(event.at("selected"))); - break; - } - default: - { - break; - } + case Event::clicked: { + spdlog::debug("Event {}: {}", type, std::string(event.at("id"))); + g_simulators->invoke_active_simulator()->get_UI_item(std::string(event.at("id")))->click(); + break; + } + case Event::value_changed: { + spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), (double)(event["value"])); + g_simulators->invoke_active_simulator() + ->get_UI_item(std::string(event.at("id"))) + ->set_value((double)event.at("value")); + break; + } + case Event::text_changed: { + spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("text"))); + g_simulators->invoke_active_simulator() + ->get_UI_item(std::string(event.at("id"))) + ->set_text(std::string(event.at("text"))); + break; + } + case Event::selected: { + spdlog::debug("Event {}: {} {}", type, std::string(event.at("id")), std::string(event.at("selected"))); + g_simulators->invoke_active_simulator() + ->get_UI_item(std::string(event.at("id"))) + ->set_selected(std::string(event.at("selected"))); + break; + } + default: { + break; + } } } - catch(const json::exception& e) + catch (const json::exception &e) { spdlog::error(e.what()); } diff --git a/tools/simulator_base.cpp b/tools/simulator_base.cpp index fd15cfa..173a247 100644 --- a/tools/simulator_base.cpp +++ b/tools/simulator_base.cpp @@ -1,20 +1,23 @@ #include "simulator_base.hpp" Simulator_base::Simulator_base(std::string name, std::chrono::milliseconds interval) - : m_name(name), m_timer([this]{ timer(); }), m_interval(interval) + : m_name(name), m_timer([this] { timer(); }), m_interval(interval) { - } -std::string Simulator_base::name() const { +std::string Simulator_base::name() const +{ // Get the name of the simulator return m_name; } -bool Simulator_base::is_ui_id_unique(std::string id) const { +bool Simulator_base::is_ui_id_unique(std::string id) const +{ // Check if the UI name is unique - for (UI_item* item : m_UI_items) { - if (item->id() == id) { + for (UI_item *item : m_UI_items) + { + if (item->id() == id) + { return false; } } @@ -22,48 +25,57 @@ bool Simulator_base::is_ui_id_unique(std::string id) const { return true; } -void Simulator_base::add_UI_item(UI_item* item) { +void Simulator_base::add_UI_item(UI_item *item) +{ // Add a UI item to the simulator - if (is_ui_id_unique(item->id()) == false) { + if (is_ui_id_unique(item->id()) == false) + { throw std::runtime_error("UI item id " + item->id() + " is not unique"); } m_UI_items_map[item->id()] = item; m_UI_items.push_back(item); } -json Simulator_base::get_UI_items() const { +json Simulator_base::get_UI_items() const +{ // Get the UI items json items; items["name"] = m_name; - for (UI_item* item : m_UI_items) { + for (UI_item *item : m_UI_items) + { items["UI_items"].push_back(item->to_json()); } return items; } -UI_item* Simulator_base::get_UI_item(std::string id) const { +UI_item *Simulator_base::get_UI_item(std::string id) const +{ // Get a UI item by id return m_UI_items_map.at(id); } -void Simulator_base::run() { +void Simulator_base::run() +{ // Start the timer m_timer.setInterval(m_interval); run_at_startup(); } -void Simulator_base::stop() { +void Simulator_base::stop() +{ // Stop the simulator m_timer.stop(); } -void Simulator_base::shutdown() { +void Simulator_base::shutdown() +{ // Shutdown the simulator system("shutdown -h now"); } -void Simulator_base::reboot() { +void Simulator_base::reboot() +{ // Reboot the simulator system("reboot"); } \ No newline at end of file diff --git a/tools/simulator_base.hpp b/tools/simulator_base.hpp index 4de4a34..ddf8e5a 100644 --- a/tools/simulator_base.hpp +++ b/tools/simulator_base.hpp @@ -1,90 +1,92 @@ #pragma once #include "UI.hpp" -#include "timer.hpp" #include "spdlog/spdlog.h" +#include "timer.hpp" -class Simulator_base { +class Simulator_base +{ - public: - /** - * @brief Construct a new Simulator_base object - * - * @param name The name of the simulator - * @param interval The interval of the timer (default is 100ms) - */ - Simulator_base(std::string name, std::chrono::milliseconds interval = std::chrono::milliseconds(100)); + public: + /** + * @brief Construct a new Simulator_base object + * + * @param name The name of the simulator + * @param interval The interval of the timer (default is 100ms) + */ + Simulator_base(std::string name, std::chrono::milliseconds interval = std::chrono::milliseconds(100)); - /** - * @brief Returns the name of the simulator - * - * @return std::string The name of the simulator - */ - std::string name() const; + /** + * @brief Returns the name of the simulator + * + * @return std::string The name of the simulator + */ + std::string name() const; - /** - * @brief This function checks if the UI id is unique - * (hence not already pushed in with the add_UI_item function) - * - * @param id The id to check - * @return true The id is unique - * @return false The id is already in use - */ - bool is_ui_id_unique(std::string id) const; + /** + * @brief This function checks if the UI id is unique + * (hence not already pushed in with the add_UI_item function) + * + * @param id The id to check + * @return true The id is unique + * @return false The id is already in use + */ + bool is_ui_id_unique(std::string id) const; - /** - * @brief This function adds a UI item to the simulator - * only if the id is unique - * Use this function only in the constructor of the simulator - * - * @param item The UI item to add - */ - void add_UI_item(UI_item* item); - /** - * @brief Get the UI items object - * - * @return json The UI items in json format - */ - json get_UI_items() const; + /** + * @brief This function adds a UI item to the simulator + * only if the id is unique + * Use this function only in the constructor of the simulator + * + * @param item The UI item to add + */ + void add_UI_item(UI_item *item); + /** + * @brief Get the UI items object + * + * @return json The UI items in json format + */ + json get_UI_items() const; - /** - * @brief Get the UI item object by id - * - * @param id The id of the UI item - * @return UI_item* A pointer to the UI item object - */ - UI_item* get_UI_item(std::string id) const; + /** + * @brief Get the UI item object by id + * + * @param id The id of the UI item + * @return UI_item* A pointer to the UI item object + */ + UI_item *get_UI_item(std::string id) const; - /** - * @brief Run the simulator - */ - void run(); + /** + * @brief Run the simulator + */ + void run(); - /** - * @brief Stop the simulator - */ - void stop(); + /** + * @brief Stop the simulator + */ + void stop(); - /** - * @brief Timer function - * This function is called every time the timer ticks - * Override this function in the derived class - */ - virtual void timer() {}; + /** + * @brief Timer function + * This function is called every time the timer ticks + * Override this function in the derived class + */ + virtual void timer() {}; - /** - * @brief Run at startup - * This function is called at startup - * Override this function in the derived class - */ - virtual void run_at_startup() {}; + /** + * @brief Run at startup + * This function is called at startup + * Override this function in the derived class + */ + virtual void run_at_startup() {}; - void shutdown(); - void reboot(); - protected: - std::string m_name; - std::vector m_UI_items; - std::map m_UI_items_map; - Timer m_timer; - std::chrono::milliseconds m_interval; + void shutdown(); + void reboot(); + + protected: + std::string m_name; + std::vector m_UI_items; + std::map m_UI_items_map; + Timer m_timer; + std::chrono::milliseconds m_interval; }; \ No newline at end of file diff --git a/tools/simulators.cpp b/tools/simulators.cpp index 1f78a4b..1233650 100644 --- a/tools/simulators.cpp +++ b/tools/simulators.cpp @@ -1,7 +1,7 @@ #include "simulators.hpp" -#include -#include "test_sim.hpp" #include "can_debugger.hpp" +#include "test_sim.hpp" +#include Simulators::Simulators() { @@ -66,14 +66,14 @@ void Simulators::switch_simulator(std::string name) std::vector Simulators::list_simulators() const { std::vector names; - for (const auto& simulator : this->m_simulators) + for (const auto &simulator : this->m_simulators) { names.push_back(simulator.first); } return names; } -Simulator_base* Simulators::invoke_active_simulator() +Simulator_base *Simulators::invoke_active_simulator() { std::lock_guard lock(m_mutex); if (this->m_simulators.empty()) @@ -84,7 +84,7 @@ Simulator_base* Simulators::invoke_active_simulator() { return nullptr; } - + m_before = this->m_simulators.at(m_current_simulator)->get_UI_items(); return this->m_simulators.at(m_current_simulator).get(); @@ -105,7 +105,7 @@ json Simulators::changed_UI_items() json after = this->m_simulators.at(m_current_simulator)->get_UI_items(); json changed; changed["event"]["type"] = "UI_changed"; - + json diff = json::diff(m_before, after); if (diff.empty()) @@ -121,18 +121,18 @@ json Simulators::changed_UI_items() try { - for (auto& item : diff) + for (auto &item : diff) { std::string path = item["path"]; size_t index = std::stoul(path.substr(10, path.find("/", 10) - 10)); if (addedIndices.find(index) != addedIndices.end()) continue; - + changed["event"]["UI_items"].push_back(after["UI_items"][index]); addedIndices.insert(index); } } - catch(const std::exception& e) + catch (const std::exception &e) { spdlog::error(e.what()); } diff --git a/tools/simulators.hpp b/tools/simulators.hpp index cd483db..5e271c7 100644 --- a/tools/simulators.hpp +++ b/tools/simulators.hpp @@ -1,25 +1,27 @@ #pragma once -#include #include +#include #include #include "simulator_base.hpp" #define INSERT_SIMULATOR(type) m_simulators.insert(std::make_pair(type().name(), std::make_unique())); -class Simulators { - public: - Simulators(); - std::string active_simulator_name() const; - void run(); - void stop(); - void switch_simulator(std::string name); - std::vector list_simulators() const; - Simulator_base* invoke_active_simulator(); - json changed_UI_items(); - private: - std::map> m_simulators; - std::string m_current_simulator = ""; - std::mutex m_mutex; - json m_before; +class Simulators +{ + public: + Simulators(); + std::string active_simulator_name() const; + void run(); + void stop(); + void switch_simulator(std::string name); + std::vector list_simulators() const; + Simulator_base *invoke_active_simulator(); + json changed_UI_items(); + + private: + std::map> m_simulators; + std::string m_current_simulator = ""; + std::mutex m_mutex; + json m_before; }; \ No newline at end of file diff --git a/tools/websocket.cpp b/tools/websocket.cpp index 3be4708..3a75fd5 100644 --- a/tools/websocket.cpp +++ b/tools/websocket.cpp @@ -1,9 +1,8 @@ #include "websocket.hpp" #include "spdlog/spdlog.h" -Websocket::Websocket(uint16_t port, std::ostream* out, std::ostream* err, - std::function on_message, - std::function on_update) +Websocket::Websocket(uint16_t port, std::ostream *out, std::ostream *err, + std::function on_message, std::function on_update) : m_on_message(on_message), m_on_update(on_update), m_port(port) { // Set logging settings @@ -11,10 +10,8 @@ Websocket::Websocket(uint16_t port, std::ostream* out, std::ostream* err, m_server.get_elog().set_custom(err != &std::cerr); m_server.get_alog().set_ostream(out); m_server.get_elog().set_ostream(err); - m_server.set_access_channels(websocketpp::log::alevel::connect + - websocketpp::log::alevel::disconnect); - m_server.clear_access_channels(websocketpp::log::alevel::frame_payload + - websocketpp::log::alevel::frame_header); + m_server.set_access_channels(websocketpp::log::alevel::connect + websocketpp::log::alevel::disconnect); + m_server.clear_access_channels(websocketpp::log::alevel::frame_payload + websocketpp::log::alevel::frame_header); // Initialize Asio m_server.init_asio(); @@ -35,10 +32,14 @@ void Websocket::run() m_server.start_accept(); // Start the ASIO io_service run loop m_server.run(); - } catch (websocketpp::exception const& e) { + } + catch (websocketpp::exception const &e) + { spdlog::critical("{}", e.what()); exit(1); - } catch (...) { + } + catch (...) + { spdlog::critical("other exception"); exit(1); } @@ -94,31 +95,29 @@ void Websocket::process_messages() lock.unlock(); - switch (a.type) { - case SUBSCRIBE: + switch (a.type) + { + case SUBSCRIBE: { + std::lock_guard guard(m_connection_lock); + m_connections.insert(a.hdl); + } + break; + case UNSUBSCRIBE: { + std::lock_guard guard(m_connection_lock); + m_connections.erase(a.hdl); + } + break; + case MESSAGE: { + std::string response = m_on_message(a.msg->get_payload()); + if (response.empty() || response == "{}") { - std::lock_guard guard(m_connection_lock); - m_connections.insert(a.hdl); + continue; } + m_server.send(a.hdl, response, websocketpp::frame::opcode::TEXT); + } + break; + default: break; - case UNSUBSCRIBE: - { - std::lock_guard guard(m_connection_lock); - m_connections.erase(a.hdl); - } - break; - case MESSAGE: - { - std::string response = m_on_message(a.msg->get_payload()); - if (response.empty() || response == "{}") - { - continue; - } - m_server.send(a.hdl, response, websocketpp::frame::opcode::TEXT); - } - break; - default: - break; } } } diff --git a/tools/websocket.hpp b/tools/websocket.hpp index 2410274..2145ce2 100644 --- a/tools/websocket.hpp +++ b/tools/websocket.hpp @@ -16,17 +16,22 @@ using websocketpp::lib::placeholders::_2; * on_message queue send to all channels */ -enum action_type { +enum action_type +{ SUBSCRIBE, UNSUBSCRIBE, MESSAGE, BROADCAST }; -struct action { - action(action_type t, websocketpp::connection_hdl h) : type(t), hdl(h) {} - action(action_type t, websocketpp::connection_hdl h, Server::message_ptr m) - : type(t), hdl(h), msg(m) {} +struct action +{ + action(action_type t, websocketpp::connection_hdl h) : type(t), hdl(h) + { + } + action(action_type t, websocketpp::connection_hdl h, Server::message_ptr m) : type(t), hdl(h), msg(m) + { + } action_type type; websocketpp::connection_hdl hdl; @@ -35,22 +40,25 @@ struct action { class Websocket { -public: - Websocket(uint16_t port, std::ostream* out, std::ostream* err, std::function on_message, std::function on_update); + public: + Websocket(uint16_t port, std::ostream *out, std::ostream *err, std::function on_message, + std::function on_update); void run(); void stop(); void process_messages(); void process_broadcast(); -private: + + private: void on_open(websocketpp::connection_hdl hdl); void on_close(websocketpp::connection_hdl hdl); void on_message(websocketpp::connection_hdl hdl, Server::message_ptr msg); -private: + + private: std::function m_on_message; std::function m_on_update; - Server m_server; + Server m_server; uint16_t m_port; - typedef std::set> con_list; + typedef std::set> con_list; con_list m_connections; std::queue m_actions;