From 18b8247cb357a9a716846854e451e306b752e542 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Tue, 1 Nov 2011 11:39:52 -0700 Subject: Add new helper function to get temperature and unit Designed along the lines of get_depth_units - except we don't define a specific number of digits to show. Use this in the one spot we need it right now in profile.c Signed-off-by: Dirk Hohndel --- dive.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'dive.c') diff --git a/dive.c b/dive.c index ed3908928..c913e9476 100644 --- a/dive.c +++ b/dive.c @@ -29,6 +29,23 @@ void add_event(struct dive *dive, int time, int type, int flags, int value, cons remember_event(name); } +double get_temp_units(unsigned int mk, const char **units) +{ + double deg; + const char *unit; + + if (output_units.temperature == FAHRENHEIT) { + deg = mkelvin_to_F(mk); + unit = UTF8_DEGREE "F"; + } else { + deg = mkelvin_to_C(mk); + unit = UTF8_DEGREE "C"; + } + if (units) + *units = unit; + return deg; +} + double get_depth_units(unsigned int mm, int *frac, const char **units) { int decimals; -- cgit v1.2.3-70-g09d2 From a487f6c9314f0fe5e4173b670d9653067285da21 Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Tue, 1 Nov 2011 19:56:14 -0700 Subject: More consistency improvements Treat SAC and OTU consistently: - SAC is now a member of struct dive - it's calculated / populated at the same time with a helper function with consistent API Create get_volume_units function that returns volumes (e.g. used in SAC rates) based on preferred units - make sure we have these conversions just once in the code. Signed-off-by: Dirk Hohndel --- dive.c | 25 +++++++++++++++++++++++++ dive.h | 8 +++++++- divelist.c | 14 ++++++-------- 3 files changed, 38 insertions(+), 9 deletions(-) (limited to 'dive.c') diff --git a/dive.c b/dive.c index c913e9476..53a874b9b 100644 --- a/dive.c +++ b/dive.c @@ -46,6 +46,31 @@ double get_temp_units(unsigned int mk, const char **units) return deg; } +double get_volume_units(unsigned int ml, int *frac, const char **units) +{ + int decimals; + double vol; + const char *unit; + + switch (output_units.volume) { + case LITER: + vol = ml / 1000.0; + unit = "l"; + decimals = 1; + break; + case CUFT: + vol = ml_to_cuft(ml); + unit = "cuft"; + decimals = 2; + break; + } + if (frac) + *frac = decimals; + if (units) + *units = unit; + return vol; +} + double get_depth_units(unsigned int mm, int *frac, const char **units) { int decimals; diff --git a/dive.h b/dive.h index 97654dc52..5eec6f170 100644 --- a/dive.h +++ b/dive.h @@ -87,8 +87,14 @@ typedef struct { } cylinder_t; extern double get_depth_units(unsigned int mm, int *frac, const char **units); +extern double get_volume_units(unsigned int mm, int *frac, const char **units); extern double get_temp_units(unsigned int mm, const char **units); +static inline double ml_to_cuft(int ml) +{ + return ml / 28317.0; +} + static inline double mm_to_feet(int mm) { return mm * 0.00328084; @@ -177,7 +183,7 @@ struct dive { depth_t visibility; temperature_t airtemp, watertemp; cylinder_t cylinder[MAX_CYLINDERS]; - int otu; + int sac, otu; struct event *events; int samples, alloc_samples; struct sample sample[]; diff --git a/divelist.c b/divelist.c index 3ddfb6582..ca984b4eb 100644 --- a/divelist.c +++ b/divelist.c @@ -315,23 +315,22 @@ static double calculate_airuse(struct dive *dive) return airuse; } -static void get_sac(struct dive *dive, int *val) +static int calculate_sac(struct dive *dive) { double airuse, pressure, sac; - *val = 0; airuse = calculate_airuse(dive); if (!airuse) - return; + return 0; if (!dive->duration.seconds) - return; + return 0; /* Mean pressure in atm: 1 atm per 10m */ pressure = 1 + (dive->meandepth.mm / 10000.0); sac = airuse / pressure * 60 / dive->duration.seconds; /* milliliters per minute.. */ - *val = sac * 1000; + return sac * 1000; } static void get_string(char **str, const char *s) @@ -364,12 +363,10 @@ static void fill_one_dive(struct dive *dive, GtkTreeModel *model, GtkTreeIter *iter) { - int sac; char *location, *cylinder; get_cylinder(dive, &cylinder); get_location(dive, &location); - get_sac(dive, &sac); /* * We only set the fields that changed: the strings. @@ -379,7 +376,7 @@ static void fill_one_dive(struct dive *dive, DIVE_NR, dive->number, DIVE_LOCATION, location, DIVE_CYLINDER, cylinder, - DIVE_SAC, sac, + DIVE_SAC, dive->sac, DIVE_OTU, dive->otu, -1); } @@ -471,6 +468,7 @@ static void fill_dive_list(void) struct dive *dive = dive_table.dives[i]; dive->otu = calculate_otu(dive); + dive->sac = calculate_sac(dive); gtk_list_store_append(store, &iter); gtk_list_store_set(store, &iter, DIVE_INDEX, i, -- cgit v1.2.3-70-g09d2 From b26ca781b87371e77b851298093b0a7136be64fa Mon Sep 17 00:00:00 2001 From: Dirk Hohndel Date: Tue, 1 Nov 2011 20:13:14 -0700 Subject: Use unit functions to get column headers, add unit function for pressure Finally getting more consistent overall in how we convert between the different units and how we decide which units to display. Signed-off-by: Dirk Hohndel --- dive.c | 24 ++++++++++++++++++++++++ dive.h | 7 +++++++ divelist.c | 21 ++------------------- profile.c | 21 +-------------------- 4 files changed, 34 insertions(+), 39 deletions(-) (limited to 'dive.c') diff --git a/dive.c b/dive.c index 53a874b9b..cb94e7925 100644 --- a/dive.c +++ b/dive.c @@ -29,6 +29,30 @@ void add_event(struct dive *dive, int time, int type, int flags, int value, cons remember_event(name); } +int get_pressure_units(unsigned int mb, const char **units) +{ + int pressure; + const char* unit; + + switch (output_units.pressure) { + case PASCAL: + pressure = mb * 100; + unit = "pascal"; + break; + case BAR: + pressure = (mb + 500) / 1000; + unit = "bar"; + break; + case PSI: + pressure = mbar_to_PSI(mb); + unit = "psi"; + break; + } + if (units) + *units = unit; + return pressure; +} + double get_temp_units(unsigned int mk, const char **units) { double deg; diff --git a/dive.h b/dive.h index 5eec6f170..fe4515ee6 100644 --- a/dive.h +++ b/dive.h @@ -86,6 +86,7 @@ typedef struct { pressure_t start, end; } cylinder_t; +extern int get_pressure_units(unsigned int mb, const char **units); extern double get_depth_units(unsigned int mm, int *frac, const char **units); extern double get_volume_units(unsigned int mm, int *frac, const char **units); extern double get_temp_units(unsigned int mm, const char **units); @@ -146,6 +147,12 @@ static inline double to_ATM(pressure_t pressure) return pressure.mbar / 1013.25; } +static inline int mbar_to_PSI(int mbar) +{ + pressure_t p = {mbar}; + return to_PSI(p); +} + struct sample { duration_t time; depth_t depth; diff --git a/divelist.c b/divelist.c index ca984b4eb..acaf08d38 100644 --- a/divelist.c +++ b/divelist.c @@ -420,27 +420,10 @@ void update_dive_list_units(void) const char *unit; GtkTreeModel *model = GTK_TREE_MODEL(dive_list.model); - switch (output_units.length) { - case METERS: - unit = "m"; - break; - case FEET: - unit = "ft"; - break; - } + (void) get_depth_units(0, NULL, &unit); gtk_tree_view_column_set_title(dive_list.depth, unit); - switch (output_units.temperature) { - case CELSIUS: - unit = UTF8_DEGREE "C"; - break; - case FAHRENHEIT: - unit = UTF8_DEGREE "F"; - break; - case KELVIN: - unit = "Kelvin"; - break; - } + (void) get_temp_units(0, &unit); gtk_tree_view_column_set_title(dive_list.temperature, unit); gtk_tree_model_foreach(model, set_one_dive, NULL); diff --git a/profile.c b/profile.c index f81ae7138..7caa5da9f 100644 --- a/profile.c +++ b/profile.c @@ -611,32 +611,13 @@ static void plot_cylinder_pressure(struct graphics_context *gc, struct plot_info plot_pressure_helper(gc, pi, INTERPOLATED_PR); } -static int mbar_to_PSI(int mbar) -{ - pressure_t p = {mbar}; - return to_PSI(p); -} - static void plot_pressure_value(struct graphics_context *gc, int mbar, int sec, int xalign, int yalign) { int pressure; const char *unit; - switch (output_units.pressure) { - case PASCAL: - pressure = mbar * 100; - unit = "pascal"; - break; - case BAR: - pressure = (mbar + 500) / 1000; - unit = "bar"; - break; - case PSI: - pressure = mbar_to_PSI(mbar); - unit = "psi"; - break; - } + pressure = get_pressure_units(mbar, &unit); text_render_options_t tro = {10, 0.2, 1.0, 0.2, xalign, yalign}; plot_text(gc, &tro, sec, mbar, "%d %s", pressure, unit); } -- cgit v1.2.3-70-g09d2