From 093802f9cc7ebaa0bfe8d862766e7e08026576f0 Mon Sep 17 00:00:00 2001 From: cnlohr Date: Tue, 20 Dec 2016 22:44:10 -0500 Subject: switch to a hidden disambiguator. --- src/disambiguator.c | 17 ++++++++++------- src/disambiguator.h | 38 ++++++++++++++++++++++++++++++++++++++ src/survive.c | 4 ++++ src/survive_data.c | 10 +++++----- 4 files changed, 57 insertions(+), 12 deletions(-) create mode 100644 src/disambiguator.h (limited to 'src') diff --git a/src/disambiguator.c b/src/disambiguator.c index f03299d..8a5a993 100644 --- a/src/disambiguator.c +++ b/src/disambiguator.c @@ -2,11 +2,11 @@ // //All MIT/x11 Licensed Code in this file may be relicensed freely under the GPL or LGPL licenses. -#include "../include/disambiguator.h" +#include "disambiguator.h" #include #include -void disambiguator_init(disambiguator * d) { +void disambiguator_init( struct disambiguator * d ) { memset(&(d->times), 0x0, sizeof(d->times)); memset(&(d->scores), 0x0, sizeof(d->scores)); d->state = D_STATE_UNLOCKED; @@ -14,9 +14,10 @@ void disambiguator_init(disambiguator * d) { d->max_confidence = 0; } -inline void disambiguator_discard(disambiguator * d, long age); +inline void disambiguator_discard( struct disambiguator * d, long age ); -void disambiguator_discard(disambiguator * d, long age) { +void disambiguator_discard( struct disambiguator * d, long age ) +{ int confidence = 0; for (unsigned int i = 0; i < DIS_NUM_VALUES; ++i) { if (d->times[i] != 0 && d->times[i] < age) { @@ -31,9 +32,10 @@ void disambiguator_discard(disambiguator * d, long age) { d->max_confidence = confidence; } -inline int disambiguator_find_nearest(disambiguator * d, long time, int max_diff); +inline int disambiguator_find_nearest( struct disambiguator * d, long time, int max_diff ); -int disambiguator_find_nearest(disambiguator * d, long time, int max_diff) { +int disambiguator_find_nearest( struct disambiguator * d, long time, int max_diff ) +{ int diff = max_diff; // max allowed diff for a match int idx = -1; for (unsigned int i = 0; i < DIS_NUM_VALUES; ++i) { @@ -49,7 +51,8 @@ int disambiguator_find_nearest(disambiguator * d, long time, int max_diff) { return idx; } -pulse_type disambiguator_step(disambiguator * d, long time, int length) { +pulse_type disambiguator_step( struct disambiguator * d, long time, int length) +{ if (length < 2750) { return d->state == D_STATE_LOCKED ? P_SWEEP : P_UNKNOWN; } diff --git a/src/disambiguator.h b/src/disambiguator.h new file mode 100644 index 0000000..0db19de --- /dev/null +++ b/src/disambiguator.h @@ -0,0 +1,38 @@ +// (C) 2016 Julian Picht, MIT/x11 License. +// +//All MIT/x11 Licensed Code in this file may be relicensed freely under the GPL or LGPL licenses. +#ifndef DISAMBIGUATOR_H +#define DISAMBIGUATOR_H + +#define DIS_NUM_VALUES 8 + +typedef enum { + D_STATE_INVALID = 0, + D_STATE_LOCKED = 1, + D_STATE_UNLOCKED = -1, +} dis_state; + +typedef enum { + P_UNKNOWN = 0, + P_SYNC = 1, + P_SWEEP = 2, +} pulse_type; + +struct disambiguator { + long times[DIS_NUM_VALUES]; + int scores[DIS_NUM_VALUES]; + dis_state state; + long last; + int max_confidence; + char code; +}; + +struct classified_pulse_ { + pulse_type t; + int length; +}; + +void disambiguator_init( struct disambiguator * d); +pulse_type disambiguator_step( struct disambiguator * d, long time, int length); + +#endif /* DISAMBIGUATOR_H */ diff --git a/src/survive.c b/src/survive.c index b12c4cc..73df29d 100644 --- a/src/survive.c +++ b/src/survive.c @@ -8,6 +8,7 @@ #include #include #include +#include "disambiguator.h" static int jsoneq(const char *json, jsmntok_t *tok, const char *s) { if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start && @@ -146,12 +147,15 @@ struct SurviveContext * survive_init() ctx->headset.ctx = ctx; memcpy( ctx->headset.codename, "HMD", 4 ); + ctx->headset.d = calloc( 1, sizeof( struct disambiguator ) ); ctx->watchman[0].ctx = ctx; memcpy( ctx->watchman[0].codename, "WM0", 4 ); + ctx->watchman[0].d = calloc( 1, sizeof( struct disambiguator ) ); ctx->watchman[1].ctx = ctx; memcpy( ctx->watchman[1].codename, "WM1", 4 ); + ctx->watchman[1].d = calloc( 1, sizeof( struct disambiguator ) ); //USB must happen last. if( r = survive_usb_init( ctx ) ) diff --git a/src/survive_data.c b/src/survive_data.c index ef35e0e..cb3e8a2 100644 --- a/src/survive_data.c +++ b/src/survive_data.c @@ -42,18 +42,18 @@ static void handle_lightcap( struct SurviveObject * so, struct LightcapElement * if( le->type != 0xfe || le->length < 50 ) return; //le->timestamp += (le->length/2); - int32_t offset = le->timestamp - so->d.last; - switch(disambiguator_step(&(so->d), le->timestamp, le->length)) { + int32_t offset = le->timestamp - so->d->last; + switch( disambiguator_step( so->d, le->timestamp, le->length ) ) { default: case P_UNKNOWN: // not currently locked case P_SYNC: ct->lightproc( so, le->sensor_id, -1, 0, le->timestamp, offset ); - so->d.code = ((le->length+125)/250) - 12; + so->d->code = ((le->length+125)/250) - 12; break; case P_SWEEP: - if (so->d.code & 1) return; - ct->lightproc( so, le->sensor_id, so->d.code >> 1, offset, le->timestamp, le->length ); + if (so->d->code & 1) return; + ct->lightproc( so, le->sensor_id, so->d->code >> 1, offset, le->timestamp, le->length ); break; } #if 0 -- cgit v1.2.3