mirror of
https://github.com/modelec/GameController.git
synced 2026-03-18 21:40:43 +01:00
lib
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
#include <SDL.h>
|
||||
#include <iostream>
|
||||
|
||||
#include <TCPSocket/TCPClient.hpp>
|
||||
#include <Modelec/TCPClient.h>
|
||||
|
||||
#include "utils.h"
|
||||
#include <Modelec/Utils.h>
|
||||
|
||||
class GameControllerHandler : public TCPClient {
|
||||
public:
|
||||
@@ -36,7 +36,7 @@ public:
|
||||
}
|
||||
|
||||
void handleMessage(const std::string &message) override {
|
||||
std::vector<std::string> tokens = Utils::split(message, ";");
|
||||
std::vector<std::string> 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<std::string> args = Utils::split(tokens[3], ",");
|
||||
std::vector<std::string> 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<Uint16>(factor * 0xFFFF);
|
||||
} else {
|
||||
strength = 0;
|
||||
|
||||
45
utils.h
45
utils.h
@@ -1,45 +0,0 @@
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#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<std::string> split(const std::string& str, const std::string& delimiter) {
|
||||
std::vector<std::string> 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<typename T>
|
||||
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));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user