Skip to content

Commit 99bb88a

Browse files
committed
Refactor to provide siminfo to help decide if and how a device can be initialize
1 parent 0a35099 commit 99bb88a

12 files changed

Lines changed: 163 additions & 124 deletions

File tree

src/monocoque/devices/serialdevice.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ int serial_wheel_free(SimDevice* this)
219219
return 0;
220220
}
221221

222-
int serialdev_init(SerialDevice* serialdevice, DeviceSettings* ds)
222+
int serialdev_init(SerialDevice* serialdevice, DeviceSettings* ds, SimInfo* siminfo)
223223
{
224224
slogi("initializing serial device on port %s to %i...", ds->serialdevsettings.portdev, ds->serialdevsettings.baud);
225225
int error = 0;
@@ -276,7 +276,7 @@ static const vtable arduino_simwind_vtable = { &arduino_simwind_update, &seriald
276276
static const vtable arduino_simhaptic_vtable = { &arduino_simhaptic_update, &serialdev_free };
277277
static const vtable serialwheel_vtable = { &serial_wheel_update, &serial_wheel_free };
278278

279-
SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) {
279+
SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms, SimInfo* siminfo) {
280280

281281
SerialDevice* this = (SerialDevice*) malloc(sizeof(SerialDevice));
282282

@@ -285,6 +285,7 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) {
285285
this->m.derived = this;
286286
this->m.vtable = &serial_simdevice_vtable;
287287
this->type = SERIALDEV_ARDUINO;
288+
int error = 0;
288289

289290
slogt("Attempting to configure arduino device with subtype: %i", ds->dev_subtype);
290291
switch (ds->dev_subtype) {
@@ -325,6 +326,10 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) {
325326
slogi("Initializing custom arduino device.");
326327
break;
327328
case (SIMDEVTYPE_SERIALHAPTIC):
329+
if(siminfo->SimSupportsHapticEffects == false)
330+
{
331+
error = MONOCOQUE_ERROR_UNSUPPORTED_SIM_FEATURE;
332+
}
328333
this->devicetype = ARDUINODEV__HAPTIC;
329334
this->m.vtable = &arduino_simhaptic_vtable;
330335
this->u.simhapticdata.motor1 = 0;
@@ -359,7 +364,7 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) {
359364
}
360365
}
361366

362-
if(this->devicetype == ARDUINODEV__HAPTIC)
367+
if(this->devicetype == ARDUINODEV__HAPTIC && error == 0)
363368
{
364369
this->m.hapticeffect.threshold = ds->threshold;
365370
this->m.hapticeffect.effecttype = ds->effect_type;
@@ -370,10 +375,15 @@ SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms) {
370375
this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config;
371376
}
372377

373-
int error = serialdev_init(this, ds);
378+
if(error == 0)
379+
{
380+
slogi("Starting serial device initialization");
381+
error = serialdev_init(this, ds, siminfo);
382+
}
374383

375-
if (error < 0)
384+
if (error != 0)
376385
{
386+
slogw("Did not initialize usb device due to error code %i", error);
377387
free(this);
378388
return NULL;
379389
}

src/monocoque/devices/simdevice.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,17 @@ int devupdate(SimDevice* this, SimData* simdata)
2424
return 0;
2525
}
2626

