2017-04-27 18:18:03 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2013-04-15 03:10:25 +00:00
|
|
|
/* statistics.c
|
2011-11-02 16:10:57 +00:00
|
|
|
*
|
2013-04-15 03:10:25 +00:00
|
|
|
* core logic for the Info & Stats page -
|
|
|
|
* char *get_minutes(int seconds);
|
2018-10-06 14:50:46 +00:00
|
|
|
* void calculate_stats_summary(struct stats_summary *out, bool selected_only);
|
2018-10-06 14:30:57 +00:00
|
|
|
* void calculate_stats_selected(stats_t *stats_selection);
|
2011-11-02 16:10:57 +00:00
|
|
|
*/
|
2013-10-06 15:55:58 +00:00
|
|
|
#include "gettext.h"
|
2013-10-05 07:29:09 +00:00
|
|
|
#include <string.h>
|
2012-12-20 23:46:47 +00:00
|
|
|
#include <ctype.h>
|
2011-11-02 16:10:57 +00:00
|
|
|
|
|
|
|
#include "dive.h"
|
|
|
|
#include "display.h"
|
2019-05-31 14:09:14 +00:00
|
|
|
#include "trip.h"
|
2013-04-15 03:10:25 +00:00
|
|
|
#include "statistics.h"
|
2019-03-10 02:21:46 +00:00
|
|
|
#include "units.h"
|
2011-11-02 16:10:57 +00:00
|
|
|
|
2013-01-24 21:08:19 +00:00
|
|
|
static void process_temperatures(struct dive *dp, stats_t *stats)
|
2013-01-24 18:58:59 +00:00
|
|
|
{
|
2018-02-18 20:55:57 +00:00
|
|
|
temperature_t min_temp, mean_temp, max_temp = {.mkelvin = 0};
|
2013-01-24 18:58:59 +00:00
|
|
|
|
2018-02-18 20:55:57 +00:00
|
|
|
max_temp.mkelvin = dp->maxtemp.mkelvin;
|
|
|
|
if (max_temp.mkelvin && (!stats->max_temp.mkelvin || max_temp.mkelvin > stats->max_temp.mkelvin))
|
|
|
|
stats->max_temp.mkelvin = max_temp.mkelvin;
|
2013-01-24 18:58:59 +00:00
|
|
|
|
2018-02-18 20:55:57 +00:00
|
|
|
min_temp.mkelvin = dp->mintemp.mkelvin;
|
|
|
|
if (min_temp.mkelvin && (!stats->min_temp.mkelvin || min_temp.mkelvin < stats->min_temp.mkelvin))
|
|
|
|
stats->min_temp.mkelvin = min_temp.mkelvin;
|
2013-01-24 18:58:59 +00:00
|
|
|
|
2018-02-18 20:55:57 +00:00
|
|
|
if (min_temp.mkelvin || max_temp.mkelvin) {
|
|
|
|
mean_temp.mkelvin = min_temp.mkelvin;
|
|
|
|
if (mean_temp.mkelvin)
|
|
|
|
mean_temp.mkelvin = (mean_temp.mkelvin + max_temp.mkelvin) / 2;
|
2013-01-24 18:58:59 +00:00
|
|
|
else
|
2018-02-18 20:55:57 +00:00
|
|
|
mean_temp.mkelvin = max_temp.mkelvin;
|
|
|
|
stats->combined_temp.mkelvin += mean_temp.mkelvin;
|
2013-01-24 18:58:59 +00:00
|
|
|
stats->combined_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-17 14:11:35 +00:00
|
|
|
static void process_dive(struct dive *dive, stats_t *stats)
|
2012-03-14 17:01:34 +00:00
|
|
|
{
|
2016-11-10 09:15:11 +00:00
|
|
|
int old_tadt, sac_time = 0;
|
2018-05-17 14:11:35 +00:00
|
|
|
int32_t duration = dive->duration.seconds;
|
2012-03-14 17:01:34 +00:00
|
|
|
|
2016-11-10 09:15:11 +00:00
|
|
|
old_tadt = stats->total_average_depth_time.seconds;
|
2013-02-08 06:48:07 +00:00
|
|
|
stats->total_time.seconds += duration;
|
|
|
|
if (duration > stats->longest_time.seconds)
|
|
|
|
stats->longest_time.seconds = duration;
|
|
|
|
if (stats->shortest_time.seconds == 0 || duration < stats->shortest_time.seconds)
|
|
|
|
stats->shortest_time.seconds = duration;
|
2018-05-17 14:11:35 +00:00
|
|
|
if (dive->maxdepth.mm > stats->max_depth.mm)
|
|
|
|
stats->max_depth.mm = dive->maxdepth.mm;
|
|
|
|
if (stats->min_depth.mm == 0 || dive->maxdepth.mm < stats->min_depth.mm)
|
|
|
|
stats->min_depth.mm = dive->maxdepth.mm;
|
2018-12-16 19:52:34 +00:00
|
|
|
stats->combined_max_depth.mm += dive->maxdepth.mm;
|
2013-01-24 18:58:59 +00:00
|
|
|
|
2018-05-17 14:11:35 +00:00
|
|
|
process_temperatures(dive, stats);
|
2012-07-01 03:12:11 +00:00
|
|
|
|
|
|
|
/* Maybe we should drop zero-duration dives */
|
2013-02-08 06:48:07 +00:00
|
|
|
if (!duration)
|
2012-07-01 03:12:11 +00:00
|
|
|
return;
|
2018-05-17 14:11:35 +00:00
|
|
|
if (dive->meandepth.mm) {
|
2016-11-10 09:15:11 +00:00
|
|
|
stats->total_average_depth_time.seconds += duration;
|
2017-03-09 16:07:30 +00:00
|
|
|
stats->avg_depth.mm = lrint((1.0 * old_tadt * stats->avg_depth.mm +
|
2018-05-17 14:11:35 +00:00
|
|
|
duration * dive->meandepth.mm) /
|
2017-03-09 16:07:30 +00:00
|
|
|
stats->total_average_depth_time.seconds);
|
2016-11-10 09:15:11 +00:00
|
|
|
}
|
2018-05-17 14:11:35 +00:00
|
|
|
if (dive->sac > 100) { /* less than .1 l/min is bogus, even with a pSCR */
|
2018-02-20 21:59:09 +00:00
|
|
|
sac_time = stats->total_sac_time.seconds + duration;
|
|
|
|
stats->avg_sac.mliter = lrint((1.0 * stats->total_sac_time.seconds * stats->avg_sac.mliter +
|
2018-05-17 14:11:35 +00:00
|
|
|
duration * dive->sac) /
|
2017-03-09 16:07:30 +00:00
|
|
|
sac_time);
|
2018-05-17 14:11:35 +00:00
|
|
|
if (dive->sac > stats->max_sac.mliter)
|
|
|
|
stats->max_sac.mliter = dive->sac;
|
|
|
|
if (stats->min_sac.mliter == 0 || dive->sac < stats->min_sac.mliter)
|
|
|
|
stats->min_sac.mliter = dive->sac;
|
2018-02-20 21:59:09 +00:00
|
|
|
stats->total_sac_time.seconds = sac_time;
|
2012-03-14 17:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
2011-11-02 16:10:57 +00:00
|
|
|
|
2013-04-15 03:10:25 +00:00
|
|
|
char *get_minutes(int seconds)
|
2012-09-10 19:17:28 +00:00
|
|
|
{
|
|
|
|
static char buf[80];
|
|
|
|
snprintf(buf, sizeof(buf), "%d:%.2d", FRACTION(seconds, 60));
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2018-10-06 08:58:12 +00:00
|
|
|
/*
|
|
|
|
* Calculate a summary of the statistics and put in the stats_summary
|
|
|
|
* structure provided in the first parameter.
|
|
|
|
* Before first use, it should be initialized with init_stats_summary().
|
|
|
|
* After use, memory must be released with free_stats_summary().
|
|
|
|
*/
|
2018-10-06 14:50:46 +00:00
|
|
|
void calculate_stats_summary(struct stats_summary *out, bool selected_only)
|
2011-11-02 16:10:57 +00:00
|
|
|
{
|
|
|
|
int idx;
|
2019-03-10 02:21:46 +00:00
|
|
|
int t_idx, d_idx, r;
|
2011-11-02 16:10:57 +00:00
|
|
|
struct dive *dp;
|
2012-09-20 00:35:52 +00:00
|
|
|
struct tm tm;
|
2012-09-10 19:17:28 +00:00
|
|
|
int current_year = 0;
|
|
|
|
int current_month = 0;
|
|
|
|
int year_iter = 0;
|
|
|
|
int month_iter = 0;
|
|
|
|
int prev_month = 0, prev_year = 0;
|
2013-11-24 01:09:34 +00:00
|
|
|
int trip_iter = 0;
|
|
|
|
dive_trip_t *trip_ptr = 0;
|
2019-03-10 02:21:46 +00:00
|
|
|
size_t size, tsize, dsize, tmsize;
|
2018-10-06 08:58:12 +00:00
|
|
|
stats_t stats = { 0 };
|
2011-11-02 16:10:57 +00:00
|
|
|
|
2012-01-15 23:21:56 +00:00
|
|
|
if (dive_table.nr > 0) {
|
2013-02-09 15:12:30 +00:00
|
|
|
stats.shortest_time.seconds = dive_table.dives[0]->duration.seconds;
|
2013-02-09 04:44:04 +00:00
|
|
|
stats.min_depth.mm = dive_table.dives[0]->maxdepth.mm;
|
2012-03-14 17:01:34 +00:00
|
|
|
stats.selection_size = dive_table.nr;
|
2012-01-15 23:21:56 +00:00
|
|
|
}
|
2012-09-10 19:17:28 +00:00
|
|
|
|
|
|
|
/* allocate sufficient space to hold the worst
|
|
|
|
* case (one dive per year or all dives during
|
|
|
|
* one month) for yearly and monthly statistics*/
|
|
|
|
|
|
|
|
size = sizeof(stats_t) * (dive_table.nr + 1);
|
2018-05-08 14:24:51 +00:00
|
|
|
tsize = sizeof(stats_t) * (NUM_DIVEMODE + 1);
|
2019-03-10 02:21:46 +00:00
|
|
|
dsize = sizeof(stats_t) * ((STATS_MAX_DEPTH / STATS_DEPTH_BUCKET) + 1);
|
|
|
|
tmsize = sizeof(stats_t) * ((STATS_MAX_TEMP / STATS_TEMP_BUCKET) + 1);
|
2018-10-06 08:58:12 +00:00
|
|
|
free_stats_summary(out);
|
|
|
|
out->stats_yearly = malloc(size);
|
|
|
|
out->stats_monthly = malloc(size);
|
|
|
|
out->stats_by_trip = malloc(size);
|
|
|
|
out->stats_by_type = malloc(tsize);
|
2019-03-10 02:21:46 +00:00
|
|
|
out->stats_by_depth = malloc(dsize);
|
|
|
|
out->stats_by_temp = malloc(tmsize);
|
|
|
|
if (!out->stats_yearly || !out->stats_monthly || !out->stats_by_trip ||
|
|
|
|
!out->stats_by_type || !out->stats_by_depth || !out->stats_by_temp)
|
2012-09-10 19:17:28 +00:00
|
|
|
return;
|
2018-10-06 08:58:12 +00:00
|
|
|
memset(out->stats_yearly, 0, size);
|
|
|
|
memset(out->stats_monthly, 0, size);
|
|
|
|
memset(out->stats_by_trip, 0, size);
|
|
|
|
memset(out->stats_by_type, 0, tsize);
|
2019-03-10 02:21:46 +00:00
|
|
|
memset(out->stats_by_depth, 0, dsize);
|
|
|
|
memset(out->stats_by_temp, 0, tmsize);
|
2018-10-06 08:58:12 +00:00
|
|
|
out->stats_yearly[0].is_year = true;
|
2012-09-10 19:17:28 +00:00
|
|
|
|
2016-01-17 19:18:36 +00:00
|
|
|
/* Setting the is_trip to true to show the location as first
|
|
|
|
* field in the statistics window */
|
2018-10-06 08:58:12 +00:00
|
|
|
out->stats_by_type[0].location = strdup(translate("gettextFromC", "All (by type stats)"));
|
|
|
|
out->stats_by_type[0].is_trip = true;
|
|
|
|
out->stats_by_type[1].location = strdup(translate("gettextFromC", divemode_text_ui[OC]));
|
|
|
|
out->stats_by_type[1].is_trip = true;
|
|
|
|
out->stats_by_type[2].location = strdup(translate("gettextFromC", divemode_text_ui[CCR]));
|
|
|
|
out->stats_by_type[2].is_trip = true;
|
|
|
|
out->stats_by_type[3].location = strdup(translate("gettextFromC", divemode_text_ui[PSCR]));
|
|
|
|
out->stats_by_type[3].is_trip = true;
|
|
|
|
out->stats_by_type[4].location = strdup(translate("gettextFromC", divemode_text_ui[FREEDIVE]));
|
|
|
|
out->stats_by_type[4].is_trip = true;
|
2016-01-17 19:18:36 +00:00
|
|
|
|
2019-03-10 02:21:46 +00:00
|
|
|
out->stats_by_depth[0].location = strdup(translate("gettextFromC", "All (by max depth stats)"));
|
|
|
|
out->stats_by_depth[0].is_trip = true;
|
|
|
|
|
|
|
|
out->stats_by_temp[0].location = strdup(translate("gettextFromC", "All (by min. temp stats)"));
|
|
|
|
out->stats_by_temp[0].is_trip = true;
|
|
|
|
|
2011-11-02 16:10:57 +00:00
|
|
|
/* this relies on the fact that the dives in the dive_table
|
|
|
|
* are in chronological order */
|
2014-05-19 21:37:19 +00:00
|
|
|
for_each_dive (idx, dp) {
|
2018-10-06 14:50:46 +00:00
|
|
|
if (selected_only && !dp->selected)
|
|
|
|
continue;
|
2012-03-14 17:01:34 +00:00
|
|
|
process_dive(dp, &stats);
|
2012-09-10 19:17:28 +00:00
|
|
|
|
|
|
|
/* yearly statistics */
|
2012-09-20 00:35:52 +00:00
|
|
|
utc_mkdate(dp->when, &tm);
|
2012-09-10 19:17:28 +00:00
|
|
|
if (current_year == 0)
|
2016-04-28 22:13:30 +00:00
|
|
|
current_year = tm.tm_year;
|
2012-09-10 19:17:28 +00:00
|
|
|
|
2016-04-28 22:13:30 +00:00
|
|
|
if (current_year != tm.tm_year) {
|
|
|
|
current_year = tm.tm_year;
|
2018-10-06 08:58:12 +00:00
|
|
|
process_dive(dp, &(out->stats_yearly[++year_iter]));
|
|
|
|
out->stats_yearly[year_iter].is_year = true;
|
2013-01-29 21:10:46 +00:00
|
|
|
} else {
|
2018-10-06 08:58:12 +00:00
|
|
|
process_dive(dp, &(out->stats_yearly[year_iter]));
|
2013-01-29 21:10:46 +00:00
|
|
|
}
|
2018-10-06 08:58:12 +00:00
|
|
|
out->stats_yearly[year_iter].selection_size++;
|
|
|
|
out->stats_yearly[year_iter].period = current_year;
|
2012-09-10 19:17:28 +00:00
|
|
|
|
2016-01-17 19:18:36 +00:00
|
|
|
/* stats_by_type[0] is all the dives combined */
|
2018-10-06 08:58:12 +00:00
|
|
|
out->stats_by_type[0].selection_size++;
|
|
|
|
process_dive(dp, &(out->stats_by_type[0]));
|
2016-01-17 19:18:36 +00:00
|
|
|
|
2018-10-06 08:58:12 +00:00
|
|
|
process_dive(dp, &(out->stats_by_type[dp->dc.divemode + 1]));
|
|
|
|
out->stats_by_type[dp->dc.divemode + 1].selection_size++;
|
2016-01-17 19:18:36 +00:00
|
|
|
|
2019-03-10 02:21:46 +00:00
|
|
|
/* stats_by_depth[0] is all the dives combined */
|
|
|
|
out->stats_by_depth[0].selection_size++;
|
|
|
|
process_dive(dp, &(out->stats_by_depth[0]));
|
|
|
|
|
|
|
|
d_idx = dp->maxdepth.mm / (STATS_DEPTH_BUCKET * 1000);
|
|
|
|
if (d_idx < 0)
|
|
|
|
d_idx = 0;
|
|
|
|
if (d_idx >= STATS_MAX_DEPTH / STATS_DEPTH_BUCKET)
|
|
|
|
d_idx = STATS_MAX_DEPTH / STATS_DEPTH_BUCKET - 1;
|
|
|
|
process_dive(dp, &(out->stats_by_depth[d_idx + 1]));
|
|
|
|
out->stats_by_depth[d_idx + 1].selection_size++;
|
|
|
|
|
|
|
|
/* stats_by_temp[0] is all the dives combined */
|
|
|
|
out->stats_by_temp[0].selection_size++;
|
|
|
|
process_dive(dp, &(out->stats_by_temp[0]));
|
|
|
|
|
|
|
|
t_idx = ((int)mkelvin_to_C(dp->mintemp.mkelvin)) / STATS_TEMP_BUCKET;
|
|
|
|
if (t_idx < 0)
|
|
|
|
t_idx = 0;
|
|
|
|
if (t_idx >= STATS_MAX_TEMP / STATS_TEMP_BUCKET)
|
|
|
|
t_idx = STATS_MAX_TEMP / STATS_TEMP_BUCKET - 1;
|
|
|
|
process_dive(dp, &(out->stats_by_temp[t_idx + 1]));
|
|
|
|
out->stats_by_temp[t_idx + 1].selection_size++;
|
|
|
|
|
2013-11-24 10:27:37 +00:00
|
|
|
if (dp->divetrip != NULL) {
|
|
|
|
if (trip_ptr != dp->divetrip) {
|
|
|
|
trip_ptr = dp->divetrip;
|
|
|
|
trip_iter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stats_by_trip[0] is all the dives combined */
|
2018-10-06 08:58:12 +00:00
|
|
|
out->stats_by_trip[0].selection_size++;
|
|
|
|
process_dive(dp, &(out->stats_by_trip[0]));
|
|
|
|
out->stats_by_trip[0].is_trip = true;
|
|
|
|
out->stats_by_trip[0].location = strdup(translate("gettextFromC", "All (by trip stats)"));
|
|
|
|
|
|
|
|
process_dive(dp, &(out->stats_by_trip[trip_iter]));
|
|
|
|
out->stats_by_trip[trip_iter].selection_size++;
|
|
|
|
out->stats_by_trip[trip_iter].is_trip = true;
|
|
|
|
out->stats_by_trip[trip_iter].location = dp->divetrip->location;
|
2013-11-24 01:09:34 +00:00
|
|
|
}
|
|
|
|
|
2012-09-10 19:17:28 +00:00
|
|
|
/* monthly statistics */
|
|
|
|
if (current_month == 0) {
|
2012-09-20 00:35:52 +00:00
|
|
|
current_month = tm.tm_mon + 1;
|
2012-09-10 19:17:28 +00:00
|
|
|
} else {
|
2012-09-20 00:35:52 +00:00
|
|
|
if (current_month != tm.tm_mon + 1)
|
|
|
|
current_month = tm.tm_mon + 1;
|
2012-09-10 19:17:28 +00:00
|
|
|
if (prev_month != current_month || prev_year != current_year)
|
|
|
|
month_iter++;
|
|
|
|
}
|
2018-10-06 08:58:12 +00:00
|
|
|
process_dive(dp, &(out->stats_monthly[month_iter]));
|
|
|
|
out->stats_monthly[month_iter].selection_size++;
|
|
|
|
out->stats_monthly[month_iter].period = current_month;
|
2012-09-10 19:17:28 +00:00
|
|
|
prev_month = current_month;
|
|
|
|
prev_year = current_year;
|
2012-03-14 17:01:34 +00:00
|
|
|
}
|
2019-03-10 02:21:46 +00:00
|
|
|
|
|
|
|
/* add labels for depth ranges up to maximum depth seen */
|
|
|
|
if (out->stats_by_depth[0].selection_size) {
|
|
|
|
d_idx = out->stats_by_depth[0].max_depth.mm;
|
|
|
|
if (d_idx > STATS_MAX_DEPTH * 1000)
|
|
|
|
d_idx = STATS_MAX_DEPTH * 1000;
|
|
|
|
for (r = 0; r * (STATS_DEPTH_BUCKET * 1000) < d_idx; ++r)
|
|
|
|
out->stats_by_depth[r+1].is_trip = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add labels for depth ranges up to maximum temperature seen */
|
|
|
|
if (out->stats_by_temp[0].selection_size) {
|
|
|
|
t_idx = (int)mkelvin_to_C(out->stats_by_temp[0].max_temp.mkelvin);
|
|
|
|
if (t_idx > STATS_MAX_TEMP)
|
|
|
|
t_idx = STATS_MAX_TEMP;
|
|
|
|
for (r = 0; r * STATS_TEMP_BUCKET < t_idx; ++r)
|
|
|
|
out->stats_by_temp[r+1].is_trip = true;
|
|
|
|
}
|
2012-03-14 17:01:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-06 08:58:12 +00:00
|
|
|
void free_stats_summary(struct stats_summary *stats)
|
|
|
|
{
|
|
|
|
free(stats->stats_yearly);
|
|
|
|
free(stats->stats_monthly);
|
|
|
|
free(stats->stats_by_trip);
|
|
|
|
free(stats->stats_by_type);
|
2019-03-10 02:21:46 +00:00
|
|
|
free(stats->stats_by_depth);
|
|
|
|
free(stats->stats_by_temp);
|
2018-10-06 08:58:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void init_stats_summary(struct stats_summary *stats)
|
|
|
|
{
|
|
|
|
stats->stats_yearly = NULL;
|
|
|
|
stats->stats_monthly = NULL;
|
|
|
|
stats->stats_by_trip = NULL;
|
|
|
|
stats->stats_by_type = NULL;
|
2019-03-10 02:21:46 +00:00
|
|
|
stats->stats_by_depth = NULL;
|
|
|
|
stats->stats_by_temp = NULL;
|
2018-10-06 08:58:12 +00:00
|
|
|
}
|
|
|
|
|
2012-08-14 04:11:09 +00:00
|
|
|
/* make sure we skip the selected summary entries */
|
2018-10-06 14:30:57 +00:00
|
|
|
void calculate_stats_selected(stats_t *stats_selection)
|
2012-03-14 17:01:34 +00:00
|
|
|
{
|
2012-08-20 12:48:07 +00:00
|
|
|
struct dive *dive;
|
|
|
|
unsigned int i, nr;
|
2012-03-14 17:01:34 +00:00
|
|
|
|
2018-10-06 14:30:57 +00:00
|
|
|
memset(stats_selection, 0, sizeof(*stats_selection));
|
2012-03-14 17:01:34 +00:00
|
|
|
|
2012-08-20 12:48:07 +00:00
|
|
|
nr = 0;
|
2012-08-21 22:51:34 +00:00
|
|
|
for_each_dive(i, dive) {
|
2012-08-20 12:48:07 +00:00
|
|
|
if (dive->selected) {
|
2018-10-06 14:30:57 +00:00
|
|
|
process_dive(dive, stats_selection);
|
2012-08-20 12:48:07 +00:00
|
|
|
nr++;
|
2011-11-02 16:10:57 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-06 14:30:57 +00:00
|
|
|
stats_selection->selection_size = nr;
|
2011-11-02 16:10:57 +00:00
|
|
|
}
|
|
|
|
|
2015-01-28 14:01:07 +00:00
|
|
|
#define SOME_GAS 5000 // 5bar drop in cylinder pressure makes cylinder used
|
|
|
|
|
2018-08-16 22:58:30 +00:00
|
|
|
bool has_gaschange_event(const struct dive *dive, const struct divecomputer *dc, int idx)
|
Start cleaning up sensor indexing for multiple sensors
This is a very timid start at making us actually use multiple sensors
without the magical special case for just CCR oxygen tracking.
It mainly does:
- turn the "sample->sensor" index into an array of two indexes, to
match the pressures themselves.
- get rid of dive->{oxygen_cylinder_index,diluent_cylinder_index},
since a CCR dive should now simply set the sample->sensor[] indices
correctly instead.
- in a couple of places, start actually looping over the sensors rather
than special-case the O2 case (although often the small "loops" are
just unrolled, since it's just two cases.
but in many cases we still end up only covering the zero sensor case,
because the CCR O2 sensor code coverage was fairly limited.
It's entirely possible (even likely) that this migth break some existing
case: it tries to be a fairly direct ("stupid") translation of the old
code, but unlike the preparatory patch this does actually does change
some semantics.
For example, right now the git loader code assumes that if the git save
data contains a o2pressure entry, it just hardcodes the O2 sensor index
to 1.
In fact, one issue is going to simply be that our file formats do not
have that multiple sensor format, but instead had very clearly encoded
things as being the CCR O2 pressure sensor.
But this is hopefully close to usable, and I will need feedback (and
maybe test cases) from people who have existing CCR dives with pressure
data.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 02:49:45 +00:00
|
|
|
{
|
2017-02-03 10:14:51 +00:00
|
|
|
bool first_gas_explicit = false;
|
2018-08-16 22:58:30 +00:00
|
|
|
const struct event *event = get_next_event(dc->events, "gaschange");
|
2017-02-03 10:14:51 +00:00
|
|
|
while (event) {
|
|
|
|
if (dc->sample && (event->time.seconds == 0 ||
|
|
|
|
(dc->samples && dc->sample[0].time.seconds == event->time.seconds)))
|
|
|
|
first_gas_explicit = true;
|
|
|
|
if (get_cylinder_index(dive, event) == idx)
|
|
|
|
return true;
|
|
|
|
event = get_next_event(event->next, "gaschange");
|
|
|
|
}
|
Start cleaning up sensor indexing for multiple sensors
This is a very timid start at making us actually use multiple sensors
without the magical special case for just CCR oxygen tracking.
It mainly does:
- turn the "sample->sensor" index into an array of two indexes, to
match the pressures themselves.
- get rid of dive->{oxygen_cylinder_index,diluent_cylinder_index},
since a CCR dive should now simply set the sample->sensor[] indices
correctly instead.
- in a couple of places, start actually looping over the sensors rather
than special-case the O2 case (although often the small "loops" are
just unrolled, since it's just two cases.
but in many cases we still end up only covering the zero sensor case,
because the CCR O2 sensor code coverage was fairly limited.
It's entirely possible (even likely) that this migth break some existing
case: it tries to be a fairly direct ("stupid") translation of the old
code, but unlike the preparatory patch this does actually does change
some semantics.
For example, right now the git loader code assumes that if the git save
data contains a o2pressure entry, it just hardcodes the O2 sensor index
to 1.
In fact, one issue is going to simply be that our file formats do not
have that multiple sensor format, but instead had very clearly encoded
things as being the CCR O2 pressure sensor.
But this is hopefully close to usable, and I will need feedback (and
maybe test cases) from people who have existing CCR dives with pressure
data.
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
2017-07-21 02:49:45 +00:00
|
|
|
if (dc->divemode == CCR) {
|
|
|
|
if (idx == get_cylinder_idx_by_use(dive, DILUENT))
|
|
|
|
return true;
|
|
|
|
if (idx == get_cylinder_idx_by_use(dive, OXYGEN))
|
|
|
|
return true;
|
|
|
|
}
|
2017-02-03 10:14:51 +00:00
|
|
|
return !first_gas_explicit && idx == 0;
|
|
|
|
}
|
|
|
|
|
2018-08-23 17:18:43 +00:00
|
|
|
bool is_cylinder_used(const struct dive *dive, int idx)
|
2013-05-31 04:40:46 +00:00
|
|
|
{
|
2018-08-23 17:18:43 +00:00
|
|
|
const struct divecomputer *dc;
|
2019-08-04 16:44:57 +00:00
|
|
|
cylinder_t *cyl;
|
|
|
|
if (idx < 0 || idx >= dive->cylinders.nr)
|
2014-01-15 18:54:41 +00:00
|
|
|
return false;
|
2013-05-31 04:40:46 +00:00
|
|
|
|
2019-08-04 16:44:57 +00:00
|
|
|
cyl = &dive->cylinders.cylinders[idx];
|
|
|
|
if ((cyl->start.mbar - cyl->end.mbar) > SOME_GAS)
|
2015-01-28 14:01:07 +00:00
|
|
|
return true;
|
2017-09-14 03:43:12 +00:00
|
|
|
|
2019-08-04 16:44:57 +00:00
|
|
|
if ((cyl->sample_start.mbar - cyl->sample_end.mbar) > SOME_GAS)
|
2017-09-14 03:43:12 +00:00
|
|
|
return true;
|
|
|
|
|
2014-05-13 22:32:45 +00:00
|
|
|
for_each_dc(dive, dc) {
|
2017-02-03 10:14:51 +00:00
|
|
|
if (has_gaschange_event(dive, dc, idx))
|
2014-11-17 00:09:19 +00:00
|
|
|
return true;
|
2013-05-31 04:40:46 +00:00
|
|
|
}
|
2014-01-15 18:54:41 +00:00
|
|
|
return false;
|
2013-05-31 04:40:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 17:18:43 +00:00
|
|
|
bool is_cylinder_prot(const struct dive *dive, int idx)
|
2017-11-29 09:16:00 +00:00
|
|
|
{
|
2018-08-23 17:18:43 +00:00
|
|
|
const struct divecomputer *dc;
|
2019-08-04 16:44:57 +00:00
|
|
|
if (idx < 0 || idx >= dive->cylinders.nr)
|
2017-11-29 09:16:00 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for_each_dc(dive, dc) {
|
|
|
|
if (has_gaschange_event(dive, dc, idx))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-08-04 16:44:57 +00:00
|
|
|
/* Returns a dynamically allocated array with dive->cylinders.nr entries,
|
|
|
|
* which has to be freed by the caller */
|
2019-06-27 05:42:09 +00:00
|
|
|
volume_t *get_gas_used(struct dive *dive)
|
2013-11-27 22:05:51 +00:00
|
|
|
{
|
|
|
|
int idx;
|
2017-02-03 10:14:51 +00:00
|
|
|
|
2019-08-04 16:44:57 +00:00
|
|
|
volume_t *gases = malloc(dive->cylinders.nr * sizeof(volume_t));
|
|
|
|
for (idx = 0; idx < dive->cylinders.nr; idx++) {
|
|
|
|
cylinder_t *cyl = &dive->cylinders.cylinders[idx];
|
2013-11-27 22:05:51 +00:00
|
|
|
pressure_t start, end;
|
|
|
|
|
|
|
|
start = cyl->start.mbar ? cyl->start : cyl->sample_start;
|
|
|
|
end = cyl->end.mbar ? cyl->end : cyl->sample_end;
|
2014-04-26 17:55:17 +00:00
|
|
|
if (end.mbar && start.mbar > end.mbar)
|
2013-11-27 22:05:51 +00:00
|
|
|
gases[idx].mliter = gas_volume(cyl, start) - gas_volume(cyl, end);
|
|
|
|
}
|
2019-06-27 05:42:09 +00:00
|
|
|
|
|
|
|
return gases;
|
2013-11-27 22:05:51 +00:00
|
|
|
}
|
|
|
|
|
2014-06-29 18:01:56 +00:00
|
|
|
/* Quite crude reverse-blender-function, but it produces a approx result */
|
|
|
|
static void get_gas_parts(struct gasmix mix, volume_t vol, int o2_in_topup, volume_t *o2, volume_t *he)
|
|
|
|
{
|
|
|
|
volume_t air = {};
|
|
|
|
|
2018-08-16 17:10:10 +00:00
|
|
|
if (gasmix_is_air(mix)) {
|
2014-06-29 18:01:56 +00:00
|
|
|
o2->mliter = 0;
|
|
|
|
he->mliter = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-08-16 17:10:10 +00:00
|
|
|
air.mliter = lrint(((double)vol.mliter * (1000 - get_he(mix) - get_o2(mix))) / (1000 - o2_in_topup));
|
|
|
|
he->mliter = lrint(((double)vol.mliter * get_he(mix)) / 1000.0);
|
2014-06-29 18:01:56 +00:00
|
|
|
o2->mliter += vol.mliter - he->mliter - air.mliter;
|
|
|
|
}
|
|
|
|
|
|
|
|
void selected_dives_gas_parts(volume_t *o2_tot, volume_t *he_tot)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
struct dive *d;
|
|
|
|
for_each_dive (i, d) {
|
|
|
|
if (!d->selected)
|
|
|
|
continue;
|
2019-06-27 05:42:09 +00:00
|
|
|
volume_t *diveGases = get_gas_used(d);
|
2019-08-04 16:44:57 +00:00
|
|
|
for (j = 0; j < d->cylinders.nr; j++) {
|
2014-06-29 18:01:56 +00:00
|
|
|
if (diveGases[j].mliter) {
|
|
|
|
volume_t o2 = {}, he = {};
|
2019-08-04 16:44:57 +00:00
|
|
|
get_gas_parts(d->cylinders.cylinders[j].gasmix, diveGases[j], O2_IN_AIR, &o2, &he);
|
2014-06-29 18:01:56 +00:00
|
|
|
o2_tot->mliter += o2.mliter;
|
|
|
|
he_tot->mliter += he.mliter;
|
|
|
|
}
|
|
|
|
}
|
2019-06-27 05:42:09 +00:00
|
|
|
free(diveGases);
|
2014-06-29 18:01:56 +00:00
|
|
|
}
|
|
|
|
}
|