Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ EXTRA_DIST = \
test/run-parse.sh \
test/fixtures/manifest.txt \
test/fixtures/shearwater_petrel2-0001.bin \
test/fixtures/shearwater_perdix2-0001.bin \
test/fixtures/hw_ostc5-0001.bin \
test/fixtures/garmin_descent_mk1-0001.bin \
test/test_descriptor_product_name.c
2 changes: 1 addition & 1 deletion src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ libdivecomputer_la_SOURCES = \
zeagle_n2ition3.h zeagle_n2ition3.c \
atomics_cobalt.h atomics_cobalt.c atomics_cobalt_parser.c \
shearwater_common.h shearwater_common.c \
shearwater_predator.h shearwater_predator.c shearwater_predator_parser.c \
shearwater_predator.h shearwater_predator.c shearwater_predator_parser.c shearwater_tank_gas.h \
shearwater_petrel.h shearwater_petrel.c \
diverite_nitekq.h diverite_nitekq.c diverite_nitekq_parser.c \
citizen_aqualand.h citizen_aqualand.c citizen_aqualand_parser.c \
Expand Down
61 changes: 60 additions & 1 deletion src/shearwater_predator_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "platform.h"
#include "array.h"
#include "field-cache.h"
#include "shearwater_tank_gas.h"

#define ISINSTANCE(parser) ( \
dc_parser_isinstance((parser), &shearwater_predator_parser_vtable) || \
Expand Down Expand Up @@ -147,6 +148,7 @@ typedef struct shearwater_predator_tank_t {
char name[2];
unsigned int battery;
dc_tank_usage_t usage;
unsigned int gasmix;
} shearwater_predator_tank_t;

struct shearwater_predator_parser_t {
Expand Down Expand Up @@ -320,6 +322,7 @@ shearwater_common_parser_create (dc_parser_t **out, dc_context_t *context, const
memset (parser->tank[i].name, 0, sizeof (parser->tank[i].name));
parser->tank[i].battery = 0;
parser->tank[i].usage = DC_TANK_USAGE_NONE;
parser->tank[i].gasmix = UNDEFINED;
parser->tankidx[i] = i;
}
parser->aimode = AI_OFF;
Expand Down Expand Up @@ -570,6 +573,21 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser)
unsigned int sub_mode = SM_OC_REC;
unsigned int stack_time_total_s = 0;
unsigned int stack_time_remaining_s;

// AI-generated (Claude)
// The gas mix in use and the tank pressures of every sample, used to work
// out which gas mix each tank is on once all samples have been seen.
unsigned int maxsamples = length > offset ? (length - offset) / parser->samplesize : 0;
unsigned int *trace_gas = (unsigned int *) calloc (maxsamples + 1, sizeof (unsigned int));
unsigned int *trace_pressure = (unsigned int *) calloc ((maxsamples + 1) * NTANKS, sizeof (unsigned int));
if (trace_gas == NULL || trace_pressure == NULL) {
ERROR (abstract->context, "Failed to allocate memory.");
free (trace_gas);
free (trace_pressure);
return DC_STATUS_NOMEMORY;
}
unsigned int gas_current = SHEARWATER_TANK_GAS_NONE;

while (offset + parser->samplesize <= length) {
// Ignore empty samples.
if (array_isequal (data + offset, parser->samplesize, 0x00)) {
Expand Down Expand Up @@ -613,6 +631,8 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser)
if (idx >= ngasmixes) {
if (idx >= NGASMIXES) {
ERROR (abstract->context, "Maximum number of gas mixes reached.");
free (trace_gas);
free (trace_pressure);
return DC_STATUS_NOMEMORY;
}
gasmix[idx].oxygen = o2;
Expand All @@ -622,11 +642,13 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser)
}

gasmix[idx].active = 1;
gas_current = idx;

o2_previous = o2;
he_previous = he;
dil_previous = ccr;
}
trace_gas[nsamples - 1] = gas_current;

