Convert the atmospheric pressure in the Information Tab to an editable field

The Information tab shows the atmospheric pressure. Make this value editable
and also ensure that changes to it are undo-able.

Signed-off-by: willemferguson <willemferguson@zoology.up.ac.za>
This commit is contained in:
willemferguson 2019-04-30 12:42:33 +02:00 committed by Dirk Hohndel
parent ca6aa38139
commit 1bdf00b2b4
19 changed files with 323 additions and 20 deletions

View file

@ -1317,9 +1317,10 @@ static struct event *find_previous_event(struct divecomputer *dc, struct event *
return previous;
}
static void fixup_surface_pressure(struct dive *dive)
pressure_t calculate_surface_pressure(const struct dive *dive)
{
struct divecomputer *dc;
const struct divecomputer *dc;
pressure_t res;
int sum = 0, nr = 0;
for_each_dc (dive, dc) {
@ -1328,8 +1329,24 @@ static void fixup_surface_pressure(struct dive *dive)
nr++;
}
}
if (nr)
dive->surface_pressure.mbar = (sum + nr / 2) / nr;
res.mbar = nr ? (sum + nr / 2) / nr : 0;
return res;
}
static void fixup_surface_pressure(struct dive *dive)
{
dive->surface_pressure = calculate_surface_pressure(dive);
}
/* if the surface pressure in the dive data is redundant to the calculated
* value (i.e., it was added by running fixup on the dive) return 0,
* otherwise return the air temperature given in the dive */
pressure_t un_fixup_surface_pressure(const struct dive *d)
{
pressure_t res = d->surface_pressure;
if (res.mbar && res.mbar == calculate_surface_pressure(d).mbar)
res.mbar = 0;
return res;
}
static void fixup_water_salinity(struct dive *dive)
@ -1823,7 +1840,8 @@ struct dive *fixup_dive(struct dive *dive)
fixup_dive_dc(dive, dc);
fixup_water_salinity(dive);
fixup_surface_pressure(dive);
if (!dive->surface_pressure.mbar)
fixup_surface_pressure(dive);
fixup_meandepth(dive);
fixup_duration(dive);
fixup_watertemp(dive);

View file

@ -533,6 +533,8 @@ extern bool dive_or_trip_less_than(struct dive_or_trip a, struct dive_or_trip b)
extern void sort_dive_table(struct dive_table *table);
extern void sort_trip_table(struct trip_table *table);
extern struct dive *fixup_dive(struct dive *dive);
extern pressure_t calculate_surface_pressure(const struct dive *dive);
extern pressure_t un_fixup_surface_pressure(const struct dive *d);
extern void fixup_dc_duration(struct divecomputer *dc);
extern int dive_getUniqID();
extern unsigned int dc_airtemp(const struct divecomputer *dc);

View file

@ -76,6 +76,13 @@ static weight_t get_weight(const char *line)
return w;
}
static pressure_t get_airpressure(const char *line)
{
pressure_t p;
p.mbar = lrint(ascii_strtod(line, NULL));
return p;
}
static pressure_t get_pressure(const char *line)
{
pressure_t p;
@ -245,6 +252,9 @@ static void parse_dive_airtemp(char *line, struct membuffer *str, void *_dive)
static void parse_dive_watertemp(char *line, struct membuffer *str, void *_dive)
{ UNUSED(str); struct dive *dive = _dive; dive->watertemp = get_temperature(line); }
static void parse_dive_airpressure(char *line, struct membuffer *str, void *_dive)
{ UNUSED(str); struct dive *dive = _dive; dive->surface_pressure = get_airpressure(line); }
static void parse_dive_duration(char *line, struct membuffer *str, void *_dive)
{ UNUSED(str); struct dive *dive = _dive; dive->duration = get_duration(line); }
@ -980,7 +990,7 @@ static void divecomputer_parser(char *line, struct membuffer *str, void *_dc)
struct keyword_action dive_action[] = {
#undef D
#define D(x) { #x, parse_dive_ ## x }
D(airtemp), D(buddy), D(cylinder), D(divemaster), D(divesiteid), D(duration),
D(airpressure), D(airtemp), D(buddy), D(cylinder), D(divemaster), D(divesiteid), D(duration),
D(gps), D(location), D(notes), D(notrip), D(rating), D(suit),
D(tags), D(visibility), D(watertemp), D(weightsystem)
};

View file

@ -1306,6 +1306,8 @@ static void try_to_fill_dive(struct dive *dive, const char *name, char *buf, str
return;
if (MATCH("visibility.dive", get_rating, &dive->visibility))
return;
if (MATCH_STATE("airpressure.dive", pressure, &dive->surface_pressure))
return;
if (state->cur_ws_index < MAX_WEIGHTSYSTEMS) {
if (MATCH("description.weightsystem", utf8_string, &dive->weightsystem[state->cur_ws_index].description))
return;

View file

@ -425,10 +425,13 @@ static void save_dc(struct membuffer *b, struct dive *dive, struct divecomputer
*/
static void create_dive_buffer(struct dive *dive, struct membuffer *b)
{
pressure_t surface_pressure = un_fixup_surface_pressure(dive);
if (dive->dc.duration.seconds > 0)
put_format(b, "duration %u:%02u min\n", FRACTION(dive->dc.duration.seconds, 60));
SAVE("rating", rating);
SAVE("visibility", visibility);
if (surface_pressure.mbar)
SAVE("airpressure", surface_pressure.mbar);
cond_put_format(dive->notrip, b, "notrip\n");
save_tags(b, dive->tag_list);
if (dive->dive_site)

View file

@ -473,6 +473,7 @@ static void save_picture(struct membuffer *b, struct picture *pic)
void save_one_dive_to_mb(struct membuffer *b, struct dive *dive, bool anonymize)
{
struct divecomputer *dc;
pressure_t surface_pressure = un_fixup_surface_pressure(dive);
put_string(b, "<dive");
if (dive->number)
@ -488,6 +489,8 @@ void save_one_dive_to_mb(struct membuffer *b, struct dive *dive, bool anonymize)
put_format(b, " divesiteid='%8x'", dive->dive_site->uuid);
}
show_date(b, dive->when);
if (surface_pressure.mbar)
put_pressure(b, surface_pressure, " airpressure='", " bar'");
if (dive->dc.duration.seconds > 0)
put_format(b, " duration='%u:%02u min'>\n",
FRACTION(dive->dc.duration.seconds, 60));

View file

@ -19,6 +19,7 @@ enum class DiveField {
DURATION,
AIR_TEMP,
WATER_TEMP,
ATM_PRESS,
DIVESITE,
DIVEMASTER,
BUDDY,

View file

@ -254,6 +254,17 @@ static inline int mbar_to_PSI(int mbar)
return to_PSI(p);
}
static inline int32_t altitude_to_pressure(int32_t altitude) // altitude in mm above sea level
{ // returns atmospheric pressure in mbar
return (int32_t) (1013.0 * exp(- altitude / 7800000.0));
}
static inline int32_t pressure_to_altitude(int32_t pressure) // pressure in mbar
{ // returns altitude in mm above sea level
return (int32_t) (log(1013.0 / pressure) * 7800000);
}
/*
* We keep our internal data in well-specified units, but
* the input and output may come in some random format. This