Refactor into C classes. Add support for SimWind.

This commit is contained in:
Paul Dino Jones 2023-05-09 14:59:41 -04:00
parent af7077b07a
commit d8df1890b3
10 changed files with 134 additions and 26 deletions

View File

@ -1,6 +1,7 @@
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include "arduino.h" #include "arduino.h"
#include "../slog/slog.h" #include "../slog/slog.h"
@ -18,11 +19,12 @@ int arduino_update(SerialDevice* serialdevice, SimData* simdata)
return result; return result;
} }
int arduino_init(SerialDevice* serialdevice) int arduino_init(SerialDevice* serialdevice, const char* portdev)
{ {
slogi("initializing arduino serial device..."); slogi("initializing arduino serial device...");
int error = 0; int error = 0;
char* port_name = "/dev/ttyACM0"; char* port_name = strdup(portdev);
slogd("Looking for port %s.\n", port_name); slogd("Looking for port %s.\n", port_name);
error = check(sp_get_port_by_name(port_name, &serialdevice->port)); error = check(sp_get_port_by_name(port_name, &serialdevice->port));
if (error != 0) if (error != 0)
@ -40,6 +42,7 @@ int arduino_init(SerialDevice* serialdevice)
check(sp_set_stopbits(serialdevice->port, 1)); check(sp_set_stopbits(serialdevice->port, 1));
check(sp_set_flowcontrol(serialdevice->port, SP_FLOWCONTROL_NONE)); check(sp_set_flowcontrol(serialdevice->port, SP_FLOWCONTROL_NONE));
free(port_name);
slogd("Successfully setup arduino serial device..."); slogd("Successfully setup arduino serial device...");
return 0; return 0;
} }

View File

@ -5,7 +5,7 @@
#include "../serialdevice.h" #include "../serialdevice.h"
int arduino_update(SerialDevice* serialdevice, SimData* simdata); int arduino_update(SerialDevice* serialdevice, SimData* simdata);
int arduino_init(SerialDevice* serialdevice); int arduino_init(SerialDevice* serialdevice, const char* portdev);
int arduino_free(SerialDevice* serialdevice); int arduino_free(SerialDevice* serialdevice);
int check(enum sp_return result); int check(enum sp_return result);

View File

