From 14b56399be8337ff49c35639f0ff06083ac85dd1 Mon Sep 17 00:00:00 2001 From: ackimixs Date: Sun, 19 May 2024 11:05:08 +0200 Subject: [PATCH] lib --- CMakeLists.txt | 10 +++++---- GameControllerHandler.h | 10 ++++----- utils.h | 45 ----------------------------------------- 3 files changed, 11 insertions(+), 54 deletions(-) delete mode 100644 utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 72b7816..9a8fdbb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,13 +6,15 @@ set(CMAKE_CXX_STANDARD 17) find_package(SDL2 REQUIRED) include_directories(${SDL2_INCLUDE_DIRS}) -find_package(PkgConfig REQUIRED) -pkg_check_modules(TCPSocket REQUIRED TCPSocket) +find_package(Modelec COMPONENTS + TCPClient + Utils + REQUIRED +) add_executable(GameController main.cpp - utils.h GameControllerHandler.h) target_link_libraries(GameController ${SDL2_LIBRARIES}) -target_link_libraries(GameController TCPSocket) +target_link_libraries(GameController Modelec::TCPClient Modelec::Utils) diff --git a/GameControllerHandler.h b/GameControllerHandler.h index df06616..c38936f 100644 --- a/GameControllerHandler.h +++ b/GameControllerHandler.h @@ -3,9 +3,9 @@ #include #include -#include +#include -#include "utils.h" +#include class GameControllerHandler : public TCPClient { public: @@ -36,7 +36,7 @@ public: } void handleMessage(const std::string &message) override { - std::vector tokens = Utils::split(message, ";"); + std::vector tokens = Modelec::split(message, ";"); if (tokens[1] == "all" || tokens[1] == "lidar") { if (tokens[2] == "set range") { @@ -46,14 +46,14 @@ public: if (tokens[1] == "all" || tokens[1] == "gc") { if (tokens[2] == "stop proximity") { - std::vector args = Utils::split(tokens[3], ","); + std::vector args = Modelec::split(tokens[3], ","); double distance = stod(args[0]); Uint16 strength; if (distance <= 100) { strength = 0xFFFF; } else if (distance <= 500) { - double factor = 1 - Utils::mapValue(distance, 100.0, this->lidarDectectionDistance, 0.0, 1.0); + double factor = 1 - Modelec::mapValue(distance, 100.0, this->lidarDectectionDistance, 0.0, 1.0); strength = static_cast(factor * 0xFFFF); } else { strength = 0; diff --git a/utils.h b/utils.h deleted file mode 100644 index e6ae5d8..0000000 --- a/utils.h +++ /dev/null @@ -1,45 +0,0 @@ -#pragma once -#include -#include - -#define PI 3.14159265358979323846 - -namespace Utils { - inline bool startWith(const std::string& str, const std::string& start) - { - return str.rfind(start, 0) == 0; - } - - inline bool endsWith(const std::string& str, const std::string& end) - { - if (str.length() >= end.length()) - { - return (0 == str.compare(str.length() - end.length(), end.length(), end)); - } - return false; - } - - inline bool contains(const std::string& str, const std::string& sub) - { - return str.find(sub) != std::string::npos; - } - - inline std::vector split(const std::string& str, const std::string& delimiter) { - std::vector tokens; - size_t prev = 0, pos = 0; - do - { - pos = str.find(delimiter, prev); - if (pos == std::string::npos) pos = str.length(); - std::string token = str.substr(prev, pos - prev); - if (!token.empty()) tokens.push_back(token); - prev = pos + delimiter.length(); - } while (pos < str.length() && prev < str.length()); - return tokens; - } - - template - T mapValue(T v, T v_min, T v_max, T v_min_prime, T v_max_prime) { - return v_min_prime + (((v - v_min) * (v_max_prime - v_min_prime)) / (v_max - v_min)); - } -} \ No newline at end of file