core: turn M_OR_FT macro into a function

No point in this being a macro. Make it return a depth_t - it
was unclear that this returns a depth in mm.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2024-12-14 10:10:19 +01:00 committed by Michael Keller
parent 0e77dd9a68
commit bd2f7e72f1
11 changed files with 105 additions and 97 deletions

View file

@ -96,8 +96,8 @@ std::unique_ptr<dive> dive_table::default_dive()
auto d = std::make_unique<dive>();
d->when = time(nullptr) + gettimezoneoffset() + 3600;
d->dcs[0].duration = 40_min;
d->dcs[0].maxdepth.mm = M_OR_FT(15, 45);
d->dcs[0].meandepth.mm = M_OR_FT(13, 39); // this creates a resonable looking safety stop
d->dcs[0].maxdepth = m_or_ft(15, 45);
d->dcs[0].meandepth = m_or_ft(13, 39); // this creates a resonable looking safety stop
make_manually_added_dive_dc(&d->dcs[0]);
fake_dc(&d->dcs[0]);
add_default_cylinder(d.get());

View file

@ -314,7 +314,7 @@ void reset_cylinders(struct dive *dive, bool track_gas)
for (cylinder_t &cyl: dive->cylinders) {
if (cyl.depth.mm == 0) /* if the gas doesn't give a mod, calculate based on prefs */
cyl.depth = dive->gas_mod(cyl.gasmix, decopo2, M_OR_FT(3,10));
cyl.depth = dive->gas_mod(cyl.gasmix, decopo2, m_or_ft(3, 10).mm);
if (track_gas)
cyl.start.mbar = cyl.end.mbar = cyl.type.workingpressure.mbar;
cyl.gas_used = 0_l;

View file

@ -660,9 +660,9 @@ std::vector<decostop> plan(struct deco_state *ds, struct diveplan &diveplan, str
* Remark: not reentrant, but the user probably won't change preferences while this is running.
*/
if (prefs.last_stop)
decostoplevels[1].mm = 0;
decostoplevels[1] = 0_m;
else
decostoplevels[1].mm = M_OR_FT(3,10);
decostoplevels[1] = m_or_ft(3, 10);
/* Let's start at the last 'sample', i.e. the last manually entered waypoint. */
const struct sample &sample = dc->samples.back();
@ -1027,7 +1027,7 @@ std::vector<decostop> plan(struct deco_state *ds, struct diveplan &diveplan, str
* otherwise odd things can happen, such as CVA causing the final ascent to start *later*
* if the ascent rate is slower, which is completely nonsensical.
* Assume final ascent takes 20s, which is the time taken to ascend at 9m/min from 3m */
ds->deco_time = clock - bottom_time - (M_OR_FT(3,10) * ( prefs.last_stop ? 2 : 1)) / last_ascend_rate + 20;
ds->deco_time = clock - bottom_time - (m_or_ft(3, 10).mm * ( prefs.last_stop ? 2 : 1)) / last_ascend_rate + 20;
} while (!is_final_plan && error == PLAN_OK);
plan_add_segment(diveplan, clock - previous_point_time, 0, current_cylinder, po2, false, divemode);

View file

@ -764,12 +764,12 @@ static void calculate_ndl_tts(struct deco_state *ds, const struct dive *dive, st
const int ascent_s_per_deco_step = 1;
/* how long time steps in deco calculations? */
const int time_stepsize = 60;
const int deco_stepsize = M_OR_FT(3, 10);
const depth_t deco_stepsize = m_or_ft(3, 10);
/* at what depth is the current deco-step? */
depth_t ascent_depth { .mm = entry.depth };
int next_stop = round_up(deco_allowed_depth(
tissue_tolerance_calc(ds, dive, dive->depth_to_bar(ascent_depth), in_planner),
surface_pressure, dive, 1).mm, deco_stepsize);
surface_pressure, dive, 1).mm, deco_stepsize.mm);
/* at what time should we give up and say that we got enuff NDL? */
/* If iterating through a dive, entry.tts_calc needs to be reset */
entry.tts_calc = 0;
@ -802,14 +802,14 @@ static void calculate_ndl_tts(struct deco_state *ds, const struct dive *dive, st
add_segment(ds, dive->depth_to_bar(ascent_depth),
gasmix, ascent_s_per_step, entry.o2pressure.mbar, divemode, prefs.decosac, in_planner);
next_stop = round_up(deco_allowed_depth(tissue_tolerance_calc(ds, dive, dive->depth_to_bar(ascent_depth), in_planner),
surface_pressure, dive, 1).mm, deco_stepsize);
surface_pressure, dive, 1).mm, deco_stepsize.mm);
}
ascent_depth.mm = next_stop;
/* And how long is the current deco-step? */
entry.stoptime_calc = 0;
entry.stopdepth_calc = next_stop;
next_stop -= deco_stepsize;
next_stop -= deco_stepsize.mm;
/* And how long is the total TTS */
while (next_stop >= 0) {
@ -829,7 +829,7 @@ static void calculate_ndl_tts(struct deco_state *ds, const struct dive *dive, st
add_segment(ds, dive->depth_to_bar(ascent_depth),
gasmix, ascent_s_per_deco_step, entry.o2pressure.mbar, divemode, prefs.decosac, in_planner);
ascent_depth.mm = next_stop;
next_stop -= deco_stepsize;
next_stop -= deco_stepsize.mm;
}
}
}

View file

@ -199,3 +199,8 @@ unsigned int get_distance(location_t loc1, location_t loc2)
return lrint(6371000 * c);
}
depth_t m_or_ft(int m, int ft)
{
int mm = prefs.units.length == units::METERS ? m * 1000 : feet_to_mm(ft);
return depth_t::from_base(mm);
}

View file

@ -19,8 +19,6 @@
#define HE_DENSITY 166
#define ZERO_C_IN_MKELVIN 273150 // mKelvin
#define M_OR_FT(_m, _f) ((prefs.units.length == units::METERS) ? ((_m) * 1000) : (feet_to_mm(_f)))
/* Salinity is expressed in weight in grams per 10l */
#define SEAWATER_SALINITY 10300
#define EN13319_SALINITY 10200
@ -193,6 +191,10 @@ static inline depth_t operator""_ft(unsigned long long ft)
return depth_t { .mm = static_cast<int32_t>(round(ft * 304.8)) };
}
// Return either the first argument in meters or the second argument in
// feet, depending on use settings.
depth_t m_or_ft(int m, int ft);
struct pressure_t : public unit_base<pressure_t>
{
int32_t mbar = 0; // pressure up to 2000 bar