@ -28,7 +28,7 @@ int serialdev_free(SimDevice* this)
return 0; return 0;
} }
int serialdev_init(SerialDevice* serialdevice) int serialdev_init(SerialDevice* serialdevice, const char* portdev)
{ {
slogi("initializing serial device..."); slogi("initializing serial device...");
int error = 0; int error = 0;
@ -36,7 +36,7 @@ int serialdev_init(SerialDevice* serialdevice)
serialdevice->type = SERIALDEV_UNKNOWN; serialdevice->type = SERIALDEV_UNKNOWN;
serialdevice->type = SERIALDEV_ARDUINO; serialdevice->type = SERIALDEV_ARDUINO;
error = arduino_init(serialdevice); error = arduino_init(serialdevice, portdev);
return error; return error;
} }
@ -52,7 +52,7 @@ SerialDevice* new_serial_device(DeviceSettings* ds) {
this->m.derived = this; this->m.derived = this;
this->m.vtable = &serial_simdevice_vtable; this->m.vtable = &serial_simdevice_vtable;
int error = serialdev_init(this); int error = serialdev_init(this, ds->serialdevsettings.portdev);
if (error != 0) if (error != 0)
{ {

View File

@ -41,10 +41,19 @@ int patestCallbackEngineRPM(const void* inputBuffer,
} }
if (data->gear_sound_data > 0)
{
*out++ = 0;
*out++ = 0;
}
else
{
*out++ = v; *out++ = v;
*out++ = v; *out++ = v;
} }
}
data->gear_sound_data = 0;
data->n=n; data->n=n;
return 0; return 0;
} }
@ -67,15 +76,26 @@ int patestCallbackGearShift(const void* inputBuffer,
for( i=0; i<framesPerBuffer; i++,n++ ) for( i=0; i<framesPerBuffer; i++,n++ )
{ {
float v = 0;
v = data->amp * sin (2 * M_PI * ((float) n) / (float) SAMPLE_RATE);
if (n>=data->table_size)
{
n=0;
}
if(data->gear_sound_data > 0) if(data->gear_sound_data > 0)
{ {
*out++ = v;
*out++ = v;
}
else
{
*out++ = 0;
*out++ = 0;
}
}
data->gear_sound_data = 0; data->gear_sound_data = 0;
*out++ = M_PI;
*out++ = M_PI;
*out++ = M_PI;
*out++ = M_PI;
}
}
data->n=n; data->n=n;
return 0; return 0;

View File

@ -1,5 +1,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <time.h>
#include <stdio.h>
#include <unistd.h>
#include "simdevice.h" #include "simdevice.h"
#include "sounddevice.h" #include "sounddevice.h"
@ -8,11 +12,22 @@
#include "../helper/parameters.h" #include "../helper/parameters.h"
#include "../slog/slog.h" #include "../slog/slog.h"
int gear_sound_set(SoundDevice* sounddevice, SimData* simdata)
{
if (sounddevice->sounddata.last_gear != simdata->gear)
{
sounddevice->sounddata.gear_sound_data = 3.14;
}
sounddevice->sounddata.last_gear = simdata->gear;
}
// we could make a vtable for these different effects too // we could make a vtable for these different effects too
int sounddev_engine_update(SimDevice* this, SimData* simdata) int sounddev_engine_update(SimDevice* this, SimData* simdata)
{ {
SoundDevice* sounddevice = (void *) this->derived; SoundDevice* sounddevice = (void *) this->derived;
gear_sound_set(sounddevice, simdata);
sounddevice->sounddata.table_size = 44100/(simdata->rpms/60); sounddevice->sounddata.table_size = 44100/(simdata->rpms/60);
} }
@ -20,12 +35,7 @@ int sounddev_gearshift_update(SimDevice* this, SimData* simdata)
{ {
SoundDevice* sounddevice = (void *) this->derived; SoundDevice* sounddevice = (void *) this->derived;
sounddevice->sounddata.gear_sound_data = 0; gear_sound_set(sounddevice, simdata);
if (sounddevice->sounddata.last_gear != simdata->gear)
{
sounddevice->sounddata.gear_sound_data = 3.14;
}
sounddevice->sounddata.last_gear = simdata->gear;
} }
int sounddev_free(SimDevice* this) int sounddev_free(SimDevice* this)
@ -50,6 +60,16 @@ int sounddev_init(SoundDevice* sounddevice)
sounddevice->sounddata.table_size = 44100/(100/60); sounddevice->sounddata.table_size = 44100/(100/60);
sounddevice->sounddata.last_gear = 0; sounddevice->sounddata.last_gear = 0;
switch (sounddevice->effecttype) {
case (SOUNDEFFECT_GEARSHIFT):
sounddevice->sounddata.pitch = 500;
sounddevice->sounddata.amp = 128;
sounddevice->sounddata.left_phase = sounddevice->sounddata.right_phase = 0;
sounddevice->sounddata.table_size = 44100/(1);
break;
}
usb_generic_shaker_init(sounddevice); usb_generic_shaker_init(sounddevice);
} }

View File

@ -45,6 +45,8 @@ int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds)
{ {
slogi("initializing usb device..."); slogi("initializing usb device...");
int error = 0; int error = 0;
usbdevice->type = USBDEV_TACHOMETER;
switch ( usbdevice->type ) switch ( usbdevice->type )
{ {
case USBDEV_UNKNOWN : case USBDEV_UNKNOWN :

View File

@ -274,6 +274,10 @@ int tester(SimDevice* devices, int numdevices)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
} }
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3); sleep(3);
fprintf(stdout, "Setting speed to 100\n"); fprintf(stdout, "Setting speed to 100\n");
@ -290,6 +294,10 @@ int tester(SimDevice* devices, int numdevices)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
} }
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3); sleep(3);
fprintf(stdout, "Setting speed to 200\n"); fprintf(stdout, "Setting speed to 200\n");
@ -306,6 +314,10 @@ int tester(SimDevice* devices, int numdevices)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
} }
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3); sleep(3);
fprintf(stdout, "Setting rpms to 2000\n"); fprintf(stdout, "Setting rpms to 2000\n");
@ -330,6 +342,10 @@ int tester(SimDevice* devices, int numdevices)
{ {
devices[x].update(&devices[x], simdata); devices[x].update(&devices[x], simdata);
} }
for (int x = 0; x < numdevices; x++)
{
devices[x].update(&devices[x], simdata);
}
sleep(3); sleep(3);
fprintf(stdout, "Setting speed to 300\n"); fprintf(stdout, "Setting speed to 300\n");

