From 92dadbc1d4bacd5af4eb1b2e5bb3e6d2f0e954fa Mon Sep 17 00:00:00 2001 From: Tim Segers Date: Thu, 20 Oct 2022 20:33:43 +0200 Subject: Change waypoint callback from function-only to function+arg combination --- src/opendeco.c | 7 ++++++- src/schedule.c | 25 +++++++++++++------------ src/schedule.h | 11 +++++++---- 3 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/opendeco.c b/src/opendeco.c index 7759b1d..9ef08fa 100644 --- a/src/opendeco.c +++ b/src/opendeco.c @@ -174,7 +174,7 @@ void print_gas_use() } } -void print_segment_callback(const decostate_t *ds, const waypoint_t wp, segtype_t type) +void print_segment_callback_fn(const decostate_t *ds, const waypoint_t wp, segtype_t type, void *arg) { static double last_depth; static double runtime; @@ -299,6 +299,11 @@ int main(int argc, char *argv[]) {.depth = abs_depth(msw_to_bar(arguments.depth)), .time = bottom_time, &bottom_gas}, }; + waypoint_callback_t print_segment_callback = { + .fn = &print_segment_callback_fn, + .arg = NULL, + }; + print_planhead(); simulate_dive(&ds, waypoints, len(waypoints), &print_segment_callback); diff --git a/src/schedule.c b/src/schedule.c index 5630ffb..1a002e0 100644 --- a/src/schedule.c +++ b/src/schedule.c @@ -37,7 +37,7 @@ int direct_ascent(const decostate_t *ds, const double depth, const double time, return gauge_depth(ceiling(&ds_, ds_.gfhi)) <= 0; } -void simulate_dive(decostate_t *ds, waypoint_t *waypoints, const int nof_waypoints, waypoint_callback_t wp_cb) +void simulate_dive(decostate_t *ds, waypoint_t *waypoints, const int nof_waypoints, waypoint_callback_t *wp_cb) { double depth = abs_depth(0); @@ -53,8 +53,8 @@ void simulate_dive(decostate_t *ds, waypoint_t *waypoints, const int nof_waypoin depth = d; - if (wp_cb) - wp_cb(ds, (waypoint_t){.depth = d, .time = t, .gas = g}, SEG_DIVE); + if (wp_cb && wp_cb->fn) + wp_cb->fn(ds, (waypoint_t){.depth = d, .time = t, .gas = g}, SEG_DIVE, wp_cb->arg); } } @@ -125,7 +125,7 @@ static int surfaced(const double depth) } decoinfo_t calc_deco(decostate_t *ds, const double start_depth, const gas_t *start_gas, const gas_t *deco_gasses, - const int nof_gasses, waypoint_callback_t wp_cb) + const int nof_gasses, waypoint_callback_t *wp_cb) { decoinfo_t ret = {.tts = 0, .ndl = 0}; @@ -157,8 +157,9 @@ decoinfo_t calc_deco(decostate_t *ds, const double start_depth, const gas_t *sta /* emit waypoint */ waypoint_time = fabs(last_waypoint_depth - depth) / asc_per_min; - if (wp_cb) - wp_cb(ds, (waypoint_t){.depth = depth, .time = waypoint_time, .gas = gas}, SEG_TRAVEL); + if (wp_cb && wp_cb->fn) + wp_cb->fn(ds, (waypoint_t){.depth = depth, .time = waypoint_time, .gas = gas}, SEG_TRAVEL, + wp_cb->arg); last_waypoint_depth = depth; @@ -167,8 +168,8 @@ decoinfo_t calc_deco(decostate_t *ds, const double start_depth, const gas_t *sta ret.tts += add_segment_const(ds, depth, 1, gas); - if (wp_cb) - wp_cb(ds, (waypoint_t){.depth = depth, .time = 1, .gas = gas}, SEG_GAS_SWITCH); + if (wp_cb && wp_cb->fn) + wp_cb->fn(ds, (waypoint_t){.depth = depth, .time = 1, .gas = gas}, SEG_GAS_SWITCH, wp_cb->arg); continue; } @@ -205,8 +206,8 @@ decoinfo_t calc_deco(decostate_t *ds, const double start_depth, const gas_t *sta waypoint_time = fabs(last_waypoint_depth - depth) / asc_per_min; enum segtype_t segtype = surfaced(depth) ? SEG_SURFACE : SEG_TRAVEL; - if (wp_cb && waypoint_time) - wp_cb(ds, (waypoint_t){.depth = depth, .time = waypoint_time, .gas = gas}, segtype); + if (wp_cb && wp_cb->fn && waypoint_time) + wp_cb->fn(ds, (waypoint_t){.depth = depth, .time = waypoint_time, .gas = gas}, segtype, wp_cb->arg); /* terminate if we surfaced */ if (surfaced(depth)) @@ -223,7 +224,7 @@ decoinfo_t calc_deco(decostate_t *ds, const double start_depth, const gas_t *sta ret.tts += stoplen; - if (wp_cb) - wp_cb(ds, (waypoint_t){.depth = depth, .time = stoplen, .gas = gas}, SEG_DECO_STOP); + if (wp_cb && wp_cb->fn) + wp_cb->fn(ds, (waypoint_t){.depth = depth, .time = stoplen, .gas = gas}, SEG_DECO_STOP, wp_cb->arg); } } diff --git a/src/schedule.h b/src/schedule.h index e3b69ec..299217e 100644 --- a/src/schedule.h +++ b/src/schedule.h @@ -29,20 +29,23 @@ typedef enum segtype_t { SEG_TRAVEL, } segtype_t; +typedef struct waypoint_callback_t { + void (*fn)(const decostate_t *ds, const waypoint_t, const segtype_t, void *arg); + void *arg; +} waypoint_callback_t; + /* global variables */ extern int SWITCH_INTERMEDIATE; /* functions */ -typedef void (*waypoint_callback_t)(const decostate_t *ds, const waypoint_t, const segtype_t); - const gas_t *best_gas(const double depth, const gas_t *gasses, const int nof_gasses); int direct_ascent(const decostate_t *ds, const double depth, const double time, const gas_t *gas); double calc_ndl(decostate_t *ds, const double depth, const double ascrate, const gas_t *gas); -void simulate_dive(decostate_t *ds, waypoint_t *waypoints, const int nof_waypoints, waypoint_callback_t wp_cb); +void simulate_dive(decostate_t *ds, waypoint_t *waypoints, const int nof_waypoints, waypoint_callback_t *wp_cb); decoinfo_t calc_deco(decostate_t *ds, const double start_depth, const gas_t *start_gas, const gas_t *deco_gasses, - const int nof_gasses, waypoint_callback_t wp_cb); + const int nof_gasses, waypoint_callback_t *wp_cb); #endif /* end of include guard: SCHEDULE_H */ -- cgit v1.2.3-70-g09d2