diff options
-rw-r--r-- | dive.c | 22 | ||||
-rw-r--r-- | libdivecomputer.c | 11 |
2 files changed, 29 insertions, 4 deletions
@@ -1243,6 +1243,23 @@ static void fixup_dc_events(struct divecomputer *dc) } } +static int interpolate_depth(struct divecomputer *dc, int idx, int lastdepth, int lasttime, int now) +{ + int i; + int nextdepth = lastdepth; + int nexttime = now; + + for (i = idx+1; i < dc->samples; i++) { + struct sample *sample = dc->sample + i; + if (sample->depth.mm < 0) + continue; + nextdepth = sample->depth.mm; + nexttime = sample->time.seconds; + break; + } + return interpolate(lastdepth, nextdepth, now-lasttime, nexttime-lasttime); +} + static void fixup_dive_dc(struct dive *dive, struct divecomputer *dc) { int i, j; @@ -1276,6 +1293,11 @@ static void fixup_dive_dc(struct dive *dive, struct divecomputer *dc) int o2_pressure = sample->o2cylinderpressure.mbar; int index; + if (depth < 0) { + depth = interpolate_depth(dc, i, lastdepth, lasttime, time); + sample->depth.mm = depth; + } + /* if we have an explicit first cylinder */ if (sample->sensor == 0 && first_cylinder != 0) sample->sensor = first_cylinder; diff --git a/libdivecomputer.c b/libdivecomputer.c index a9d90d837..b5f90cb35 100644 --- a/libdivecomputer.c +++ b/libdivecomputer.c @@ -231,7 +231,6 @@ static void handle_event(struct divecomputer *dc, struct sample *sample, dc_samp void sample_cb(dc_sample_type_t type, dc_sample_value_t value, void *userdata) { - unsigned int mm; static unsigned int nsensor = 0; struct divecomputer *dc = userdata; struct sample *sample; @@ -252,7 +251,10 @@ sample_cb(dc_sample_type_t type, dc_sample_value_t value, void *userdata) switch (type) { case DC_SAMPLE_TIME: nsensor = 0; - mm = 0; + + // The previous sample gets some sticky values + // that may have been around from before, even + // if there was no new data if (sample) { sample->in_deco = in_deco; sample->ndl.seconds = ndl; @@ -260,11 +262,12 @@ sample_cb(dc_sample_type_t type, dc_sample_value_t value, void *userdata) sample->stopdepth.mm = stopdepth; sample->setpoint.mbar = po2; sample->cns = cns; - mm = sample->depth.mm; } + // Create a new sample. + // Mark depth as negative sample = prepare_sample(dc); sample->time.seconds = value.time; - sample->depth.mm = mm; + sample->depth.mm = -1; finish_sample(dc); break; case DC_SAMPLE_DEPTH: |