summaryrefslogtreecommitdiffstats
path: root/dive.c
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2015-02-07 08:31:16 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2015-02-07 08:39:14 -0800
commit8f9a20a908c0bd7bfdf6e539ea3fe83a6a83cd06 (patch)
tree60fe43fdaa341094b9f58c2f70d2bc1535ac3ea7 /dive.c
parentcf915cdfad8b61c5473f61bfc4c37a377e3bc192 (diff)
downloadsubsurface-8f9a20a908c0bd7bfdf6e539ea3fe83a6a83cd06.tar.gz
Deal with setpoints in samples when switching a dive from CCR to OC
For some dive computers (at least the Shearwater Predator, I haven't checked if there are others), libdivecomputer used to return setpoint values in each sample even if the dive computer was in OC mode. Those setpoint values are redundant and confuse our algorithm that tries to detect if a dive is OC or CCR. So when manually switching from CCR to OC we make sure that there are no setpoint values in the samples. This is a destructive change - if the user switches to OC by mistake and accepts that change, even when switching back to CCR the setpoint changes during the dive are lost. I rewrote the code dealing with the events as it was rather confused. Looping over the events that way didn't make any sense since get_next_event() is guaranteed to give you the first (if any) event of the requested name. See #826 Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'dive.c')
-rw-r--r--dive.c34
1 files changed, 21 insertions, 13 deletions
diff --git a/dive.c b/dive.c
index 7b3ab21ef..c120dde0e 100644
--- a/dive.c
+++ b/dive.c
@@ -872,25 +872,33 @@ int explicit_first_cylinder(struct dive *dive, struct divecomputer *dc)
return 0;
}
+/* this gets called when the dive mode has changed (so OC vs. CC)
+ * there are two places we might have setpoints... events or in the samples
+ */
void update_setpoint_events(struct divecomputer *dc)
{
- struct event *ev = get_next_event(dc->events, "SP change");
- bool changed = false;
+ struct event *ev;
int new_setpoint = 0;
if (dc->divemode == CCR)
- new_setpoint = prefs.defaultsetpoint;
-
- while (ev) {
- if (ev->time.seconds == 0) {
- ev->value = new_setpoint;
- changed = true;
- }
- ev = get_next_event(ev->next, "SP change");
- }
- if (!changed) {
+ new_setpoint = prefs.defaultsetpoint;
+
+ if (dc->divemode == OC)
+ // make sure there's no setpoint in the samples
+ // this is an irreversible change - so switching a dive to OC
+ // by mistake when it's actually CCR is _bad_
+ for (int i = 0; i < dc->samples; i++)
+ dc->sample[i].setpoint.mbar = 0;
+
+ // an "SP change" event at t=0 is currently our marker for OC vs CCR
+ // this will need to change to a saner setup, but for now we can just
+ // check if such an event is there and adjust it, or add that event
+ ev = get_next_event(dc->events, "SP change");
+ if (ev && ev->time.seconds == 0) {
+ ev->value = new_setpoint;
+ } else {
if (!add_event(dc, 0, SAMPLE_EVENT_PO2, 0, new_setpoint, "SP change"))
- printf("Could not add setpoint change event\n");
+ fprintf(stderr, "Could not add setpoint change event\n");
}
}