[PATCH 1/4] Add "get_gasmix()" helper function to iterate over gas changes

Linus Torvalds torvalds at linux-foundation.org
Fri Jul 28 11:59:12 PDT 2017


From: Linus Torvalds <torvalds at linux-foundation.org>
Date: Fri, 28 Jul 2017 10:22:17 -0700
Subject: [PATCH 1/4] Add "get_gasmix()" helper function to iterate over gas changes

We have a few places that used to get the gasmix by looking at the
sensor index in the plot data, which really doesn't work any more.

To make it easier for those users to convert to the new world order,
this adds a "get_gasmix()" function.  The gasmix function takes as its
argument the dive, the dive computer, and the time.

In addition, for good performance (to avoid looping over the event list
over and over and over again) it maintains a pointer to the next gas
switch event, and the previous gas.  Those need to be initialized to
NULL by the caller, so the standard use-case pattern basically looks
like this:

	struct gasmix *gasmix = NULL;
	struct event *ev = NULL;

	loop over samples or plot events in increasing time order: {
		...
		gasmix = get_gasmix(dive, dc, time, &ev, gasmix);
		...
	}

and then you can see what the currently breathing gas is at that time.

If for some reason you need to walk backwards in time, you can just pass
in a NULL gasmix again, which will reset the event iterator (at the cost
of now having to walk all the events again).

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

Helper function, obviously no actual behavioural changes.

This might be useful for the "TankItem" thing too, to colorize the bottom 
bar by gas use.

 core/dive.h | 17 +++++++++++++++++
 1 file changed, 17 insertions(+)

diff --git a/core/dive.h b/core/dive.h
index eb976f56..d19ccca4 100644
--- a/core/dive.h
+++ b/core/dive.h
@@ -913,6 +913,23 @@ void delete_single_dive(int idx);
 
 struct event *get_next_event(struct event *event, const char *name);
 
+static inline struct gasmix *get_gasmix(struct dive *dive, struct divecomputer *dc, int time, struct event **evp, struct gasmix *gasmix)
+{
+	struct event *ev = *evp;
+
+	if (!gasmix) {
+		int cyl = explicit_first_cylinder(dive, dc);
+		gasmix = &dive->cylinder[cyl].gasmix;
+		ev = dc->events;
+	}
+	while (ev && ev->time.seconds < time) {
+		gasmix = get_gasmix_from_event(dive, ev);
+		ev = get_next_event(ev->next, "gaschange");
+	}
+	*evp = ev;
+	return gasmix;
+}
+
 
 /* these structs holds the information that
  * describes the cylinders / weight systems.
-- 
2.13.1.518.g0d864c4df



More information about the subsurface mailing list