Add support for save file for tyre diameters
This commit is contained in:
parent
a26b090935
commit
4ad6a1339c
|
|
@ -37,10 +37,10 @@ add_executable(monocoque src/monocoque/monocoque.c)
|
|||
if(USE_PULSEAUDIO)
|
||||
message("Using pulseaudio backend...")
|
||||
add_compile_definitions(USE_PULSEAUDIO=true)
|
||||
target_link_libraries(monocoque m ${LIBUSB_LIBRARY} hidapi-libusb pulse serialport xml2 argtable2 config gameloop helper devices slog simulatorapi)
|
||||
target_link_libraries(monocoque m ${LIBUSB_LIBRARY} hidapi-libusb pulse serialport xml2 argtable2 jansson config gameloop helper devices slog simulatorapi)
|
||||
else()
|
||||
message("Using portaudio backend...")
|
||||
target_link_libraries(monocoque m ${LIBUSB_LIBRARY} hidapi-libusb portaudio serialport xml2 argtable2 config gameloop helper devices slog simulatorapi)
|
||||
target_link_libraries(monocoque m ${LIBUSB_LIBRARY} hidapi-libusb portaudio serialport xml2 argtable2 jansson config gameloop helper devices slog simulatorapi)
|
||||
endif()
|
||||
|
||||
target_include_directories(monocoque PUBLIC config ${LIBUSB_INCLUDE_DIR} ${LIBXML_INCLUDE_DIR})
|
||||
|
|
|
|||
|
|
@ -1,7 +1,9 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <fcntl.h>
|
||||
#include <jansson.h>
|
||||
|
||||
#include "usbhapticdevice.h"
|
||||
#include "../../helper/confighelper.h"
|
||||
|
|
@ -15,6 +17,7 @@
|
|||
#define maxthrottle 0
|
||||
#define maxXvelocity 0.001
|
||||
|
||||
|
||||
bool hasTyreDiameter(SimData* simdata)
|
||||
{
|
||||
if (simdata->tyrediameter[0] == -1 || simdata->tyrediameter[1] == -1 || simdata->tyrediameter[2] == -1 || simdata->tyrediameter[3] == -1)
|
||||
|
|
@ -24,6 +27,145 @@ bool hasTyreDiameter(SimData* simdata)
|
|||
return true;
|
||||
}
|
||||
|
||||
void loadtyreconfig(SimData* simdata, FILE* configfile)
|
||||
{
|
||||
json_t *root;
|
||||
json_error_t error;
|
||||
|
||||
root = json_loadf(configfile, 0, NULL);
|
||||
|
||||
if(!root)
|
||||
{
|
||||
slogw("could not open config file for tyre diameters");
|
||||
return;
|
||||
}
|
||||
|
||||
if(!json_is_object(root))
|
||||
{
|
||||
slogw("Malformed content (1) in tyre diameters config");
|
||||
json_decref(root);
|
||||
return;
|
||||
}
|
||||
|
||||
json_t* cararray = json_object_get(root, "cars");
|
||||
if(!json_is_array(cararray))
|
||||
{
|
||||
slogw("Malformed content (2) in tyre diameters config");
|
||||
json_decref(root);
|
||||
json_decref(cararray);
|
||||
return;
|
||||
}
|
||||
|
||||
for(int i = 0; i < json_array_size(cararray); i++)
|
||||
{
|
||||
json_t *data, *sha, *commit, *message;
|
||||
const char *message_text;
|
||||
|
||||
data = json_array_get(cararray, i);
|
||||
if(!json_is_object(data))
|
||||
{
|
||||
slogw("Malformed content (3) in tyre diameters config");
|
||||
break;
|
||||
}
|
||||
json_t* jcar = json_object_get(data, "car");
|
||||
|
||||
if(!json_is_string(jcar))
|
||||
{
|
||||
slogw("Malformed content (4) in tyre diameters config");
|
||||
|
||||
json_decref(data);
|
||||
json_decref(jcar);
|
||||
break;
|
||||
}
|
||||
const char* car = json_string_value(jcar);
|
||||
|
||||
json_t* jtyre0 = json_object_get(data, "tyre0");
|
||||
json_t* jtyre1 = json_object_get(data, "tyre1");
|
||||
json_t* jtyre2 = json_object_get(data, "tyre2");
|
||||
json_t* jtyre3 = json_object_get(data, "tyre3");
|
||||
|
||||
double diameter0 = json_real_value(jtyre0);
|
||||
double diameter1 = json_real_value(jtyre1);
|
||||
double diameter2 = json_real_value(jtyre2);
|
||||
double diameter3 = json_real_value(jtyre3);
|
||||
|
||||
slogt("car is: %s", car);
|
||||
slogt("diameter 0 is %f", diameter0);
|
||||
slogt("diameter 1 is %f", diameter1);
|
||||
slogt("diameter 2 is %f", diameter2);
|
||||
slogt("diameter 3 is %f", diameter3);
|
||||
|
||||
json_decref(data);
|
||||
json_decref(jcar);
|
||||
json_decref(jtyre0);
|
||||
json_decref(jtyre1);
|
||||
json_decref(jtyre2);
|
||||
json_decref(jtyre3);
|
||||
|
||||
if(simdata->car != NULL)
|
||||
{
|
||||
if (strcicmp(car, simdata->car) == 0)
|
||||
{
|
||||
simdata->tyrediameter[0] = diameter0;
|
||||
simdata->tyrediameter[1] = diameter1;
|
||||
simdata->tyrediameter[2] = diameter2;
|
||||
simdata->tyrediameter[3] = diameter3;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
json_decref(root);
|
||||
json_decref(cararray);
|
||||
}
|
||||
|
||||
void savetyreconfig(SimData* simdata, FILE* configfile)
|
||||
{
|
||||
|
||||
json_t* object = json_object();
|
||||
json_t* array = json_array();
|
||||
json_t* car = json_string(simdata->car);
|
||||
json_t* tyre0 = json_real(simdata->tyrediameter[0]);
|
||||
json_t* tyre1 = json_real(simdata->tyrediameter[1]);
|
||||
json_t* tyre2 = json_real(simdata->tyrediameter[2]);
|
||||
json_t* tyre3 = json_real(simdata->tyrediameter[3]);
|
||||
json_object_set_new(object, "car", car);
|
||||
json_object_set_new(object, "tyre0", tyre0);
|
||||
json_object_set_new(object, "tyre1", tyre1);
|
||||
json_object_set_new(object, "tyre2", tyre2);
|
||||
json_object_set_new(object, "tyre3", tyre3);
|
||||
|
||||
json_array_append(array, object);
|
||||
|
||||
json_t* file = json_loadf(configfile, 0, NULL);
|
||||
|
||||
if(!file)
|
||||
{
|
||||
slogw("could not open config file for tyre diameters");
|
||||
return;
|
||||
}
|
||||
|
||||
if(!json_is_object(file))
|
||||
{
|
||||
json_object_set_new(file, "cars", array);
|
||||
}
|
||||
else
|
||||
{
|
||||
json_t* cararray = json_object_get(file, "cars");
|
||||
json_array_append(cararray, array);
|
||||
}
|
||||
|
||||
json_dumpf(file, configfile, 0);
|
||||
|
||||
json_decref(tyre0);
|
||||
json_decref(tyre1);
|
||||
json_decref(tyre2);
|
||||
json_decref(tyre3);
|
||||
json_decref(car);
|
||||
json_decref(object);
|
||||
json_decref(array);
|
||||
}
|
||||
|
||||
void getTyreDiameter(SimData* simdata)
|
||||
{
|
||||
if(simdata->velocity > minvelocity && simdata->brake <= maxbrake && simdata->gas <= maxthrottle)
|
||||
|
|
@ -42,7 +184,7 @@ void getTyreDiameter(SimData* simdata)
|
|||
}
|
||||
|
||||
|
||||
int slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, int* configcheck)
|
||||
int slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, int useconfig, int* configcheck, FILE* configfile)
|
||||
{
|
||||
int play = 0;
|
||||
double wheelslip[4];
|
||||
|
|
@ -51,7 +193,6 @@ int slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, int
|
|||
wheelslip[2] = 0;
|
||||
wheelslip[3] = 0;
|
||||
|
||||
|
||||
switch (effecttype)
|
||||
{
|
||||
case (EFFECT_TYRESLIP):
|
||||
|
|
@ -64,8 +205,19 @@ int slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, int
|
|||
// if not saved version exists get tyre diameter and save it
|
||||
// use config check variable to track if the config check has been performed
|
||||
// avoid many opens of the same file
|
||||
getTyreDiameter(simdata);
|
||||
*configcheck = 1;
|
||||
if(useconfig == 1 && configfile != NULL && *configcheck == 0)
|
||||
{
|
||||
loadtyreconfig(simdata, configfile);
|
||||
*configcheck = 1;
|
||||
}
|
||||
|
||||
if(hasTyreDiameter(simdata)==false)
|
||||
{
|
||||
getTyreDiameter(simdata);
|
||||
if(useconfig == 1)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
if(hasTyreDiameter(simdata)==true)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -1,8 +1,9 @@
|
|||
#ifndef _HAPTICEFFECT_H
|
||||
#define _HAPTICEFFECT_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include "../simulatorapi/simapi/simapi/simdata.h"
|
||||
|
||||
int slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, int* configcheck);
|
||||
int slipeffect(SimData* simdata, int effecttype, int tyre, double threshold, int useconfig, int* configcheck, FILE* configfile);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ int devupdate(SimDevice* this, SimData* simdata)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds)
|
||||
int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds, MonocoqueSettings* ms)
|
||||
{
|
||||
slogi("initializing simdevices...");
|
||||
int devices = 0;
|
||||
|
|
@ -41,7 +41,9 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds)
|
|||
//simdevices[j].initialized = true;
|
||||
simdevices[j].type = SIMDEV_USB;
|
||||
simdevices[j].tyre = ds[j].tyre;
|
||||
simdevices[j].configcheck = 0;
|
||||
simdevices[j].useconfig = ms->useconfig;
|
||||
simdevices[j].configcheck = &ms->configcheck;
|
||||
simdevices[j].tyrediameterconfig = ms->tyre_diameter_config;
|
||||
devices++;
|
||||
}
|
||||
else
|
||||
|
|
@ -60,7 +62,9 @@ int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds)
|
|||
simdevices[j].initialized = true;
|
||||
simdevices[j].type = SIMDEV_SOUND;
|
||||
simdevices[j].tyre = ds[j].tyre;
|
||||
simdevices[j].configcheck = 0;
|
||||
simdevices[j].useconfig = ms->useconfig;
|
||||
simdevices[j].configcheck = &ms->configcheck;
|
||||
simdevices[j].tyrediameterconfig = ms->tyre_diameter_config;
|
||||
devices++;
|
||||
}
|
||||
else
|
||||
|
|
|
|||
|
|
@ -25,7 +25,9 @@ struct SimDevice
|
|||
DeviceType type;
|
||||
// possibly move these to a haptic effect struct
|
||||
MonocoqueTyreIdentifier tyre;
|
||||
int configcheck;
|
||||
int useconfig;
|
||||
int* configcheck;
|
||||
FILE* tyrediameterconfig;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
|
|
@ -122,7 +124,7 @@ int update(SimDevice* simdevice, SimData* simdata);
|
|||
|
||||
int devupdate(SimDevice* simdevice, SimData* simdata);
|
||||
|
||||
int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds);
|
||||
int devinit(SimDevice* simdevices, int numdevices, DeviceSettings* ds, MonocoqueSettings* ms);
|
||||
|
||||
int devfree(SimDevice* simdevices, int numdevices);
|
||||
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ int sounddev_tyreslip_update(SimDevice* this, SimData* simdata)
|
|||
{
|
||||
SoundDevice* sounddevice = (void *) this->derived;
|
||||
|
||||
int play = slipeffect(simdata, sounddevice->effecttype, this->tyre, sounddevice->slipthreshold, &this->configcheck);
|
||||
int play = slipeffect(simdata, sounddevice->effecttype, this->tyre, sounddevice->slipthreshold, this->useconfig, this->configcheck, this->tyrediameterconfig);
|
||||
|
||||
if (play > 0)
|
||||
{
|
||||
|
|
@ -60,7 +60,7 @@ int sounddev_tyrelock_update(SimDevice* this, SimData* simdata)
|
|||
{
|
||||
SoundDevice* sounddevice = (void *) this->derived;
|
||||
|
||||
int play = slipeffect(simdata, sounddevice->effecttype, this->tyre, sounddevice->slipthreshold, &this->configcheck);
|
||||
int play = slipeffect(simdata, sounddevice->effecttype, this->tyre, sounddevice->slipthreshold, this->useconfig, this->configcheck, this->tyrediameterconfig);
|
||||
|
||||
if (play > 0)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ int usbdev_update(SimDevice* this, SimData* simdata)
|
|||
tachdev_update(&usbdevice->u.tachdevice, simdata);
|
||||
break;
|
||||
case USBDEV_GENERICHAPTIC :
|
||||
usbhapticdev_update(&usbdevice->u.hapticdevice, simdata, this->tyre, &this->configcheck);
|
||||
usbhapticdev_update(&usbdevice->u.hapticdevice, simdata, this->tyre, this->useconfig, this->configcheck, this->tyrediameterconfig);
|
||||
break;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,10 +11,10 @@
|
|||
#include "../../slog/slog.h"
|
||||
|
||||
|
||||
int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdata, int tyre, int* configcheck)
|
||||
int usbhapticdev_update(USBGenericHapticDevice* usbhapticdevice, SimData* simdata, int tyre, int useconfig, int* configcheck, FILE* configfile)
|
||||
{
|
||||
|
||||
int play = slipeffect(simdata, usbhapticdevice->effecttype, tyre, usbhapticdevice->threshold, configcheck);
|
||||
int play = slipeffect(simdata, usbhapticdevice->effecttype, tyre, usbhapticdevice->threshold, useconfig, configcheck, configfile);
|
||||
|
||||
if (play != usbhapticdevice->state)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@ typedef struct
|
|||
USBGenericHapticDevice;
|
||||
|
||||
|
||||
int usbhapticdev_update(USBGenericHapticDevice* hapticdevice, SimData* simdata, int tyre, int* configcheck);
|
||||
int usbhapticdev_update(USBGenericHapticDevice* hapticdevice, SimData* simdata, int tyre, int useconfig, int* configcheck, FILE* configfile);
|
||||
int usbhapticdev_init(USBGenericHapticDevice* hapticdevice, DeviceSettings* ds);
|
||||
int usbhapticdev_free(USBGenericHapticDevice* hapticdevice);
|
||||
|
||||
|
|
|
|||
|
|
@ -77,6 +77,9 @@ typedef struct
|
|||
{
|
||||
ProgramAction program_action;
|
||||
Simulator sim_name;
|
||||
int configcheck;
|
||||
int useconfig;
|
||||
FILE* tyre_diameter_config;
|
||||
}
|
||||
MonocoqueSettings;
|
||||
|
||||
|
|
@ -141,4 +144,6 @@ int devsetup(const char* device_type, const char* device_subtype, const char* co
|
|||
|
||||
int settingsfree(DeviceSettings ds);
|
||||
|
||||
int strcicmp(char const *a, char const *b);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -63,6 +63,10 @@ int main(int argc, char** argv)
|
|||
create_user_dir("/.config/");
|
||||
create_user_dir("/.cache/");
|
||||
char* config_file_str = ( char* ) malloc(1 + strlen(home_dir_str) + strlen("/.config/") + strlen("monocoque/monocoque.config"));
|
||||
size_t diameters_file_sz = 1 + strlen(home_dir_str) + strlen("/.config/") + strlen("monocoque/diameters.json");
|
||||
char* diameters_file_str = ( char* ) malloc(1 + strlen(home_dir_str) + strlen("/.config/") + strlen("monocoque/diameters.json"));
|
||||
snprintf(diameters_file_str, diameters_file_sz, "%s/.config/monocoque/diameters.json", home_dir_str);
|
||||
|
||||
char* cache_dir_str = ( char* ) malloc(1 + strlen(home_dir_str) + strlen("/.cache/monocoque/"));
|
||||
strcpy(config_file_str, home_dir_str);
|
||||
strcat(config_file_str, "/.config/");
|
||||
|
|
@ -70,6 +74,7 @@ int main(int argc, char** argv)
|
|||
strcat(cache_dir_str, "/.cache/monocoque/");
|
||||
strcat(config_file_str, "monocoque/monocoque.config");
|
||||
|
||||
|
||||
slog_config_t slgCfg;
|
||||
slog_config_get(&slgCfg);
|
||||
slgCfg.eColorFormat = SLOG_COLORING_TAG;
|
||||
|
|
@ -92,11 +97,19 @@ int main(int argc, char** argv)
|
|||
slog_disable(SLOG_DEBUG);
|
||||
}
|
||||
|
||||
ms->tyre_diameter_config = fopen(diameters_file_str, "r");
|
||||
ms->useconfig = 1;
|
||||
if (ms->tyre_diameter_config == NULL)
|
||||
{
|
||||
slogw("Could not initialize tyre diameters save file.");
|
||||
}
|
||||
ms->configcheck = 0;
|
||||
free(diameters_file_str);
|
||||
|
||||
slogi("Loading configuration file: %s", config_file_str);
|
||||
config_t cfg;
|
||||
config_init(&cfg);
|
||||
config_setting_t* config_devices = NULL;
|
||||
|
||||
if (!config_read_file(&cfg, config_file_str))
|
||||
{
|
||||
fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg));
|
||||
|
|
@ -125,7 +138,7 @@ int main(int argc, char** argv)
|
|||
else
|
||||
{
|
||||
int devices = 0;
|
||||
devices = devinit(tachdev, 1, ds);
|
||||
devices = devinit(tachdev, 1, ds, ms);
|
||||
if(devices < 1)
|
||||
{
|
||||
error = MONOCOQUE_ERROR_INVALID_DEV;
|
||||
|
|
@ -198,7 +211,7 @@ int main(int argc, char** argv)
|
|||
|
||||
setupsound();
|
||||
SimDevice* devices = malloc(numdevices * sizeof(SimDevice));
|
||||
int initdevices = devinit(devices, configureddevices, ds);
|
||||
int initdevices = devinit(devices, configureddevices, ds, ms);
|
||||
|
||||
if (p->program_action == A_PLAY)
|
||||
{
|
||||
|
|
@ -265,6 +278,8 @@ configcleanup:
|
|||
config_destroy(&cfg);
|
||||
|
||||
cleanup_final:
|
||||
|
||||
fclose(ms->tyre_diameter_config);
|
||||
free(ms);
|
||||
free(p);
|
||||
exit(0);
|
||||
|
|
|
|||
Loading…
Reference in New Issue