diff --git a/src/monocoque/devices/CMakeLists.txt b/src/monocoque/devices/CMakeLists.txt index 57298d4..011df60 100644 --- a/src/monocoque/devices/CMakeLists.txt +++ b/src/monocoque/devices/CMakeLists.txt @@ -13,8 +13,6 @@ set(devices_source_files tachdevice.c wheeldevice.h wheeldevice.c - usbhapticdevice.h - usbhapticdevice.c hapticeffect.h hapticeffect.c sound.h diff --git a/src/monocoque/devices/hapticeffect.c b/src/monocoque/devices/hapticeffect.c index 3e1e373..3aae500 100644 --- a/src/monocoque/devices/hapticeffect.c +++ b/src/monocoque/devices/hapticeffect.c @@ -6,6 +6,7 @@ #include #include "usbhapticdevice.h" +#include "simdevice.h" #include "../../helper/confighelper.h" #include "../../simulatorapi/simapi/simapi/simdata.h" #include "../../simulatorapi/simapi/simapi/simmapper.h" @@ -194,6 +195,60 @@ void getTyreDiameter(SimData* simdata) } } +int initializeHapticEffect(HapticEffect* h, HapticEffectSettings* hs, MonocoqueSettings* ms) +{ + + switch (hs->effect_type) + { + case (EFFECT_ENGINERPM): + h->effecttype = EFFECT_ENGINERPM; + slogi("Initializing haptic effect for engine vibrations."); + break; + case (EFFECT_GEARSHIFT): + h->effecttype = EFFECT_GEARSHIFT; + slogi("Initializing haptic effect for gear shift vibrations."); + break; + case (EFFECT_TYRESLIP): + h->effecttype = EFFECT_TYRESLIP; + slogi("Initializing haptic effect for tyre slip vibrations."); + break; + case (EFFECT_TYRELOCK): + h->effecttype = EFFECT_TYRELOCK; + slogi("Initializing haptic effect for tyre lock vibrations."); + break; + case (EFFECT_ABSBRAKES): + h->effecttype = EFFECT_ABSBRAKES; + slogi("Initializing haptic effect for abs vibrations."); + break; + + case (EFFECT_SUSPENSION): + h->effecttype = EFFECT_SUSPENSION; + slogi("Initializing haptic effect for suspension vibrations."); + break; + } + + h->tyre = hs->tyre; + slogt("Haptic effect: %i %i, tyre %i %i", h->effecttype, hs->effect_type, h->tyre, hs->tyre); + + h->threshold = hs->threshold; + h->modulationType = hs->modulation; + h->basefrequency = hs->frequency; + h->frequencyMax = hs->frequencyMax; + h->baseamplitude = hs->amplitude; + h->amplitudeMax = hs->amplitudeMax; + h->motorposition = hs->motorposition; + h->duration = hs->duration; + + slogt("haptic duration: %f", h->duration); + slogt("haptic base frequency: %i", h->basefrequency); + slogt("haptic base amplitude: %i", h->baseamplitude); + slogt("haptic motorposition: %i", h->motorposition); + + h->useconfig = ms->useconfig; + h->configcheck = &ms->configcheck; + h->tyrediameterconfig = ms->tyre_diameter_config; +} + double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, int useconfig, int* configcheck, char* configfile) { @@ -212,7 +267,7 @@ double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, if(sim_slip_ratio == 0) { - slogt("wheel vibration calculation with wheel config set to %i configchecked %i configfile %s car %s sim %i", useconfig, *configcheck, configfile, simdata->car, simdata->simexe); + //slogt("wheel vibration calculation with wheel config set to %i configchecked %i configfile %s car %s sim %i", useconfig, *configcheck, configfile, simdata->car, simdata->simexe); switch (effecttype) { @@ -246,7 +301,7 @@ double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, case EFFECT_SUSPENSION: break; default: - slogd("Unknown effect type"); + slogd("Unknown effect type %i", effecttype); } if(simdata->Yvelocity <= 0) { diff --git a/src/monocoque/devices/hapticeffect.h b/src/monocoque/devices/hapticeffect.h index efbbef8..570c7c3 100644 --- a/src/monocoque/devices/hapticeffect.h +++ b/src/monocoque/devices/hapticeffect.h @@ -2,20 +2,51 @@ #define _HAPTICEFFECT_H #include -#include "../helper/confighelper.h" #include "../simulatorapi/simapi/simapi/simdata.h" + +typedef enum +{ + HAPTIC_EFFECT_MODULATION_NONE = 0, + HAPTIC_EFFECT_MODULATION_FREQUENCY = 1, + HAPTIC_EFFECT_MODULATION_AMPLIFY = 2, +} +HapticEffectModulationType; + + +typedef struct +{ + uint32_t curr_frequency; + uint32_t curr_amplitude; + double curr_duration; + uint32_t last_gear; +} +CurrentEffectData; + typedef struct { - double threshold; VibrationEffectType effecttype; MonocoqueTyreIdentifier tyre; + HapticEffectModulationType modulationType; + + uint32_t volume; + double duration; + double threshold; + uint32_t motorposition; + uint32_t basefrequency; + uint32_t frequencyMax; + uint32_t baseamplitude; + uint32_t amplitudeMax; + + CurrentEffectData live_effect; + int useconfig; int* configcheck; char* tyrediameterconfig; } HapticEffect; +int initializeHapticEffect(HapticEffect* h, HapticEffectSettings* hs, MonocoqueSettings* ms); double slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, int useconfig, int* configcheck, char* configfile); bool hasTyreDiameter(SimData* simdata); int loadtyreconfig(SimData* simdata, char* configfile, bool setDiameters); diff --git a/src/monocoque/devices/serialdevice.c b/src/monocoque/devices/serialdevice.c index a80a574..deadc87 100644 --- a/src/monocoque/devices/serialdevice.c +++ b/src/monocoque/devices/serialdevice.c @@ -155,9 +155,9 @@ int arduino_simhaptic_update(SimDevice* this, SimData* simdata) } uint8_t effectspeed = ceil(255 * play); - uint8_t motor = serialdevice->motorsposition; + uint8_t motor = this->hapticeffect.motorposition; - if (play != serialdevice->state) + if (play != serialdevice->hapticstate) { // motor 1 if (motor == 0 || motor == 4 || motor == 7 || motor == 8 || motor == 10 || motor == 11 || motor == 13 || motor == 14) @@ -173,7 +173,7 @@ int arduino_simhaptic_update(SimDevice* this, SimData* simdata) serialdevice->u.simhapticdata.motor3 = 1; slogt("Updating arduino haptic device with effect type %i speed motor speed %i on motor %i from original effect %f with ampfactor %f", this->hapticeffect.effecttype, serialdevice->u.simhapticdata.effect3, 3, rplay, serialdevice->ampfactor); } - serialdevice->state = play; + serialdevice->hapticstate = play; } size_t size = sizeof(SimHapticData); @@ -197,7 +197,7 @@ int serialdev_free(SimDevice* this) serialdevice->u.simhapticdata.motor3 = 1; serialdevice->u.simhapticdata.effect4 = 0; serialdevice->u.simhapticdata.motor4 = 1; - serialdevice->state = 0; + serialdevice->hapticstate = 0; size_t size = sizeof(SimHapticData); monocoque_serial_write_block(serialdevice->id, &serialdevice->u.simhapticdata, size, 9000); @@ -356,7 +356,7 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms, SimIn this->u.simhapticdata.effect2 = 0; this->u.simhapticdata.effect3 = 0; this->u.simhapticdata.effect4 = 0; - this->state = 0; + this->hapticstate = 0; this->ampfactor = ds->serialdevsettings.ampfactor; this->fanpower = ds->serialdevsettings.fanpower; slogi("Initializing arduino device for haptic effects."); @@ -388,10 +388,10 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms, SimIn if(this->devicetype == ARDUINODEV__HAPTIC && error == 0) { - this->m.hapticeffect.threshold = ds->threshold; - this->m.hapticeffect.effecttype = ds->effect_type; - slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->effect_type); - this->m.hapticeffect.tyre = ds->tyre; + this->m.hapticeffect.threshold = ds->hapticsettings.threshold; + this->m.hapticeffect.effecttype = ds->hapticsettings.effect_type; + slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->hapticsettings.effect_type); + this->m.hapticeffect.tyre = ds->hapticsettings.tyre; this->m.hapticeffect.useconfig = ms->useconfig; this->m.hapticeffect.configcheck = &ms->configcheck; this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config; diff --git a/src/monocoque/devices/simdevice.h b/src/monocoque/devices/simdevice.h index e0594ab..f5bf67c 100644 --- a/src/monocoque/devices/simdevice.h +++ b/src/monocoque/devices/simdevice.h @@ -67,7 +67,7 @@ typedef struct int baudrate; double ampfactor; double fanpower; - double state; + double hapticstate; union { SimWindData simwinddata; @@ -88,10 +88,10 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms, SimIn /********* USB HID Devices *****/ typedef enum { - USBDEV_UNKNOWN = 0, - USBDEV_TACHOMETER = 1, - USBDEV_GENERICHAPTIC = 2, - USBDEV_WHEEL = 3 + USBDEV_UNKNOWN = 0, + USBDEV_TACHOMETER = 1, + USBDEV_GENERICHAPTIC = 2, + USBDEV_WHEEL_OR_PEDALS = 3 } USBType; @@ -100,12 +100,14 @@ typedef struct SimDevice m; int id; hid_device* handle; + FILE* filehandle; + char* dev; + double hapticstate; USBType type; union { TachDevice tachdevice; WheelDevice wheeldevice; - USBGenericHapticDevice hapticdevice; } u; } USBDevice; @@ -122,7 +124,6 @@ typedef struct SimDevice m; int id; int configcheck; - SoundEffectModulationType modulationType; SoundData sounddata; //#ifdef USE_PULSEAUDIO pa_stream *stream; @@ -154,7 +155,8 @@ int simdevfree(SimDevice* this); /****** USB SubTypes ****************/ -int wheeldev_update(USBDevice* usbdevice, SimData* simdata); +int wheelhapticdev_update(SimDevice* this, SimData* simdata); +int wheeldev_update(SimDevice* this, SimData* simdata); int wheeldev_init(USBDevice* usbdevice, DeviceSettings* ds); int wheeldev_free(USBDevice* usbdevice); diff --git a/src/monocoque/devices/sounddevice.c b/src/monocoque/devices/sounddevice.c index 09ec995..c5d4883 100644 --- a/src/monocoque/devices/sounddevice.c +++ b/src/monocoque/devices/sounddevice.c @@ -15,13 +15,15 @@ #include "../helper/parameters.h" #include "../slog/slog.h" -int gear_sound_set(SoundDevice* sounddevice, SimData* simdata) +int gear_sound_set(SimDevice* this, SimData* simdata) { + SoundDevice* sounddevice = (void *) this->derived; + if (sounddevice->sounddata.last_gear != simdata->gear && simdata->gear > 1) { //sounddevice->sounddata.gear_sound_data = 3.14; - sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency; - sounddevice->sounddata.curr_amplitude = sounddevice->sounddata.amplitude; + sounddevice->sounddata.curr_frequency = this->hapticeffect.basefrequency; + sounddevice->sounddata.curr_amplitude = this->hapticeffect.baseamplitude; sounddevice->sounddata.curr_duration = 0; } sounddevice->sounddata.last_gear = simdata->gear; @@ -30,27 +32,29 @@ int gear_sound_set(SoundDevice* sounddevice, SimData* simdata) } -double modulate(SoundDevice* sounddevice, double raw_effect, SoundEffectModulationType modulation) +double modulate(SimDevice* this, double raw_effect, HapticEffectModulationType modulation) { + SoundDevice* sounddevice = (void *) this->derived; + double modulated_effect = raw_effect; switch (modulation) { - case SOUND_EFFECT_MODULATION_FREQUENCY: - modulated_effect = ((sounddevice->sounddata.frequencyMax - sounddevice->sounddata.frequency) * raw_effect) + sounddevice->sounddata.frequency; + case HAPTIC_EFFECT_MODULATION_FREQUENCY: + modulated_effect = ((this->hapticeffect.frequencyMax - this->hapticeffect.basefrequency) * raw_effect) + this->hapticeffect.basefrequency; sounddevice->sounddata.curr_frequency = modulated_effect; - sounddevice->sounddata.curr_amplitude = sounddevice->sounddata.amplitude; - slogt("set curr frequency to %f from raw effect %f and base frequency %i", sounddevice->sounddata.curr_frequency, raw_effect, sounddevice->sounddata.frequency); + sounddevice->sounddata.curr_amplitude = this->hapticeffect.baseamplitude; + slogt("set curr frequency to %f from raw effect %f and base frequency %i", sounddevice->sounddata.curr_frequency, raw_effect, this->hapticeffect.basefrequency); break; - case SOUND_EFFECT_MODULATION_AMPLIFY: - modulated_effect = ((sounddevice->sounddata.amplitudeMax - sounddevice->sounddata.amplitude) * raw_effect) + sounddevice->sounddata.amplitude; + case HAPTIC_EFFECT_MODULATION_AMPLIFY: + modulated_effect = ((this->hapticeffect.amplitudeMax - this->hapticeffect.baseamplitude) * raw_effect) + this->hapticeffect.baseamplitude; sounddevice->sounddata.curr_amplitude = modulated_effect; - sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency; - slogt("set curr amplitude to %i from raw effect %f and base amplitude %i", sounddevice->sounddata.curr_amplitude, raw_effect, sounddevice->sounddata.amplitude); + sounddevice->sounddata.curr_frequency = this->hapticeffect.basefrequency; + slogt("set curr amplitude to %i from raw effect %f and base amplitude %i", sounddevice->sounddata.curr_amplitude, raw_effect, this->hapticeffect.baseamplitude); break; - case SOUND_EFFECT_MODULATION_NONE: + case HAPTIC_EFFECT_MODULATION_NONE: default: - sounddevice->sounddata.curr_frequency = sounddevice->sounddata.frequency; - sounddevice->sounddata.curr_amplitude = sounddevice->sounddata.amplitude; + sounddevice->sounddata.curr_frequency = this->hapticeffect.basefrequency; + sounddevice->sounddata.curr_amplitude = this->hapticeffect.baseamplitude; break; } @@ -64,7 +68,7 @@ int sounddev_engine_update(SimDevice* this, SimData* simdata) double effect = ((double)simdata->rpms/60)/((double)simdata->maxrpm/60); slogt("Set base effect of %f from rpms of %i", effect, simdata->rpms); - modulate(sounddevice, effect, sounddevice->modulationType); + modulate(this, effect, this->hapticeffect.modulationType); } int sounddev_tyreslip_update(SimDevice* this, SimData* simdata) @@ -72,11 +76,11 @@ int sounddev_tyreslip_update(SimDevice* this, SimData* simdata) SoundDevice* sounddevice = (void *) this->derived; double effect = slipeffect(simdata, this->hapticeffect.effecttype, this->hapticeffect.tyre, this->hapticeffect.threshold, this->hapticeffect.useconfig, this->hapticeffect.configcheck, this->hapticeffect.tyrediameterconfig); - slogt("Updating sound device frequency from original tyre slip effect %f", effect); + slogt("Updating sound device frequency from original %i tyre slip effect %f", this->hapticeffect.effecttype, effect); if (effect > 0) { - modulate(sounddevice, effect, sounddevice->modulationType); + modulate(this, effect, this->hapticeffect.modulationType); } else { @@ -96,7 +100,7 @@ int sounddev_tyrelock_update(SimDevice* this, SimData* simdata) if (play > 0) { - modulate(sounddevice, play, sounddevice->modulationType); + modulate(this, play, this->hapticeffect.modulationType); } else { @@ -114,7 +118,7 @@ int sounddev_absbrakes_update(SimDevice* this, SimData* simdata) if (play > 0) { - modulate(sounddevice, play, sounddevice->modulationType); + modulate(this, play, this->hapticeffect.modulationType); } else { @@ -134,7 +138,7 @@ int sounddev_suspension_update(SimDevice* this, SimData* simdata) if (effect > 0) { - effect = modulate(sounddevice, effect, sounddevice->modulationType); + effect = modulate(this, effect, this->hapticeffect.modulationType); } else { @@ -147,9 +151,7 @@ int sounddev_suspension_update(SimDevice* this, SimData* simdata) int sounddev_gearshift_update(SimDevice* this, SimData* simdata) { - SoundDevice* sounddevice = (void *) this->derived; - - gear_sound_set(sounddevice, simdata); + gear_sound_set(this, simdata); } @@ -163,28 +165,17 @@ int sounddev_free(SimDevice* this) return 0; } -int sounddev_init(SoundDevice* sounddevice, const char* devname, MonocoqueTyreIdentifier tyre, SoundDeviceSettings sds) +int sounddev_init(SoundDevice* sounddevice, const char* devname, SoundDeviceSettings sds) { slogi("initializing standalone sound device..."); slogi("volume is: %i", sds.volume); - slogi("Modulation type: %i", sds.modulation); - slogi("frequency is: %i", sds.frequency); - slogi("frequency Max is: %i", sds.frequencyMax); - slogi("amplitude is: %i", sds.amplitude); - slogi("amplitude Max is: %i", sds.amplitudeMax); slogi("pan is: %i", sds.pan); slogi("channels is: %i", sds.channels); - slogi("duration is: %f", sds.duration); slogi("noise is: %i", sds.noise); - sounddevice->modulationType = sds.modulation; - sounddevice->sounddata.frequency = sds.frequency; - sounddevice->sounddata.frequencyMax = sds.frequencyMax; - sounddevice->sounddata.amplitude = sds.amplitude; - sounddevice->sounddata.amplitudeMax = sds.amplitudeMax; sounddevice->sounddata.noise = (double)sds.noise; sounddevice->sounddata.curr_duration = 0; @@ -240,7 +231,7 @@ SoundDevice* new_sound_device(DeviceSettings* ds, MonocoqueSettings* ms, SimInfo this->m.derived = this; int error = 0; - switch (ds->effect_type) + switch (ds->hapticsettings.effect_type) { case (EFFECT_TYRESLIP): case (EFFECT_TYRELOCK): @@ -258,73 +249,32 @@ SoundDevice* new_sound_device(DeviceSettings* ds, MonocoqueSettings* ms, SimInfo if(error == 0) { - slogt("Attempting to configure sound device with subtype: %i", ds->effect_type); - switch (ds->effect_type) + initializeHapticEffect(&this->m.hapticeffect, &ds->hapticsettings, ms); + slogt("Attempting to configure sound device with subtype: %i", ds->hapticsettings.effect_type); + switch (ds->hapticsettings.effect_type) { case (EFFECT_ENGINERPM): - this->m.hapticeffect.effecttype = EFFECT_ENGINERPM; this->m.vtable = &engine_sound_simdevice_vtable; slogi("Initializing sound device for engine vibrations."); break; case (EFFECT_GEARSHIFT): - this->m.hapticeffect.effecttype = EFFECT_GEARSHIFT; this->m.vtable = &gear_sound_simdevice_vtable; slogi("Initializing sound device for gear shift vibrations."); break; case (EFFECT_TYRESLIP): - this->m.hapticeffect.effecttype = EFFECT_TYRESLIP; - this->m.hapticeffect.threshold = ds->threshold; - - this->m.hapticeffect.threshold = ds->threshold; - slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->effect_type); - this->m.hapticeffect.tyre = ds->tyre; - this->m.hapticeffect.useconfig = ms->useconfig; - this->m.hapticeffect.configcheck = &ms->configcheck; - this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config; - this->m.vtable = &tyreslip_sound_simdevice_vtable; slogi("Initializing sound device for tyre slip vibrations."); break; case (EFFECT_TYRELOCK): - this->m.hapticeffect.effecttype = EFFECT_TYRELOCK; - this->m.hapticeffect.threshold = ds->threshold; - - this->m.hapticeffect.threshold = ds->threshold; - slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->effect_type); - this->m.hapticeffect.tyre = ds->tyre; - this->m.hapticeffect.useconfig = ms->useconfig; - this->m.hapticeffect.configcheck = &ms->configcheck; - this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config; - this->m.vtable = &tyrelock_sound_simdevice_vtable; - slogi("Initializing sound device for tyre slip vibrations."); + slogi("Initializing sound device for tyre lock vibrations."); break; case (EFFECT_ABSBRAKES): - this->m.hapticeffect.effecttype = EFFECT_ABSBRAKES; - this->m.hapticeffect.threshold = ds->threshold; - - this->m.hapticeffect.threshold = ds->threshold; - slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->effect_type); - this->m.hapticeffect.tyre = ds->tyre; - this->m.hapticeffect.useconfig = ms->useconfig; - this->m.hapticeffect.configcheck = &ms->configcheck; - this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config; - this->m.vtable = &absbrakes_sound_simdevice_vtable; slogi("Initializing sound device for abs vibrations."); break; case (EFFECT_SUSPENSION): - this->m.hapticeffect.effecttype = EFFECT_SUSPENSION; - this->m.hapticeffect.threshold = ds->threshold; - - this->m.hapticeffect.threshold = ds->threshold; - slogt("Haptic effect: %i %i on tyre %i", this->m.hapticeffect.effecttype, ds->effect_type, ds->tyre); - this->m.hapticeffect.tyre = ds->tyre; - this->m.hapticeffect.useconfig = ms->useconfig; - this->m.hapticeffect.configcheck = &ms->configcheck; - this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config; - this->m.vtable = &suspension_sound_simdevice_vtable; slogi("Initializing sound device for suspension vibrations."); break; @@ -334,7 +284,7 @@ SoundDevice* new_sound_device(DeviceSettings* ds, MonocoqueSettings* ms, SimInfo if(error == 0) { slogt("Attempting to use sound device %s", ds->sounddevsettings.dev); - error = sounddev_init(this, ds->sounddevsettings.dev, ds->tyre, ds->sounddevsettings); + error = sounddev_init(this, ds->sounddevsettings.dev, ds->sounddevsettings); } if (error != 0) diff --git a/src/monocoque/devices/sounddevice.h b/src/monocoque/devices/sounddevice.h index 33824c4..597be23 100644 --- a/src/monocoque/devices/sounddevice.h +++ b/src/monocoque/devices/sounddevice.h @@ -8,23 +8,11 @@ //#endif -typedef enum -{ - SOUND_EFFECT_MODULATION_NONE = 0, - SOUND_EFFECT_MODULATION_FREQUENCY = 1, - SOUND_EFFECT_MODULATION_AMPLIFY = 2, -} -SoundEffectModulationType; - #define MAX_TABLE_SIZE (6000) typedef struct { uint8_t last_gear; int volume; - uint32_t frequency; - uint32_t frequencyMax; - uint32_t amplitude; - uint32_t amplitudeMax; double duration; double curr_frequency; uint32_t curr_amplitude; diff --git a/src/monocoque/devices/usb/cslelitev3.c b/src/monocoque/devices/usb/cslelitev3.c index 0e42957..14ea3b8 100644 --- a/src/monocoque/devices/usb/cslelitev3.c +++ b/src/monocoque/devices/usb/cslelitev3.c @@ -3,7 +3,7 @@ #include #include -#include "usbhapticdevice.h" +#include "cslelitev3.h" #include "../../helper/confighelper.h" #include "../slog/slog.h" @@ -11,7 +11,7 @@ const char* SYSFSRUMBLEPATH = "/sys/module/hid_fanatec/drivers/hid:f*/0003:0EB7:183B.*/rumble"; -int cslelitev3_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, int play) +int cslelitev3_update(USBDevice* usbdevice, int effecttype, int play) { int res = 0; @@ -34,25 +34,25 @@ int cslelitev3_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, i } - fprintf(usbhapticdevice->filehandle, "%i\n", value); - fflush(usbhapticdevice->filehandle); + fprintf(usbdevice->filehandle, "%i\n", value); + fflush(usbdevice->filehandle); return res; } -int cslelitev3_free(USBGenericHapticDevice* usbhapticdevice) +int cslelitev3_free(USBDevice* usbdevice) { int res = 0; - free(usbhapticdevice->dev); + free(usbdevice->dev); - fflush(usbhapticdevice->filehandle); - fclose(usbhapticdevice->filehandle); + fflush(usbdevice->filehandle); + fclose(usbdevice->filehandle); return res; } -int cslelitev3_init(USBGenericHapticDevice* usbhapticdevice) +int cslelitev3_init(USBDevice* usbdevice) { slogi("initializing CSL Elite V3 Pedals..."); @@ -75,7 +75,7 @@ int cslelitev3_init(USBGenericHapticDevice* usbhapticdevice) { if (i == 0) { - usbhapticdevice->dev = strdup(globlist.gl_pathv[i]); + usbdevice->dev = strdup(globlist.gl_pathv[i]); } i++; } @@ -91,9 +91,9 @@ int cslelitev3_init(USBGenericHapticDevice* usbhapticdevice) return res; } - usbhapticdevice->filehandle = fopen(usbhapticdevice->dev, "w"); + usbdevice->filehandle = fopen(usbdevice->dev, "w"); - if (!usbhapticdevice->filehandle) + if (!usbdevice->filehandle) { sloge("Could not open pedal device..."); return res; diff --git a/src/monocoque/devices/usb/cslelitev3.h b/src/monocoque/devices/usb/cslelitev3.h index cff75a9..d9f9e1a 100644 --- a/src/monocoque/devices/usb/cslelitev3.h +++ b/src/monocoque/devices/usb/cslelitev3.h @@ -1,8 +1,11 @@ #ifndef _CSLELITEV3_H #define _CSLELITEV3_H -int cslelitev3_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, int play); -int cslelitev3_init(USBGenericHapticDevice* usbhapticdevice); -int cslelitev3_free(USBGenericHapticDevice* usbhapticdevice); +#include "../wheeldevice.h" +#include "../simdevice.h" + +int cslelitev3_update(USBDevice* usbdevice, int effecttype, int play); +int cslelitev3_init(USBDevice* usbdevice); +int cslelitev3_free(USBDevice* usbdevice); #endif diff --git a/src/monocoque/devices/usb/simagicp1000.c b/src/monocoque/devices/usb/simagicp1000.c index b070919..d34dfe6 100644 --- a/src/monocoque/devices/usb/simagicp1000.c +++ b/src/monocoque/devices/usb/simagicp1000.c @@ -3,7 +3,7 @@ #include #include -#include "usbhapticdevice.h" +#include "simagicp1000.h" #include "../../helper/confighelper.h" #include "../slog/slog.h" @@ -14,12 +14,12 @@ const size_t SIMAGIC_P1000_BUFSIZE = 49; const char* SIMAGICP1000DEVSTRING = "SIMAGIC P1000 Pedals"; -int senddevreport(USBGenericHapticDevice* usbhapticdevice, unsigned char* buf, size_t bufsize) +int senddevreport(USBDevice* usbdevice, unsigned char* buf, size_t bufsize) { int res = 0; - if (usbhapticdevice->handle) + if (usbdevice->handle) { - res = hid_send_feature_report(usbhapticdevice->handle, buf, SIMAGIC_P1000_BUFSIZE); + res = hid_send_feature_report(usbdevice->handle, buf, SIMAGIC_P1000_BUFSIZE); slogd("sent %i bytes to %s", bufsize, SIMAGICP1000DEVSTRING); slogt("sent bytes %02x %02x %02x %02x %02x %02x %02x", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]); } @@ -30,7 +30,7 @@ int senddevreport(USBGenericHapticDevice* usbhapticdevice, unsigned char* buf, s return res != bufsize; } -int simagicp1000_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, int play) +int simagicp1000_update(USBDevice* usbdevice, int effecttype, int play) { int res = 0; @@ -54,17 +54,17 @@ int simagicp1000_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, { case (EFFECT_TYRESLIP): bytes[2] = 0x02; - senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + senddevreport(usbdevice, bytes, SIMAGIC_P1000_BUFSIZE); break; case (EFFECT_TYRELOCK): bytes[2] = 0x01; - senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + senddevreport(usbdevice, bytes, SIMAGIC_P1000_BUFSIZE); break; case (EFFECT_ABSBRAKES): bytes[2] = 0x02; - senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + senddevreport(usbdevice, bytes, SIMAGIC_P1000_BUFSIZE); bytes[2] = 0x01; - senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + senddevreport(usbdevice, bytes, SIMAGIC_P1000_BUFSIZE); break; } } @@ -72,19 +72,19 @@ int simagicp1000_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, { bytes[0] = 241; bytes[2] = 0x01; - senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + senddevreport(usbdevice, bytes, SIMAGIC_P1000_BUFSIZE); bytes[2] = 0x02; - senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + senddevreport(usbdevice, bytes, SIMAGIC_P1000_BUFSIZE); } return res; } -int simagicp1000_free(USBGenericHapticDevice* usbhapticdevice) +int simagicp1000_free(USBDevice* usbdevice) { int res = 0; - hid_close(usbhapticdevice->handle); + hid_close(usbdevice->handle); res = hid_exit(); return res; @@ -92,7 +92,7 @@ int simagicp1000_free(USBGenericHapticDevice* usbhapticdevice) return res; } -int simagicp1000_init(USBGenericHapticDevice* usbhapticdevice) +int simagicp1000_init(USBDevice* usbdevice) { slogi("initializing %s...", SIMAGICP1000DEVSTRING); @@ -100,9 +100,9 @@ int simagicp1000_init(USBGenericHapticDevice* usbhapticdevice) res = hid_init(); - usbhapticdevice->handle = hid_open(SIMAGIC_VENDOR_ID, SIMAGIC_PRODUCT_ID_P1000, NULL); + usbdevice->handle = hid_open(SIMAGIC_VENDOR_ID, SIMAGIC_PRODUCT_ID_P1000, NULL); - if (!usbhapticdevice->handle) + if (!usbdevice->handle) { sloge("Could not find attached %s", SIMAGICP1000DEVSTRING); res = hid_exit(); @@ -123,7 +123,7 @@ int simagicp1000_init(USBGenericHapticDevice* usbhapticdevice) bytes[5] = 0x00; bytes[6] = 0x01; bytes[7] = 0x02; - res = senddevreport(usbhapticdevice, bytes, SIMAGIC_P1000_BUFSIZE); + res = senddevreport(usbdevice, bytes, SIMAGIC_P1000_BUFSIZE); slogd("Initialization returned %i", res); if(res != 0) { diff --git a/src/monocoque/devices/usb/simagicp1000.h b/src/monocoque/devices/usb/simagicp1000.h index 77770c5..38d5d90 100644 --- a/src/monocoque/devices/usb/simagicp1000.h +++ b/src/monocoque/devices/usb/simagicp1000.h @@ -1,8 +1,11 @@ #ifndef _SIMAGICP1000_H #define _SIMAGICP1000_H -int simagicp1000_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, int play); -int simagicp1000_init(USBGenericHapticDevice* usbhapticdevice); -int simagicp1000_free(USBGenericHapticDevice* usbhapticdevice); +#include "../wheeldevice.h" +#include "../simdevice.h" + +int simagicp1000_update(USBDevice* usbdevice, int effecttype, int play); +int simagicp1000_init(USBDevice* usbdevice); +int simagicp1000_free(USBDevice* usbdevice); #endif diff --git a/src/monocoque/devices/usb/simnetpedals.c b/src/monocoque/devices/usb/simnetpedals.c index 0e104a1..2507eb0 100644 --- a/src/monocoque/devices/usb/simnetpedals.c +++ b/src/monocoque/devices/usb/simnetpedals.c @@ -3,7 +3,7 @@ #include #include -#include "usbhapticdevice.h" +#include "simnetpedals.h" #include "../../helper/confighelper.h" #include "../slog/slog.h" @@ -21,7 +21,7 @@ //Byte[8]: motor 3 amplitude (0 to 100%) -int simnetpedals_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, int play) +int simnetpedals_update(USBDevice* usbdevice, int effecttype, int play, int motorposition, int frequency, int amplitude) { int res = 0; @@ -33,23 +33,23 @@ int simnetpedals_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, } bytes[0] = 0x01; bytes[1] = 0x00; - bytes[2] = usbhapticdevice->motorposition; - uint8_t motormultiple = usbhapticdevice->motorposition - 1; - if(usbhapticdevice->motorposition == 0x04) + bytes[2] = motorposition; + uint8_t motormultiple = motorposition - 1; + if(motorposition == 0x04) { motormultiple = 2; } if(play > 0) { - bytes[3 + (motormultiple * 2)] = usbhapticdevice->frequency; - bytes[4 + (motormultiple * 2)] = usbhapticdevice->amplitude; + bytes[3 + (motormultiple * 2)] = frequency; + bytes[4 + (motormultiple * 2)] = amplitude; } - if (usbhapticdevice->handle) + if (usbdevice->handle) { slogt("writing bytes x%02xx%02xx%02xx%02xx%02xx%02xx%02xx%02xx%02x", bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7], bytes[8]); - res = hid_write(usbhapticdevice->handle, bytes, SIMNETPEDALS_BUFSIZE); + res = hid_write(usbdevice->handle, bytes, SIMNETPEDALS_BUFSIZE); } else { @@ -59,17 +59,17 @@ int simnetpedals_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, return res; } -int simnetpedals_free(USBGenericHapticDevice* usbhapticdevice) +int simnetpedals_free(USBDevice* usbdevice) { int res = 0; - hid_close(usbhapticdevice->handle); + hid_close(usbdevice->handle); res = hid_exit(); return res; } -int simnetpedals_init(USBGenericHapticDevice* usbhapticdevice) +int simnetpedals_init(USBDevice* usbdevice) { slogi("initializing simnet pedals..."); @@ -77,14 +77,14 @@ int simnetpedals_init(USBGenericHapticDevice* usbhapticdevice) res = hid_init(); - usbhapticdevice->handle = hid_open(0xcafe, 0xa301, NULL); + usbdevice->handle = hid_open(0xcafe, 0xa301, NULL); - if (!usbhapticdevice->handle) + if (!usbdevice->handle) { sloge("Could not find attached Simnet Pedals"); res = hid_exit(); return 1; } - slogd("Found Simnet Pedals handle %i...", usbhapticdevice->handle); + slogd("Found Simnet Pedals handle %i...", usbdevice->handle); return res; } diff --git a/src/monocoque/devices/usb/simnetpedals.h b/src/monocoque/devices/usb/simnetpedals.h index 95d7413..23a4f17 100644 --- a/src/monocoque/devices/usb/simnetpedals.h +++ b/src/monocoque/devices/usb/simnetpedals.h @@ -1,8 +1,11 @@ #ifndef _SIMNETPEDALS_H #define _SIMNETPEDALS_H -int simnetpedals_update(USBGenericHapticDevice* usbhapticdevice, int effecttype, int play); -int simnetpedals_init(USBGenericHapticDevice* usbhapticdevice); -int simnetpedals_free(USBGenericHapticDevice* usbhapticdevice); +#include "../wheeldevice.h" +#include "../simdevice.h" + +int simnetpedals_update(USBDevice* usbdevice, int effecttype, int play, int motorposition, int frequency, int amplitude); +int simnetpedals_init(USBDevice* usbdevice); +int simnetpedals_free(USBDevice* usbdevice); #endif diff --git a/src/monocoque/devices/usb/wheels/cammusc12.h b/src/monocoque/devices/usb/wheels/cammusc12.h index 8f236a4..2bbaf19 100644 --- a/src/monocoque/devices/usb/wheels/cammusc12.h +++ b/src/monocoque/devices/usb/wheels/cammusc12.h @@ -2,7 +2,6 @@ #define _CAMMUSC12_H #include "../../wheeldevice.h" - #include "../../simdevice.h" int cammusc12_update(USBDevice* wheeldevice, int maxrpm, int rpm, int gear, int velocity); diff --git a/src/monocoque/devices/usbdevice.c b/src/monocoque/devices/usbdevice.c index 8317489..cc773ae 100644 --- a/src/monocoque/devices/usbdevice.c +++ b/src/monocoque/devices/usbdevice.c @@ -19,11 +19,9 @@ int usbdev_update(SimDevice* this, SimData* simdata) case USBDEV_TACHOMETER : tachdev_update(usbdevice, simdata); break; - case USBDEV_WHEEL : - wheeldev_update(usbdevice, simdata); - break; - case USBDEV_GENERICHAPTIC : - usbhapticdev_update(&usbdevice->u.hapticdevice, simdata, this->hapticeffect.tyre, this->hapticeffect.useconfig, this->hapticeffect.configcheck, this->hapticeffect.tyrediameterconfig); + case USBDEV_WHEEL_OR_PEDALS : + // this should be directed via vtable to wheeldevice.c + //wheeldev_update(usbdevice, simdata); break; } @@ -41,12 +39,9 @@ int usbdev_free(SimDevice* this) case USBDEV_TACHOMETER : tachdev_free(usbdevice); break; - case USBDEV_WHEEL : + case USBDEV_WHEEL_OR_PEDALS : wheeldev_free(usbdevice); break; - case USBDEV_GENERICHAPTIC : - usbhapticdev_free(&usbdevice->u.hapticdevice); - break; } free(usbdevice); @@ -66,53 +61,55 @@ int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds, SimInfo* siminfo) case USBDEV_TACHOMETER : error = tachdev_init(usbdevice, ds); break; - case USBDEV_WHEEL : + case USBDEV_WHEEL_OR_PEDALS : error = wheeldev_init(usbdevice, ds); break; - case USBDEV_GENERICHAPTIC : - error = usbhapticdev_init(&usbdevice->u.hapticdevice, ds, siminfo); - break; } return error; } + static const vtable usb_simdevice_vtable = { &usbdev_update, &usbdev_free }; +static const vtable usb_wheeldevice_vtable = { &wheeldev_update, &usbdev_free }; +static const vtable usb_wheelhaptic_vtable = { &wheelhapticdev_update, &usbdev_free }; USBDevice* new_usb_device(DeviceSettings* ds, MonocoqueSettings* ms, SimInfo* siminfo) { USBDevice* this = (USBDevice*) malloc(sizeof(USBDevice)); + int error = 0; this->m.update = &update; this->m.free = &simdevfree; this->m.derived = this; this->m.vtable = &usb_simdevice_vtable; - - // TODO: turn this into a switch when we get more devices this->type = USBDEV_TACHOMETER; if(ds->dev_subtype == SIMDEVTYPE_USBWHEEL) { - this->type = USBDEV_WHEEL; + this->type = USBDEV_WHEEL_OR_PEDALS; + this->m.vtable = &usb_wheeldevice_vtable; } - - // really generic haptic isn't and shouldn't be it's own type - // it's an attribute that is added to a device via composition - // same if that haptic device is a serial device - if (ds->dev_subtype == SIMDEVTYPE_USBHAPTIC) + if(ds->has_haptic_effects == true) { - this->m.hapticeffect.threshold = ds->threshold; - this->m.hapticeffect.effecttype = ds->effect_type; - this->m.hapticeffect.tyre = ds->tyre; - this->m.hapticeffect.useconfig = ms->useconfig; - this->m.hapticeffect.configcheck = &ms->configcheck; - this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config; - this->type = USBDEV_GENERICHAPTIC; + if(siminfo->SimSupportsHapticEffects == false) + { + // if the user added haptic effects to the config and the sim does not support it, but still wishes to use features + // just remove the haptic effect from the config? + slogi("This sim does not support haptic effects"); + error = MONOCOQUE_ERROR_UNSUPPORTED_SIM_FEATURE; + } + else + { + int error = 0; + initializeHapticEffect(&this->m.hapticeffect, &ds->hapticsettings, ms); + this->m.vtable = &usb_wheelhaptic_vtable; + } } - int error = usbdev_init(this, ds, siminfo); + error = usbdev_init(this, ds, siminfo); if (error != 0) { diff --git a/src/monocoque/devices/usbhapticdevice.c b/src/monocoque/devices/usbhapticdevice.c index d93613b..dab7f2d 100644 --- a/src/monocoque/devices/usbhapticdevice.c +++ b/src/monocoque/devices/usbhapticdevice.c @@ -17,7 +17,7 @@ int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdata, int tyre, int useconfig, int* configcheck, char* configfile) { - double play = slipeffect(simdata, usbhapticdevice->effecttype, tyre, usbhapticdevice->threshold, useconfig, configcheck, configfile); + double play = slipeffect(simdata, this->hapticeffect.effecttype, tyre, this->hapticeffect.threshold, useconfig, configcheck, configfile); if (play != usbhapticdevice->state) { @@ -28,13 +28,13 @@ int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdat switch ( usbhapticdevice->type ) { case USBHAPTIC_CSLELITEV3PEDALS: - cslelitev3_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + cslelitev3_update(usbhapticdevice, this->hapticeffect.effecttype, rplay); break; case USBHAPTIC_SIMAGICP1000PEDALS: - simagicp1000_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + simagicp1000_update(usbhapticdevice, this->hapticeffect.effecttype, rplay); break; case USBHAPTIC_SIMNETPEDALS: - simnetpedals_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + simnetpedals_update(usbhapticdevice, this->hapticeffect.effecttype, rplay); break; } } @@ -43,13 +43,13 @@ int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdat switch ( usbhapticdevice->type ) { case USBHAPTIC_CSLELITEV3PEDALS: - cslelitev3_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + cslelitev3_update(usbhapticdevice, this->hapticeffect.effecttype, rplay); break; case USBHAPTIC_SIMAGICP1000PEDALS: - simagicp1000_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + simagicp1000_update(usbhapticdevice, this->hapticeffect.effecttype, rplay); break; case USBHAPTIC_SIMNETPEDALS: - simnetpedals_update(usbhapticdevice, usbhapticdevice->effecttype, rplay); + simnetpedals_update(usbhapticdevice, this->hapticeffect.effecttype, rplay); break; } } @@ -86,25 +86,7 @@ int usbhapticdev_init(USBGenericHapticDevice* usbhapticdevice, DeviceSettings* d } int error = 0; - usbhapticdevice->state = 0; - usbhapticdevice->value0 = ds->usbdevsettings.value0; - usbhapticdevice->value1 = ds->usbdevsettings.value1; - usbhapticdevice->state = usbhapticdevice->value0; - usbhapticdevice->effecttype = ds->effect_type; - usbhapticdevice->threshold = ds->threshold; - - if(ds->effect_type == EFFECT_TYRESLIP) - { - usbhapticdevice->effecttype = EFFECT_TYRESLIP; - } - if(ds->effect_type == EFFECT_TYRELOCK) - { - usbhapticdevice->effecttype = EFFECT_TYRELOCK; - } - if(ds->effect_type == EFFECT_ABSBRAKES) - { - usbhapticdevice->effecttype = EFFECT_ABSBRAKES; - } + initializeHapticEffect(&this->m.hapticeffect, &ds->hapticsettings, ms); slogi("initializing standalone usb haptic device..."); // detection of usb device model @@ -119,9 +101,6 @@ int usbhapticdev_init(USBGenericHapticDevice* usbhapticdevice, DeviceSettings* d break; case (SIMDEVSUBTYPE_SIMNETPEDALS): usbhapticdevice->type = USBHAPTIC_SIMNETPEDALS; - usbhapticdevice->motorposition = ds->usbdevsettings.motorsposition; - usbhapticdevice->frequency = ds->usbdevsettings.frequency; - usbhapticdevice->amplitude = ds->usbdevsettings.amplitude; error = simnetpedals_init(usbhapticdevice); break; default: diff --git a/src/monocoque/devices/usbhapticdevice.h b/src/monocoque/devices/usbhapticdevice.h index 31ce0ed..bc06873 100644 --- a/src/monocoque/devices/usbhapticdevice.h +++ b/src/monocoque/devices/usbhapticdevice.h @@ -18,15 +18,6 @@ HapticType; typedef struct { int id; - HapticType type; - double state; - double threshold; - int value0; - int value1; - uint32_t frequency; - uint32_t amplitude; - uint32_t motorposition; - VibrationEffectType effecttype; FILE* filehandle; hid_device* handle; char* dev; diff --git a/src/monocoque/devices/wheeldevice.c b/src/monocoque/devices/wheeldevice.c index 6f328ee..7d4b0e4 100644 --- a/src/monocoque/devices/wheeldevice.c +++ b/src/monocoque/devices/wheeldevice.c @@ -2,19 +2,71 @@ #include #include + #include "usb/wheels/logitechg29.h" #include "usb/wheels/cammusc5.h" #include "usb/wheels/cammusc12.h" #include "usb/wheels/gtneo.h" #include "serial/moza.h" + +#include "usb/cslelitev3.h" +#include "usb/simagicp1000.h" +#include "usb/simnetpedals.h" + #include "simdevice.h" -#include "../helper/confighelper.h" #include "../simulatorapi/simapi/simapi/simdata.h" #include "../slog/slog.h" -int wheeldev_update(USBDevice* usbdevice, SimData* simdata) +int wheelhapticdev_update(SimDevice* this, SimData* simdata) { + USBDevice* usbdevice = (void *) this->derived; + + double play = slipeffect(simdata, this->hapticeffect.effecttype, this->hapticeffect.tyre, this->hapticeffect.threshold, this->hapticeffect.useconfig, this->hapticeffect.configcheck, this->hapticeffect.tyrediameterconfig); + + if (play != usbdevice->hapticstate) + { + int rplay = 0; + if(play > 0) + { + rplay = 1; + switch ( usbdevice->type ) + { + case WHEELDEV_CSLELITEV3PEDALS: + cslelitev3_update(usbdevice, this->hapticeffect.effecttype, rplay); + break; + case WHEELDEV_SIMAGICP1000PEDALS: + simagicp1000_update(usbdevice, this->hapticeffect.effecttype, rplay); + break; + case WHEELDEV_SIMNETPEDALS: + simnetpedals_update(usbdevice, this->hapticeffect.effecttype, rplay, this->hapticeffect.motorposition, this->hapticeffect.basefrequency, this->hapticeffect.baseamplitude); + break; + } + } + else + { + switch ( usbdevice->type ) + { + case WHEELDEV_CSLELITEV3PEDALS: + cslelitev3_update(usbdevice, this->hapticeffect.effecttype, rplay); + break; + case WHEELDEV_SIMAGICP1000PEDALS: + simagicp1000_update(usbdevice, this->hapticeffect.effecttype, rplay); + break; + case WHEELDEV_SIMNETPEDALS: + simnetpedals_update(usbdevice, this->hapticeffect.effecttype, rplay, this->hapticeffect.motorposition, 0, 0); + break; + } + } + usbdevice->hapticstate = play; + } + return 0; +} + +int wheeldev_update(SimDevice* this, SimData* simdata) +{ + USBDevice* usbdevice = (void *) this->derived; + WheelDevice* wheeldevice = &usbdevice->u.wheeldevice; switch ( wheeldevice->type ) { @@ -72,6 +124,15 @@ int wheeldev_free(USBDevice* usbdevice) free(usbdevice->m.device_specific_config_file); } break; + case WHEELDEV_CSLELITEV3PEDALS: + cslelitev3_free(usbdevice); + break; + case WHEELDEV_SIMAGICP1000PEDALS: + simagicp1000_free(usbdevice); + break; + case WHEELDEV_SIMNETPEDALS: + simnetpedals_free(usbdevice); + break; } return 0; @@ -79,8 +140,9 @@ int wheeldev_free(USBDevice* usbdevice) int wheeldev_init(USBDevice* usbdevice, DeviceSettings* ds) { - slogi("initializing wheel device..."); + slogi("initializing wheel or pedals device..."); int error = 0; + // detection of wheel model WheelDevice* wheeldevice = &usbdevice->u.wheeldevice; switch (ds->dev_subsubtype) { @@ -138,11 +200,20 @@ int wheeldev_init(USBDevice* usbdevice, DeviceSettings* ds) error = -1; } break; - default: - wheeldevice->type = WHEELDEV_UNKNOWN; - slogw("Unknown cammus wheel detected, trying C5"); - error = cammusc5_init(usbdevice); + case (SIMDEVSUBTYPE_SIMAGICP1000PEDALS): + wheeldevice->type = USBHAPTIC_SIMAGICP1000PEDALS; + error = simagicp1000_init(usbdevice); break; + case (SIMDEVSUBTYPE_CSLELITEV3PEDALS): + wheeldevice->type = USBHAPTIC_CSLELITEV3PEDALS; + error = cslelitev3_init(usbdevice); + break; + case (SIMDEVSUBTYPE_SIMNETPEDALS): + wheeldevice->type = USBHAPTIC_SIMNETPEDALS; + error = simnetpedals_init(usbdevice); + break; + default: + slogw("Possibly unknown device"); } diff --git a/src/monocoque/devices/wheeldevice.h b/src/monocoque/devices/wheeldevice.h index 182f71f..8e96008 100644 --- a/src/monocoque/devices/wheeldevice.h +++ b/src/monocoque/devices/wheeldevice.h @@ -8,12 +8,15 @@ typedef enum { - WHEELDEV_UNKNOWN = 0, - WHEELDEV_CAMMUSC5 = 1, - WHEELDEV_CAMMUSC12 = 2, - WHEELDEV_MOZAR5 = 3, - WHEELDEV_SIMAGICGTNEO = 4, - WHEELDEV_LOGITECHG29 = 5 + WHEELDEV_UNKNOWN = 0, + WHEELDEV_CAMMUSC5 = 1, + WHEELDEV_CAMMUSC12 = 2, + WHEELDEV_MOZAR5 = 3, + WHEELDEV_SIMAGICGTNEO = 4, + WHEELDEV_LOGITECHG29 = 5, + WHEELDEV_CSLELITEV3PEDALS = 6, + WHEELDEV_SIMAGICP1000PEDALS = 7, + WHEELDEV_SIMNETPEDALS = 8 } WheelType; diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index c8eebc9..2a815d4 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -37,35 +37,35 @@ int strtoeffecttype(const char* effect, DeviceSettings* ds) if (strcicmp(effect, "Engine") == 0) { ds->is_valid = true; - ds->effect_type = EFFECT_ENGINERPM; + ds->hapticsettings.effect_type = EFFECT_ENGINERPM; } if (strcicmp(effect, "Gear") == 0) { ds->is_valid = true; - ds->effect_type = EFFECT_GEARSHIFT; + ds->hapticsettings.effect_type = EFFECT_GEARSHIFT; } if (strcicmp(effect, "Suspension") == 0) { ds->is_valid = true; - ds->effect_type = EFFECT_SUSPENSION; + ds->hapticsettings.effect_type = EFFECT_SUSPENSION; } if (strcicmp(effect, "ABS") == 0) { ds->is_valid = true; slogt("found abas effect set"); - ds->effect_type = EFFECT_ABSBRAKES; + ds->hapticsettings.effect_type = EFFECT_ABSBRAKES; } if ((strcicmp(effect, "SLIP") == 0) || (strcicmp(effect, "TYRESLIP") == 0) || (strcicmp(effect, "TIRESLIP") == 0)) { ds->is_valid = true; slogt("found tyreslip effect set"); - ds->effect_type = EFFECT_TYRESLIP; + ds->hapticsettings.effect_type = EFFECT_TYRESLIP; } if ((strcicmp(effect, "LOCK") == 0) || (strcicmp(effect, "TYRELOCK") == 0) || (strcicmp(effect, "TIRELOCK") == 0)) { ds->is_valid = true; slogt("found tyreslock effect set"); - ds->effect_type = EFFECT_TYRELOCK; + ds->hapticsettings.effect_type = EFFECT_TYRELOCK; } if (ds->is_valid == false) @@ -591,31 +591,31 @@ int gettyre(config_setting_t* device_settings, DeviceSettings* ds) { const char* temp; int found = config_setting_lookup_string(device_settings, "tyre", &temp); - ds->tyre = ALLFOUR; + ds->hapticsettings.tyre = ALLFOUR; if (strcicmp(temp, "FRONTS") == 0) { - ds->tyre = FRONTS; + ds->hapticsettings.tyre = FRONTS; } if (strcicmp(temp, "REARS") == 0) { - ds->tyre = REARS; + ds->hapticsettings.tyre = REARS; } if (strcicmp(temp, "FRONTLEFT") == 0) { - ds->tyre = FRONTLEFT; + ds->hapticsettings.tyre = FRONTLEFT; } if (strcicmp(temp, "FRONTRIGHT") == 0) { - ds->tyre = FRONTRIGHT; + ds->hapticsettings.tyre = FRONTRIGHT; } if (strcicmp(temp, "REARLEFT") == 0) { - ds->tyre = REARLEFT; + ds->hapticsettings.tyre = REARLEFT; } if (strcicmp(temp, "REARRIGHT") == 0) { - ds->tyre = REARRIGHT; + ds->hapticsettings.tyre = REARRIGHT; } } @@ -725,54 +725,91 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co } } - if (ds->dev_subtype == SIMDEVTYPE_USBHAPTIC || ds->dev_subtype == SIMDEVTYPE_USBWHEEL || ds->dev_subtype == SIMDEVTYPE_SERIALWHEEL) - { - // logic for different devices - int b = 0; - - const char* temp; - int found = config_setting_lookup_string(device_settings, "subtype", &temp); - if(temp != NULL && found > 0) - { - b = strtodevsubsubtype(temp, ds); - } - ds->usbdevsettings.frequency = 0; - ds->usbdevsettings.amplitude = 0; - ds->usbdevsettings.motorsposition = 0; - config_setting_lookup_int(device_settings, "frequency", &ds->usbdevsettings.frequency); - config_setting_lookup_int(device_settings, "amplitude", &ds->usbdevsettings.amplitude); - - int motorposition = 1; - config_setting_lookup_int(device_settings, "motors", &motorposition); - ds->usbdevsettings.motorsposition = motorposition; - } if (ds->dev_subtype == SIMDEVTYPE_USBHAPTIC || ds->dev_type == SIMDEV_SOUND || ds->dev_subtype == SIMDEVTYPE_SERIALHAPTIC) { slogt("analysing haptic effect settings"); + ds->has_haptic_effects = true; const char* effect; config_setting_lookup_string(device_settings, "effect", &effect); strtoeffecttype(effect, ds); - if (ds->effect_type == EFFECT_TYRESLIP || ds->effect_type == EFFECT_TYRELOCK || ds->effect_type == EFFECT_ABSBRAKES || ds->effect_type == EFFECT_SUSPENSION ) + if (ds->hapticsettings.effect_type == EFFECT_TYRESLIP || ds->hapticsettings.effect_type == EFFECT_TYRELOCK || ds->hapticsettings.effect_type == EFFECT_ABSBRAKES || ds->hapticsettings.effect_type == EFFECT_SUSPENSION ) { gettyre(device_settings, ds); ds->threshold = 0; int found = config_setting_lookup_float(device_settings, "threshold", &ds->threshold); } + slogi("reading configured haptic effect settings"); + ds->hapticsettings.frequency = 0; + ds->hapticsettings.frequencyMax = 0; + ds->hapticsettings.amplitude = 50; + ds->hapticsettings.amplitudeMax = 50; + ds->hapticsettings.volume = 0; + if (ds->hapticsettings.effect_type == EFFECT_GEARSHIFT) + { + ds->hapticsettings.duration = .125; + } + if (device_settings != NULL) + { + + config_setting_lookup_int(device_settings, "volume", &ds->hapticsettings.volume); + config_setting_lookup_int(device_settings, "frequency", &ds->hapticsettings.frequency); + config_setting_lookup_int(device_settings, "frequencyMax", &ds->hapticsettings.frequencyMax); + config_setting_lookup_int(device_settings, "amplitude", &ds->hapticsettings.amplitude); + config_setting_lookup_float(device_settings, "duration", &ds->sounddevsettings.duration); + config_setting_lookup_int(device_settings, "amplitudeMax", &ds->hapticsettings.amplitudeMax); + + const char* temp = NULL; + int found = 0; + found = config_setting_lookup_string(device_settings, "modulation", &temp); + ds->hapticsettings.modulation = EFFECT_MODULATION_NONE; + if (found == 0) + { + ds->hapticsettings.modulation = EFFECT_MODULATION_NONE; + slogd("Effect modulation not found, set to none"); + } + else + { + if(strcicmp(temp, "FREQUENCY") == 0) + { + ds->hapticsettings.modulation = EFFECT_MODULATION_FREQUENCY; + if(ds->hapticsettings.frequencyMax == 0 || ds->hapticsettings.frequencyMax < ds->hapticsettings.frequency) + { + ds->hapticsettings.modulation = EFFECT_MODULATION_NONE; + slogw("Falling back to no frequency modulation since frequencyMax is either not set or set below target frequency"); + } + else + { + slogi("Effect modulation found, set to FREQUENCY"); + } + } + else if(strcicmp(temp, "AMPLIFY") == 0) + { + ds->hapticsettings.modulation = EFFECT_MODULATION_AMPLIFY; + slogi("Effect modulation found, set to AMPLIFY"); + } + else + { + slogw("%s is not a valid modulation type, falling back to no effect modulation"); + ds->hapticsettings.modulation = EFFECT_MODULATION_NONE; + } + } + + ds->hapticsettings.motorposition = 0; + int motorposition = 1; + config_setting_lookup_int(device_settings, "motors", &motorposition); + ds->hapticsettings.motorposition = motorposition; + } + if (ds->dev_type == SIMDEV_SOUND) { slogi("reading configured sound device settings"); - ds->sounddevsettings.frequency = 0; - ds->sounddevsettings.frequencyMax = 0; - ds->sounddevsettings.amplitude = 50; - ds->sounddevsettings.amplitudeMax = 50; ds->sounddevsettings.volume = 0; ds->sounddevsettings.pan = 0; ds->sounddevsettings.channels = 1; - ds->sounddevsettings.duration = 2.0; ds->sounddevsettings.noise = 0; - if (ds->effect_type == EFFECT_GEARSHIFT) + if (ds->hapticsettings.effect_type == EFFECT_GEARSHIFT) { ds->sounddevsettings.duration = .125; } @@ -780,13 +817,8 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co { config_setting_lookup_int(device_settings, "volume", &ds->sounddevsettings.volume); - config_setting_lookup_int(device_settings, "frequency", &ds->sounddevsettings.frequency); - config_setting_lookup_int(device_settings, "frequencyMax", &ds->sounddevsettings.frequencyMax); - config_setting_lookup_int(device_settings, "amplitude", &ds->sounddevsettings.amplitude); - config_setting_lookup_int(device_settings, "amplitudeMax", &ds->sounddevsettings.amplitudeMax); config_setting_lookup_int(device_settings, "pan", &ds->sounddevsettings.pan); config_setting_lookup_int(device_settings, "channels", &ds->sounddevsettings.channels); - config_setting_lookup_float(device_settings, "duration", &ds->sounddevsettings.duration); config_setting_lookup_int(device_settings, "noise", &ds->sounddevsettings.noise); const char* temp = NULL; @@ -805,40 +837,8 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co } - found = config_setting_lookup_string(device_settings, "modulation", &temp); - ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_NONE; - if (found == 0) - { - ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_NONE; - slogd("Effect modulation not found, set to none"); - } - else - { - if(strcicmp(temp, "FREQUENCY") == 0) - { - ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_FREQUENCY; - if(ds->sounddevsettings.frequencyMax == 0 || ds->sounddevsettings.frequencyMax < ds->sounddevsettings.frequency) - { - ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_NONE; - slogw("Falling back to no frequency modulation since frequencyMax is either not set or set below target frequency"); - } - else - { - slogi("Effect modulation found, set to FREQUENCY"); - } - } - else if(strcicmp(temp, "AMPLIFY") == 0) - { - ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_AMPLIFY; - slogi("Effect modulation found, set to AMPLIFY"); - } - else - { - slogw("%s is not a valid modulation type, falling back to no effect modulation"); - ds->sounddevsettings.modulation = SOUND_EFFECT_MODULATION_NONE; - } - } } + } } diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index 5e8bb8b..1496ab4 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -74,6 +74,14 @@ typedef enum } VibrationEffectType; +typedef enum +{ + EFFECT_MODULATION_NONE = 0, + EFFECT_MODULATION_FREQUENCY = 1, + EFFECT_MODULATION_AMPLIFY = 2, +} +EffectModulationType; + typedef enum { MOTOR_1 = 0, @@ -177,29 +185,35 @@ SerialDeviceSettings; typedef struct { - uint32_t frequency; uint32_t volume; - int lowbound_frequency; - int upperbound_frequency; - uint32_t frequencyMax; - uint32_t amplitudeMax; - uint32_t amplitude; int pan; int channels; double duration; char* dev; - SoundEffectModulationType modulation; int noise; } SoundDeviceSettings; typedef struct { - int value0; - int value1; uint32_t frequency; uint32_t amplitude; - MotorPosition motorsposition; + uint32_t volume; + uint32_t frequencyMax; + uint32_t amplitudeMax; + + double threshold; + double duration; + + MotorPosition motorposition; + VibrationEffectType effect_type; + MonocoqueTyreIdentifier tyre; + EffectModulationType modulation; +} +HapticEffectSettings; + +typedef struct +{ char* dev; } USBDeviceSettings; @@ -211,14 +225,15 @@ typedef struct DeviceType dev_type; DeviceSubType dev_subtype; DeviceSubSubType dev_subsubtype; + bool has_haptic_effects; // to get really fancy move the effect information to it's own structure that would be a member of // any device settings member structure that can carry an effect - VibrationEffectType effect_type; - MonocoqueTyreIdentifier tyre; + double threshold; bool has_config; char* specific_config_file; // union? + HapticEffectSettings hapticsettings; TachometerSettings tachsettings; SerialDeviceSettings serialdevsettings; SoundDeviceSettings sounddevsettings;