More refactoring to improve serial devices

This commit is contained in:
Paul Dino Jones 2023-05-15 14:23:33 +00:00
parent 978c607d66
commit d2ff88aabb
5 changed files with 19 additions and 48 deletions

View File

@ -4,7 +4,7 @@
#include <string.h> #include <string.h>
#include "arduino.h" #include "arduino.h"
#include "../slog/slog.h" #include "../../slog/slog.h"
#define arduino_timeout 2000 #define arduino_timeout 2000

View File

@ -15,26 +15,7 @@ int serialdev_update(SimDevice* this, SimData* simdata)
{ {
SerialDevice* serialdevice = (void *) this->derived; 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); arduino_update(serialdevice, simdata);
simdata->velocity = velocity;
return 0; return 0;
} }

View File

@ -57,9 +57,9 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds)
USBDevice* sim = new_usb_device(&ds[j]); USBDevice* sim = new_usb_device(&ds[j]);
if (sim != NULL) if (sim != NULL)
{ {
simdevices[j] = sim->m;
simdevices[j].initialized = true; simdevices[j].initialized = true;
simdevices[j].type = SIMDEV_USB; simdevices[j].type = SIMDEV_USB;
simdevices[j] = sim->m;
devices++; devices++;
} }
else else
@ -74,9 +74,9 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds)
if (sim != NULL) if (sim != NULL)
{ {
simdevices[j] = sim->m;
simdevices[j].initialized = true; simdevices[j].initialized = true;
simdevices[j].type = SIMDEV_SOUND; simdevices[j].type = SIMDEV_SOUND;
simdevices[j] = sim->m;
devices++; devices++;
} }
else else
@ -90,10 +90,9 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds)
SerialDevice* sim = new_serial_device(&ds[j]); SerialDevice* sim = new_serial_device(&ds[j]);
if (sim != NULL) if (sim != NULL)
{ {
simdevices[j] = sim->m;
simdevices[j].initialized = true; simdevices[j].initialized = true;
simdevices[j].type = SIMDEV_SERIAL; simdevices[j].type = SIMDEV_SERIAL;
simdevices[j] = sim->m;
devices++; devices++;
} }

View File

@ -14,7 +14,7 @@
int gear_sound_set(SoundDevice* sounddevice, SimData* simdata) 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; sounddevice->sounddata.gear_sound_data = 3.14;
} }

View File

@ -14,8 +14,7 @@
#include "../simulatorapi/simmapper.h" #include "../simulatorapi/simmapper.h"
#include "../slog/slog.h" #include "../slog/slog.h"
#define DEFAULT_UPDATE_RATE 30.0 #define DEFAULT_UPDATE_RATE 240.0
#define SERIAL_UPDATE_RATE 60.0
int showstats(SimData* simdata) int showstats(SimData* simdata)
{ {
@ -180,6 +179,15 @@ int looper(SimDevice* devices, int numdevices, Simulator simulator)
return error; 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; struct termios newsettings, canonicalmode;
tcgetattr(0, &canonicalmode); tcgetattr(0, &canonicalmode);
newsettings = canonicalmode; newsettings = canonicalmode;
@ -199,33 +207,16 @@ int looper(SimDevice* devices, int numdevices, Simulator simulator)
simdatamap(simdata, simmap, simulator); simdatamap(simdata, simmap, simulator);
showstats(simdata); showstats(simdata);
t++; t++;
if(simdata->rpms<250) s++;
if(simdata->rpms<100)
{ {
simdata->rpms=250; simdata->rpms=100;
} }
for (int x = 0; x < numdevices; x++) for (int x = 0; x < numdevices; x++)
{
if (devices[x].type == SIMDEV_SERIAL)
{
if(s>=4)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
}
}
else
{
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) ) if( poll(&mypoll, 1, 1000.0/update_rate) )
{ {
scanf("%c", &ch); scanf("%c", &ch);