From 76e1d700bf2b3eac225e3965bb562e16e3615fd5 Mon Sep 17 00:00:00 2001 From: cnlohr Date: Thu, 26 Apr 2018 23:25:10 -0400 Subject: Give a shot at the new config system. --- src/poser_charlesrefine.c | 6 +- src/survive.c | 24 +++++--- src/survive_config.c | 135 +++++++++++++++++++++++++++++++++++++++++---- src/survive_config.h | 9 ++- src/survive_driver_dummy.c | 6 +- src/survive_playback.c | 10 ++-- 6 files changed, 160 insertions(+), 30 deletions(-) (limited to 'src') diff --git a/src/poser_charlesrefine.c b/src/poser_charlesrefine.c index b35a878..b14d882 100644 --- a/src/poser_charlesrefine.c +++ b/src/poser_charlesrefine.c @@ -55,9 +55,9 @@ int PoserCharlesRefine(SurviveObject *so, PoserData *pd) { //TODO: Actually do Madgwick's algorithm LinmathQuat applymotion; const SurvivePose * object_pose = &so->OutPose; - imuData->gyro[0] *= 1./240.; - imuData->gyro[1] *= 1./240.; - imuData->gyro[2] *= 1./240.; + imuData->gyro[0] *= 1./1000.; + imuData->gyro[1] *= 1./1000.; + imuData->gyro[2] *= 1./1000.; quatfromeuler( applymotion, imuData->gyro ); //printf( "%f %f %f\n", imuData->gyro [0], imuData->gyro [1], imuData->gyro [2] ); diff --git a/src/survive.c b/src/survive.c index 13aeee9..20a2a1b 100644 --- a/src/survive.c +++ b/src/survive.c @@ -94,6 +94,8 @@ void survive_verify_FLT_size(uint32_t user_size) { } SurviveContext *survive_init_internal(int argc, char *const *argv) { + int i; + #ifdef RUNTIME_SYMNUM if (!did_runtime_symnum) { EnumerateSymbols(SymnumCheck); @@ -131,15 +133,17 @@ SurviveContext *survive_init_internal(int argc, char *const *argv) { ctx->state = SURVIVE_STOPPED; + survive_config_populate_ctx( ctx ); + ctx->global_config_values = malloc(sizeof(config_group)); ctx->temporary_config_values = malloc(sizeof(config_group)); ctx->lh_config = malloc(sizeof(config_group) * NUM_LIGHTHOUSES); // initdata - init_config_group(ctx->global_config_values, 10); - init_config_group(ctx->temporary_config_values, 20); - init_config_group(&ctx->lh_config[0], 10); - init_config_group(&ctx->lh_config[1], 10); + init_config_group(ctx->global_config_values, 10, ctx); + init_config_group(ctx->temporary_config_values, 20, ctx); + for( i = 0; i < NUM_LIGHTHOUSES; i++ ) + init_config_group(&ctx->lh_config[i], 10, ctx); // Process command-line parameters. char *const *av = argv + 1; @@ -176,7 +180,9 @@ SurviveContext *survive_init_internal(int argc, char *const *argv) { const char *name = *av + 2; // Skip the '--'; bool flagArgument = (av + 1 == argvend) || av[1][0] == '-'; - if (flagArgument) { + if ( strcmp( name, "help" ) == 0 ) + showhelp = 1; + else if (flagArgument) { bool value = strncmp("no-", name, 3) != 0; if (value == 0) { name += 3; // Skip "no-" @@ -202,15 +208,19 @@ SurviveContext *survive_init_internal(int argc, char *const *argv) { fprintf(stderr, " --playback [log file] - Read events from the given file instead of USB devices.\n"); fprintf(stderr, " --playback-factor [f] - Time factor of playback -- 1 is run at the same timing as " "original, 0 is run as fast as possible.\n"); - return 0; } config_read(ctx, survive_configs(ctx, "configfile", SC_GET, "config.json")); ctx->activeLighthouses = survive_configi(ctx, "lighthousecount", SC_SETCONFIG, 2); - config_read_lighthouse(ctx->lh_config, &(ctx->bsd[0]), 0); config_read_lighthouse(ctx->lh_config, &(ctx->bsd[1]), 1); + if( showhelp ) + { + survive_print_known_configs( ctx ); + return 0; + } + ctx->faultfunction = survivefault; ctx->notefunction = survivenote; ctx->lightproc = survive_default_light_process; diff --git a/src/survive_config.c b/src/survive_config.c index d67cd8e..92c0125 100644 --- a/src/survive_config.c +++ b/src/survive_config.c @@ -10,16 +10,102 @@ #endif #include "math.h" #include +#include + +//Static-time registration system. + +struct static_conf_t +{ + union + { + FLT f; + int i; + char * s; + } data_default; + const char * name; + const char * description; + char type; +}; + +static struct static_conf_t static_configs[MAX_SHORTHAND_CONFIGS]; + +void survive_config_bind_variable( char vt, int * variable, const char * name, const char * description, ... ) +{ + va_list ap; + va_start(ap, description); + int i; + struct static_conf_t * config; + for( i = 0; i < MAX_SHORTHAND_CONFIGS; i++ ) + { + config = &static_configs[i]; + if( !config->name || strcmp( config->name, name ) == 0 ) break; + } + if( i == MAX_SHORTHAND_CONFIGS ) + { + fprintf( stderr, "Fatal: Too many static configuration items. Please recompile with a higher MAX_STATIC_CONFIGS\n" ); + exit( -1 ); + } + if( !config->description ) config->description = description; + if( !config->name ) config->name = name; + if( config->type && config->type != vt ) + { + fprintf( stderr, "Fatal: Internal error on variable %s. Types disagree.\n", name ); + exit( -2 ); + } + config->type = vt; + switch( vt ) + { + case 'i': config->data_default.i = va_arg(ap, int); break; + case 'f': config->data_default.f = va_arg(ap, FLT); break; + case 's': config->data_default.s = va_arg(ap, char *); break; + default: + fprintf( stderr, "Fatal: Internal error on variable %s. Unknown type %c\n", name, vt ); + } + *variable = i; +} + +void survive_config_populate_ctx( SurviveContext * ctx ) +{ + int i; + struct static_conf_t * config; + for( i = 0; i < MAX_SHORTHAND_CONFIGS; i++ ) + { + config = &static_configs[i]; + switch( config->type ) + { + case 'i': ctx->shorthand_configs[i].i = config->data_default.i; + case 'f': ctx->shorthand_configs[i].f = config->data_default.f; + case 's': ctx->shorthand_configs[i].s = config->data_default.s; + } + } +} + +void survive_print_known_configs( SurviveContext * ctx ) +{ + int i; + struct static_conf_t * config; + for( i = 0; i < MAX_SHORTHAND_CONFIGS; i++ ) + { + config = &static_configs[i]; + if( !config->name ) break; + switch( config->type ) + { + case 'i': printf( "%10d %20s %s\n", config->data_default.i, config->name, config->description ); break; + case 'f': printf( "%10f %20s %s\n", config->data_default.f, config->name, config->description ); break; + case 's': printf( "%10s %20s %s\n", config->data_default.s, config->name, config->description ); break; + } + } + + //XXX TODO: Maybe this should print out the actual config values after being updated from the rest of the config system? + //struct config_group *global_config_values; + //struct config_group *temporary_config_values; // Set per-session, from command-line. Not saved but override global_config_values +} + + -//#define MAX_CONFIG_ENTRIES 100 -//#define MAX_LIGHTHOUSES 2 -// config_group global_config_values; -// config_group lh_config[MAX_LIGHTHOUSES]; //lighthouse configs -// static uint16_t used_entries = 0; -// static FILE *config_file = NULL; const FLT *config_set_float_a(config_group *cg, const char *tag, const FLT *values, uint8_t count); void init_config_entry(config_entry *ce) { @@ -27,6 +113,7 @@ void init_config_entry(config_entry *ce) { ce->tag = NULL; ce->type = CONFIG_UNKNOWN; ce->elements = 0; + ce->shorthand_place = -1; } void destroy_config_entry(config_entry *ce) { @@ -40,11 +127,12 @@ void destroy_config_entry(config_entry *ce) { } } -void init_config_group(config_group *cg, uint8_t count) { +void init_config_group(config_group *cg, uint8_t count, SurviveContext * ctx) { uint16_t i = 0; cg->used_entries = 0; cg->max_entries = count; cg->config_entries = NULL; + cg->ctx = ctx; if (count == 0) return; @@ -214,7 +302,7 @@ uint16_t config_read_float_array(config_group *cg, const char *tag, FLT *values, return count; } -config_entry *next_unused_entry(config_group *cg) { +config_entry *next_unused_entry(config_group *cg, const char * tag) { config_entry *cv = NULL; // assert(cg->used_entries < cg->max_entries); @@ -224,13 +312,28 @@ config_entry *next_unused_entry(config_group *cg) { cv = cg->config_entries + cg->used_entries; cg->used_entries++; + + + int i; + struct static_conf_t * config; + for( i = 0; i < MAX_SHORTHAND_CONFIGS; i++ ) + { + config = &static_configs[i]; + if( !config->name ) break; + if( strcmp( config->name, tag ) == 0 ) + { + cv->shorthand_place = i; + break; + } + } + return cv; } const char *config_set_str(config_group *cg, const char *tag, const char *value) { config_entry *cv = find_config_entry(cg, tag); if (cv == NULL) - cv = next_unused_entry(cg); + cv = next_unused_entry(cg,tag); sstrcpy(&(cv->tag), tag); @@ -241,37 +344,43 @@ const char *config_set_str(config_group *cg, const char *tag, const char *value) } cv->type = CONFIG_STRING; + if( cv->shorthand_place >= 0 ) cg->ctx->shorthand_configs[cv->shorthand_place].s = value; + return value; } const uint32_t config_set_uint32(config_group *cg, const char *tag, const uint32_t value) { config_entry *cv = find_config_entry(cg, tag); if (cv == NULL) - cv = next_unused_entry(cg); + cv = next_unused_entry(cg,tag); sstrcpy(&(cv->tag), tag); cv->numeric.i = value; cv->type = CONFIG_UINT32; + if( cv->shorthand_place >= 0 ) cg->ctx->shorthand_configs[cv->shorthand_place].i = value; + return value; } const FLT config_set_float(config_group *cg, const char *tag, const FLT value) { config_entry *cv = find_config_entry(cg, tag); if (cv == NULL) - cv = next_unused_entry(cg); + cv = next_unused_entry(cg,tag); sstrcpy(&(cv->tag), tag); cv->numeric.f = value; cv->type = CONFIG_FLOAT; + if( cv->shorthand_place >= 0 ) cg->ctx->shorthand_configs[cv->shorthand_place].f = value; + return value; } const FLT *config_set_float_a(config_group *cg, const char *tag, const FLT *values, uint8_t count) { config_entry *cv = find_config_entry(cg, tag); if (cv == NULL) - cv = next_unused_entry(cg); + cv = next_unused_entry(cg,tag); sstrcpy(&(cv->tag), tag); @@ -548,3 +657,5 @@ const char *survive_configs(SurviveContext *ctx, const char *tag, char flags, co return def; } + + diff --git a/src/survive_config.h b/src/survive_config.h index 23d80c8..37e904c 100644 --- a/src/survive_config.h +++ b/src/survive_config.h @@ -26,18 +26,20 @@ typedef struct { } numeric; char *data; uint32_t elements; + int shorthand_place; } config_entry; typedef struct config_group { config_entry *config_entries; uint16_t used_entries; uint16_t max_entries; + SurviveContext * ctx; } config_group; //extern config_group global_config_values; //extern config_group lh_config[2]; //lighthouse configs -void init_config_group(config_group *cg, uint8_t count); +void init_config_group(config_group *cg, uint8_t count, SurviveContext * ctx); void destroy_config_group(config_group* cg); //void config_init(); @@ -60,4 +62,9 @@ uint16_t config_read_float_array(config_group *cg, const char *tag, FLT* values, uint32_t config_read_uint32(config_group *cg, const char *tag, const uint32_t def); const char* config_read_str(config_group *cg, const char *tag, const char *def); +//These are for the internal non-function configuration system. +void survive_config_bind_variable( char vt, int * variable, const char * name, const char * description, ... ); +void survive_print_known_configs(); +void survive_config_populate_ctx( SurviveContext * ctx ); + #endif diff --git a/src/survive_driver_dummy.c b/src/survive_driver_dummy.c index e2e01a4..01d0482 100644 --- a/src/survive_driver_dummy.c +++ b/src/survive_driver_dummy.c @@ -8,6 +8,8 @@ #include "survive_config.h" #include "os_generic.h" +STATIC_CONFIG_ITEM_I( DUMMY_DRIVER_ENABLE, "dummy-driver-enable", "Load a dummy driver for testing.", 0 ); + struct SurviveDriverDummy { SurviveContext * ctx; SurviveObject * so; @@ -53,9 +55,7 @@ int dummy_haptic( SurviveObject * so, uint8_t reserved, uint16_t pulseHigh, uint int DriverRegDummy(SurviveContext *ctx) { - int enable_dummy_driver = survive_configi( ctx, "dummy_driver_enable", SC_GET, 0 ); - - if( !enable_dummy_driver ) return 0; + if( !GCONFIGI(DUMMY_DRIVER_ENABLE) ) return 0; SurviveDriverDummy *sp = calloc(1, sizeof(SurviveDriverDummy)); sp->ctx = ctx; diff --git a/src/survive_playback.c b/src/survive_playback.c index 7e22b47..a5c4519 100644 --- a/src/survive_playback.c +++ b/src/survive_playback.c @@ -21,6 +21,10 @@ ssize_t getdelim(char **lineptr, size_t *n, int delimiter, FILE *stream); ssize_t getline(char **lineptr, size_t *n, FILE *stream); #endif + +STATIC_CONFIG_ITEM_F( PLAYBACK_FACTOR, "playback-factor", "Time factor of playback -- 1 is run at the same timing as original, 0 is run as fast as possible.", 1.0f ); + + typedef struct SurviveRecordingData { bool alwaysWriteStdOut; bool writeRawLight; @@ -172,7 +176,6 @@ struct SurvivePlaybackData { FILE *playback_file; int lineno; - FLT time_factor; double next_time_us; bool hasRawLight; }; @@ -292,7 +295,7 @@ static int playback_poll(struct SurviveContext *ctx, void *_driver) { line = 0; } - if (driver->next_time_us * driver->time_factor > timestamp_in_us()) + if (driver->next_time_us * GCONFIGF( PLAYBACK_FACTOR ) > timestamp_in_us()) return 0; driver->next_time_us = 0; @@ -384,7 +387,6 @@ int DriverRegPlayback(SurviveContext *ctx) { SurvivePlaybackData *sp = calloc(1, sizeof(SurvivePlaybackData)); sp->ctx = ctx; sp->playback_dir = playback_file; - sp->time_factor = survive_configf(ctx, "playback-factor", SC_GET, 1.f); sp->playback_file = fopen(playback_file, "r"); if (sp->playback_file == 0) { @@ -392,7 +394,7 @@ int DriverRegPlayback(SurviveContext *ctx) { return -1; } - SV_INFO("Using playback file '%s' with timefactor of %f", playback_file, sp->time_factor); + SV_INFO("Using playback file '%s' with timefactor of %f", playback_file, GCONFIGF( PLAYBACK_FACTOR ) ); SurviveObject *hmd = survive_create_hmd(ctx, "Playback", sp); SurviveObject *wm0 = survive_create_wm0(ctx, "Playback", sp, 0); SurviveObject *wm1 = survive_create_wm1(ctx, "Playback", sp, 0); -- cgit v1.2.3