fix: the quarter-degree rounding was wrong below zero, in three copies
`(quarters + 2) / 4` is round-half-up only for positive values: C truncates a negative quotient toward zero, so -3.00 C read as -2, -1.00 C as 0 and -0.75 C as 0 - nine of fifteen negative quarter-values off by a whole degree, always toward zero. `(quarters + 2) >> 2` is an arithmetic shift, which floors, and is right across the whole range. It is also smaller: the shift skips the bias correction signed division needs. The formula was written three times - thermistor::celsius(), which nothing called, and inline at both live call sites - so the defect had three homes and so would its fix. One `thermistor::whole_degrees()` now, called from both, and the dead entry point is gone (rules 6, 27). test/consteval.cpp is new and is what should have caught it: whole_degrees across zero including both ties and both table limits, the curve's start and saturation points and its monotonicity, and the thermistor table anchored where the Beta equation fixes it - the count at which the divider reads the thermistor's nominal resistance must read the nominal temperature - plus both clamps and the fall across every step. Red-green: four assertions fire against the old division. Beside it: the cubic's three coefficients are named rather than inlined and restated in prose (rule 5), the consteval table builders take explicit 32-bit types (rule 25), the curve's clamp reads the table's own size (rule 36), and the serial override says what expects the rate rather than what the board has always done (rules 12, 13). And bootloader::handle_reset()'s watchdog diversion is gone. Its own comment called it a leftover "kept only because it is free and cannot hurt", and it did not: pureboot peeks WDRF without clearing it and hands back on purpose, so a watchdog reset arrives here with the flag still set and the diversion jumped into the loader with MCUSR already cleared - opening the activation window that policy exists to close. Clearing MCUSR is the whole job and stays. 8206 -> 8168 bytes, byte-identical between generated and reflect. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -5,20 +5,25 @@
|
||||
|
||||
#include <libavr/flash.hpp>
|
||||
|
||||
// The auto-mode fan curve, tabulated at compile time: a cubic in C
|
||||
// (0.002246*x^3 - 0.09*x^2 + 0.91*x, zero below 20 C) as a flash_table of
|
||||
// duty percent per C.
|
||||
// The auto-mode fan curve, tabulated at compile time as a flash_table of duty
|
||||
// percent per C: a cubic that is held at zero below the temperature the fan
|
||||
// starts at.
|
||||
namespace app::curve {
|
||||
|
||||
inline constexpr double cubic_term = 0.002246;
|
||||
inline constexpr double square_term = -0.09;
|
||||
inline constexpr double linear_term = 0.91;
|
||||
inline constexpr std::int8_t start_celsius = 20;
|
||||
|
||||
namespace detail {
|
||||
|
||||
consteval std::uint8_t duty_entry(int celsius)
|
||||
consteval std::uint8_t duty_entry(std::int32_t celsius)
|
||||
{
|
||||
double x = celsius;
|
||||
if (x < 20) {
|
||||
if (x < start_celsius) {
|
||||
return 0;
|
||||
}
|
||||
double duty = 0.002246 * x * x * x - 0.09 * x * x + 0.91 * x;
|
||||
double duty = cubic_term * x * x * x + square_term * x * x + linear_term * x;
|
||||
if (duty < 0) {
|
||||
duty = 0;
|
||||
}
|
||||
@@ -30,7 +35,7 @@ consteval std::uint8_t duty_entry(int celsius)
|
||||
|
||||
inline constexpr avr::flash_table<[] {
|
||||
std::array<std::uint8_t, 100> out{};
|
||||
for (int t = 0; t < 100; ++t) {
|
||||
for (std::int32_t t = 0; t < static_cast<std::int32_t>(out.size()); ++t) {
|
||||
out[static_cast<std::size_t>(t)] = duty_entry(t);
|
||||
}
|
||||
return out;
|
||||
@@ -39,14 +44,15 @@ inline constexpr avr::flash_table<[] {
|
||||
|
||||
} // namespace detail
|
||||
|
||||
// Duty percent for a temperature (clamped to the 0..99 C table window).
|
||||
// Duty percent for a temperature, clamped to the table's own window.
|
||||
inline std::uint8_t duty(std::int8_t celsius)
|
||||
{
|
||||
constexpr auto highest = static_cast<std::int8_t>(detail::table.size() - 1);
|
||||
if (celsius < 0) {
|
||||
celsius = 0;
|
||||
}
|
||||
if (celsius > 99) {
|
||||
celsius = 99;
|
||||
if (celsius > highest) {
|
||||
celsius = highest;
|
||||
}
|
||||
return detail::table[static_cast<std::uint8_t>(celsius)];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user