From 3c4c7e98fd35b19e1148019ce1d6db1a664e6977 Mon Sep 17 00:00:00 2001
From: "Robert C. Helling" <helling@atdotde.de>
Date: Tue, 18 Nov 2014 10:30:24 +0100
Subject: [PATCH] Cleaning up pressures

Added comment about meaning of o2pressure in struct plot_info.

Turned some pressures from double (in bar) to pressure_t (in mbar)

Moved prototypes to header file.

Signed-off-by: Robert C. Helling <helling@atdotde.de>
---
 gaspressures.c |  6 ++---
 gaspressures.h | 13 +++++++++++
 profile.c      | 70 +++++++++++++++++++++++++++-------------------------------
 profile.h      | 13 ++++++-----
 4 files changed, 57 insertions(+), 45 deletions(-)

diff --git a/gaspressures.c b/gaspressures.c
index 58e5704..192eb28 100644
--- a/gaspressures.c
+++ b/gaspressures.c
@@ -97,7 +97,7 @@ static void dump_pr_track(pr_track_t **track_pr)
  * segments according to how big of a time_pressure area
  * they have.
  */
-static void fill_missing_segment_pressures(pr_track_t *list, enum interpolation_strategy strategy)
+void fill_missing_segment_pressures(pr_track_t *list, enum interpolation_strategy strategy)
 {
 	double magic;
 
@@ -171,7 +171,7 @@ void dump_pr_interpolate(int i, pr_interpolate_t interpolate_pr)
 #endif
 
 
-static struct pr_interpolate_struct get_pr_interpolate_data(pr_track_t *segment, struct plot_info *pi, int cur, int pressure)
+struct pr_interpolate_struct get_pr_interpolate_data(pr_track_t *segment, struct plot_info *pi, int cur, int pressure)
 { // cur = index to pi->entry corresponding to t_end of segment;
 	struct pr_interpolate_struct interpolate;
 	int i;
@@ -223,7 +223,7 @@ static struct pr_interpolate_struct get_pr_interpolate_data(pr_track_t *segment,
 	return interpolate;
 }
 
-static void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi, pr_track_t **track_pr, bool o2_flag)
+void fill_missing_tank_pressures(struct dive *dive, struct plot_info *pi, pr_track_t **track_pr, bool o2_flag)
 {
 	int cyl, i;
 	struct plot_data *entry;
diff --git a/gaspressures.h b/gaspressures.h
index 420c117..3c2ae08 100644
--- a/gaspressures.h
+++ b/gaspressures.h
@@ -1,6 +1,9 @@
 #ifndef GASPRESSURES_H
 #define GASPRESSURES_H
 
+#include <dive.h>
+#include <profile.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -29,6 +32,16 @@ struct pr_interpolate_struct {
 
 enum interpolation_strategy {SAC, TIME, CONSTANT};
 
+
+int calculate_ccr_po2(struct plot_data *entry, struct divecomputer *dc);
+
+void fill_missing_segment_pressures(pr_track_t *, enum interpolation_strategy strategy);
+struct pr_interpolate_struct get_pr_interpolate_data(pr_track_t *, struct plot_info *, int, int);
+void fill_missing_tank_pressures(struct dive *, struct plot_info *, pr_track_t **, bool);
+void populate_pressure_information(struct dive *, struct divecomputer *, struct plot_info *, int);
+
+
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/profile.c b/profile.c
index 63023b5..9f329e3 100644
--- a/profile.c
+++ b/profile.c
@@ -5,6 +5,7 @@
 #include <limits.h>
 #include <string.h>
 #include <assert.h>
+#include <profile.h>
 
 #include "dive.h"
 #include "display.h"
@@ -24,16 +25,10 @@ int selected_dive = -1; /* careful: 0 is a valid value */
 unsigned int dc_number = 0;
 
 static struct plot_data *last_pi_entry_new = NULL;
-double calculate_ccr_po2(struct plot_data *entry, struct divecomputer *dc);
-
-void fill_missing_segment_pressures(pr_track_t *);
-struct pr_interpolate_struct get_pr_interpolate_data(pr_track_t *, struct plot_info *, int);
-void fill_missing_tank_pressures(struct dive *, struct plot_info *, pr_track_t **, int);
-void populate_pressure_information(struct dive *, struct divecomputer *, struct plot_info *, int);
 
 #ifdef DEBUG_PI
 /* debugging tool - not normally used */
-static void dump_pi(struct plot_info *pi)
+void dump_pi(struct plot_info *pi)
 {
 	int i;
 
@@ -152,7 +147,7 @@ void remember_event(const char *eventname)
 }
 
 /* Get local sac-rate (in ml/min) between entry1 and entry2 */
-static int get_local_sac(struct plot_data *entry1, struct plot_data *entry2, struct dive *dive)
+int get_local_sac(struct plot_data *entry1, struct plot_data *entry2, struct dive *dive)
 {
 	int index = entry1->cylinderindex;
 	cylinder_t *cyl;
@@ -182,7 +177,7 @@ static int get_local_sac(struct plot_data *entry1, struct plot_data *entry2, str
 	return airuse / atm * 60 / duration;
 }
 
-static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
+void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot_data *first, struct plot_data *last, int index)
 {
 	struct plot_data *p = entry;
 	int time = entry->sec;
@@ -217,7 +212,7 @@ static void analyze_plot_info_minmax_minute(struct plot_data *entry, struct plot
 	entry->avg[index] = (avg + nr / 2) / nr;
 }
 
-static void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
+void analyze_plot_info_minmax(struct plot_data *entry, struct plot_data *first, struct plot_data *last)
 {
 	analyze_plot_info_minmax_minute(entry, first, last, 0);
 	analyze_plot_info_minmax_minute(entry, first, last, 1);
@@ -576,10 +571,10 @@ struct plot_data *populate_plot_entries(struct dive *dive, struct divecomputer *
 		entry->in_deco = sample->in_deco;
 		entry->cns = sample->cns;
 		if (dc->dctype == CCR) {
-			entry->o2pressure = sample->setpoint.mbar / 1000.0;   // for rebreathers
-			entry->o2sensor[0] = sample->o2sensor[0].mbar / 1000.0; // for up to three rebreather O2 sensors
-			entry->o2sensor[1] = sample->o2sensor[1].mbar / 1000.0;
-			entry->o2sensor[2] = sample->o2sensor[2].mbar / 1000.0;
+			entry->o2pressure.mbar = sample->setpoint.mbar;      // for rebreathers
+			entry->o2sensor[0].mbar = sample->o2sensor[0].mbar;  // for up to three rebreather O2 sensors
+			entry->o2sensor[1].mbar = sample->o2sensor[1].mbar;
+			entry->o2sensor[2].mbar = sample->o2sensor[2].mbar;
 		} else {
 			entry->pressures.o2 = sample->setpoint.mbar / 1000.0;
 		}
@@ -741,7 +736,7 @@ static void calculate_ndl_tts(double tissue_tolerance, struct plot_data *entry,
 		while (entry->ndl_calc < max_ndl && deco_allowed_depth(tissue_tolerance, surface_pressure, dive, 1) <= 0) {
 			entry->ndl_calc += time_stepsize;
 			tissue_tolerance = add_segment(depth_to_mbar(entry->depth, dive) / 1000.0,
-						       &dive->cylinder[cylinderindex].gasmix, time_stepsize, entry->o2pressure * 1000, dive, prefs.bottomsac);
+						       &dive->cylinder[cylinderindex].gasmix, time_stepsize,  entry->o2pressure.mbar , dive, prefs.bottomsac);
 		}
 		/* we don't need to calculate anything else */
 		return;
@@ -753,7 +748,7 @@ static void calculate_ndl_tts(double tissue_tolerance, struct plot_data *entry,
 	/* Add segments for movement to stopdepth */
 	for (; ascent_depth > next_stop; ascent_depth -= ascent_mm_per_step, entry->tts_calc += ascent_s_per_step) {
 		tissue_tolerance = add_segment(depth_to_mbar(ascent_depth, dive) / 1000.0,
-					       &dive->cylinder[cylinderindex].gasmix, ascent_s_per_step, entry->o2pressure * 1000, dive, prefs.decosac);
+					       &dive->cylinder[cylinderindex].gasmix, ascent_s_per_step, entry->o2pressure.mbar , dive, prefs.decosac);
 		next_stop = ROUND_UP(deco_allowed_depth(tissue_tolerance, surface_pressure, dive, 1), deco_stepsize);
 	}
 	ascent_depth = next_stop;
@@ -771,13 +766,13 @@ static void calculate_ndl_tts(double tissue_tolerance, struct plot_data *entry,
 
 		entry->tts_calc += time_stepsize;
 		tissue_tolerance = add_segment(depth_to_mbar(ascent_depth, dive) / 1000.0,
-					       &dive->cylinder[cylinderindex].gasmix, time_stepsize, entry->o2pressure * 1000, dive, prefs.decosac);
+					       &dive->cylinder[cylinderindex].gasmix, time_stepsize, entry->o2pressure.mbar, dive, prefs.decosac);
 
 		if (deco_allowed_depth(tissue_tolerance, surface_pressure, dive, 1) <= next_stop) {
 			/* move to the next stop and add the travel between stops */
 			for (; ascent_depth > next_stop; ascent_depth -= ascent_mm_per_deco_step, entry->tts_calc += ascent_s_per_deco_step)
 				add_segment(depth_to_mbar(ascent_depth, dive) / 1000.0,
-					    &dive->cylinder[cylinderindex].gasmix, ascent_s_per_deco_step, entry->o2pressure * 1000, dive, prefs.decosac);
+					    &dive->cylinder[cylinderindex].gasmix, ascent_s_per_deco_step, entry->o2pressure.mbar, dive, prefs.decosac);
 			ascent_depth = next_stop;
 			next_stop -= deco_stepsize;
 		}
@@ -805,7 +800,7 @@ void calculate_deco_information(struct dive *dive, struct divecomputer *dc, stru
 		for (j = t0 + time_stepsize; j <= t1; j += time_stepsize) {
 			int depth = interpolate(entry[-1].depth, entry[0].depth, j - t0, t1 - t0);
 			double min_pressure = add_segment(depth_to_mbar(depth, dive) / 1000.0,
-							  &dive->cylinder[entry->cylinderindex].gasmix, time_stepsize, entry->o2pressure * 1000, dive, entry->sac);
+							  &dive->cylinder[entry->cylinderindex].gasmix, time_stepsize, entry->o2pressure.mbar, dive, entry->sac);
 			tissue_tolerance = min_pressure;
 			if (j - t0 < time_stepsize)
 				time_stepsize = j - t0;
@@ -856,21 +851,21 @@ void calculate_deco_information(struct dive *dive, struct divecomputer *dc, stru
  * calculates the po2 value from the sensor data. Several rules are applied, depending on how many o2 sensors
  * there are and the differences among the readings from these sensors.
  */
-double calculate_ccr_po2(struct plot_data *entry, struct divecomputer *dc) {
-	double sump = 0.0, minp = 999.9, maxp = -999.9;
-	double diff_limit = 100; // The limit beyond which O2 sensor differences are considered significant (default = 100 mbar)
+int calculate_ccr_po2(struct plot_data *entry, struct divecomputer *dc) {
+	int sump = 0, minp = 999999, maxp = -999999;
+	int diff_limit = 100; // The limit beyond which O2 sensor differences are considered significant (default = 100 mbar)
 	int i, np = 0;
 
 	for (i=0; i < dc->no_o2sensors; i++)
-		if (entry->o2sensor[i]) { // Valid reading
+		if (entry->o2sensor[i].mbar) { // Valid reading
 			++np;
-			sump += entry->o2sensor[i];
-			minp = MIN(minp, entry->o2sensor[i]);
-			maxp = MAX(maxp, entry->o2sensor[i]);
+			sump += entry->o2sensor[i].mbar;
+			minp = MIN(minp, entry->o2sensor[i].mbar);
+			maxp = MAX(maxp, entry->o2sensor[i].mbar);
 		}
 	switch (np) {
 	case 0: // Uhoh
-		return entry->o2pressure;
+		return entry->o2pressure.mbar / 1000.0;
 	case 1: // Return what we have
 		return sump;
 	case 2: // Take the average
@@ -889,7 +884,7 @@ double calculate_ccr_po2(struct plot_data *entry, struct divecomputer *dc) {
 		}
 	default: // This should not happen
 		assert(np <= 3);
-		return 0.0;
+		return 0;
 	}
 }
 
@@ -944,26 +939,27 @@ void fill_o2_values(struct divecomputer *dc, struct plot_info *pi, struct dive *
  * for plotting. This function called by: create_plot_info_new() */
 {
 	int i, j;
-	double last_sensor[3], o2pressure, amb_pressure;
+	pressure_t last_sensor[3], o2pressure;
+	pressure_t amb_pressure;
 
 	for (i = 0; i < pi->nr; i++) {
 		struct plot_data *entry = pi->entry + i;
 		if (dc->dctype == CCR) {
 			if (i == 0) {	// For 1st iteration, initialise the last_sensor values
 				for (j = 0; j < dc->no_o2sensors; j++)
-					last_sensor[j] = pi->entry->o2sensor[j];
+					last_sensor[j].mbar = pi->entry->o2sensor[j].mbar;
 			} else {	// Now re-insert the missing oxygen pressure values
 				for (j = 0; j < dc->no_o2sensors; j++)
-					if (entry->o2sensor[j])
-						last_sensor[j] = entry->o2sensor[j];
+					if (entry->o2sensor[j].mbar)
+						last_sensor[j].mbar = entry->o2sensor[j].mbar;
 					else
-						entry->o2sensor[j] = last_sensor[j];
+						entry->o2sensor[j].mbar = last_sensor[j].mbar;
 			}		// having initialised the empty o2 sensor values for this point on the profile,
-			amb_pressure = depth_to_mbar(entry->depth, dive) / 1000.0;
-			o2pressure = calculate_ccr_po2(entry,dc);	// ...calculate the po2 based on the sensor data
-			entry->o2pressure = MIN(o2pressure, amb_pressure);
+			amb_pressure.mbar = depth_to_mbar(entry->depth, dive);
+			o2pressure.mbar = calculate_ccr_po2(entry,dc);	// ...calculate the po2 based on the sensor data
+			entry->o2pressure.mbar = MIN(o2pressure.mbar, amb_pressure.mbar);
 		} else {
-			entry->o2pressure = 0.0;  // initialise po2 to zero for dctype = OC
+			entry->o2pressure.mbar = 0;  // initialise po2 to zero for dctype = OC
 		}
 	}
 }
diff --git a/profile.h b/profile.h
index 196a881..d057f04 100644
--- a/profile.h
+++ b/profile.h
@@ -1,6 +1,8 @@
 #ifndef PROFILE_H
 #define PROFILE_H
 
+#include <dive.h>
+
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -20,11 +22,11 @@ struct plot_data {
 	unsigned int in_deco : 1;
 	int cylinderindex;
 	int sec;
-	/* pressure[0] is sensor pressure [when CCR, the pressure of the oxygen cylinder]
-	 * pressure[1] is interpolated pressure */
+	/* pressure[0] is sensor cylinder pressure [when CCR, the pressure of the diluent cylinder]
+	 * pressure[1] is interpolated cylinder pressure */
 	int pressure[2];
-	/* diluentpressure[0] is diluent pressure [CCR]
-	 * diluentpressure[1] is interpolated diluent pressure [CCR] */
+	/* o2pressure[0] is o2 cylinder pressure [CCR]
+	 * o2pressure[1] is interpolated o2 cylinder pressure [CCR] */
 	int o2cylinderpressure[2];
 	int temperature;
 	/* Depth info */
@@ -40,7 +42,8 @@ struct plot_data {
 	int smoothed;
 	int sac;
 	struct gas_pressures pressures;
-	double o2pressure, o2sensor[3]; //for rebreathers with up to 3 PO2 sensors
+	pressure_t o2pressure;  // for rebreathers, this is consensus measured po2, or setpoint otherwise. 0 for OC.
+	pressure_t o2sensor[3]; //for rebreathers with up to 3 PO2 sensors
 	double mod, ead, end, eadd;
 	velocity_t velocity;
 	int speed;
-- 
1.9.3 (Apple Git-50)