27-
int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds, MonocoqueSettings* ms)
27+
int devinit(SimDevice* simdevices, SimInfo* siminfo, int numdevices, DeviceSettings* ds, MonocoqueSettings* ms)
2828
{
29-
slogi("initializing simdevices...");
29+
slogi("initializing simdevices for simapi %i...", siminfo->simulatorapi);
3030
int devices = 0;
3131

3232
for (int j = 0; j < numdevices; j++)
3333
{
3434
simdevices[j].initialized = false;
3535

3636
if (ds[j].dev_type == SIMDEV_USB) {
37-
USBDevice* sim = new_usb_device(&ds[j], ms);
37+
USBDevice* sim = new_usb_device(&ds[j], ms, siminfo);
3838
if (sim != NULL)
3939
{
4040
simdevices[j] = sim->m;
@@ -56,7 +56,7 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds, Monocoque
5656
}
5757
else
5858
{
59-
SoundDevice* sim = new_sound_device(&ds[j], ms);
59+
SoundDevice* sim = new_sound_device(&ds[j], ms, siminfo);
6060
if (sim != NULL)
6161
{
6262

@@ -75,7 +75,7 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds, Monocoque
7575

7676
if (ds[j].dev_type == SIMDEV_SERIAL) {
7777

78-
SerialDevice* sim = new_serial_device(&ds[j], ms);
78+
SerialDevice* sim = new_serial_device(&ds[j], ms, siminfo);
7979
if (sim != NULL)
8080
{
8181
simdevices[j] = sim->m;

src/monocoque/devices/simdevice.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ int arduino_simwind_update(SimDevice* this, SimData* simdata);
8282
int arduino_simhaptic_update(SimDevice* this, SimData* simdata);
8383
int serialdev_free(SimDevice* this);
8484

85-
SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms);
85+
SerialDevice* new_serial_device(DeviceSettings* ds, MonocoqueSettings* ms, SimInfo* siminfo);
8686

8787
/********* USB HID Devices *****/
8888
typedef enum
@@ -112,7 +112,7 @@ USBDevice;
112112
int usbdev_update(SimDevice* this, SimData* simdata);
113113
int usbdev_free(SimDevice* this);
114114

115-
USBDevice* new_usb_device(DeviceSettings* ds, MonocoqueSettings* ms);
115+
USBDevice* new_usb_device(DeviceSettings* ds, MonocoqueSettings* ms, SimInfo* siminfo);
116116

117117

118118
/********* Sound Devices *****/
@@ -137,15 +137,15 @@ int sounddev_gearshift_update(SimDevice* this, SimData* simdata);
137137
int sounddev_tyreslip_update(SimDevice* this, SimData* simdata);
138138
int sounddev_free(SimDevice* this);
139139

140-
SoundDevice* new_sound_device(DeviceSettings* ds, MonocoqueSettings* ms);
140+
SoundDevice* new_sound_device(DeviceSettings* ds, MonocoqueSettings* ms, SimInfo* siminfo);
141141

142142
/***** Generic Methods *********/
143143

144144
int update(SimDevice* simdevice, SimData* simdata);
145145

146146
int devupdate(SimDevice* simdevice, SimData* simdata);
147147

148-
int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds, MonocoqueSettings* ms);
148+
int devinit(SimDevice* simdevices, SimInfo* siminfo, int numdevices, DeviceSettings* ds, MonocoqueSettings* ms);
149149

150150
int devfree(SimDevice* simdevices, int numdevices);
151151

src/monocoque/devices/sounddevice.c

Lines changed: 92 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -231,88 +231,112 @@ static const vtable tyrelock_sound_simdevice_vtable = { &sounddev_tyrelock_updat
231231
static const vtable absbrakes_sound_simdevice_vtable = { &sounddev_absbrakes_update, &sounddev_free };
232232
static const vtable suspension_sound_simdevice_vtable = { &sounddev_suspension_update, &sounddev_free };
233233

234-
SoundDevice* new_sound_device(DeviceSettings* ds, MonocoqueSettings* ms) {
234+
SoundDevice* new_sound_device(DeviceSettings* ds, MonocoqueSettings* ms, SimInfo* siminfo) {
235235

236236
SoundDevice* this = (SoundDevice*) malloc(sizeof(SoundDevice));
237237

238238
this->m.update = &update;
239239
this->m.free = &simdevfree;
240240
this->m.derived = this;
241+
int error = 0;
241242

242-
slogt("Attempting to configure sound device with subtype: %i", ds->effect_type);
243-
switch (ds->effect_type) {
244-
case (EFFECT_ENGINERPM):
245-
this->m.hapticeffect.effecttype = EFFECT_ENGINERPM;
246-
this->m.vtable = &engine_sound_simdevice_vtable;
247-
slogi("Initializing sound device for engine vibrations.");
248-
break;
249-
case (EFFECT_GEARSHIFT):
250-
this->m.hapticeffect.effecttype = EFFECT_GEARSHIFT;
251-
this->m.vtable = &gear_sound_simdevice_vtable;
252-
slogi("Initializing sound device for gear shift vibrations.");
253-
break;
243+
switch (ds->effect_type)
244+
{
254245
case (EFFECT_TYRESLIP):
255-
this->m.hapticeffect.effecttype = EFFECT_TYRESLIP;
256-
this->m.hapticeffect.threshold = ds->threshold;
257-
258-
this->m.hapticeffect.threshold = ds->threshold;
259-
slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->effect_type);
260-
this->m.hapticeffect.tyre = ds->tyre;
261-
this->m.hapticeffect.useconfig = ms->useconfig;
262-
this->m.hapticeffect.configcheck = &ms->configcheck;
263-
this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config;
264-
265-
this->m.vtable = &tyreslip_sound_simdevice_vtable;
266-
slogi("Initializing sound device for tyre slip vibrations.");
267-
break;
268246
case (EFFECT_TYRELOCK):
269-
this->m.hapticeffect.effecttype = EFFECT_TYRELOCK;
270-
this->m.hapticeffect.threshold = ds->threshold;
271-
272-
this->m.hapticeffect.threshold = ds->threshold;
273-
slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->effect_type);
274-
this->m.hapticeffect.tyre = ds->tyre;
275-
this->m.hapticeffect.useconfig = ms->useconfig;
276-
this->m.hapticeffect.configcheck = &ms->configcheck;
277-
this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config;
278-
279-
this->m.vtable = &tyrelock_sound_simdevice_vtable;
280-
slogi("Initializing sound device for tyre slip vibrations.");
281-
break;
282247
case (EFFECT_ABSBRAKES):
283-
this->m.hapticeffect.effecttype = EFFECT_ABSBRAKES;
284-
this->m.hapticeffect.threshold = ds->threshold;
285-
286-
this->m.hapticeffect.threshold = ds->threshold;
287-
slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->effect_type);
288-
this->m.hapticeffect.tyre = ds->tyre;
289-
this->m.hapticeffect.useconfig = ms->useconfig;
290-
this->m.hapticeffect.configcheck = &ms->configcheck;
291-
this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config;
292-
293-
this->m.vtable = &absbrakes_sound_simdevice_vtable;
294-
slogi("Initializing sound device for abs vibrations.");
295-
break;
296-
297248
case (EFFECT_SUSPENSION):
298-
this->m.hapticeffect.effecttype = EFFECT_SUSPENSION;
299-
this->m.hapticeffect.threshold = ds->threshold;
300-
301-
this->m.hapticeffect.threshold = ds->threshold;
302-
slogt("Haptic effect: %i %i on tyre %i", this->m.hapticeffect.effecttype, ds->effect_type, ds->tyre);
303-
this->m.hapticeffect.tyre = ds->tyre;
304-
this->m.hapticeffect.useconfig = ms->useconfig;
305-
this->m.hapticeffect.configcheck = &ms->configcheck;
306-
this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config;
307-
308-
this->m.vtable = &suspension_sound_simdevice_vtable;
309-
slogi("Initializing sound device for suspension vibrations.");
310-
break;
249+
if(siminfo->SimSupportsHapticEffects == false)
250+
{
251+
slogw("Skipping sound effect setup because sim does not support haptic effects");
252+
error = MONOCOQUE_ERROR_UNSUPPORTED_SIM_FEATURE;
253+
}
254+
defaut:
255+
error = 0;
256+
}
257+
258+
259+
if(error == 0)
260+
{
261+
slogt("Attempting to configure sound device with subtype: %i", ds->effect_type);
262+
switch (ds->effect_type)
263+
{
264+
case (EFFECT_ENGINERPM):
265+
this->m.hapticeffect.effecttype = EFFECT_ENGINERPM;
266+
this->m.vtable = &engine_sound_simdevice_vtable;
267+
slogi("Initializing sound device for engine vibrations.");
268+
break;
269+
case (EFFECT_GEARSHIFT):
270+
this->m.hapticeffect.effecttype = EFFECT_GEARSHIFT;
271+
this->m.vtable = &gear_sound_simdevice_vtable;
272+
slogi("Initializing sound device for gear shift vibrations.");
273+
break;
274+
case (EFFECT_TYRESLIP):
275+
this->m.hapticeffect.effecttype = EFFECT_TYRESLIP;
276+
this->m.hapticeffect.threshold = ds->threshold;
277+
278+
this->m.hapticeffect.threshold = ds->threshold;
279+
slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->effect_type);
280+
this->m.hapticeffect.tyre = ds->tyre;
281+
this->m.hapticeffect.useconfig = ms->useconfig;
282+
this->m.hapticeffect.configcheck = &ms->configcheck;
283+
this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config;
284+
285+
this->m.vtable = &tyreslip_sound_simdevice_vtable;
286+
slogi("Initializing sound device for tyre slip vibrations.");
287+
break;
288+
case (EFFECT_TYRELOCK):
289+
this->m.hapticeffect.effecttype = EFFECT_TYRELOCK;
290+
this->m.hapticeffect.threshold = ds->threshold;
291+
292+
this->m.hapticeffect.threshold = ds->threshold;
293+
slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->effect_type);
294+
this->m.hapticeffect.tyre = ds->tyre;
295+
this->m.hapticeffect.useconfig = ms->useconfig;
296+
this->m.hapticeffect.configcheck = &ms->configcheck;
297+
this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config;
298+
299+
this->m.vtable = &tyrelock_sound_simdevice_vtable;
300+
slogi("Initializing sound device for tyre slip vibrations.");
301+
break;
302+
case (EFFECT_ABSBRAKES):
303+
this->m.hapticeffect.effecttype = EFFECT_ABSBRAKES;
304+
this->m.hapticeffect.threshold = ds->threshold;
305+
306+
this->m.hapticeffect.threshold = ds->threshold;
307+
slogt("Haptic effect: %i %i", this->m.hapticeffect.effecttype, ds->effect_type);
308+
this->m.hapticeffect.tyre = ds->tyre;
309+
this->m.hapticeffect.useconfig = ms->useconfig;
310+
this->m.hapticeffect.configcheck = &ms->configcheck;
311+
this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config;
312+
313+
this->m.vtable = &absbrakes_sound_simdevice_vtable;
314+
slogi("Initializing sound device for abs vibrations.");
315+
break;
316+
317+
case (EFFECT_SUSPENSION):
318+
this->m.hapticeffect.effecttype = EFFECT_SUSPENSION;
319+
this->m.hapticeffect.threshold = ds->threshold;
320+
321+
this->m.hapticeffect.threshold = ds->threshold;
322+
slogt("Haptic effect: %i %i on tyre %i", this->m.hapticeffect.effecttype, ds->effect_type, ds->tyre);
323+
this->m.hapticeffect.tyre = ds->tyre;
324+
this->m.hapticeffect.useconfig = ms->useconfig;
325+
this->m.hapticeffect.configcheck = &ms->configcheck;
326+
this->m.hapticeffect.tyrediameterconfig = ms->tyre_diameter_config;
327+
328+
this->m.vtable = &suspension_sound_simdevice_vtable;
329+
slogi("Initializing sound device for suspension vibrations.");
330+
break;
331+
}
311332
}
312333

313-
slogt("Attempting to use sound device %s", ds->sounddevsettings.dev);
334+
if(error == 0)
335+
{
336+
slogt("Attempting to use sound device %s", ds->sounddevsettings.dev);
337+
error = sounddev_init(this, ds->sounddevsettings.dev, ds->tyre, ds->sounddevsettings);
338+
}
314339

315-
int error = sounddev_init(this, ds->sounddevsettings.dev, ds->tyre, ds->sounddevsettings);
316340
if (error != 0)
317341
{
318342
free(this);

src/monocoque/devices/usbdevice.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ int usbdev_free(SimDevice* this)
5454
return 0;
5555
}
5656

57-
int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds)
57+
int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds, SimInfo* siminfo)
5858
{
5959
slogi("initializing usb device...");
6060
int error = 0;
@@ -70,7 +70,7 @@ int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds)
7070
error = wheeldev_init(usbdevice, ds);
7171
break;
7272
case USBDEV_GENERICHAPTIC :
73-
error = usbhapticdev_init(&usbdevice->u.hapticdevice, ds);
73+
error = usbhapticdev_init(&usbdevice->u.hapticdevice, ds, siminfo);
7474
break;
7575
}
7676

@@ -79,7 +79,7 @@ int usbdev_init(USBDevice* usbdevice, DeviceSettings* ds)
7979

8080
static const vtable usb_simdevice_vtable = { &usbdev_update, &usbdev_free };
8181

82-
USBDevice* new_usb_device(DeviceSettings* ds, MonocoqueSettings* ms) {
82+
USBDevice* new_usb_device(DeviceSettings* ds, MonocoqueSettings* ms, SimInfo* siminfo) {
8383

8484
USBDevice* this = (USBDevice*) malloc(sizeof(USBDevice));
8585

@@ -112,10 +112,11 @@ USBDevice* new_usb_device(DeviceSettings* ds, MonocoqueSettings* ms) {
112112
this->type = USBDEV_GENERICHAPTIC;
113113
}
114114

115-
int error = usbdev_init(this, ds);
115+
int error = usbdev_init(this, ds, siminfo);
116116

117117
if (error != 0)
118118
{
119+
slogw("Did not initialize usb device due to error code %i", error);
119120
free(this);
120121
return NULL;
121122
}

src/monocoque/devices/usbhapticdevice.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,14 @@ int usbhapticdev_free(USBGenericHapticDevice* usbhapticdevice)
6767
return 0;
6868
}
6969

70-
int usbhapticdev_init(USBGenericHapticDevice* usbhapticdevice, DeviceSettings* ds)
70+
int usbhapticdev_init(USBGenericHapticDevice* usbhapticdevice, DeviceSettings* ds, SimInfo* siminfo)
7171
{
72+
if(siminfo->SimSupportsHapticEffects == false)
73+
{
74+
slogi("This sim does not support haptic effects");
75+
return MONOCOQUE_ERROR_UNSUPPORTED_SIM_FEATURE;
76+
}
77+
7278
int error = 0;
7379
usbhapticdevice->state = 0;
7480
usbhapticdevice->value0 = ds->usbdevsettings.value0;

src/monocoque/devices/usbhapticdevice.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ USBGenericHapticDevice;
3131

3232

3333
int usbhapticdev_update(USBGenericHapticDevice* hapticdevice, SimData* simdata, int tyre, int useconfig, int* configcheck, char* configfile);
34-
int usbhapticdev_init(USBGenericHapticDevice* hapticdevice, DeviceSettings* ds);
34+
int usbhapticdev_init(USBGenericHapticDevice* hapticdevice, DeviceSettings* ds, SimInfo* siminfo);
3535
int usbhapticdev_free(USBGenericHapticDevice* hapticdevice);
3636

3737
#endif

0 commit comments

Comments
 (0)