// Tank pressure
if (logversion >= 7) {
Expand Down Expand Up @@ -654,6 +676,7 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser)
}
tank[id].endpressure = pressure;
tank[id].battery |= battery;
trace_pressure[(nsamples - 1) * NTANKS + id] = pressure;
}
}
}
Expand All @@ -673,6 +696,8 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser)
tank[id].endpressure = pressure;
}
tank[id].endpressure = pressure;
if (nsamples)
trace_pressure[(nsamples - 1) * NTANKS + id] = pressure;
}
}
}
Expand Down Expand Up @@ -838,6 +863,8 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser)
for (unsigned int i = 0; i <= 4; ++i) {
if (parser->opening[i] == UNDEFINED || parser->closing[i] == UNDEFINED) {
ERROR (abstract->context, "Opening or closing record %u not found.", i);
free (trace_gas);
free (trace_pressure);
return DC_STATUS_DATAFORMAT;
}
}
Expand Down Expand Up @@ -916,6 +943,9 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser)
parser->headersize = headersize;
parser->footersize = footersize;
parser->ngasmixes = 0;
unsigned int gasmap[NGASMIXES];
for (unsigned int i = 0; i < NGASMIXES; ++i)
gasmap[i] = UNDEFINED;
if (divemode != M_FREEDIVE) {
for (unsigned int i = 0; i < ngasmixes; ++i) {
if (gasmix[i].oxygen == 0 && gasmix[i].helium == 0)
Expand All @@ -924,10 +954,39 @@ shearwater_predator_parser_cache (shearwater_predator_parser_t *parser)
continue;
if (gasmix[i].diluent && !shearwater_predator_is_ccr (divemode))
continue;
gasmap[i] = parser->ngasmixes;
parser->gasmix[parser->ngasmixes] = gasmix[i];
parser->ngasmixes++;
}
}

// AI-generated (Claude)
// For open circuit dives, the begin pressure is the highest reading, not
// the first one: a stage with its valve closed until the gas switch first
// reads the pressure left in its regulator. And link each tank to the gas
// mix it was used on, if that is clear from the pressure drops.
unsigned int assigned[NTANKS];
unsigned int sidemount = 0;
unsigned int oc = divemode == M_OC_TEC || divemode == M_OC_REC || divemode == M_AVELO;
for (unsigned int i = 0; i < NTANKS; ++i) {
assigned[i] = SHEARWATER_TANK_GAS_NONE;
if (tank[i].usage == DC_TANK_USAGE_SIDEMOUNT)
sidemount |= 1u << i;
for (unsigned int n = 0; n < nsamples && oc; ++n) {
if (trace_pressure[n * NTANKS + i] > tank[i].beginpressure)
tank[i].beginpressure = trace_pressure[n * NTANKS + i];
}
}
if (oc) {
// At least 10 bar, in units of 2 psi, rounded up.
shearwater_tank_gas_assign (trace_gas, trace_pressure, nsamples, NTANKS, ngasmixes,
sidemount, 73, assigned);
}
for (unsigned int i = 0; i < NTANKS; ++i)
tank[i].gasmix = assigned[i] < NGASMIXES ? gasmap[assigned[i]] : UNDEFINED;
free (trace_gas);
free (trace_pressure);