View File

@ -68,6 +68,11 @@ int strtodevsubtype(const char* device_subtype, DeviceSettings* ds, int simdev)
ds->dev_subtype = SIMDEVTYPE_SHIFTLIGHTS; ds->dev_subtype = SIMDEVTYPE_SHIFTLIGHTS;
break; break;
} }
if (strcicmp(device_subtype, "SimWind") == 0)
{
ds->dev_subtype = SIMDEVTYPE_SIMWIND;
break;
}
case SIMDEV_SOUND: case SIMDEV_SOUND:
if (strcicmp(device_subtype, "Engine") == 0) if (strcicmp(device_subtype, "Engine") == 0)
{ {
@ -232,7 +237,7 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
return error; return error;
} }
if (ms->program_action == A_PLAY) if (ms->program_action == A_PLAY || ms->program_action == A_TEST)
{ {
error = loadconfig(config_file, ds); error = loadconfig(config_file, ds);
} }
@ -254,11 +259,34 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
slogi("Tachometer granularity set to %i", ds->tachsettings.granularity); slogi("Tachometer granularity set to %i", ds->tachsettings.granularity);
} }
ds->tachsettings.use_pulses = true; ds->tachsettings.use_pulses = true;
if (ms->program_action == A_PLAY) if (ms->program_action == A_PLAY || ms->program_action == A_TEST)
{ {
ds->tachsettings.use_pulses = false; ds->tachsettings.use_pulses = false;
} }
} }
if (ds->dev_subtype == SIMDEVTYPE_SIMWIND || ds->dev_subtype == SIMDEVTYPE_SHIFTLIGHTS)
{
if (device_settings != NULL)
{
const char* temp;
config_setting_lookup_string(device_settings, "devpath", &temp);
ds->serialdevsettings.portdev = strdup(temp);
}
}
return error; return error;
} }
int settingsfree(DeviceSettings ds)
{
if (ds.dev_subtype == SIMDEVTYPE_SIMWIND || ds.dev_subtype == SIMDEVTYPE_SHIFTLIGHTS)
{
if (ds.serialdevsettings.portdev != NULL)
{
free(ds.serialdevsettings.portdev);
}
}
return 0;
}

View File

@ -22,8 +22,9 @@ typedef enum
SIMDEVTYPE_UNKNOWN = 0, SIMDEVTYPE_UNKNOWN = 0,
SIMDEVTYPE_TACHOMETER = 1, SIMDEVTYPE_TACHOMETER = 1,
SIMDEVTYPE_SHIFTLIGHTS = 2, SIMDEVTYPE_SHIFTLIGHTS = 2,
SIMDEVTYPE_ENGINESOUND = 3, SIMDEVTYPE_SIMWIND = 3,
SIMDEVTYPE_GEARSOUND = 4 SIMDEVTYPE_ENGINESOUND = 4,
SIMDEVTYPE_GEARSOUND = 5
} }
DeviceSubType; DeviceSubType;
@ -75,12 +76,19 @@ typedef struct
} }
TachometerSettings; TachometerSettings;
typedef struct
{
char* portdev;
}
SerialDeviceSettings;
typedef struct typedef struct
{ {
bool is_valid; bool is_valid;
DeviceType dev_type; DeviceType dev_type;
DeviceSubType dev_subtype; DeviceSubType dev_subtype;
TachometerSettings tachsettings; TachometerSettings tachsettings;
SerialDeviceSettings serialdevsettings;
} }
DeviceSettings; DeviceSettings;
@ -88,4 +96,6 @@ int strtogame(const char* game, MonocoqueSettings* ms);
int devsetup(const char* device_type, const char* device_subtype, const char* config_files, MonocoqueSettings* ms, DeviceSettings* ds, config_setting_t* device_settings); int devsetup(const char* device_type, const char* device_subtype, const char* config_files, MonocoqueSettings* ms, DeviceSettings* ds, config_setting_t* device_settings);
int settingsfree(DeviceSettings ds);
#endif #endif

View File

@ -215,7 +215,16 @@ int main(int argc, char** argv)
} }
} }
devfree(devices, initdevices); devfree(devices, initdevices);
i = 0;
// improve the api around the config helper
// i don't like that i stack allocated but hid a malloc inside
for( i = 0; i < configureddevices; i++)
{
settingsfree(ds[i]);
}
} }