From 9fe1951db29a3470e525c9a4e03612d6f7d5ecbb Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Wed, 4 Mar 2020 19:41:40 +0100 Subject: [PATCH] core: split out create_gas_change_event() from add_gas_change_event() For undo, we want to create gas change events without adding them immediately to the dive computer. Signed-off-by: Berthold Stoeger --- core/dive.c | 38 ++++++++++++++++++++++---------------- core/dive.h | 1 + 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/core/dive.c b/core/dive.c index d46f2de1d..849425389 100644 --- a/core/dive.c +++ b/core/dive.c @@ -167,6 +167,26 @@ struct event *create_event(unsigned int time, int type, int flags, int value, co return ev; } +/* warning: does not test idx for validity */ +struct event *create_gas_switch_event(struct dive *dive, struct divecomputer *dc, int seconds, int idx) +{ + /* The gas switch event format is insane for historical reasons */ + struct gasmix mix = get_cylinder(dive, idx)->gasmix; + int o2 = get_o2(mix); + int he = get_he(mix); + struct event *ev; + int value; + + o2 = (o2 + 5) / 10; + he = (he + 5) / 10; + value = o2 + (he << 16); + + ev = create_event(seconds, he ? SAMPLE_EVENT_GASCHANGE2 : SAMPLE_EVENT_GASCHANGE, 0, value, "gaschange"); + ev->gas.index = idx; + ev->gas.mix = mix; + return ev; +} + struct event *clone_event_rename(const struct event *ev, const char *name) { return create_event(ev->time.seconds, ev->type, ev->flags, ev->value, name); @@ -205,22 +225,8 @@ void add_gas_switch_event(struct dive *dive, struct divecomputer *dc, int second report_error("Unknown cylinder index: %d", idx); return; } - /* The gas switch event format is insane for historical reasons */ - struct gasmix mix = get_cylinder(dive, idx)->gasmix; - int o2 = get_o2(mix); - int he = get_he(mix); - struct event *ev; - int value; - - o2 = (o2 + 5) / 10; - he = (he + 5) / 10; - value = o2 + (he << 16); - - ev = add_event(dc, seconds, he ? SAMPLE_EVENT_GASCHANGE2 : SAMPLE_EVENT_GASCHANGE, 0, value, "gaschange"); - if (ev) { - ev->gas.index = idx; - ev->gas.mix = mix; - } + struct event *ev = create_gas_switch_event(dive, dc, seconds, idx); + add_event_to_dc(dc, ev); } /* Substitutes an event in a divecomputer for another. No reordering is performed! */ diff --git a/core/dive.h b/core/dive.h index 727d4bbff..2f4e96504 100644 --- a/core/dive.h +++ b/core/dive.h @@ -378,6 +378,7 @@ extern bool is_cylinder_used(const struct dive *dive, int idx); extern bool is_cylinder_prot(const struct dive *dive, int idx); extern void add_gas_switch_event(struct dive *dive, struct divecomputer *dc, int time, int idx); extern struct event *create_event(unsigned int time, int type, int flags, int value, const char *name); +extern struct event *create_gas_switch_event(struct dive *dive, struct divecomputer *dc, int seconds, int idx); extern struct event *clone_event_rename(const struct event *ev, const char *name); extern void add_event_to_dc(struct divecomputer *dc, struct event *ev); extern void swap_event(struct divecomputer *dc, struct event *from, struct event *to);