diff --git a/src/monocoque/devices/serial/arduino.c b/src/monocoque/devices/serial/arduino.c index 1f27c36..751f364 100644 --- a/src/monocoque/devices/serial/arduino.c +++ b/src/monocoque/devices/serial/arduino.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "arduino.h" #include "../slog/slog.h" @@ -18,11 +19,12 @@ int arduino_update(SerialDevice* serialdevice, SimData* simdata) return result; } -int arduino_init(SerialDevice* serialdevice) +int arduino_init(SerialDevice* serialdevice, const char* portdev) { slogi("initializing arduino serial device..."); int error = 0; - char* port_name = "/dev/ttyACM0"; + char* port_name = strdup(portdev); + slogd("Looking for port %s.\n", port_name); error = check(sp_get_port_by_name(port_name, &serialdevice->port)); if (error != 0) @@ -40,6 +42,7 @@ int arduino_init(SerialDevice* serialdevice) check(sp_set_stopbits(serialdevice->port, 1)); check(sp_set_flowcontrol(serialdevice->port, SP_FLOWCONTROL_NONE)); + free(port_name); slogd("Successfully setup arduino serial device..."); return 0; } diff --git a/src/monocoque/devices/serial/arduino.h b/src/monocoque/devices/serial/arduino.h index e3e4eea..49ab499 100644 --- a/src/monocoque/devices/serial/arduino.h +++ b/src/monocoque/devices/serial/arduino.h @@ -5,7 +5,7 @@ #include "../serialdevice.h" 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 check(enum sp_return result); diff --git a/src/monocoque/devices/serialdevice.c b/src/monocoque/devices/serialdevice.c index 4bcf765..89cdd7b 100644 --- a/src/monocoque/devices/serialdevice.c +++ b/src/monocoque/devices/serialdevice.c @@ -28,7 +28,7 @@ int serialdev_free(SimDevice* this) return 0; } -int serialdev_init(SerialDevice* serialdevice) +int serialdev_init(SerialDevice* serialdevice, const char* portdev) { slogi("initializing serial device..."); int error = 0; @@ -36,7 +36,7 @@ int serialdev_init(SerialDevice* serialdevice) serialdevice->type = SERIALDEV_UNKNOWN; serialdevice->type = SERIALDEV_ARDUINO; - error = arduino_init(serialdevice); + error = arduino_init(serialdevice, portdev); return error; } @@ -52,7 +52,7 @@ SerialDevice* new_serial_device(DeviceSettings* ds) { this->m.derived = this; this->m.vtable = &serial_simdevice_vtable; - int error = serialdev_init(this); + int error = serialdev_init(this, ds->serialdevsettings.portdev); if (error != 0) { diff --git a/src/monocoque/devices/sound/usb_generic_shaker.c b/src/monocoque/devices/sound/usb_generic_shaker.c index a211c83..a140f12 100644 --- a/src/monocoque/devices/sound/usb_generic_shaker.c +++ b/src/monocoque/devices/sound/usb_generic_shaker.c @@ -35,16 +35,25 @@ int patestCallbackEngineRPM(const void* inputBuffer, float v = 0; v = data->amp * sin (2 * M_PI * ((float) n) / (float) SAMPLE_RATE); - if (n>=data->table_size) - { - n=0; - } + if (n>=data->table_size) + { + n=0; + } + if (data->gear_sound_data > 0) + { + *out++ = 0; + *out++ = 0; + } + else + { *out++ = v; *out++ = v; + } } + data->gear_sound_data = 0; data->n=n; return 0; } @@ -67,15 +76,26 @@ int patestCallbackGearShift(const void* inputBuffer, for( i=0; igear_sound_data > 0 ) + float v = 0; + v = data->amp * sin (2 * M_PI * ((float) n) / (float) SAMPLE_RATE); + + if (n>=data->table_size) { - data->gear_sound_data = 0; - *out++ = M_PI; - *out++ = M_PI; - *out++ = M_PI; - *out++ = M_PI; + n=0; + } + + if(data->gear_sound_data > 0) + { + *out++ = v; + *out++ = v; + } + else + { + *out++ = 0; + *out++ = 0; } } + data->gear_sound_data = 0; data->n=n; return 0; diff --git a/src/monocoque/devices/sounddevice.c b/src/monocoque/devices/sounddevice.c index bc0b752..61538f1 100644 --- a/src/monocoque/devices/sounddevice.c +++ b/src/monocoque/devices/sounddevice.c @@ -1,5 +1,9 @@ #include #include +#include + +#include +#include #include "simdevice.h" #include "sounddevice.h" @@ -8,11 +12,22 @@ #include "../helper/parameters.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 int sounddev_engine_update(SimDevice* this, SimData* simdata) { SoundDevice* sounddevice = (void *) this->derived; + gear_sound_set(sounddevice, simdata); + 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->sounddata.gear_sound_data = 0; - if (sounddevice->sounddata.last_gear != simdata->gear) - { - sounddevice->sounddata.gear_sound_data = 3.14; - } - sounddevice->sounddata.last_gear = simdata->gear; + gear_sound_set(sounddevice, simdata); } int sounddev_free(SimDevice* this) @@ -50,6 +60,16 @@ int sounddev_init(SoundDevice* sounddevice) sounddevice->sounddata.table_size = 44100/(100/60); 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); } diff --git a/src/monocoque/devices/usbdevice.c b/src/monocoque/devices/usbdevice.c index 95d883c..b5ec0fc 100644 --- a/src/monocoque/devices/usbdevice.c +++ b/src/monocoque/devices/usbdevice.c @@ -45,6 +45,8 @@ int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds) { slogi("initializing usb device..."); int error = 0; + + usbdevice->type = USBDEV_TACHOMETER; switch ( usbdevice->type ) { case USBDEV_UNKNOWN : diff --git a/src/monocoque/gameloop/gameloop.c b/src/monocoque/gameloop/gameloop.c index 6ceff9f..7699b0c 100644 --- a/src/monocoque/gameloop/gameloop.c +++ b/src/monocoque/gameloop/gameloop.c @@ -274,6 +274,10 @@ int tester(SimDevice* devices, int numdevices) { devices[x].update(&devices[x], simdata); } + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } sleep(3); fprintf(stdout, "Setting speed to 100\n"); @@ -290,6 +294,10 @@ int tester(SimDevice* devices, int numdevices) { devices[x].update(&devices[x], simdata); } + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } sleep(3); fprintf(stdout, "Setting speed to 200\n"); @@ -306,6 +314,10 @@ int tester(SimDevice* devices, int numdevices) { devices[x].update(&devices[x], simdata); } + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } sleep(3); fprintf(stdout, "Setting rpms to 2000\n"); @@ -330,6 +342,10 @@ int tester(SimDevice* devices, int numdevices) { devices[x].update(&devices[x], simdata); } + for (int x = 0; x < numdevices; x++) + { + devices[x].update(&devices[x], simdata); + } sleep(3); fprintf(stdout, "Setting speed to 300\n"); diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index 9ed4563..a039907 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -68,6 +68,11 @@ int strtodevsubtype(const char* device_subtype, DeviceSettings* ds, int simdev) ds->dev_subtype = SIMDEVTYPE_SHIFTLIGHTS; break; } + if (strcicmp(device_subtype, "SimWind") == 0) + { + ds->dev_subtype = SIMDEVTYPE_SIMWIND; + break; + } case SIMDEV_SOUND: 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; } - if (ms->program_action == A_PLAY) + if (ms->program_action == A_PLAY || ms->program_action == A_TEST) { 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); } 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; } } + 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; } + +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; +} diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index 1900deb..69952d5 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -22,8 +22,9 @@ typedef enum SIMDEVTYPE_UNKNOWN = 0, SIMDEVTYPE_TACHOMETER = 1, SIMDEVTYPE_SHIFTLIGHTS = 2, - SIMDEVTYPE_ENGINESOUND = 3, - SIMDEVTYPE_GEARSOUND = 4 + SIMDEVTYPE_SIMWIND = 3, + SIMDEVTYPE_ENGINESOUND = 4, + SIMDEVTYPE_GEARSOUND = 5 } DeviceSubType; @@ -75,12 +76,19 @@ typedef struct } TachometerSettings; +typedef struct +{ + char* portdev; +} +SerialDeviceSettings; + typedef struct { bool is_valid; DeviceType dev_type; DeviceSubType dev_subtype; TachometerSettings tachsettings; + SerialDeviceSettings serialdevsettings; } 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 settingsfree(DeviceSettings ds); + #endif diff --git a/src/monocoque/monocoque.c b/src/monocoque/monocoque.c index 3e72b3b..79d7cdb 100644 --- a/src/monocoque/monocoque.c +++ b/src/monocoque/monocoque.c @@ -215,7 +215,16 @@ int main(int argc, char** argv) } } + 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]); + } }