FastLED 3.10.6
Loading...
Searching...
No Matches

◆ computePulseCount()

u8 fl::anonymous_namespace{uart_wave_encoder.cpp.hpp}::computePulseCount ( u32 timing_ns,
u32 pulse_width_ns )

Compute HIGH pulse count for a given timing value.

Parameters
timing_nsTiming value in nanoseconds (T0H or T1H)
pulse_width_nsWidth of each pulse in nanoseconds
Returns
Number of HIGH pulses (best-fit: floor or ceil, whichever is closer)

Tries both floor and ceil, picks the one with smaller error. Ties are broken by preferring the lower (floor) value, which keeps the HIGH time shorter – safer for most LED protocols. For example, WS2812 T1H=875ns at 250ns/pulse: floor=3 (750ns, err=125), ceil=4 (1000ns, err=125) -> picks 3 (shorter HIGH time).

Definition at line 36 of file uart_wave_encoder.cpp.hpp.

36 {
37 if (pulse_width_ns == 0) return 0;
38 u8 lo = static_cast<u8>(timing_ns / pulse_width_ns);
39 u8 hi = lo + 1;
40 u32 err_lo = absDiff(static_cast<u32>(lo) * pulse_width_ns, timing_ns);
41 u32 err_hi = absDiff(static_cast<u32>(hi) * pulse_width_ns, timing_ns);
42 // Prefer lo on tie (shorter HIGH time is safer)
43 return (err_hi < err_lo) ? hi : lo;
44}
u32 absDiff(u32 a, u32 b) FL_NO_EXCEPT
Absolute value of difference (unsigned-safe)
unsigned char u8
Definition stdint.h:131

References absDiff(), computePulseCount(), and fl::FL_NO_EXCEPT.

Referenced by computePulseCount(), and fitUartWave().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: