CCR code: Change to sample structure

1) All the variables in the sample structures are strongly typed
2) Two additional types were declared in units.h:
     o2pressure_t
     bearing_t
3) The following variables were added:
     diluentpressure
     o2setpoint
     o2sensor[3]
4) Changes to a number of files were made to chanf
     sample->po2 to sample->po2.mbar
     bearing to bearring.degrees

Signed-off-by: Willem Ferguson <willemferguson@zoology.up.ac.za>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
Willem Ferguson 2014-06-03 19:21:41 +02:00 committed by Dirk Hohndel
parent 1fec6453a2
commit eaf6d56487
11 changed files with 64 additions and 51 deletions

2
dive.c
View file

@ -1105,7 +1105,7 @@ static void merge_samples(struct divecomputer *res, struct divecomputer *a, stru
sample.sensor = as->sensor;
if (as->cns)
sample.cns = as->cns;
if (as->po2)
if (as->po2.mbar)
sample.po2 = as->po2;
if (as->ndl.seconds)
sample.ndl = as->ndl;

34
dive.h
View file

@ -141,21 +141,25 @@ static inline depth_t gas_mod(struct gasmix *mix, pressure_t po2_limit) {
void get_gas_string(const struct gasmix *gasmix, char *text, int len);
const char *gasname(const struct gasmix *gasmix);
struct sample {
duration_t time;
depth_t depth;
temperature_t temperature;
pressure_t cylinderpressure;
int sensor; /* Cylinder pressure sensor index */
duration_t ndl;
duration_t stoptime;
depth_t stopdepth;
bool in_deco;
int cns;
int po2;
int heartbeat;
int bearing;
};
struct sample // BASE TYPE BYTES UNITS RANGE DESCRIPTION
{ // --------- ----- ----- ----- -----------
duration_t time; // uint32_t 4 seconds (0-68 yrs) elapsed dive time up to this sample
duration_t stoptime; // uint32_t 4 seconds (0-18 h) time duration of next deco stop
duration_t ndl; // uint32_t 4 seconds (0-18 h) time duration before no-deco limit
depth_t depth; // int32_t 4 mm (0-2000 km) dive depth of this sample
depth_t stopdepth; // int32_t 4 mm (0-2000 km) depth of next deco stop
temperature_t temperature; // int32_t 4 mdegrK (0-2 MdegK) ambient temperature
pressure_t cylinderpressure; // int32_t 4 mbar (0-2 Mbar) main cylinder pressure
pressure_t diluentpressure; // int32_t 4 mbar (0-2 Mbar) CCR diluent pressure (rebreather)
o2pressure_t po2; // uint16_t 2 mbar (0-65 bar) O2 partial pressure
o2pressure_t o2setpoint; // uint16_t 2 mbar (0-65 bar) CCR O2 setpoint (rebreather)
o2pressure_t o2sensor[3]; // uint16_t 6 mbar (0-65 bar) Up to 3 PO2 sensor values (rebreather)
bearing_t bearing; // int16_t 2 degrees (-32k to 32k deg) compass bearing
uint8_t sensor; // uint8_t 1 sensorID (0-255) ID of cylinder pressure sensor
uint8_t cns; // uint8_t 1 % (0-255 %) cns% accumulated
uint8_t heartbeat; // uint8_t 1 beats/m (0-255) heart rate measurement
bool in_deco; // bool 1 y/n y/n this sample is part of deco
}; // Total size of structure: 48 bytes, excluding padding at end
struct divetag {
/*

View file

@ -178,8 +178,8 @@ static int calculate_otu(struct dive *dive)
struct sample *sample = dc->sample + i;
struct sample *psample = sample - 1;
t = sample->time.seconds - psample->time.seconds;
if (sample->po2) {
po2 = sample->po2;
if (sample->po2.mbar) {
po2 = sample->po2.mbar;
} else {
int o2 = active_o2(dive, dc, sample->time);
po2 = o2 * depth_to_atm(sample->depth.mm, dive);
@ -242,8 +242,8 @@ static int calculate_cns(struct dive *dive)
struct sample *sample = dc->sample + i;
struct sample *psample = sample - 1;
t = sample->time.seconds - psample->time.seconds;
if (sample->po2) {
po2 = sample->po2;
if (sample->po2.mbar) {
po2 = sample->po2.mbar;
} else {
int o2 = active_o2(dive, dc, sample->time);
po2 = o2 / depth_to_atm(sample->depth.mm, dive);
@ -326,7 +326,7 @@ static void add_dive_to_deco(struct dive *dive)
for (j = t0; j < t1; j++) {
int depth = interpolate(psample->depth.mm, sample->depth.mm, j - t0, t1 - t0);
(void)add_segment(depth_to_mbar(depth, dive) / 1000.0,
&dive->cylinder[sample->sensor].gasmix, 1, sample->po2, dive);
&dive->cylinder[sample->sensor].gasmix, 1, sample->po2.mbar, dive);
}
}
}

View file

@ -198,7 +198,7 @@ sample_cb(dc_sample_type_t type, dc_sample_value_t value, void *userdata)
sample->ndl.seconds = ndl;
sample->stoptime.seconds = stoptime;
sample->stopdepth.mm = stopdepth;
sample->po2 = po2;
sample->po2.mbar = po2;
sample->cns = cns;
}
sample = prepare_sample(dc);
@ -225,7 +225,7 @@ sample_cb(dc_sample_type_t type, dc_sample_value_t value, void *userdata)
sample->heartbeat = value.heartbeat;
break;
case DC_SAMPLE_BEARING:
sample->bearing = value.bearing;
sample->bearing.degrees = value.bearing;
break;
case DC_SAMPLE_VENDOR:
printf(" <vendor time='%u:%02u' type=\"%u\" size=\"%u\">", FRACTION(sample->time.seconds, 60),
@ -237,10 +237,10 @@ sample_cb(dc_sample_type_t type, dc_sample_value_t value, void *userdata)
#if DC_VERSION_CHECK(0, 3, 0)
case DC_SAMPLE_SETPOINT:
/* for us a setpoint means constant pO2 from here */
sample->po2 = po2 = rint(value.setpoint * 1000);
sample->po2.mbar = po2 = rint(value.setpoint * 1000);
break;
case DC_SAMPLE_PPO2:
sample->po2 = po2 = rint(value.ppo2 * 1000);
sample->po2.mbar = po2 = rint(value.ppo2 * 1000);
break;
case DC_SAMPLE_CNS:
sample->cns = cns = rint(value.cns * 100);

View file

@ -375,8 +375,7 @@ static void parse_sample_keyvalue(void *_sample, const char *key, const char *va
}
if (!strcmp(key, "po2")) {
pressure_t p = get_pressure(value);
// Ugh, typeless.
sample->po2 = p.mbar;
sample->po2.mbar = p.mbar;
return;
}
if (!strcmp(key, "heartbeat")) {
@ -384,7 +383,7 @@ static void parse_sample_keyvalue(void *_sample, const char *key, const char *va
return;
}
if (!strcmp(key, "bearing")) {
sample->bearing = atoi(value);
sample->bearing.degrees = atoi(value);
return;
}
report_error("Unexpected sample key/value pair (%s/%s)", key, value);

View file

@ -1327,7 +1327,7 @@ static void sample_start(void)
cur_sample->stoptime.seconds = laststoptime;
cur_sample->stopdepth.mm = laststopdepth;
cur_sample->cns = lastcns;
cur_sample->po2 = lastpo2;
cur_sample->po2.mbar = lastpo2;
cur_sample->sensor = lastsensor;
}
@ -1342,7 +1342,7 @@ static void sample_end(void)
laststoptime = cur_sample->stoptime.seconds;
laststopdepth = cur_sample->stopdepth.mm;
lastcns = cur_sample->cns;
lastpo2 = cur_sample->po2;
lastpo2 = cur_sample->po2.mbar;
cur_sample = NULL;
}
@ -1929,7 +1929,7 @@ extern int shearwater_profile_sample(void *handle, int columns, char **data, cha
if (data[2])
cur_sample->temperature.mkelvin = metric ? C_to_mkelvin(atof(data[2])) : F_to_mkelvin(atof(data[2]));
if (data[3])
cur_sample->po2 = atof(data[3]) * 1000;
cur_sample->po2.mbar = atof(data[3]) * 1000;
if (data[4])
cur_sample->ndl.seconds = atoi(data[4]) * 60;
if (data[5])

View file

@ -159,7 +159,7 @@ double tissue_at_end(struct dive *dive, char **cached_datap)
}
if (i > 0)
lastdepth = psample->depth.mm;
tissue_tolerance = interpolate_transition(dive, t0, t1, lastdepth, sample->depth.mm, &dive->cylinder[gasidx].gasmix, sample->po2);
tissue_tolerance = interpolate_transition(dive, t0, t1, lastdepth, sample->depth.mm, &dive->cylinder[gasidx].gasmix, sample->po2.mbar);
psample = sample;
t0 = t1;
}
@ -263,7 +263,7 @@ static struct dive *create_dive_from_plan(struct diveplan *diveplan, struct dive
cyl = &master_dive->cylinder[0];
oldgasmix = cyl->gasmix;
sample = prepare_sample(dc);
sample->po2 = dp->po2;
sample->po2.mbar = dp->po2;
finish_sample(dc);
while (dp) {
struct gasmix gasmix = dp->gasmix;
@ -300,7 +300,7 @@ static struct dive *create_dive_from_plan(struct diveplan *diveplan, struct dive
/* need to insert a first sample for the new gas */
add_gas_switch_event(dive, dc, lasttime + 1, idx);
sample = prepare_sample(dc);
sample[-1].po2 = po2;
sample[-1].po2.mbar = po2;
sample->time.seconds = lasttime + 1;
sample->depth.mm = lastdepth;
finish_sample(dc);
@ -311,8 +311,8 @@ static struct dive *create_dive_from_plan(struct diveplan *diveplan, struct dive
sample = prepare_sample(dc);
/* set po2 at beginning of this segment */
/* and keep it valid for last sample - where it likely doesn't matter */
sample[-1].po2 = po2;
sample->po2 = po2;
sample[-1].po2.mbar = po2;
sample->po2.mbar = po2;
sample->time.seconds = lasttime = time;
sample->depth.mm = lastdepth = depth;
update_cylinder_pressure(dive, sample[-1].depth.mm, depth, time - sample[-1].time.seconds,
@ -703,7 +703,7 @@ void plan(struct diveplan *diveplan, char **cached_datap, struct dive **divep, s
/* we start with gas 0, then check if that was changed */
gas = dive->cylinder[0].gasmix;
get_gas_from_events(&dive->dc, sample->time.seconds, &gas);
po2 = dive->dc.sample[dive->dc.samples - 1].po2;
po2 = dive->dc.sample[dive->dc.samples - 1].po2.mbar;
if ((current_cylinder = get_gasidx(dive, &gas)) == -1) {
report_error(translate("gettextFromC", "Can't find gas %s"), gasname(&gas));
current_cylinder = 0;

View file

@ -834,7 +834,7 @@ struct plot_data *populate_plot_entries(struct dive *dive, struct divecomputer *
pi->has_ndl |= sample->ndl.seconds;
entry->in_deco = sample->in_deco;
entry->cns = sample->cns;
entry->po2 = sample->po2 / 1000.0;
entry->po2 = sample->po2.mbar / 1000.0;
/* FIXME! sensor index -> cylinder index translation! */
entry->cylinderindex = sample->sensor;
SENSOR_PRESSURE(entry) = sample->cylinderpressure.mbar;
@ -843,7 +843,7 @@ struct plot_data *populate_plot_entries(struct dive *dive, struct divecomputer *
else
entry->temperature = lasttemp;
entry->heartbeat = sample->heartbeat;
entry->bearing = sample->bearing;
entry->bearing = sample->bearing.degrees;
/* skip events that happened at this time */
while (ev && ev->time.seconds == time)

View file

@ -262,12 +262,12 @@ static void save_sample(struct membuffer *b, struct sample *sample, struct sampl
old->cns = sample->cns;
}
if (sample->po2 != old->po2) {
put_milli(b, " po2=", sample->po2, "bar");
if (sample->po2.mbar != old->po2.mbar) {
put_milli(b, " po2=", sample->po2.mbar, "bar");
old->po2 = sample->po2;
}
show_index(b, sample->heartbeat, "heartbeat=", "");
show_index(b, sample->bearing, "bearing=", "°");
show_index(b, sample->bearing.degrees, "bearing=", "°");
put_format(b, "\n");
}

View file

@ -279,12 +279,12 @@ static void save_sample(struct membuffer *b, struct sample *sample, struct sampl
old->cns = sample->cns;
}
if (sample->po2 != old->po2) {
put_milli(b, " po2='", sample->po2, " bar'");
if (sample->po2.mbar != old->po2.mbar) {
put_milli(b, " po2='", sample->po2.mbar, " bar'");
old->po2 = sample->po2;
}
show_index(b, sample->heartbeat, "heartbeat='", "'");
show_index(b, sample->bearing, "bearing='", "'");
show_index(b, sample->bearing.degrees, "bearing='", "'");
put_format(b, " />\n");
}

22
units.h
View file

@ -59,22 +59,32 @@ typedef int64_t timestamp_t;
typedef struct
{
int seconds;
uint32_t seconds; // durations up to 68 yrs
} duration_t;
typedef struct
{
int mm;
} depth_t;
int32_t mm;
} depth_t; // depth to 2000 km
typedef struct
{
int mbar;
int32_t mbar; // pressure up to 2000 bar
} pressure_t;
typedef struct
{
int mkelvin;
uint16_t mbar;
} o2pressure_t; // pressure up to 65 bar
typedef struct
{
int16_t degrees;
} bearing_t; // compass bearing
typedef struct
{
int32_t mkelvin; // up to 1750 degrees K
} temperature_t;
typedef struct
@ -242,4 +252,4 @@ struct units {
}
#endif
#endif
#endif