diff --git a/README.md b/README.md new file mode 100644 index 0000000..e86d409 --- /dev/null +++ b/README.md @@ -0,0 +1,84 @@ +# unisim_cpp +## Guidelines +There are a few rules to follow when creating a big project with multiple people. +### 1 Pragma once +Change this setting in Qt creator \ +Edit > Preferences > C++ > File Naming > Headers > Include guard template and check the box "Use pragma once instead" +### 2 File naming +Change this setting in Qt creator \ +Edit > Preferences > C++ > File Naming > Headers > Suffix +Change this to hpp +Change this setting in Qt creator \ +Edit > Preferences > C++ > File Naming > Sources > Suffix +Change this to cpp +### 3 How to write type names +Use snake case and make the first letter a capital letter to show that it is a type name +### 4 Linting rules +Please run clang-format using the given .clang-fomrat file and check that linting passes on bitbucket piplines +### 5 No broken code in main branch +Always use a pull request to merge code in the main branch, this makes it so that no broken code can get in the main branch. +## Creating a new simulator +[How to create a client ui](create_client.md)\ +[How to create a server sim](create_server.md) +## Setup +### Requirements +Fedora packages: +```bash +sudo dnf install gtest-devel cmake git ninja-build qt6-qttools-devel qt6-qtsvg-devel qt6-qtwebsockets-devel qt6-qtserialport-devel libsocketcan qt6-qtserialbus-devel libgpiod-devel +``` +### Clone repo +Easiest way to download a repo from our bitbucket is to use an ssh-key. Put or generate the key on the system and copy the private key to your bitbucket account. (Cog > Personal Bitbucket Settings > SSH keys) Then run this command on the system. +```bash +git clone --recurse-submodules git@bitbucket.org:belgiantrain/unisim-cpp.git +``` + +### Server build +```bash +cmake -S . -B ./build/ -G Ninja +cmake --build ./build/ --target unisim_server #your sims (sim_*) +``` + +### Client build +```bash +cmake -S . -B ./build/ -G Ninja +cmake --build ./build/ --target unisim_client #your ui (ui_*) +``` + +### Running the project +Copy the libsim_* and/or libui_* together with the server or client to /usr/local/bin +```bash +sudo cp build/bin/unisim_* build/bin/libsim_* build/bin/libui_* /usr/local/bin/ +``` +then run +```bash +unisim_client -s $Server_IP +``` +or +```bash +unisim_server +``` +for more info on each executables commandline params run with -h option + +### Enabling CAN bus on Raspberry Pi +Append this to /boot/config.txt +``` +dtparam=spi=on +dtoverlay=mcp2515-can0,spi0-0,oscillator=16000000,interrupt=22 +dtoverlay=spi-bcm2835-overlay +``` +Copy can_dev.service to /etc/systemd/system. Reboot to apply changes. +```bash +sudo cp can_dev.service /etc/systemd/system +sudo systemctl enable can_dev.service +sudo reboot +``` + +### Autostart server on boot +Copy unsim_cpp.service to /etc/systemd/system +If you use can you could copy can_dev.service as well +(Remove -c can0 if you don't use can) +```bash +sudo cp unisim_cpp.service /etc/systemd/system +sudo systemctl daemon-reload +sudo systemctl enable --now unisim_cpp.service +``` \ No newline at end of file diff --git a/create_client.md b/create_client.md new file mode 100644 index 0000000..4ef95a4 --- /dev/null +++ b/create_client.md @@ -0,0 +1,69 @@ +## Client UI +To create a new simulator ui follow these steps +1) Make sure the client folder is folded down +2) Right click on unisim_client and select Add new +3) Choose Qt in files and classes and then select Qt widgets designer form class + Choose +4) Select Mainwindow + Next +5) Change the classname to the name of your sim use underscores as spaces, don't forget classnames always start with a capital letter +6) Select the path by clicking browse, and make sure it is in the client/simulators folder + Next +7) Set add to project to None + Finished +8) Refresh CMake by right clicking on the project name + Run Cmake, it should show your sim as ui_ + filename +9) Change the hpp file as following. This is an example out of test sim +OPTIONAL Add id_to_label is optional if you want to use value labels +```cpp +#pragma once +#include "simulator_base.hpp" + +namespace Ui +{ +class Test_Sim; +} + +class Test_Sim : public Simulator_base +{ + Q_OBJECT + + public: + explicit Test_Sim(Web_socket_wrapper *web_socket = nullptr, QWidget *parent = nullptr); + ~Test_Sim(); + // Optional + QLabel *id_to_label(QWidget* widget) override; + + private: + Ui::Test_Sim *ui; +}; +``` +10) Change the cpp file as following. This is an example out of test sim. PUSH_UI_ITEM is the method to add new QWidgets to the similator UI +OPTIONAL Add id_to_label is optional if you want to use value labels switch case is also an option instead of if +```cpp +#include "test.hpp" +#include "ui_test.h" + +EXPORT_UI(Test_Sim); + +Test_Sim::Test_Sim(Web_socket_wrapper *web_socket, QWidget *parent) + : Simulator_base("Test Sim", web_socket, parent), ui(new Ui::Test_Sim) +{ + ui->setupUi(this); + + PUSH_UI_ITEM(item); +} + +Test_Sim::~Test_Sim() +{ + delete ui; +} + +QLabel *Test_Sim::id_to_label(QWidget* widget) +{ + if (widget == ui->slider) + return ui->slider_label; + if (widget == ui->thermo) + return ui->thermo_label; + if (widget == ui->dial) + return ui->dial_label; + + return nullptr; +} +``` +For extra examples look at the existing simulators \ No newline at end of file diff --git a/create_server.md b/create_server.md new file mode 100644 index 0000000..392380b --- /dev/null +++ b/create_server.md @@ -0,0 +1,70 @@ +## Server simulator +To create a simulator server follow these steps +1) Make sure the server folder is folded down +2) Right click on unisim_server and select Add new +3) Choose C/C++ in files and classes and then select C++ class + Choose +4) Change the classname to the name of your sim use underscores as spaces, don't forget classnames always start with a capital letter +5) Set as base Simulator_base and check "Add Q_Object" +6) Select the path by clicking browse and make sure it is in the server/simulators folder + Next +7) Set add to project to None + Finish +8) Refresh CMake by right clicking on the project name + Run Cmake, it should show your sim as sim_ + filename +9) Change the hpp file as following. This is an example out of test sim +```cpp +#pragma once +#include "simulator_base.hpp" + +class Test_Sim : public Simulator_base +{ + Q_OBJECT + public: + explicit Test_Sim(Communication *comms = nullptr, QObject *parent = nullptr); + // Optional + void timer() override; + void run_at_startup() override; + void run_at_stop() override; + + private: + // Put you're UI items and external connection classes here + //UI_button m_button; + //Visa m_device; + //Output_card m_output; + //GPIO m_gpio_pin0; +}; +``` +10) Change the cpp file as following. This is an example out of test sim. PUSH_UI_ITEM is the method to add new UI_items to the similator +```cpp +#include "test.hpp" + +EXPORT_SIM(Test_Sim); + +Test_Sim::Test_Sim(Communication *com, QObject *parent) + : Simulator_base("Test Sim", com, std::chrono::milliseconds(1000), parent), + // Put you're variable constructors here + m_button("Off", Color::White, Color::Red) +{ + PUSH_UI_ITEM(m_ui_item); + + // Put you're connect statements here + connect(&m_button, &UI_button::clicked, this, [=, this] { + m_button.set_text("On"); + }); +} + +void Test_Sim::timer() +{ + SPDLOG_INFO("Timer triggered"); +} + +void Test_Sim::run_at_startup() +{ + // Run at startup + SPDLOG_INFO("Test_Sim started"); +} +void Test_Sim::run_at_stop() +{ + // Run at startup + SPDLOG_INFO("Test_Sim stoped"); +} +``` + +For extra examples look at the existing simulators \ No newline at end of file