X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=profile.c;h=78643cd753d1c393760c593e6d7957377ddbabbf;hb=2b4fa480ae57766889c5a20ff1cb59427469d401;hp=7832dccafd36242c7835f45ae1ac9e115a3d13e4;hpb=41a1cf4b1967c689f4c3dab60ac284a1313bb9b4;p=ext%2Fsubsurface.git diff --git a/profile.c b/profile.c index 7832dcc..78643cd 100644 --- a/profile.c +++ b/profile.c @@ -860,6 +860,68 @@ static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi, } } +static int get_cylinder_index(struct dive *dive, struct event *ev) +{ + int i; + + /* + * Try to find a cylinder that matches the O2 percentage + * in the gas change event 'value' field. + * + * Crazy suunto gas change events. We really should do + * this in libdivecomputer or something. + */ + for (i = 0; i < MAX_CYLINDERS; i++) { + cylinder_t *cyl = dive->cylinder+i; + int o2 = (cyl->gasmix.o2.permille + 5) / 10; + if (o2 == ev->value) + return i; + } + + return 0; +} + +static struct event *get_next_gaschange(struct event *event) +{ + while (event) { + if (!strcmp(event->name, "gaschange")) + return event; + event = event->next; + } + return event; +} + +static int set_cylinder_index(struct plot_info *pi, int i, int cylinderindex, unsigned int end) +{ + while (i < pi->nr) { + struct plot_data *entry = pi->entry+i; + if (entry->sec > end) + break; + if (entry->cylinderindex != cylinderindex) { + entry->cylinderindex = cylinderindex; + entry->pressure[0] = 0; + } + i++; + } + return i; +} + +static void check_gas_change_events(struct dive *dive, struct plot_info *pi) +{ + int i = 0, cylinderindex = 0; + struct event *ev = get_next_gaschange(dive->events); + + if (!ev) + return; + + do { + i = set_cylinder_index(pi, i, cylinderindex, ev->time.seconds); + cylinderindex = get_cylinder_index(dive, ev); + ev = get_next_gaschange(ev->next); + } while (ev); + set_cylinder_index(pi, i, cylinderindex, ~0u); +} + /* * Create a plot-info with smoothing and ranged min/max * @@ -887,9 +949,6 @@ static struct plot_info *create_plot_info(struct dive *dive) sec = 0; lastindex = 0; lastdepth = -1; - for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */ - track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1); - current = track_pr[dive->sample[0].cylinderindex]; for (i = 0; i < dive->samples; i++) { int depth; struct sample *sample = dive->sample+i; @@ -897,9 +956,29 @@ static struct plot_info *create_plot_info(struct dive *dive) entry = pi->entry + i + 2; sec = entry->sec = sample->time.seconds; depth = entry->depth = sample->depth.mm; - entry->same_cylinder = sample->cylinderindex == cylinderindex; - entry->cylinderindex = cylinderindex = sample->cylinderindex; + entry->cylinderindex = sample->cylinderindex; SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar; + entry->temperature = sample->temperature.mkelvin; + + if (depth || lastdepth) + lastindex = i+2; + + lastdepth = depth; + if (depth > pi->maxdepth) + pi->maxdepth = depth; + } + + check_gas_change_events(dive, pi); + + for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) /* initialize the start pressures */ + track_pr[cyl] = pr_track_alloc(dive->cylinder[cyl].start.mbar, -1); + current = track_pr[dive->sample[0].cylinderindex]; + for (i = 0; i < dive->samples; i++) { + entry = pi->entry + i + 2; + + entry->same_cylinder = entry->cylinderindex == cylinderindex; + cylinderindex = entry->cylinderindex; + /* track the segments per cylinder and their pressure/time integral */ if (!entry->same_cylinder) { current->end = SENSOR_PRESSURE(entry-1); @@ -921,15 +1000,8 @@ static struct plot_info *create_plot_info(struct dive *dive) current->pressure_time += (entry->sec - (entry-1)->sec) * (1 + entry->depth / 10000.0); missing_pr |= !SENSOR_PRESSURE(entry); - entry->temperature = sample->temperature.mkelvin; - - if (depth || lastdepth) - lastindex = i+2; - - lastdepth = depth; - if (depth > pi->maxdepth) - pi->maxdepth = depth; } + current->t_end = entry->sec; for (cyl = 0; cyl < MAX_CYLINDERS; cyl++) { /* initialize the end pressures */ int pr = dive->cylinder[cyl].end.mbar;