parser->ntanks = 0;
for (unsigned int i = 0; i < NTANKS; ++i) {
if (tank[i].active) {
Expand Down Expand Up @@ -1090,7 +1149,7 @@ shearwater_predator_parser_get_field (dc_parser_t *abstract, dc_field_type_t typ
tank->workpressure = 0.0;
tank->beginpressure = parser->tank[flags].beginpressure * 2 * PSI / BAR;
tank->endpressure = parser->tank[flags].endpressure * 2 * PSI / BAR;
tank->gasmix = DC_GASMIX_UNKNOWN;
tank->gasmix = parser->tank[flags].gasmix == UNDEFINED ? DC_GASMIX_UNKNOWN : parser->tank[flags].gasmix;
tank->usage = parser->tank[flags].usage;
break;
case DC_FIELD_SALINITY:
Expand Down
123 changes: 123 additions & 0 deletions src/shearwater_tank_gas.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* libdivecomputer
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/

// AI-generated (Claude)

/* Internal matching of Shearwater air integration transmitters to gas mixes. */
#ifndef SHEARWATER_TANK_GAS_H
#define SHEARWATER_TANK_GAS_H

#define SHEARWATER_TANK_GAS_NONE 0xFFFFFFFF
#define SHEARWATER_TANK_GAS_MAXTANKS 8
#define SHEARWATER_TANK_GAS_MAXGASES 20

/*
* The logs record the gas mix in use and the transmitter pressures, but not
* which gas mix each transmitter is on. Assign each transmitter the gas mix
* it was clearly used most on: the pressure drop while that mix was in use.
*
* gas[n] is the gas mix in use at sample n (or SHEARWATER_TANK_GAS_NONE), and
* pressure[n * ntanks + i] is the reading of transmitter i at sample n (0 if
* there is none). Readings before a transmitter's highest reading are ignored,
* so the regulator of a closed stage bleeding down doesn't count as use. The
* transmitters in the sidemount mask are counted together and get the same
* gas mix. A transmitter gets a gas mix only if it used at least minuse, and
* at least twice as much as on any other mix. Otherwise result[i] is
* SHEARWATER_TANK_GAS_NONE.
*/
static void
shearwater_tank_gas_assign (const unsigned int gas[], const unsigned int pressure[],
unsigned int nsamples, unsigned int ntanks, unsigned int ngases,
unsigned int sidemount, int minuse, unsigned int result[])
{
int use[SHEARWATER_TANK_GAS_MAXTANKS][SHEARWATER_TANK_GAS_MAXGASES] = {{0}};
const unsigned int stride = ntanks;

if (ntanks > SHEARWATER_TANK_GAS_MAXTANKS)
ntanks = SHEARWATER_TANK_GAS_MAXTANKS;
if (ngases > SHEARWATER_TANK_GAS_MAXGASES)
ngases = SHEARWATER_TANK_GAS_MAXGASES;

for (unsigned int i = 0; i < ntanks; ++i) {
unsigned int highest = 0, start = 0;
for (unsigned int n = 0; n < nsamples; ++n) {
if (pressure[n * stride + i] > highest) {
highest = pressure[n * stride + i];
start = n;
}
}
if (!highest)
continue;

// A drop between two readings only counts if the same gas mix was
// in use throughout, including the samples without a reading.
unsigned int previous = highest, previous_gas = gas[start], changed = 0;
for (unsigned int n = start + 1; n < nsamples; ++n) {
if (gas[n] != previous_gas)
changed = 1;
unsigned int p = pressure[n * stride + i];
if (p == 0)
continue;
if (!changed && gas[n] < ngases)
use[i][gas[n]] += (int) previous - (int) p;
previous = p;
previous_gas = gas[n];
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
changed = 0;
}
}

// Sidemount tanks are breathed alternately on one gas mix.
unsigned int count = 0;
int pooled[SHEARWATER_TANK_GAS_MAXGASES] = {0};
for (unsigned int i = 0; i < ntanks; ++i) {
if (sidemount & (1u << i)) {
count++;
for (unsigned int g = 0; g < ngases; ++g)
pooled[g] += use[i][g];
}
}
if (count >= 2) {
for (unsigned int i = 0; i < ntanks; ++i) {
if (sidemount & (1u << i)) {
for (unsigned int g = 0; g < ngases; ++g)
use[i][g] = pooled[g];
}
}
}

for (unsigned int i = 0; i < ntanks; ++i) {
unsigned int best = 0;
int second = 0;
for (unsigned int g = 1; g < ngases; ++g) {
if (use[i][g] > use[i][best]) {
if (use[i][best] > second)
second = use[i][best];
best = g;
} else if (use[i][g] > second) {
second = use[i][g];
}
}
if (ngases && use[i][best] >= minuse && use[i][best] >= 2 * second)
result[i] = best;
else
result[i] = SHEARWATER_TANK_GAS_NONE;
}
}

#endif
8 changes: 6 additions & 2 deletions test/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ AUTOMAKE_OPTIONS = subdir-objects
AM_CPPFLAGS = -I$(top_builddir)/include -I$(top_srcdir)/include
LDADD = $(top_builddir)/src/libdivecomputer.la

check_PROGRAMS = test_descriptor_product_name test_mares_iconhd_freedive
check_PROGRAMS = test_descriptor_product_name test_mares_iconhd_freedive test_shearwater_tank_gas test_shearwater_perdix2_tanks

test_descriptor_product_name_SOURCES = test_descriptor_product_name.c
test_mares_iconhd_freedive_SOURCES = test_mares_iconhd_freedive.c $(top_srcdir)/src/array.c
test_mares_iconhd_freedive_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src
test_shearwater_tank_gas_SOURCES = test_shearwater_tank_gas.c
test_shearwater_tank_gas_CPPFLAGS = $(AM_CPPFLAGS) -I$(top_srcdir)/src
test_shearwater_perdix2_tanks_SOURCES = test_shearwater_perdix2_tanks.c

TESTS = test_descriptor_product_name test_mares_iconhd_freedive
TESTS = test_descriptor_product_name test_mares_iconhd_freedive test_shearwater_tank_gas test_shearwater_perdix2_tanks

EXTRA_DIST = \
run-parse.sh \
fixtures/manifest.txt \
fixtures/shearwater_petrel2-0001.bin \
fixtures/shearwater_perdix2-0001.bin \
fixtures/hw_ostc5-0001.bin \
fixtures/garmin_descent_mk1-0001.bin
1 change: 1 addition & 0 deletions test/fixtures/manifest.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
# All dives are the owner's own recordings, cleared for redistribution under
# the library licence. No GPS or personal location data is present.
shearwater_petrel2-0001.bin|Shearwater|Petrel 2
shearwater_perdix2-0001.bin|Shearwater|Perdix 2
hw_ostc5-0001.bin|Heinrichs Weikamp|OSTC 5
garmin_descent_mk1-0001.bin|Garmin|Descent™ Mk1
Binary file added test/fixtures/shearwater_perdix2-0001.bin
Binary file not shown.
Loading