From 3692d31343a26bb4285c2f621501454431af87bf Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Fri, 14 Jul 2023 22:11:01 +0000 Subject: [PATCH] Use new simapi to get sim automatically. Use light version of simapi --- src/monocoque/devices/simdevice.c | 5 +- src/monocoque/gameloop/gameloop.c | 76 ++++++++++++++++++++--- src/monocoque/gameloop/gameloop.h | 2 +- src/monocoque/helper/confighelper.c | 2 +- src/monocoque/helper/confighelper.h | 8 --- src/monocoque/helper/parameters.h | 6 ++ src/monocoque/monocoque.c | 19 +++--- src/monocoque/simulatorapi/CMakeLists.txt | 1 - 8 files changed, 87 insertions(+), 32 deletions(-) diff --git a/src/monocoque/devices/simdevice.c b/src/monocoque/devices/simdevice.c index 8c1792b..9ab91dc 100644 --- a/src/monocoque/devices/simdevice.c +++ b/src/monocoque/devices/simdevice.c @@ -30,7 +30,8 @@ int devfree(SimDevice* simdevices, int numdevices) slogi("freeing %i simdevices...", numdevices); int devices = 0; - for (int j = 0; j < numdevices; j++) + int freed = 0; + for (int j = 0; j < 1; j++) { SimDevice simdev = simdevices[j]; if (simdev.initialized == true) @@ -39,7 +40,7 @@ int devfree(SimDevice* simdevices, int numdevices) } } - free(simdevices); + //free(simdevices); return 0; } diff --git a/src/monocoque/gameloop/gameloop.c b/src/monocoque/gameloop/gameloop.c index 676bd20..a238e6a 100644 --- a/src/monocoque/gameloop/gameloop.c +++ b/src/monocoque/gameloop/gameloop.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -15,6 +16,7 @@ #include "../slog/slog.h" #define DEFAULT_UPDATE_RATE 240.0 +#define SIM_CHECK_RATE 1.0 int showstats(SimData* simdata) { @@ -165,19 +167,12 @@ int showstats(SimData* simdata) fflush(stdout); } -int looper(SimDevice* devices, int numdevices, Simulator simulator) + +int clilooper(SimDevice* devices, int numdevices, Parameters* p, SimData* simdata, SimMap* simmap) { slogi("preparing game loop with %i devices...", numdevices); - SimData* simdata = malloc(sizeof(SimData)); - SimMap* simmap = malloc(sizeof(SimMap)); - int error = siminit(simdata, simmap, simulator); - - if (error != MONOCOQUE_ERROR_NONE) - { - return error; - } slogi("sending initial data to devices"); simdata->velocity = 16; @@ -204,7 +199,7 @@ int looper(SimDevice* devices, int numdevices, Simulator simulator) int go = true; while (go == true) { - simdatamap(simdata, simmap, simulator); + simdatamap(simdata, simmap, p->sim); showstats(simdata); t++; s++; @@ -237,6 +232,67 @@ int looper(SimDevice* devices, int numdevices, Simulator simulator) devices[x].update(&devices[x], simdata); } + + return 0; +} + +int looper(SimDevice* devices, int numdevices, Parameters* p) +{ + + SimData* simdata = malloc(sizeof(SimData)); + SimMap* simmap = malloc(sizeof(SimMap)); + + struct termios newsettings, canonicalmode; + tcgetattr(0, &canonicalmode); + newsettings = canonicalmode; + newsettings.c_lflag &= (~ICANON & ~ECHO); + newsettings.c_cc[VMIN] = 1; + newsettings.c_cc[VTIME] = 0; + tcsetattr(0, TCSANOW, &newsettings); + char ch; + struct pollfd mypoll = { STDIN_FILENO, POLLIN|POLLPRI }; + + fprintf(stdout, "Searching for sim data... Press q to quit...\n"); + + p->simon = false; + double update_rate = SIM_CHECK_RATE; + int go = true; + while (go == true) + { + + + + + if (p->simon == false) + { + getSim(simdata, simmap, &p->simon, &p->sim); + } + + if (p->simon == true) + { + clilooper(devices, numdevices, p, simdata, simmap); + } + if (p->simon == true) + { + p->simon = false; + fprintf(stdout, "Searching for sim data... Press q again to quit...\n"); + sleep(2); + } + + if( poll(&mypoll, 1, 1000.0/update_rate) ) + { + scanf("%c", &ch); + if(ch == 'q') + { + go = false; + } + } + } + + fprintf(stdout, "\n"); + fflush(stdout); + tcsetattr(0, TCSANOW, &canonicalmode); + free(simdata); free(simmap); diff --git a/src/monocoque/gameloop/gameloop.h b/src/monocoque/gameloop/gameloop.h index 07496ca..714ce22 100644 --- a/src/monocoque/gameloop/gameloop.h +++ b/src/monocoque/gameloop/gameloop.h @@ -2,4 +2,4 @@ #include "../helper/parameters.h" int tester(SimDevice* devices, int numdevices); -int looper (SimDevice* devices, int numdevices, Simulator simulator); +int looper(SimDevice* devices, int numdevices, Parameters* p); diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index a039907..49dbd37 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -40,7 +40,7 @@ int strtogame(const char* game, MonocoqueSettings* ms) if (strcicmp(game, "test") == 0) { slogd("Setting simulator to Test Data"); - ms->sim_name = SIMULATOR_MONOCOQUE_TEST; + ms->sim_name = SIMULATOR_SIMAPI_TEST; } else { diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index 69952d5..ef7a585 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -28,14 +28,6 @@ typedef enum } DeviceSubType; -typedef enum -{ - SIMULATOR_MONOCOQUE_TEST = 0, - SIMULATOR_ASSETTO_CORSA = 1, - SIMULATOR_RFACTOR = 2, - SIMULATOR_RFACTOR2 = 3 -} -Simulator; typedef enum { diff --git a/src/monocoque/helper/parameters.h b/src/monocoque/helper/parameters.h index f50dcb3..2756425 100644 --- a/src/monocoque/helper/parameters.h +++ b/src/monocoque/helper/parameters.h @@ -1,6 +1,9 @@ #ifndef _PARAMETERS_H #define _PARAMETERS_H +#include +#include "../simulatorapi/simapi/simapi/simapi.h" + typedef struct { int program_action; @@ -9,6 +12,9 @@ typedef struct int max_revs; int granularity; int verbosity_count; + + Simulator sim; + bool simon; } Parameters; diff --git a/src/monocoque/monocoque.c b/src/monocoque/monocoque.c index afa8f47..7967067 100644 --- a/src/monocoque/monocoque.c +++ b/src/monocoque/monocoque.c @@ -153,7 +153,7 @@ int main(int argc, char** argv) { error = MONOCOQUE_ERROR_NONE; DeviceSettings settings; - ds[i] = settings; + config_setting_t* config_device = config_setting_get_elem(config_devices, i); const char* device_type; const char* device_subtype; @@ -164,12 +164,13 @@ int main(int argc, char** argv) if (error == MONOCOQUE_ERROR_NONE) { - error = devsetup(device_type, device_subtype, device_config_file, ms, &ds[i], config_device); + error = devsetup(device_type, device_subtype, device_config_file, ms, &settings, config_device); } if (error == MONOCOQUE_ERROR_NONE) { numdevices++; } + ds[i] = settings; i++; @@ -183,15 +184,15 @@ int main(int argc, char** argv) if (p->program_action == A_PLAY) { - slogi("running monocoque in gameloop mode.."); - error = strtogame(p->sim_string, ms); - if (error != MONOCOQUE_ERROR_NONE) - { - goto cleanup_final; - } + //slogi("running monocoque in gameloop mode.."); + //error = strtogame(p->sim_string, ms); + //if (error != MONOCOQUE_ERROR_NONE) + //{ + // goto cleanup_final; + //} - error = looper(devices, initdevices, ms->sim_name); + error = looper(devices, initdevices, p); if (error == MONOCOQUE_ERROR_NONE) { slogi("Game loop exited succesfully with error code: %i", error); diff --git a/src/monocoque/simulatorapi/CMakeLists.txt b/src/monocoque/simulatorapi/CMakeLists.txt index 8ca6420..07abbb2 100644 --- a/src/monocoque/simulatorapi/CMakeLists.txt +++ b/src/monocoque/simulatorapi/CMakeLists.txt @@ -13,4 +13,3 @@ set(simulatorapi_source_files ) add_library(simulatorapi STATIC ${simulatorapi_source_files}) -target_compile_definitions(simulatorapi PRIVATE SIMMAP_ALL)