Skip to content

Source: math8.h

math8

Integer trig, beat timing, saturating arithmetic and a fast generator.

Functions

Return Name Description
constexpr uint8_t sin8 constexpr
constexpr uint8_t cos8 constexpr
constexpr uint8_t triwave8 constexpr A triangle wave, the textbook fold of a ramp and a sharper alternative to the sine.
constexpr uint8_t atan2_8 constexpr
constexpr uint8_t dist8 constexpr
constexpr uint8_t qadd8 constexpr Add or subtract clamping at the ends, so a bright pixel plus more stays bright.
constexpr uint8_t qsub8 constexpr
constexpr uint8_t nscale8 constexpr Scale a byte by a fraction, under the name an LED developer already reaches for.
constexpr uint8_t map8 constexpr Rescale a byte onto a range, the input's top reaching the range's top exactly.
constexpr uint8_t beat8 constexpr
constexpr uint8_t beatsin8 constexpr A sine oscillating between two bounds at a given rate, with a timebase and a fixed offset.
constexpr uint16_t beatsin16 constexpr

sin8

constexpr

constexpr constexpr uint8_t sin8(uint8_t i)

cos8

constexpr

constexpr constexpr uint8_t cos8(uint8_t i)

triwave8

constexpr

constexpr constexpr uint8_t triwave8(uint8_t i)

A triangle wave, the textbook fold of a ramp and a sharper alternative to the sine.


atan2_8

constexpr

constexpr constexpr uint8_t atan2_8(int16_t y, int16_t x)

dist8

constexpr

constexpr constexpr uint8_t dist8(int16_t dx, int16_t dy)

qadd8

constexpr

constexpr constexpr uint8_t qadd8(uint8_t a, uint8_t b)

Add or subtract clamping at the ends, so a bright pixel plus more stays bright.


qsub8

constexpr

constexpr constexpr uint8_t qsub8(uint8_t a, uint8_t b)

nscale8

constexpr

constexpr constexpr uint8_t nscale8(uint8_t val, uint8_t scale)

Scale a byte by a fraction, under the name an LED developer already reaches for.


map8

constexpr

constexpr constexpr uint8_t map8(uint8_t in, uint8_t rangeStart, uint8_t rangeEnd)

Rescale a byte onto a range, the input's top reaching the range's top exactly.


beat8

constexpr

constexpr constexpr uint8_t beat8(uint8_t bpm, uint32_t ms, uint32_t timebase = 0)

beatsin8

constexpr

constexpr constexpr uint8_t beatsin8(uint8_t bpm, uint32_t ms, uint8_t low = 0, uint8_t high = 255, uint32_t timebase = 0, uint8_t phase = 0)

A sine oscillating between two bounds at a given rate, with a timebase and a fixed offset.


beatsin16

constexpr

constexpr constexpr uint16_t beatsin16(uint8_t bpm, uint32_t ms, uint16_t low = 0, uint16_t high = 65535, uint32_t timebase = 0, uint8_t phase = 0)

Variables

Return Name Description
constexpr uint8_t sin8_lut constexpr The sine table over a 256-step circle, from which the cosine is a quarter turn.

sin8_lut

constexpr

constexpr uint8_t sin8_lut = {
    128,131,134,137,140,144,147,150,153,156,159,162,165,168,171,174,
    177,179,182,185,188,191,193,196,199,201,204,206,209,211,213,216,
    218,220,222,224,226,228,230,232,234,235,237,239,240,241,243,244,
    245,246,248,249,250,250,251,252,253,253,254,254,254,255,255,255,
    255,255,255,255,254,254,254,253,253,252,251,250,250,249,248,246,
    245,244,243,241,240,239,237,235,234,232,230,228,226,224,222,220,
    218,216,213,211,209,206,204,201,199,196,193,191,188,185,182,179,
    177,174,171,168,165,162,159,156,153,150,147,144,140,137,134,131,
    128,125,122,119,116,112,109,106,103,100,97,94,91,88,85,82,
    79,77,74,71,68,65,63,60,57,55,52,50,47,45,43,40,
    38,36,34,32,30,28,26,24,22,21,19,17,16,15,13,12,
    11,10,8,7,6,6,5,4,3,3,2,2,2,1,1,1,
    1,1,1,1,2,2,2,3,3,4,5,6,6,7,8,10,
    11,12,13,15,16,17,19,21,22,24,26,28,30,32,34,36,
    38,40,43,45,47,50,52,55,57,60,63,65,68,71,74,77,
    79,82,85,88,91,94,97,100,103,106,109,112,116,119,122,125
}

The sine table over a 256-step circle, from which the cosine is a quarter turn.

Random8

class Random8
src/core/util/math8.h:121

A small seedable generator, cheap on the hot path and reproducible per effect.

Public Methods

constexpr inline explicit constexpr Random8(uint32_t seed = 0x1234ABCDu)

constexpr inline constexpr void seed(uint32_t s) : Seed the sequence, so a run is reproducible.

constexpr inline constexpr uint8_t next8() : The next byte in the sequence.

constexpr inline constexpr uint16_t next16() : The next two bytes.

constexpr inline constexpr uint8_t below(uint8_t bound) : The next value below a bound, or between two.

constexpr inline constexpr uint8_t below(uint8_t min, uint8_t max) : The next value below a bound, or between two.

More info

This is the recognizable surface an embedded or LED developer already knows, under the same names, written fresh against our own architecture. Prior art is FastLED's lib8tion by Mark Kriegsman: we carry the ideas, the names and the textbook algorithms, and the code is ours.

It is all integer and table-backed where that pays, with no float and no heap, so it is safe in the render loop. The time-dependent helpers take the current time as a parameter rather than reading a clock, which keeps the time source at the domain edge instead of buried in core. A timebase shifts the phase origin, so an effect wanting its oscillation to start at a chosen moment passes that moment.

Why the range map divides rather than shifts

The input's top must reach the range's top exactly. An earlier form scaled by a power of two, which made the top unreachable and collapsed a one-step span: a bar height of one could never be reached. Dividing by the full byte range fixes it, and the span is held wide so the widest case cannot wrap.