[PATCH 1/2] Make sample pressure helper functions available to everybody

Linus Torvalds torvalds at linux-foundation.org
Tue Jul 25 19:26:55 PDT 2017


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Tue, 25 Jul 2017 18:33:10 -0700
Subject: [PATCH 1/2] Make sample pressure helper functions available to everybody

We had a "add_sample_pressure()" helper functions that was local to just
the libdivecomputer downloading code, but it really is applicable to
pretty much any code that adds cylinder pressure data to a sample.

Also add another helper: "legacy_format_o2pressures()" which checks the
sample data to see if we can use the legacy format, and returns the o2
pressure sensor to use for that legacy format.

Because both the XML and the git save format will need a way to save the
compatible old-style information, when possible, but save an extended
format for when we have data from multiple concurrent sensors.

Signed-off-by: Linus Torvalds <torvalds at linux-foundation.org>
---

I should have made the "add_sample_pressure()" thing public from the 
beginning.

And the "legacy_format_o2pressures()" may look odd here with no actual 
users yet, but it's fairly simple code and it will make the next patch 
more legible because it can just use these simple helpers that both the 
XML and git format will want to use.

 core/dive.c            | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++
 core/dive.h            |  2 ++
 core/libdivecomputer.c | 41 -------------------------
 3 files changed, 85 insertions(+), 41 deletions(-)

diff --git a/core/dive.c b/core/dive.c
index 8270c547..42ef687e 100644
--- a/core/dive.c
+++ b/core/dive.c
@@ -35,6 +35,89 @@ const char *cylinderuse_text[] = {
 };
 const char *divemode_text[] = { "OC", "CCR", "PSCR", "Freedive" };
 
+/*
+ * Adding a cylinder pressure sample field is not quite as trivial as it
+ * perhaps should be.
+ *
+ * We try to keep the same sensor index for the same sensor, so that even
+ * if the dive computer doesn't give pressure information for every sample,
+ * we don't move pressure information around between the different sensor
+ * indexes.
+ *
+ * The "prepare_sample()" function will always copy the sensor indices
+ * from the previous sample, so the indexes are pre-populated (but the
+ * pressures obviously are not)
+ */
+void add_sample_pressure(struct sample *sample, int sensor, int mbar)
+{
+	int idx;
+
+	if (!mbar)
+		return;
+
+	/* Do we already have a slot for this sensor */
+	for (idx = 0; idx < MAX_SENSORS; idx++) {
+		if (sensor != sample->sensor[idx])
+			continue;
+		sample->pressure[idx].mbar = mbar;
+		return;
+	}
+
+	/* Pick the first unused index if we couldn't reuse one */
+	for (idx = 0; idx < MAX_SENSORS; idx++) {
+		if (sample->pressure[idx].mbar)
+			continue;
+		sample->sensor[idx] = sensor;
+		sample->pressure[idx].mbar = mbar;
+		return;
+	}
+
+	/* We do not have enough slots for the pressure samples. */
+	/* Should we warn the user about dropping pressure data? */
+}
+
+/*
+ * The legacy format for sample pressures has a single pressure
+ * for each sample that can have any sensor, plus a possible
+ * "o2pressure" that is fixed to the Oxygen sensor for a CCR dive.
+ *
+ * For more complex pressure data, we have to use explicit
+ * cylinder indexes for each sample.
+ *
+ * This function returns a negative number for "no legacy mode",
+ * or a non-negative number that indicates the o2 sensor index.
+ */
+int legacy_format_o2pressures(struct dive *dive, struct divecomputer *dc)
+{
+	int i, o2sensor;
+
+	o2sensor = (dc->divemode == CCR) ? get_cylinder_idx_by_use(dive, OXYGEN) : -1;
+	for (i = 0; i < dc->samples; i++) {
+		struct sample *s = dc->sample + i;
+		int seen_pressure = 0, idx;
+
+		for (idx = 0; idx < MAX_SENSORS; idx++) {
+			int sensor = s->sensor[idx];
+			pressure_t p = s->pressure[idx];
+
+			if (!p.mbar)
+				continue;
+			if (sensor == o2sensor)
+				continue;
+			if (seen_pressure)
+				return -1;
+			seen_pressure = 1;
+		}
+	}
+
+	/*
+	 * Use legacy mode: if we have no O2 sensor we return a
+	 * positive sensor index that is guaranmteed to not match
+	 * any sensor (we encode it as 8 bits).
+	 */
+	return o2sensor < 0 ? 256 : o2sensor;
+}
+
 int event_is_gaschange(struct event *ev)
 {
 	return ev->type == SAMPLE_EVENT_GASCHANGE ||
diff --git a/core/dive.h b/core/dive.h
index df0c4107..eb976f56 100644
--- a/core/dive.h
+++ b/core/dive.h
@@ -760,6 +760,8 @@ extern void clear_table(struct dive_table *table);
 
 extern struct sample *prepare_sample(struct divecomputer *dc);
 extern void finish_sample(struct divecomputer *dc);
+extern void add_sample_pressure(struct sample *sample, int sensor, int mbar);
+extern int legacy_format_o2pressures(struct dive *dive, struct divecomputer *dc);
 
 extern bool has_hr_data(struct divecomputer *dc);
 
diff --git a/core/libdivecomputer.c b/core/libdivecomputer.c
index 819db022..2c5f607d 100644
--- a/core/libdivecomputer.c
+++ b/core/libdivecomputer.c
@@ -307,47 +307,6 @@ static void handle_gasmix(struct divecomputer *dc, struct sample *sample, int id
 	current_gas_index = idx;
 }
 
-/*
- * Adding a cylinder pressure sample field is not quite as trivial as it
- * perhaps should be.
- *
- * We try to keep the same sensor index for the same sensor, so that even
- * if the dive computer doesn't give pressure information for every sample,
- * we don't move pressure information around between the different sensor
- * indexes.
- *
- * The "prepare_sample()" function will always copy the sensor indices
- * from the previous sample, so the indexes are pre-populated (but the
- * pressures obviously are not)
- */
-static void add_sample_pressure(struct sample *sample, int sensor, int mbar)
-{
-	int idx;
-
-	if (!mbar)
-		return;
-
-	/* Do we already have a slot for this sensor */
-	for (idx = 0; idx < MAX_SENSORS; idx++) {
-		if (sensor != sample->sensor[idx])
-			continue;
-		sample->pressure[idx].mbar = mbar;
-		return;
-	}
-
-	/* Pick the first unused index if we couldn't reuse one */
-	for (idx = 0; idx < MAX_SENSORS; idx++) {
-		if (sample->pressure[idx].mbar)
-			continue;
-		sample->sensor[idx] = sensor;
-		sample->pressure[idx].mbar = mbar;
-		return;
-	}
-
-	/* We do not have enough slots for the pressure samples. */
-	/* Should we warn the user about dropping pressure data? */
-}
-
 void
 sample_cb(dc_sample_type_t type, dc_sample_value_t value, void *userdata)
 {
-- 
2.13.1.518.g0d864c4df



More information about the subsurface mailing list