From 94d60ad56f9c8cc80c93bca5a56725ade648867b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Thu, 16 Jul 2020 22:18:18 +0200 Subject: [PATCH] Add gui for serial configuration --- include/opengl-playground/imu-serial.hpp | 5 ++++ src/imu-serial.cpp | 28 +++++++++++++++++++ src/main.cpp | 34 +++++++++++++++++++++++- 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/include/opengl-playground/imu-serial.hpp b/include/opengl-playground/imu-serial.hpp index f88a13a..1d9890e 100644 --- a/include/opengl-playground/imu-serial.hpp +++ b/include/opengl-playground/imu-serial.hpp @@ -15,6 +15,11 @@ class ImuSerial void start(); void stop(); + void set_device(const std::string &device); + void set_baudrate(const unsigned int baud); + unsigned int get_baudrate(); + const std::string &get_device(); + bool runs(); glm::quat get_quaternion(); glm::vec3 get_accel(); glm::vec3 get_gyro(); diff --git a/src/imu-serial.cpp b/src/imu-serial.cpp index 4ec6e06..a0b0d4d 100644 --- a/src/imu-serial.cpp +++ b/src/imu-serial.cpp @@ -244,6 +244,34 @@ void ImuSerial::stop() } } +void ImuSerial::set_device(const std::string &device) +{ + this->serial_device = device; +} + +void ImuSerial::set_baudrate(const unsigned int baud) +{ + this->baud = baud; +} + +unsigned int ImuSerial::get_baudrate() +{ + return this->baud; +} + +const std::string &ImuSerial::get_device() +{ + return this->serial_device; +} + +bool ImuSerial::runs() +{ + if (this->worker.joinable() && this->stop_flag == 0) + return true; + else + return false; +} + glm::quat ImuSerial::get_quaternion() { std::lock_guard(this->quat_mutex); diff --git a/src/main.cpp b/src/main.cpp index 293ddb1..5e0c337 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -16,6 +16,38 @@ #define WINDOW_WIDTH 1500 #define WINDOW_HEIGHT 1000 +static void handle_serial_port_gui(ImuSerial &imu_comm) +{ + static char foo[512] = {0}; + static int baud_rate; + static bool first = true; + bool runs = imu_comm.runs(); + + + if (first) { + imu_comm.get_device().copy(foo, sizeof(foo), 0); + baud_rate = imu_comm.get_baudrate(); + first = false; + } + + ImGui::Begin("Serial Config"); + ImGui::InputText("Serial Device", foo, sizeof(foo)); + ImGui::InputInt("Baudrate", &baud_rate); + bool button = ImGui::Button((runs ? "Stop" : "Start")); + ImGui::End(); + + if (button) { + if (runs) { + imu_comm.stop(); + } else { + imu_comm.set_device(std::string(foo)); + imu_comm.set_baudrate((unsigned int)baud_rate); + imu_comm.start(); + } + } + +} + int main(int argc, char **argv) { (void)argc; @@ -73,7 +105,6 @@ int main(int argc, char **argv) glEnable(GL_DEPTH_TEST); auto imu_serial = ImuSerial("/dev/ttyUSB0", 115200); - imu_serial.start(); while (run) { @@ -108,6 +139,7 @@ int main(int argc, char **argv) ImGui_ImplSDL2_NewFrame(window->getWindow()); ImGui::NewFrame(); + handle_serial_port_gui(imu_serial); ImGui::Begin("Camera");