struct sample *sample = dive->sample + i;
int time = sample->time.seconds;
int depth = sample->depth.mm;
- int press = sample->tankpressure.mbar;
+ int press = sample->cylinderpressure.mbar;
int temp = sample->temperature.mkelvin;
if (lastdepth)
sample.depth = as->depth;
if (as->temperature.mkelvin)
sample.temperature = as->temperature;
- if (as->tankpressure.mbar)
- sample.tankpressure = as->tankpressure;
- if (as->tankindex)
- sample.tankindex = as->tankindex;
+ if (as->cylinderpressure.mbar)
+ sample.cylinderpressure = as->cylinderpressure;
+ if (as->cylinderindex)
+ sample.cylinderindex = as->cylinderindex;
res = add_sample(&sample, at, res);
return res;
}
+/* Pick whichever has any info (if either). Prefer 'a' */
+static void merge_cylinder_type(cylinder_type_t *res, cylinder_type_t *a, cylinder_type_t *b)
+{
+ if (a->size.mliter)
+ b = a;
+ *res = *b;
+}
+
+static void merge_cylinder_mix(gasmix_t *res, gasmix_t *a, gasmix_t *b)
+{
+ if (a->o2.permille)
+ b = a;
+ *res = *b;
+}
+
+static void merge_cylinder_info(cylinder_t *res, cylinder_t *a, cylinder_t *b)
+{
+ merge_cylinder_type(&res->type, &a->type, &b->type);
+ merge_cylinder_mix(&res->gasmix, &a->gasmix, &b->gasmix);
+}
+
/*
* This could do a lot more merging. Right now it really only
* merges almost exact duplicates - something that happens easily
MERGE_MIN(res, a, b, watertemp.mkelvin);
MERGE_MAX(res, a, b, beginning_pressure.mbar);
MERGE_MAX(res, a, b, end_pressure.mbar);
- for (i = 0; i < MAX_MIXES; i++) {
- if (a->gasmix[i].o2.permille) {
- res->gasmix[i] = a->gasmix[i];
- continue;
- }
- res->gasmix[i] = b->gasmix[i];
- }
+ for (i = 0; i < MAX_CYLINDERS; i++)
+ merge_cylinder_info(res->cylinder+i, a->cylinder + i, b->cylinder + i);
+
return merge_samples(res, a, b, 0);
}
*
* We also strive to make '0' a meaningless number saying "not
* initialized", since many values are things that may not have
- * been reported (eg tank pressure or temperature from dive
+ * been reported (eg cylinder pressure or temperature from dive
* computers that don't support them). But sometimes -1 is an even
* more explicit way of saying "not there".
*
* temperatures. Doing temperatures in celsius or fahrenheit would
* make for loss of precision when converting from one to the other,
* and using millikelvin is SI-like but also means that a temperature
- * of '0' is clearly just a missing temperature or tank pressure.
+ * of '0' is clearly just a missing temperature or cylinder pressure.
*
* Also strive to use units that can not possibly be mistaken for a
* valid value in a "normal" system without conversion. If the max
typedef struct {
volume_t size;
- pressure_t pressure;
-} tank_type_t;
+ pressure_t workingpressure;
+} cylinder_type_t;
+
+typedef struct {
+ cylinder_type_t type;
+ gasmix_t gasmix;
+} cylinder_t;
static inline int to_feet(depth_t depth)
{
duration_t time;
depth_t depth;
temperature_t temperature;
- pressure_t tankpressure;
- int tankindex;
+ pressure_t cylinderpressure;
+ int cylinderindex;
};
-#define MAX_MIXES (4)
+#define MAX_CYLINDERS (4)
struct dive {
const char *name;
depth_t visibility;
temperature_t airtemp, watertemp;
pressure_t beginning_pressure, end_pressure;
- gasmix_t gasmix[MAX_MIXES];
+ cylinder_t cylinder[MAX_CYLINDERS];
int samples;
struct sample sample[];
};
static struct sample *sample;
static struct tm tm;
static int suunto, uemis;
-static int event_index, gasmix_index;
+static int event_index, cylinder_index;
static time_t utc_mktime(struct tm *tm)
{
/* libdivecomputer does negative percentages. */
if (*buffer == '-')
return;
- if (gasmix_index < MAX_MIXES)
+ if (cylinder_index < MAX_CYLINDERS)
percent(buffer, _fraction);
}
/* Ignore n2 percentages. There's no value in them. */
}
+static void cylindersize(char *buffer, void *_volume)
+{
+ volume_t *volume = _volume;
+ union int_or_float val;
+
+ switch (integer_or_float(buffer, &val)) {
+ case FLOAT:
+ volume->mliter = val.fp * 1000 + 0.5;
+ break;
+
+ default:
+ printf("Strange volume reading %s\n", buffer);
+ break;
+ }
+ free(buffer);
+}
+
static void utf8_string(char *buffer, void *_res)
{
*(char **)_res = buffer;
{
return MATCH(".reading.dive_time", sampletime, &sample->time) ||
MATCH(".reading.water_pressure", water_pressure, &sample->depth) ||
- MATCH(".reading.active_tank", get_index, &sample->tankindex) ||
- MATCH(".reading.tank_pressure", centibar, &sample->tankpressure) ||
+ MATCH(".reading.active_tank", get_index, &sample->cylinderindex) ||
+ MATCH(".reading.tank_pressure", centibar, &sample->cylinderpressure) ||
MATCH(".reading.dive_temperature", decicelsius, &sample->temperature) ||
0;
}
int len = strlen(name);
start_match("sample", name, buf);
- if (MATCH(".sample.pressure", pressure, &sample->tankpressure))
+ if (MATCH(".sample.pressure", pressure, &sample->cylinderpressure))
return;
- if (MATCH(".sample.cylpress", pressure, &sample->tankpressure))
+ if (MATCH(".sample.cylpress", pressure, &sample->cylinderpressure))
return;
if (MATCH(".sample.depth", depth, &sample->depth))
return;
*/
static int suunto_dive_match(struct dive *dive, const char *name, int len, char *buf)
{
- return MATCH(".o2pct", percent, &dive->gasmix[0].o2) ||
- MATCH(".hepct_0", percent, &dive->gasmix[0].he) ||
- MATCH(".o2pct_2", percent, &dive->gasmix[1].o2) ||
- MATCH(".hepct_1", percent, &dive->gasmix[1].he) ||
- MATCH(".o2pct_3", percent, &dive->gasmix[2].o2) ||
- MATCH(".hepct_2", percent, &dive->gasmix[2].he) ||
- MATCH(".o2pct_4", percent, &dive->gasmix[3].o2) ||
- MATCH(".hepct_3", percent, &dive->gasmix[3].he);
+ return MATCH(".o2pct", percent, &dive->cylinder[0].gasmix.o2) ||
+ MATCH(".hepct_0", percent, &dive->cylinder[0].gasmix.he) ||
+ MATCH(".o2pct_2", percent, &dive->cylinder[1].gasmix.o2) ||
+ MATCH(".hepct_1", percent, &dive->cylinder[1].gasmix.he) ||
+ MATCH(".o2pct_3", percent, &dive->cylinder[2].gasmix.o2) ||
+ MATCH(".hepct_2", percent, &dive->cylinder[2].gasmix.he) ||
+ MATCH(".o2pct_4", percent, &dive->cylinder[3].gasmix.o2) ||
+ MATCH(".hepct_3", percent, &dive->cylinder[3].gasmix.he) ||
+ MATCH(".cylindersize", cylindersize, &dive->cylinder[0].type.size) ||
+ MATCH(".cylinderworkpressure", pressure, &dive->cylinder[0].type.workingpressure) ||
+ 0;
}
static int buffer_value(char *buffer)
if (MATCH(".notes", utf8_string, &dive->notes))
return;
- if (MATCH(".o2", gasmix, &dive->gasmix[gasmix_index].o2))
+ if (MATCH(".cylinder.size", cylindersize, &dive->cylinder[cylinder_index].type.size))
+ return;
+ if (MATCH(".cylinder.workpressure", pressure, &dive->cylinder[cylinder_index].type.workingpressure))
+ return;
+
+ if (MATCH(".o2", gasmix, &dive->cylinder[cylinder_index].gasmix.o2))
return;
- if (MATCH(".n2", gasmix_nitrogen, &dive->gasmix[gasmix_index]))
+ if (MATCH(".n2", gasmix_nitrogen, &dive->cylinder[cylinder_index].gasmix))
return;
- if (MATCH(".he", gasmix, &dive->gasmix[gasmix_index].he))
+ if (MATCH(".he", gasmix, &dive->cylinder[cylinder_index].gasmix.he))
return;
/* Suunto XML files are some crazy sh*t. */
return p;
}
-static void sanitize_gasmix(struct dive *dive)
+static void sanitize_gasmix(gasmix_t *mix)
{
- int i;
+ unsigned int o2, he;
- for (i = 0; i < MAX_MIXES; i++) {
- gasmix_t *mix = dive->gasmix+i;
- unsigned int o2, he;
-
- o2 = mix->o2.permille;
- he = mix->he.permille;
-
- /* Regular air: leave empty */
- if (!he) {
- if (!o2)
- continue;
- /* 20.9% or 21% O2 is just air */
- if (o2 >= 209 && o2 <= 210) {
- mix->o2.permille = 0;
- continue;
- }
+ o2 = mix->o2.permille;
+ he = mix->he.permille;
+
+ /* Regular air: leave empty */
+ if (!he) {
+ if (!o2)
+ return;
+ /* 20.9% or 21% O2 is just air */
+ if (o2 >= 209 && o2 <= 210) {
+ mix->o2.permille = 0;
+ return;
}
+ }
+
+ /* Sane mix? */
+ if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
+ return;
+ fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
+ memset(mix, 0, sizeof(*mix));
+}
+
+/*
+ * There are two ways to give cylinder size information:
+ * - total amount of gas in cuft (depends on working pressure and physical size)
+ * - physical size
+ *
+ * where "physical size" is the one that actually matters and is sane.
+ *
+ * We internally use physical size only. But we save the workingpressure
+ * so that we can do the conversion if required.
+ */
+static void sanitize_cylinder_type(cylinder_type_t *type)
+{
+ /* If we have no working pressure, it had *better* be just a physical size! */
+ if (!type->workingpressure.mbar)
+ return;
+
+ /*
+ * 35l tanks? Do they exist?
+ * Assume this is a "size in cuft" thing.
+ */
+ if (type->size.mliter > 35000) {
+ double volume_of_air = type->size.mliter * 28.317; /* cu ft to milliliter */
+ double atm = type->workingpressure.mbar / 1013.25; /* working pressure in atm */
+ double volume = volume_of_air / atm; /* milliliters at 1 atm: "true size" */
+ type->size.mliter = volume;
+ }
+}
+
+static void sanitize_cylinder_info(struct dive *dive)
+{
+ int i;
- /* Sane mix? */
- if (o2 <= 1000 && he <= 1000 && o2+he <= 1000)
- continue;
- fprintf(stderr, "Odd gasmix: %d O2 %d He\n", o2, he);
- memset(mix, 0, sizeof(*mix));
+ for (i = 0; i < MAX_CYLINDERS; i++) {
+ sanitize_gasmix(&dive->cylinder[i].gasmix);
+ sanitize_cylinder_type(&dive->cylinder[i].type);
}
}
return;
if (!dive->name)
dive->name = generate_name(dive);
- sanitize_gasmix(dive);
+ sanitize_cylinder_info(dive);
record_dive(dive);
dive = NULL;
- gasmix_index = 0;
+ cylinder_index = 0;
}
static void suunto_start(void)
event_index++;
}
-static void gasmix_start(void)
+static void cylinder_start(void)
{
}
-static void gasmix_end(void)
+static void cylinder_end(void)
{
- gasmix_index++;
+ cylinder_index++;
}
static void sample_start(void)
{ "SAMPLE", sample_start, sample_end },
{ "reading", sample_start, sample_end },
{ "event", event_start, event_end },
- { "gasmix", gasmix_start, gasmix_end },
+ { "gasmix", cylinder_start, cylinder_end },
+ { "cylinder", cylinder_start, cylinder_end },
{ "pre_dive", uemis_start, uemis_end },
{ NULL, }
};
cairo_stroke(cr);
}
-static int get_tank_pressure_range(struct dive *dive, double *scalex, double *scaley)
+static int get_cylinder_pressure_range(struct dive *dive, double *scalex, double *scaley)
{
int i;
double min, max;
struct sample *sample = dive->sample + i;
double bar;
- if (!sample->tankpressure.mbar)
+ if (!sample->cylinderpressure.mbar)
continue;
- bar = sample->tankpressure.mbar;
+ bar = sample->cylinderpressure.mbar;
if (bar < min)
min = bar;
if (bar > max)
return 1;
}
-static void plot_tank_pressure(struct dive *dive, cairo_t *cr,
+static void plot_cylinder_pressure(struct dive *dive, cairo_t *cr,
double topx, double topy, double maxx, double maxy)
{
int i;
double scalex, scaley;
- if (!get_tank_pressure_range(dive, &scalex, &scaley))
+ if (!get_cylinder_pressure_range(dive, &scalex, &scaley))
return;
cairo_set_source_rgba(cr, 0.2, 1.0, 0.2, 0.80);
struct sample *sample = dive->sample + i;
sec = sample->time.seconds;
- mbar = sample->tankpressure.mbar;
+ mbar = sample->cylinderpressure.mbar;
if (!mbar)
continue;
cairo_line_to(cr, SCALE(sec, mbar));
/* Depth profile */
plot_profile(dive, cr, topx, topy, maxx, maxy);
- /* Tank pressure plot? */
- plot_tank_pressure(dive, cr, topx, topy, maxx, maxy);
+ /* Cylinder pressure plot? */
+ plot_cylinder_pressure(dive, cr, topx, topy, maxx, maxy);
/* Bounding box last */
scalex = scaley = 1.0;
show_utf8(f, dive->notes, " <notes>","</notes>\n");
}
-static void save_gasmix(FILE *f, struct dive *dive)
+static void save_cylinder_info(FILE *f, struct dive *dive)
{
int i;
- for (i = 0; i < MAX_MIXES; i++) {
- gasmix_t *mix = dive->gasmix+i;
- int o2 = mix->o2.permille, he = mix->he.permille;
- int n2 = 1000 - o2 - he;
+ for (i = 0; i < MAX_CYLINDERS; i++) {
+ cylinder_t *cylinder = dive->cylinder+i;
+ int volume = cylinder->type.size.mliter;
+ int pressure = cylinder->type.workingpressure.mbar;
+ int o2 = cylinder->gasmix.o2.permille;
+ int he = cylinder->gasmix.he.permille;
- if (!mix->o2.permille)
+ /* No cylinder information at all? */
+ if (!o2 && !volume)
return;
- fprintf(f, " <gasmix o2='%u.%u%%'", FRACTION(o2, 10));
- if (mix->he.permille)
- fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
- fprintf(f, " n2='%u.%u%%' />\n", FRACTION(n2, 10));
+ fprintf(f, " <cylinder");
+ if (o2) {
+ int n2 = 1000 - o2 - he;
+ fprintf(f, " o2='%u.%u%%'", FRACTION(o2, 10));
+ if (he)
+ fprintf(f, " he='%u.%u%%'", FRACTION(he, 10));
+ fprintf(f, " n2='%u.%u%%'", FRACTION(n2, 10));
+ }
+ if (volume) {
+ fprintf(f, " size='%u.%03u l'", FRACTION(volume, 1000));
+ if (pressure)
+ fprintf(f, " workpressure='%u.%03u bar'", FRACTION(pressure, 1000));
+ }
+ fprintf(f, " />\n");
}
}
FRACTION(sample->time.seconds,60),
FRACTION(sample->depth.mm, 1000));
show_temperature(f, sample->temperature, " temp='", "'");
- show_pressure(f, sample->tankpressure, " pressure='", "'");
- if (sample->tankindex)
- fprintf(f, " tankindex='%d'", sample->tankindex);
+ show_pressure(f, sample->cylinderpressure, " pressure='", "'");
+ if (sample->cylinderindex)
+ fprintf(f, " cylinderindex='%d'", sample->cylinderindex);
fprintf(f, " />\n");
}
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec);
save_overview(f, dive);
- save_gasmix(f, dive);
+ save_cylinder_info(f, dive);
for (i = 0; i < dive->samples; i++)
save_sample(f, dive->sample+i);
fprintf(f, "</dive>\n");