From b99773a7ee0fb3b4cdb2cd1276c3348df28ba366 Mon Sep 17 00:00:00 2001 From: Paul Dino Jones Date: Wed, 5 Jun 2024 04:21:39 +0000 Subject: [PATCH] Refactor tyre effects to take threshold input from config file. Spell threshold correctly. --- src/monocoque/devices/hapticeffect.c | 18 +++++++++--------- src/monocoque/devices/hapticeffect.h | 2 +- src/monocoque/devices/simdevice.h | 1 + src/monocoque/devices/sounddevice.c | 17 +++++++++++------ src/monocoque/devices/sounddevice.h | 1 + src/monocoque/devices/usbhapticdevice.c | 4 ++-- src/monocoque/devices/usbhapticdevice.h | 1 + src/monocoque/helper/confighelper.c | 7 +++++-- src/monocoque/helper/confighelper.h | 6 +++++- 9 files changed, 36 insertions(+), 21 deletions(-) diff --git a/src/monocoque/devices/hapticeffect.c b/src/monocoque/devices/hapticeffect.c index 2a65cc1..72db458 100644 --- a/src/monocoque/devices/hapticeffect.c +++ b/src/monocoque/devices/hapticeffect.c @@ -42,7 +42,7 @@ void getTyreDiameter(SimData* simdata) } -int slipeffect(SimData* simdata, int effecttype, int tyre, double threshhold) +int slipeffect(SimData* simdata, int effecttype, int tyre, double threshold) { int play = 0; double wheelslip[4]; @@ -91,28 +91,28 @@ int slipeffect(SimData* simdata, int effecttype, int tyre, double threshhold) if (tyre == FRONTLEFT || tyre == FRONTS || tyre == ALLFOUR) { - if(wheelslip[0] < -.5) + if(wheelslip[0] < -threshold) { play++; } } if (tyre == FRONTRIGHT || tyre == FRONTS || tyre == ALLFOUR) { - if(wheelslip[1] < -.5) + if(wheelslip[1] < -threshold) { play++; } } if (tyre == REARLEFT || tyre == REARS || tyre == ALLFOUR) { - if(wheelslip[2] < -.5) + if(wheelslip[2] < -threshold) { play++; } } if (tyre == REARRIGHT || tyre == REARS || tyre == ALLFOUR) { - if(wheelslip[3] < -.5) + if(wheelslip[3] < -threshold) { play++; } @@ -123,28 +123,28 @@ int slipeffect(SimData* simdata, int effecttype, int tyre, double threshhold) case (EFFECT_TYRELOCK): if (tyre == FRONTLEFT || tyre == FRONTS || tyre == ALLFOUR) { - if(wheelslip[0] > .75) + if(wheelslip[0] > threshold) { play++; } } if (tyre == FRONTRIGHT || tyre == FRONTS || tyre == ALLFOUR) { - if(wheelslip[1] > .75) + if(wheelslip[1] > threshold) { play++; } } if (tyre == REARLEFT || tyre == REARS || tyre == ALLFOUR) { - if(wheelslip[2] > .75) + if(wheelslip[2] > threshold) { play++; } } if (tyre == REARRIGHT || tyre == REARS || tyre == ALLFOUR) { - if(wheelslip[3] > .75) + if(wheelslip[3] > threshold) { play++; } diff --git a/src/monocoque/devices/hapticeffect.h b/src/monocoque/devices/hapticeffect.h index ebabd03..2f7150e 100644 --- a/src/monocoque/devices/hapticeffect.h +++ b/src/monocoque/devices/hapticeffect.h @@ -3,6 +3,6 @@ #include "../simulatorapi/simapi/simapi/simdata.h" -int slipeffect(SimData* simdata, int effecttype, int tyre, double threshhold); +int slipeffect(SimData* simdata, int effecttype, int tyre, double threshold); #endif diff --git a/src/monocoque/devices/simdevice.h b/src/monocoque/devices/simdevice.h index 23f6071..1b1ca63 100644 --- a/src/monocoque/devices/simdevice.h +++ b/src/monocoque/devices/simdevice.h @@ -95,6 +95,7 @@ typedef struct int id; SoundType type; VibrationEffectType effecttype; + double slipthreshold; SoundData sounddata; #ifdef USE_PULSEAUDIO pa_stream *stream; diff --git a/src/monocoque/devices/sounddevice.c b/src/monocoque/devices/sounddevice.c index 7ed8ad5..a132c7e 100644 --- a/src/monocoque/devices/sounddevice.c +++ b/src/monocoque/devices/sounddevice.c @@ -14,8 +14,6 @@ #include "../helper/parameters.h" #include "../slog/slog.h" -#define slipthreshold 0.75 - int gear_sound_set(SoundDevice* sounddevice, SimData* simdata) { if (sounddevice->sounddata.last_gear != simdata->gear && simdata->gear > 1) @@ -44,7 +42,7 @@ int sounddev_tyreslip_update(SimDevice* this, SimData* simdata) { SoundDevice* sounddevice = (void *) this->derived; - int play = slipeffect(simdata, sounddevice->effecttype, this->tyre, slipthreshold); + int play = slipeffect(simdata, sounddevice->effecttype, this->tyre, sounddevice->slipthreshold); if (play > 0) { @@ -62,7 +60,7 @@ int sounddev_tyrelock_update(SimDevice* this, SimData* simdata) { SoundDevice* sounddevice = (void *) this->derived; - int play = slipeffect(simdata, sounddevice->effecttype, this->tyre, slipthreshold); + int play = slipeffect(simdata, sounddevice->effecttype, this->tyre, sounddevice->slipthreshold); if (play > 0) { @@ -110,7 +108,7 @@ int sounddev_free(SimDevice* this) return 0; } -int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int frequency, int pan, double duration) +int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int frequency, int pan, double duration, double threshold) { slogi("initializing standalone sound device..."); @@ -147,8 +145,15 @@ int sounddev_init(SoundDevice* sounddevice, const char* devname, int volume, int case (EFFECT_TYRESLIP): sounddevice->sounddata.duration = duration; sounddevice->sounddata.curr_duration = duration; + sounddevice->slipthreshold = threshold; streamname = "TyreSlip"; break; + case (EFFECT_TYRELOCK): + sounddevice->sounddata.duration = duration; + sounddevice->sounddata.curr_duration = duration; + sounddevice->slipthreshold = threshold; + streamname = "TyreLock"; + break; case (EFFECT_ABSBRAKES): sounddevice->sounddata.duration = duration; sounddevice->sounddata.curr_duration = duration; @@ -212,7 +217,7 @@ SoundDevice* new_sound_device(DeviceSettings* ds) { slogt("Attempting to use device %s", ds->sounddevsettings.dev); - int error = sounddev_init(this, ds->sounddevsettings.dev, ds->sounddevsettings.volume, ds->sounddevsettings.frequency, ds->sounddevsettings.pan, ds->sounddevsettings.duration); + int error = sounddev_init(this, ds->sounddevsettings.dev, ds->sounddevsettings.volume, ds->sounddevsettings.frequency, ds->sounddevsettings.pan, ds->sounddevsettings.duration, ds->threshold); if (error != 0) { free(this); diff --git a/src/monocoque/devices/sounddevice.h b/src/monocoque/devices/sounddevice.h index 00eb949..0f28aff 100644 --- a/src/monocoque/devices/sounddevice.h +++ b/src/monocoque/devices/sounddevice.h @@ -20,6 +20,7 @@ typedef struct int last_gear; int volume; int frequency; + double duration; int curr_frequency; double curr_duration; diff --git a/src/monocoque/devices/usbhapticdevice.c b/src/monocoque/devices/usbhapticdevice.c index 817b713..a0aba4f 100644 --- a/src/monocoque/devices/usbhapticdevice.c +++ b/src/monocoque/devices/usbhapticdevice.c @@ -9,12 +9,11 @@ #include "../../simulatorapi/simapi/simapi/simdata.h" #include "../../slog/slog.h" -#define slipthreshold 0.75 int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdata) { - int play = slipeffect(simdata, usbhapticdevice->effecttype, usbhapticdevice->tyre, slipthreshold); + int play = slipeffect(simdata, usbhapticdevice->effecttype, usbhapticdevice->tyre, usbhapticdevice->threshold); if (play != usbhapticdevice->state) @@ -54,6 +53,7 @@ int usbhapticdev_init(USBGenericHapticDevice* usbhapticdevice, DeviceSettings* d usbhapticdevice->state = usbhapticdevice->value0; usbhapticdevice->tyre = ds->tyre; usbhapticdevice->effecttype = ds->effect_type; + usbhapticdevice->threshold = ds->threshold; usbhapticdevice->handle = fopen(usbhapticdevice->dev, "w"); if(usbhapticdevice->handle == 0) diff --git a/src/monocoque/devices/usbhapticdevice.h b/src/monocoque/devices/usbhapticdevice.h index 46e16f1..f86b43d 100644 --- a/src/monocoque/devices/usbhapticdevice.h +++ b/src/monocoque/devices/usbhapticdevice.h @@ -18,6 +18,7 @@ typedef struct int id; HapticType type; double state; + double threshold; int value0; int value1; VibrationEffectType effecttype; diff --git a/src/monocoque/helper/confighelper.c b/src/monocoque/helper/confighelper.c index 3634468..d8ab45a 100644 --- a/src/monocoque/helper/confighelper.c +++ b/src/monocoque/helper/confighelper.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -357,11 +358,13 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co 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) + if (ds->effect_type == EFFECT_TYRESLIP || ds->effect_type == EFFECT_TYRELOCK || ds->effect_type == EFFECT_ABSBRAKES) { gettyre(device_settings, ds); + ds->threshold = .75; + int found = config_setting_lookup_float(device_settings, "threshold", &ds->threshold); } - + if (ds->dev_type == SIMDEV_SOUND) { slogi("reading configured sound device settings"); diff --git a/src/monocoque/helper/confighelper.h b/src/monocoque/helper/confighelper.h index 0cf85da..f7f98cd 100644 --- a/src/monocoque/helper/confighelper.h +++ b/src/monocoque/helper/confighelper.h @@ -121,13 +121,17 @@ typedef struct bool is_valid; DeviceType dev_type; DeviceSubType dev_subtype; + // 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; // union? TachometerSettings tachsettings; SerialDeviceSettings serialdevsettings; SoundDeviceSettings sounddevsettings; USBDeviceSettings usbdevsettings; - MonocoqueTyreIdentifier tyre; + } DeviceSettings;