From d2ff88aabb22ab9c3595596146eef3389d419342 Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Mon, 15 May 2023 14:23:33 +0000 Subject: [PATCH] More refactoring to improve serial devices --- src/monocoque/devices/serial/arduino.c | 2 +- src/monocoque/devices/serialdevice.c | 19 ------------- src/monocoque/devices/simdevice.c | 7 +++-- src/monocoque/devices/sounddevice.c | 2 +- src/monocoque/gameloop/gameloop.c | 37 ++++++++++---------------- 5 files changed, 19 insertions(+), 48 deletions(-) diff --git a/src/monocoque/devices/serial/arduino.c b/src/monocoque/devices/serial/arduino.c index 751f364..10c7800 100644 --- a/src/monocoque/devices/serial/arduino.c +++ b/src/monocoque/devices/serial/arduino.c @@ -4,7 +4,7 @@ #include #include "arduino.h" -#include "../slog/slog.h" +#include "../../slog/slog.h" #define arduino_timeout 2000 diff --git a/src/monocoque/devices/serialdevice.c b/src/monocoque/devices/serialdevice.c index 9eb915b..ffcf146 100644 --- a/src/monocoque/devices/serialdevice.c +++ b/src/monocoque/devices/serialdevice.c @@ -15,26 +15,7 @@ int serialdev_update(SimDevice* this, SimData* simdata) { SerialDevice* serialdevice = (void *) this->derived; - // not sure if i want to change simapi to mph - // the arduino will use mph as speed in my code - // 255 is a good max top speed in mph - double velocity = simdata->velocity; - 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; return 0; } diff --git a/src/monocoque/devices/simdevice.c b/src/monocoque/devices/simdevice.c index 45724be..f737991 100644 --- a/src/monocoque/devices/simdevice.c +++ b/src/monocoque/devices/simdevice.c @@ -57,9 +57,9 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds) USBDevice* sim = new_usb_device(&ds[j]); if (sim != NULL) { + simdevices[j] = sim->m; simdevices[j].initialized = true; simdevices[j].type = SIMDEV_USB; - simdevices[j] = sim->m; devices++; } else @@ -74,9 +74,9 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds) if (sim != NULL) { + simdevices[j] = sim->m; simdevices[j].initialized = true; simdevices[j].type = SIMDEV_SOUND; - simdevices[j] = sim->m; devices++; } else @@ -90,10 +90,9 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds) SerialDevice* sim = new_serial_device(&ds[j]); if (sim != NULL) { - + simdevices[j] = sim->m; simdevices[j].initialized = true; simdevices[j].type = SIMDEV_SERIAL; - simdevices[j] = sim->m; devices++; } diff --git a/src/monocoque/devices/sounddevice.c b/src/monocoque/devices/sounddevice.c index 61538f1..54b2668 100644 --- a/src/monocoque/devices/sounddevice.c +++ b/src/monocoque/devices/sounddevice.c @@ -14,7 +14,7 @@ int gear_sound_set(SoundDevice* sounddevice, SimData* simdata) { - if (sounddevice->sounddata.last_gear != simdata->gear) + if (sounddevice->sounddata.last_gear != simdata->gear && simdata->gear != 0) { sounddevice->sounddata.gear_sound_data = 3.14; } diff --git a/src/monocoque/gameloop/gameloop.c b/src/monocoque/gameloop/gameloop.c index 6b69618..56257ed 100644 --- a/src/monocoque/gameloop/gameloop.c +++ b/src/monocoque/gameloop/gameloop.c @@ -14,8 +14,7 @@ #include "../simulatorapi/simmapper.h" #include "../slog/slog.h" -#define DEFAULT_UPDATE_RATE 30.0 -#define SERIAL_UPDATE_RATE 60.0 +#define DEFAULT_UPDATE_RATE 240.0 int showstats(SimData* simdata) { @@ -180,6 +179,15 @@ int looper(SimDevice* devices, int numdevices, Simulator simulator) return error; } + slogi("sending initial data to devices"); + simdata->velocity = 16; + simdata->rpms = 100; + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } + sleep(3); + struct termios newsettings, canonicalmode; tcgetattr(0, &canonicalmode); newsettings = canonicalmode; @@ -199,33 +207,16 @@ int looper(SimDevice* devices, int numdevices, Simulator simulator) simdatamap(simdata, simmap, simulator); showstats(simdata); t++; - if(simdata->rpms<250) + s++; + if(simdata->rpms<100) { - simdata->rpms=250; + simdata->rpms=100; } for (int x = 0; x < numdevices; x++) { - if (devices[x].type == SIMDEV_SERIAL) - { - if(s>=4) - { - devices[x].update(&devices[x], simdata); - } - } - else - { - devices[x].update(&devices[x], simdata); - } + devices[x].update(&devices[x], simdata); } - if(t>=update_rate) - { - t=0; - } - if(s>=4) - { - s=0; - } if( poll(&mypoll, 1, 1000.0/update_rate) ) { scanf("%c", &ch);