From 978c607d6698be2983c40032773e6a6791536c1b Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Sat, 13 May 2023 15:33:02 -0400 Subject: [PATCH] Improved gameloop for serial devices. More simwind hacks --- src/monocoque/devices/serialdevice.c | 15 ++++++++++++++- src/monocoque/devices/simdevice.c | 3 +++ src/monocoque/gameloop/gameloop.c | 17 +++++++++++++++-- 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/monocoque/devices/serialdevice.c b/src/monocoque/devices/serialdevice.c index e6521ca..9eb915b 100644 --- a/src/monocoque/devices/serialdevice.c +++ b/src/monocoque/devices/serialdevice.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "simdevice.h" #include "serialdevice.h" @@ -18,7 +19,19 @@ int serialdev_update(SimDevice* this, SimData* simdata) // the arduino will use mph as speed in my code // 255 is a good max top speed in mph double velocity = simdata->velocity; - simdata->velocity = simdata->velocity * 0.6213712; + if (simdata->velocity > 10) + { + simdata->velocity = ceil(simdata->velocity * 0.6213712); + slogi("trying to set velocity %i", simdata->velocity); + } + else + { + simdata->velocity = 0; + } + if (simdata->velocity >= 255) + { + simdata->velocity = 255; + } arduino_update(serialdevice, simdata); simdata->velocity = velocity; diff --git a/src/monocoque/devices/simdevice.c b/src/monocoque/devices/simdevice.c index a3ec17e..45724be 100644 --- a/src/monocoque/devices/simdevice.c +++ b/src/monocoque/devices/simdevice.c @@ -58,6 +58,7 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds) if (sim != NULL) { simdevices[j].initialized = true; + simdevices[j].type = SIMDEV_USB; simdevices[j] = sim->m; devices++; } @@ -74,6 +75,7 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds) { simdevices[j].initialized = true; + simdevices[j].type = SIMDEV_SOUND; simdevices[j] = sim->m; devices++; } @@ -90,6 +92,7 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds) { simdevices[j].initialized = true; + simdevices[j].type = SIMDEV_SERIAL; simdevices[j] = sim->m; devices++; diff --git a/src/monocoque/gameloop/gameloop.c b/src/monocoque/gameloop/gameloop.c index 7699b0c..6b69618 100644 --- a/src/monocoque/gameloop/gameloop.c +++ b/src/monocoque/gameloop/gameloop.c @@ -14,7 +14,8 @@ #include "../simulatorapi/simmapper.h" #include "../slog/slog.h" -#define DEFAULT_UPDATE_RATE 120.0 +#define DEFAULT_UPDATE_RATE 30.0 +#define SERIAL_UPDATE_RATE 60.0 int showstats(SimData* simdata) { @@ -191,6 +192,7 @@ int looper(SimDevice* devices, int numdevices, Simulator simulator) double update_rate = DEFAULT_UPDATE_RATE; int t=0; + int s=0; int go = true; while (go == true) { @@ -205,7 +207,7 @@ int looper(SimDevice* devices, int numdevices, Simulator simulator) { if (devices[x].type == SIMDEV_SERIAL) { - if(t>=update_rate) + if(s>=4) { devices[x].update(&devices[x], simdata); } @@ -220,6 +222,10 @@ int looper(SimDevice* devices, int numdevices, Simulator simulator) { t=0; } + if(s>=4) + { + s=0; + } if( poll(&mypoll, 1, 1000.0/update_rate) ) { scanf("%c", &ch); @@ -233,6 +239,13 @@ int looper(SimDevice* devices, int numdevices, Simulator simulator) fflush(stdout); tcsetattr(0, TCSANOW, &canonicalmode); + simdata->velocity = 0; + simdata->rpms = 100; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + free(simdata); free(simmap);