Files
fantemp/test/consteval.cpp
BlackMark ba37ecea04 test: the format and ASCII rules stop being a habit, and the stated sizes stop drifting
libavr's guidance binds this repo too, and until now nothing here checked it -
`ctest` runs `libavr_format_test()` over this tree's own sources now (rules 11
and 33), skipping rather than passing where clang-format is absent. It caught
drift on its first run: a file written this week and edited after formatting.

Where the README states a measured size, `libavr_size_claim_test()` holds it to
the image and holds the image to the prose: advancing the library pin moved
three of these across the fleet with nothing saying so, and re-recording one
now requires the sentence that quotes it to move too.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-12 23:02:40 +02:00

85 lines
3.2 KiB
C++

// Compile-only battery, built with the cross compiler so the target's 16-bit
// int is exercised. Two of this firmware's three numeric surfaces are decided
// entirely at compile time - the fan curve and the thermistor table - and the
// third, quarter-degrees to whole ones, is the arithmetic between them.
#include <cstdint>
#include "curve.hpp"
#include "thermistor.hpp"
namespace {
using app::thermistor::whole_degrees;
// Rounding to nearest, across zero. A negative quotient truncates toward zero
// in C, so a `(q + 2) / 4` reads -3.00 C as -2 and every case below the tie
// with it; the arithmetic shift floors, which is what these hold it to.
static_assert(whole_degrees(0) == 0);
static_assert(whole_degrees(1) == 0); // +0.25
static_assert(whole_degrees(2) == 1); // +0.50, the tie
static_assert(whole_degrees(3) == 1); // +0.75
static_assert(whole_degrees(4) == 1); // +1.00
static_assert(whole_degrees(-1) == 0); // -0.25
static_assert(whole_degrees(-2) == 0); // -0.50, the tie, toward zero
static_assert(whole_degrees(-3) == -1);
static_assert(whole_degrees(-4) == -1);
static_assert(whole_degrees(-12) == -3);
static_assert(whole_degrees(-160) == -40); // the table's own floor
static_assert(whole_degrees(500) == 125); // and its ceiling
// The curve is held at zero below the temperature the fan starts at, and
// saturates inside the table's window rather than at its edge.
using app::curve::detail::duty_entry;
static_assert(duty_entry(app::curve::start_celsius - 1) == 0);
static_assert(duty_entry(app::curve::start_celsius) == 0);
static_assert(duty_entry(30) == 7);
static_assert(duty_entry(40) == 36);
static_assert(duty_entry(49) == 93);
static_assert(duty_entry(50) == 100);
static_assert(duty_entry(99) == 100);
// Monotone across the whole table: a warmer reading never asks for less air.
consteval bool curve_rises()
{
for (std::int32_t t = 1; t < 100; ++t) {
if (duty_entry(t) < duty_entry(t - 1)) {
return false;
}
}
return true;
}
static_assert(curve_rises());
// The thermistor table, anchored where the Beta equation fixes it rather than
// against numbers this file computed the same way: the divider reads the
// thermistor's nominal resistance at
// adc = full_scale * nominal / (series + nominal), and the equation's own
// definition puts that count at the nominal temperature.
using app::thermistor::detail::quarters_entry;
inline constexpr std::int32_t nominal_count =
static_cast<std::int32_t>(app::thermistor::adc_full_scale * app::thermistor::nominal_resistance /
(app::thermistor::series_resistor + app::thermistor::nominal_resistance));
static_assert(quarters_entry(nominal_count / 4) >= 100); // 25.00 C, in quarters
static_assert(quarters_entry(nominal_count / 4 + 1) < 100); // and the step below it
// Both clamps, at the ends the divider cannot leave.
static_assert(quarters_entry(0) == 125 * 4);
static_assert(quarters_entry(255) == -40 * 4);
// An NTC on this divider falls with the count: more counts is more resistance
// is a colder sensor, over every step of the table.
consteval bool thermistor_falls()
{
for (std::int32_t i = 1; i < 256; ++i) {
if (quarters_entry(i) > quarters_entry(i - 1)) {
return false;
}
}
return true;
}
static_assert(thermistor_falls());
} // namespace