24#ifndef ENERGY_BUCKET_H
25#define ENERGY_BUCKET_H
41 while ((shift < 31) && (largest *
static_cast< float >(1UL << (shift + 1)) <= 65535.0F))
54 float largest{ 0.0F };
55 for (
const auto value : cal)
84constexpr uint16_t
toFixed(
const float cal,
const uint8_t shift)
87 return static_cast< uint16_t
>(cal *
static_cast< float >(1UL << shift) + 0.5F);
102template< u
int8_t NMin, u
int8_t NMax, u
int8_t N >
118template< u
int8_t NMin, u
int8_t NMax, u
int8_t SumShift = 0, u
int8_t N >
121 static_assert((NMin >= 1) && (NMin <= NMax),
"empty range of sample counts");
122 static_assert(NMax <= (64U << SumShift),
"sumP must stay within 24 bits: at most 64 sample sets per cycle, or shift sumP");
124 constexpr float sumScale{
static_cast< float >(1UL << SumShift) };
128 for (uint8_t phase = 0; phase < N; ++phase)
130 for (uint8_t n = NMin; n <= NMax; ++n)
132 table.value[phase][n - NMin] =
toFixed(cal[phase] * sumScale / n, table.shift);
149 for (
const auto value : cal)
168inline __attribute__((always_inline)) int32_t
contribution(
const int32_t averagePower,
const uint16_t calFixed,
const uint8_t shift)
172 const bool negative{ averagePower < 0 };
173 const uint32_t magnitude{
static_cast< uint32_t
>(negative ? -averagePower : averagePower) };
179 const uint8_t remaining{
static_cast< uint8_t
>(shift -
FRACTION_BITS - 8) };
191 rounded =
static_cast< int32_t
>(((product >> 8) + (1UL << (remaining - 9))) >> (remaining - 8));
195 rounded =
static_cast< int32_t
>((product + (1UL << (remaining - 1))) >> remaining);
198 return negative ? -rounded : rounded;
212constexpr uint16_t
gcd(
const uint16_t a,
const uint16_t b)
214 return b ?
gcd(b, a % b) : a;
226template< u
int8_t SUPPLY_FREQUENCY >
230 constexpr int32_t numerator{ 100 /
gcd(100, unitsPerJoule) };
231 constexpr int32_t denominator{ unitsPerJoule /
gcd(100, unitsPerJoule) };
233 return (units * numerator + (units < 0 ? -denominator / 2 : denominator / 2)) / denominator;
constexpr uint8_t SUPPLY_FREQUENCY
static void multU24x16_to32_hi8(uint32_t &result, A a, B b)
Optimized 24×16→40 unsigned multiplication, returning bits 8..39.
Assembly-optimized multiplication functions for AVR microcontrollers.
constexpr uint8_t calibrationShift(const float(&cal)[N])
Largest shift that keeps every calibration value within 16 bits.
constexpr uint16_t toFixed(const float cal, const uint8_t shift)
A calibration value as a 16-bit fixed-point constant.
constexpr uint8_t FRACTION_BITS
int32_t contribution(const int32_t averagePower, const uint16_t calFixed, const uint8_t shift)
Contribution of one mains cycle of one phase to the bucket.
constexpr uint16_t gcd(const uint16_t a, const uint16_t b)
Greatest common divisor, to reduce fractions at compile time.
constexpr PerSampleCalibration< NMin, NMax, N > toFixedPerSample(const float(&cal)[N])
Build the per-sample calibration table at compile time.
constexpr uint8_t shiftFor(const float largest)
Largest shift that keeps a value within 16 bits once scaled by 2^shift.
constexpr int32_t toHundredthsOfJoule(const int32_t units)
An energy in bucket units, in hundredths of a joule (for the datalog).
constexpr bool isValidCalibration(const float(&cal)[N])
Check that the calibration values can be represented.
constexpr float largestOf(const float(&cal)[N])
Largest of the calibration values.
constexpr int32_t fromWatts(const int32_t watts)
A power in watts, in bucket units per mains cycle.
cal / n for every phase and every expected sample count n, in fixed point.
uint16_t value[N][NMax - NMin+1]