WS2812_RMT Driver 0.1.0
API reference
Loading...
Searching...
No Matches
ws2812_rmt_functions.hpp
Go to the documentation of this file.
1
13#ifndef WS2812_RMT_FUNCTIONS_HPP
14#define WS2812_RMT_FUNCTIONS_HPP
15
16#include <algorithm>
17#include <array>
18#include <cstddef>
19#include <cstdint>
20#include <span>
21
22#include "ws2812_rmt_detail.hpp"
23#include "ws2812_rmt_gamma.hpp"
24#include "ws2812_rmt_types.hpp"
25
26namespace ws2812_rmt {
27
35inline constexpr uint64_t kNsPerSecond = 1'000'000'000ULL;
36
45[[nodiscard]] constexpr uint8_t Scale8(uint8_t v, uint8_t b) noexcept {
46 const uint32_t product = static_cast<uint32_t>(v) * static_cast<uint32_t>(b);
47 return static_cast<uint8_t>((product + 127U) / 255U);
48}
49
56[[nodiscard]] constexpr uint8_t ApplyGamma(uint8_t v, Gamma gamma) noexcept {
57 switch (gamma) {
58 case Gamma::None:
59 return v;
60 case Gamma::Gamma22:
61 return kGamma22[v];
62 default:
63 break;
64 }
65 return v;
66}
67
74[[nodiscard]] constexpr uint8_t ChannelOf(const Colour& colour, Channel channel) noexcept {
75 switch (channel) {
76 case Channel::R:
77 return colour.r;
78 case Channel::G:
79 return colour.g;
80 case Channel::B:
81 return colour.b;
82 case Channel::W:
83 return colour.w;
84 default:
85 break;
86 }
87 return 0U;
88}
89
98[[nodiscard]] constexpr uint8_t WireValue(uint8_t v, uint8_t brightness, Gamma gamma) noexcept {
99 return Scale8(ApplyGamma(v, gamma), brightness);
100}
101
108[[nodiscard]] constexpr bool IsValidOrder(const ColourOrder& order) noexcept {
109 if ((order.count != 3U) && (order.count != 4U)) {
110 return false;
111 }
112 uint32_t seen = 0U;
113 for (uint32_t i = 0U; i < static_cast<uint32_t>(order.count); ++i) {
114 const Channel c = order.ch[i];
115 if (!detail::IsEnumerator(c)) {
116 return false;
117 }
118 const uint32_t bit = uint32_t{1} << (static_cast<uint32_t>(detail::ToUnderlying(c)) & 31U);
119 if (((seen & bit) != 0U) || ((order.count == 3U) && (c == Channel::W))) {
120 return false;
121 }
122 seen |= bit;
123 }
124 return true;
125}
126
137[[nodiscard]] constexpr size_t EncodePixel(const Colour& colour, const ColourOrder& order,
138 uint8_t brightness, Gamma gamma,
139 std::span<uint8_t> out) noexcept {
140 if (!IsValidOrder(order) || (out.size() < static_cast<size_t>(order.count))) {
141 return 0U;
142 }
143 for (size_t i = 0U; i < static_cast<size_t>(order.count); ++i) {
144 out[i] = WireValue(ChannelOf(colour, order.ch[i]), brightness, gamma);
145 }
146 return static_cast<size_t>(order.count);
147}
148
159[[nodiscard]] constexpr size_t EncodeFrame(std::span<const Colour> pixels, const ColourOrder& order,
160 uint8_t brightness, Gamma gamma,
161 std::span<uint8_t> out) noexcept {
162 if (!IsValidOrder(order)) {
163 return 0U;
164 }
165 const auto width = static_cast<size_t>(order.count);
166 if (out.size() < (pixels.size() * width)) {
167 return 0U;
168 }
169 size_t offset = 0U;
170 for (const Colour& colour : pixels) {
171 offset += EncodePixel(colour, order, brightness, gamma, out.subspan(offset, width));
172 }
173 return offset;
174}
175
185[[nodiscard]] constexpr size_t DecodeWire(std::span<const uint8_t> wire, const ColourOrder& order,
186 std::span<Colour> out) noexcept {
187 if (!IsValidOrder(order)) {
188 return 0U;
189 }
190 const size_t width = (order.count == 4U) ? 4U : 3U; // IsValidOrder() allowed only 3 or 4
191 const size_t pixels = std::min(wire.size() / width, out.size());
192 for (size_t p = 0U; p < pixels; ++p) {
193 Colour colour{};
194 for (size_t i = 0U; i < width; ++i) {
195 const uint8_t v = wire[(p * width) + i];
196 switch (order.ch[i]) {
197 case Channel::R:
198 colour.r = v;
199 break;
200 case Channel::G:
201 colour.g = v;
202 break;
203 case Channel::B:
204 colour.b = v;
205 break;
206 case Channel::W:
207 colour.w = v;
208 break;
209 default:
210 break;
211 }
212 }
213 out[p] = colour;
214 }
215 return pixels;
216}
217
225[[nodiscard]] constexpr Colour ExtractWhite(const Colour& colour) noexcept {
226 const uint8_t white = std::min({colour.r, colour.g, colour.b});
227 return Colour{.r = static_cast<uint8_t>(colour.r - white),
228 .g = static_cast<uint8_t>(colour.g - white),
229 .b = static_cast<uint8_t>(colour.b - white),
230 .w = white};
231}
232
239[[nodiscard]] constexpr Colour Wheel(uint8_t position) noexcept {
240 const uint32_t pos = position;
241 if (pos < 85U) {
242 return Colour{.r = static_cast<uint8_t>(255U - (pos * 3U)),
243 .g = static_cast<uint8_t>(pos * 3U),
244 .b = 0,
245 .w = 0};
246 }
247 if (pos < 170U) {
248 const uint32_t p = pos - 85U;
249 return Colour{.r = 0,
250 .g = static_cast<uint8_t>(255U - (p * 3U)),
251 .b = static_cast<uint8_t>(p * 3U),
252 .w = 0};
253 }
254 const uint32_t p = pos - 170U;
255 return Colour{.r = static_cast<uint8_t>(p * 3U),
256 .g = 0,
257 .b = static_cast<uint8_t>(255U - (p * 3U)),
258 .w = 0};
259}
260
268[[nodiscard]] constexpr uint64_t NsToTicks(uint64_t ns, uint64_t hz) noexcept {
269 return ((ns * hz) + (kNsPerSecond / 2U)) / kNsPerSecond;
270}
271
272namespace detail {
273
275[[nodiscard]] constexpr uint64_t CeilTicks(uint64_t ns, uint64_t hz) noexcept {
276 return ((ns * hz) + (kNsPerSecond - 1U)) / kNsPerSecond;
277}
278
280[[nodiscard]] constexpr uint64_t FloorTicks(uint64_t ns, uint64_t hz) noexcept {
281 return (ns * hz) / kNsPerSecond;
282}
283
285struct TickWindow {
286 uint64_t lo{0};
287 uint64_t hi{0};
288};
289
292[[nodiscard]] constexpr TickWindow WindowOf(const Range& r, uint64_t hz, uint64_t cap_ns) noexcept {
293 TickWindow w{.lo = CeilTicks(r.min_ns, hz), .hi = 0U};
294 const uint64_t cap_hi = CeilTicks(cap_ns, hz) - 1U;
295 w.hi = (r.max_ns == 0U) ? cap_hi : std::min(FloorTicks(r.max_ns, hz), cap_hi);
296 return w;
297}
298
300[[nodiscard]] constexpr uint64_t Aim(const Range& r, const TickWindow& w, uint64_t hz) noexcept {
301 return std::clamp(NsToTicks(r.nominal_ns, hz), w.lo, w.hi);
302}
303
305[[nodiscard]] constexpr bool RangeValid(const Range& r) noexcept {
306 const bool bounded_ok =
307 (r.max_ns == 0U) || ((r.max_ns >= r.min_ns) && (r.max_ns <= kMaxDurationNs));
308 const bool nominal_ok = (r.nominal_ns >= r.min_ns) && (r.nominal_ns <= kMaxDurationNs) &&
309 ((r.max_ns == 0U) || (r.nominal_ns <= r.max_ns));
310 return (r.min_ns > 0U) && bounded_ok && nominal_ok;
311}
312
315[[nodiscard]] constexpr bool TimingValid(const Timing& t) noexcept {
316 if (!RangeValid(t.t0h) || !RangeValid(t.t0l) || !RangeValid(t.t1h) || !RangeValid(t.t1l)) {
317 return false;
318 }
319 const bool period_ok =
320 (t.period_min_ns <= kMaxPeriodNs) &&
321 ((t.period_max_ns == 0U) ||
322 ((t.period_max_ns >= t.period_min_ns) && (t.period_max_ns <= kMaxPeriodNs) &&
323 ((t.t0h.min_ns + t.t0l.min_ns) <= t.period_max_ns) &&
324 ((t.t1h.min_ns + t.t1l.min_ns) <= t.period_max_ns)));
325 const uint64_t reset_cap = static_cast<uint64_t>(kMaxResetUs) * 1000U;
326 return period_ok && (t.reset_min_ns > 0U) && (t.reset_min_ns <= reset_cap) &&
327 (t.max_low_ns <= t.reset_min_ns);
328}
329
331struct TickBase {
332 uint64_t hz;
333 uint64_t cap_ns;
335};
336
339[[nodiscard]] constexpr bool ResolveBit(const Range& high, const Range& low, const Timing& t,
340 const TickBase& base, uint32_t& th, uint32_t& tl) noexcept {
341 const uint64_t hz = base.hz;
342 const uint64_t cap_ns = base.cap_ns;
343 const TickWindow hw = WindowOf(high, hz, cap_ns);
344 const TickWindow lw = WindowOf(low, hz, cap_ns);
345 if ((hw.lo > hw.hi) || (lw.lo > lw.hi) || (hw.hi > 0xFFFF'FFFFULL) || (lw.hi > 0xFFFF'FFFFULL)) {
346 return false;
347 }
348 const uint64_t h = Aim(high, hw, hz);
349 uint64_t l = Aim(low, lw, hz);
350 const uint64_t need = CeilTicks(t.period_min_ns, hz);
351 if ((h + l) < need) {
352 l = need - h;
353 }
354 const bool too_long =
355 (t.period_max_ns != 0U) && (((h + l) * kNsPerSecond) > (t.period_max_ns * hz));
356 if ((l > lw.hi) || too_long) {
357 return false;
358 }
359 th = static_cast<uint32_t>(h);
360 tl = static_cast<uint32_t>(l);
361 return true;
362}
363
367[[nodiscard]] constexpr bool InWindow(const Range& r, uint32_t ticks, uint64_t hz,
368 uint64_t cap_ns) noexcept {
369 const uint64_t scaled = static_cast<uint64_t>(ticks) * kNsPerSecond;
370 const bool above_min = scaled >= (static_cast<uint64_t>(r.min_ns) * hz);
371 bool below_max = true;
372 if (r.max_ns != 0U) {
373 below_max = scaled <= (static_cast<uint64_t>(r.max_ns) * hz);
374 } else if (cap_ns != 0U) {
375 below_max = scaled < (cap_ns * hz);
376 } else {
377 // No upper bound and no cap: nothing to check.
378 }
379 return above_min && below_max;
380}
381
382} // namespace detail
383
401[[nodiscard]] constexpr Result<Waveform> ResolveWaveform(const Timing& timing,
402 uint32_t resolution_hz,
403 uint32_t reset_us) noexcept {
404 using Failure = Result<Waveform>;
405 if ((resolution_hz == 0U) || (resolution_hz > kMaxResolutionHz) || (reset_us > kMaxResetUs) ||
406 !detail::TimingValid(timing)) {
407 return Failure::Failure(Error::TimingUnreachable);
408 }
409 const uint64_t hz = resolution_hz;
410 const auto cap_ns =
411 static_cast<uint64_t>((timing.max_low_ns != 0U) ? timing.max_low_ns : timing.reset_min_ns);
412 Waveform w{};
413 w.resolution_hz = resolution_hz;
414 const detail::TickBase base{.hz = hz, .cap_ns = cap_ns};
415 if (!detail::ResolveBit(timing.t0h, timing.t0l, timing, base, w.t0h_ticks, w.t0l_ticks) ||
416 !detail::ResolveBit(timing.t1h, timing.t1l, timing, base, w.t1h_ticks, w.t1l_ticks)) {
417 return Failure::Failure(Error::TimingUnreachable);
418 }
419 const uint64_t default_us =
420 ((static_cast<uint64_t>(timing.reset_min_ns) + 999U) / 1000U) + kResetMarginUs;
421 const uint64_t wanted_ns =
422 ((reset_us == 0U) ? default_us : static_cast<uint64_t>(reset_us)) * 1000U;
423 const uint64_t floor_ticks = detail::FloorTicks(timing.reset_min_ns, hz) + 1U;
424 w.reset_ticks = static_cast<uint32_t>(std::max(NsToTicks(wanted_ns, hz), floor_ticks));
425 // The longest low inside a frame: the smallest latch time any document of the variant prints,
426 // less 10 percent for the pixel's own threshold spread, and strictly below that.
427 const uint64_t gap_ns = cap_ns - (cap_ns / 10U);
428 w.max_gap_ticks = static_cast<uint32_t>(detail::CeilTicks(gap_ns, hz) - 1U);
429 // The driver's own lows must obey the limit it hands the port.
430 if ((w.t0l_ticks > w.max_gap_ticks) || (w.t1l_ticks > w.max_gap_ticks)) {
431 return Failure::Failure(Error::TimingUnreachable);
432 }
433 return Failure::Success(w);
434}
435
443[[nodiscard]] constexpr uint64_t TicksToNs(uint64_t ticks, uint32_t resolution_hz) noexcept {
444 if (resolution_hz == 0U) {
445 return 0U;
446 }
447 return ((ticks * kNsPerSecond) + (static_cast<uint64_t>(resolution_hz) - 1U)) / resolution_hz;
448}
449
459[[nodiscard]] constexpr bool WaveformInWindow(Variant variant, const Waveform& wave) noexcept {
460 if ((variant == Variant::Custom) || !detail::IsEnumerator(variant) ||
461 (wave.resolution_hz == 0U)) {
462 return false;
463 }
464 const Timing t = ProfileTiming(variant);
465 const uint64_t hz = wave.resolution_hz;
466 const auto cap_ns = static_cast<uint64_t>((t.max_low_ns != 0U) ? t.max_low_ns : t.reset_min_ns);
467 const bool windows = detail::InWindow(t.t0h, wave.t0h_ticks, hz, 0U) &&
468 detail::InWindow(t.t0l, wave.t0l_ticks, hz, cap_ns) &&
469 detail::InWindow(t.t1h, wave.t1h_ticks, hz, 0U) &&
470 detail::InWindow(t.t1l, wave.t1l_ticks, hz, cap_ns);
471 const uint64_t p0 = (static_cast<uint64_t>(wave.t0h_ticks) + wave.t0l_ticks) * kNsPerSecond;
472 const uint64_t p1 = (static_cast<uint64_t>(wave.t1h_ticks) + wave.t1l_ticks) * kNsPerSecond;
473 const uint64_t pmin = static_cast<uint64_t>(t.period_min_ns) * hz;
474 const uint64_t pmax = static_cast<uint64_t>(t.period_max_ns) * hz;
475 const bool periods_min = (p0 >= pmin) && (p1 >= pmin);
476 const bool periods_max = (t.period_max_ns == 0U) || ((p0 <= pmax) && (p1 <= pmax));
477 const bool reset_ok = (static_cast<uint64_t>(wave.reset_ticks) * kNsPerSecond) >
478 (static_cast<uint64_t>(t.reset_min_ns) * hz);
479 return windows && periods_min && periods_max && reset_ok;
480}
481
488[[nodiscard]] constexpr uint64_t FrameBits(size_t pixels, uint8_t channels) noexcept {
489 return static_cast<uint64_t>(pixels) * static_cast<uint64_t>(channels) * 8U;
490}
491
500[[nodiscard]] constexpr uint64_t FrameTimeNs(size_t pixels, uint8_t channels,
501 const Waveform& wave) noexcept {
502 const uint64_t zero = static_cast<uint64_t>(wave.t0h_ticks) + wave.t0l_ticks;
503 const uint64_t one = static_cast<uint64_t>(wave.t1h_ticks) + wave.t1l_ticks;
504 const uint64_t ticks = (FrameBits(pixels, channels) * std::max(zero, one)) + wave.reset_ticks;
505 return TicksToNs(ticks, wave.resolution_hz);
506}
507
515[[nodiscard]] constexpr uint32_t MaxRefreshHz(size_t pixels, uint8_t channels,
516 const Waveform& wave) noexcept {
517 const uint64_t ns = FrameTimeNs(pixels, channels, wave);
518 return (ns == 0U) ? 0U : static_cast<uint32_t>(kNsPerSecond / ns);
519}
520
528[[nodiscard]] constexpr size_t MaxPixelsAtRate(uint32_t rate_hz, uint8_t channels,
529 const Waveform& wave) noexcept {
530 const uint64_t one_pixel =
531 FrameTimeNs(size_t{1}, channels, wave) - FrameTimeNs(size_t{0}, channels, wave);
532 const uint64_t budget = (rate_hz == 0U) ? 0U : (kNsPerSecond / rate_hz);
533 const uint64_t reset = FrameTimeNs(size_t{0}, channels, wave);
534 if ((one_pixel == 0U) || (budget <= reset)) {
535 return 0U;
536 }
537 return static_cast<size_t>((budget - reset) / one_pixel);
538}
539
542inline constexpr uint32_t kRmtMaxPrescale = 256;
543
544namespace detail {
545
548[[nodiscard]] constexpr uint32_t RmtRealRate(uint32_t source_hz, uint32_t requested_hz) noexcept {
549 for (uint32_t group = 1U; group <= kRmtMaxPrescale; ++group) {
550 const uint32_t group_hz = source_hz / group;
551 const uint64_t divider =
552 (static_cast<uint64_t>(group_hz) + static_cast<uint64_t>(requested_hz / 2U)) / requested_hz;
553 if ((divider != 0U) && (divider <= kRmtMaxPrescale)) {
554 return static_cast<uint32_t>(group_hz / divider);
555 }
556 }
557 return 0U;
558}
559
560} // namespace detail
561
575[[nodiscard]] constexpr Result<uint32_t> ResolveRmtRate(uint32_t source_hz, uint32_t requested_hz,
576 uint32_t tolerance_ppm = 1000U) noexcept {
577 if ((requested_hz == 0U) || (source_hz == 0U)) {
579 }
580 const uint32_t real = detail::RmtRealRate(source_hz, requested_hz);
581 const uint64_t deviation = (real > requested_hz) ? static_cast<uint64_t>(real - requested_hz)
582 : static_cast<uint64_t>(requested_hz - real);
583 if ((real == 0U) ||
584 ((deviation * 1'000'000ULL) > (static_cast<uint64_t>(tolerance_ppm) * requested_hz))) {
586 }
587 return Result<uint32_t>::Success(real);
588}
589
605[[nodiscard]] constexpr uint32_t MinLogicHighMv(Variant variant, uint32_t vdd_mv) noexcept {
606 const auto vdd = static_cast<uint64_t>(std::min(vdd_mv, kMaxSupplyMv));
607 const auto seventy = static_cast<uint32_t>((vdd * 70U) / 100U);
608 switch (variant) {
610 // The strictest of the documents of the profile: V5.0 2.7 V fixed, V6.0 and V6.1 0.63 x VDD,
611 // V6 (2025) 0.55 x VDD, 1313-V6 0.5 x VDD.
612 return std::max(kV5MinHighMv, static_cast<uint32_t>((vdd * 63U) / 100U));
613 case Variant::Sk6812:
615 return std::max(kSk6812MinHighMv, seventy);
616 case Variant::Ws2812:
618 case Variant::Ws2812c:
619 case Variant::Ws2812d:
621 case Variant::Custom:
622 return seventy;
623 default:
624 break;
625 }
626 return seventy;
627}
628
634[[nodiscard]] constexpr Timing TimingOf(const Config& config) noexcept {
635 return (config.variant == Variant::Custom) ? config.custom : ProfileTiming(config.variant);
636}
637
644[[nodiscard]] constexpr ColourOrder OrderOf(const Config& config) noexcept {
645 return config.use_default_order ? DefaultOrder(config.variant) : config.order;
646}
647
648namespace detail {
649
652[[nodiscard]] constexpr bool OrderAndResetOk(const Config& config) noexcept {
653 const bool custom = config.variant == Variant::Custom;
654 const ColourOrder order = OrderOf(config);
655 const bool order_ok =
656 IsValidOrder(order) && (custom || (order.count == DefaultOrder(config.variant).count));
657 const uint64_t reset_ns = static_cast<uint64_t>(config.reset_us) * 1000U;
658 const bool reset_ok = (config.reset_us == 0U) || ((config.reset_us <= kMaxResetUs) &&
659 (reset_ns > TimingOf(config).reset_min_ns));
660 return order_ok && reset_ok;
661}
662
663} // namespace detail
664
681[[nodiscard]] constexpr Result<void> Validate(const Config& config, size_t capacity) noexcept {
682 using Failure = Result<void>;
683 if (!detail::IsEnumerator(config.variant) || !detail::IsEnumerator(config.gamma)) {
684 return Failure::Failure(Error::InvalidArgument);
685 }
686 const bool custom = config.variant == Variant::Custom;
687 if (custom ? !detail::TimingValid(config.custom) : !(config.custom == Timing{})) {
688 return Failure::Failure(Error::InvalidArgument);
689 }
690 const bool count_ok =
691 (capacity > 0U) && (capacity <= kMaxPixels) && (config.pixel_count <= capacity);
692 const bool misc_ok =
693 (config.power_on_settle_us <= kMaxSettleUs) && (config.transmit_slack_ms <= kMaxSlackMs);
694 if (!(detail::OrderAndResetOk(config) && count_ok && misc_ok)) {
695 return Failure::Failure(Error::InvalidArgument);
696 }
697 return Failure::Success();
698}
699
702} // namespace ws2812_rmt
703
704#endif // WS2812_RMT_FUNCTIONS_HPP
constexpr uint64_t NsToTicks(uint64_t ns, uint64_t hz) noexcept
round(ns * hz / 1e9), halves up, in 64 bits (ns and hz below 2^32 cannot overflow).
Definition ws2812_rmt_functions.hpp:268
constexpr uint64_t kNsPerSecond
Nanoseconds in a second, for the integer tick arithmetic.
Definition ws2812_rmt_functions.hpp:35
constexpr Timing TimingOf(const Config &config) noexcept
The timing a configuration selects.
Definition ws2812_rmt_functions.hpp:634
constexpr bool IsValidOrder(const ColourOrder &order) noexcept
Whether order is usable: a count of 3 (distinct channels among R, G, B) or 4 (all four).
Definition ws2812_rmt_functions.hpp:108
constexpr size_t MaxPixelsAtRate(uint32_t rate_hz, uint8_t channels, const Waveform &wave) noexcept
The longest strip that still refreshes at rate_hz.
Definition ws2812_rmt_functions.hpp:528
constexpr size_t DecodeWire(std::span< const uint8_t > wire, const ColourOrder &order, std::span< Colour > out) noexcept
The inverse of EncodeFrame() at brightness 255 and no gamma, for models and tests.
Definition ws2812_rmt_functions.hpp:185
constexpr uint8_t ApplyGamma(uint8_t v, Gamma gamma) noexcept
The transfer curve of a channel value.
Definition ws2812_rmt_functions.hpp:56
constexpr uint64_t FrameTimeNs(size_t pixels, uint8_t channels, const Waveform &wave) noexcept
Worst-case time of a frame: every bit a one (or a zero, whichever is longer), plus the reset.
Definition ws2812_rmt_functions.hpp:500
constexpr Result< uint32_t > ResolveRmtRate(uint32_t source_hz, uint32_t requested_hz, uint32_t tolerance_ppm=1000U) noexcept
The tick rate an ESP32 RMT channel really runs at, computed as ESP-IDF v5.5 does (rmt_common....
Definition ws2812_rmt_functions.hpp:575
constexpr uint32_t MinLogicHighMv(Variant variant, uint32_t vdd_mv) noexcept
The lowest logic-high level the part's DIN accepts at a supply voltage, from the part's electrical ta...
Definition ws2812_rmt_functions.hpp:605
constexpr ColourOrder OrderOf(const Config &config) noexcept
The wire order a configuration selects.
Definition ws2812_rmt_functions.hpp:644
constexpr Colour Wheel(uint8_t position) noexcept
A colour wheel: red to green to blue to red in three segments of 85 steps.
Definition ws2812_rmt_functions.hpp:239
constexpr uint64_t FrameBits(size_t pixels, uint8_t channels) noexcept
The number of bits in a frame.
Definition ws2812_rmt_functions.hpp:488
constexpr uint8_t ChannelOf(const Colour &colour, Channel channel) noexcept
One channel of a colour.
Definition ws2812_rmt_functions.hpp:74
constexpr uint8_t WireValue(uint8_t v, uint8_t brightness, Gamma gamma) noexcept
The wire value of a channel: gamma first, then brightness (linear dimming of the corrected value).
Definition ws2812_rmt_functions.hpp:98
constexpr uint8_t Scale8(uint8_t v, uint8_t b) noexcept
round(v * b / 255), halves up: exact at b = 255 (identity) and b = 0 (zero), monotone in both argumen...
Definition ws2812_rmt_functions.hpp:45
constexpr Result< void > Validate(const Config &config, size_t capacity) noexcept
Check a configuration against a strip capacity without touching hardware. Pure and constexpr,...
Definition ws2812_rmt_functions.hpp:681
constexpr size_t EncodeFrame(std::span< const Colour > pixels, const ColourOrder &order, uint8_t brightness, Gamma gamma, std::span< uint8_t > out) noexcept
Encode a whole strip, pixel 0 first.
Definition ws2812_rmt_functions.hpp:159
constexpr uint32_t kRmtMaxPrescale
Definition ws2812_rmt_functions.hpp:542
constexpr Colour ExtractWhite(const Colour &colour) noexcept
Move the common part of red, green and blue into the white channel, for an RGBW strip that should sho...
Definition ws2812_rmt_functions.hpp:225
constexpr Result< Waveform > ResolveWaveform(const Timing &timing, uint32_t resolution_hz, uint32_t reset_us) noexcept
Turn a timing into ticks at the port's real tick rate.
Definition ws2812_rmt_functions.hpp:401
constexpr bool WaveformInWindow(Variant variant, const Waveform &wave) noexcept
The windows check of a waveform against a named profile, in exact integer arithmetic: the mixed-chain...
Definition ws2812_rmt_functions.hpp:459
constexpr size_t EncodePixel(const Colour &colour, const ColourOrder &order, uint8_t brightness, Gamma gamma, std::span< uint8_t > out) noexcept
Encode one pixel into wire order.
Definition ws2812_rmt_functions.hpp:137
constexpr uint64_t TicksToNs(uint64_t ticks, uint32_t resolution_hz) noexcept
Convert ticks back to nanoseconds, rounding up (frame arithmetic, never a window check).
Definition ws2812_rmt_functions.hpp:443
constexpr uint32_t MaxRefreshHz(size_t pixels, uint8_t channels, const Waveform &wave) noexcept
The refresh rate the frame time allows.
Definition ws2812_rmt_functions.hpp:515
constexpr uint32_t kResetMarginUs
Added to the datasheet reset minimum when Config::reset_us is 0, in microseconds (A-3).
Definition ws2812_rmt_types.hpp:331
constexpr ColourOrder DefaultOrder(Variant variant) noexcept
The colour order a named variant uses unless Config overrides it.
Definition ws2812_rmt_types.hpp:488
constexpr uint32_t kMaxResolutionHz
The highest tick rate the driver accepts from a port, in Hz.
Definition ws2812_rmt_types.hpp:339
constexpr uint32_t kMaxSettleUs
Longest power-on settle Config may ask for, in microseconds.
Definition ws2812_rmt_types.hpp:335
constexpr uint32_t kMaxDurationNs
Longest single high or low duration a Custom timing may name, in ns (covers 400 kbit/s).
Definition ws2812_rmt_types.hpp:324
constexpr uint32_t kV5MinHighMv
The fixed floor of VIH of the V5 family, in millivolts (V5.0, datasheet p.2); later revisions print a...
Definition ws2812_rmt_types.hpp:346
constexpr uint32_t kMaxResetUs
Longest reset a timing or Config::reset_us may name, in microseconds.
Definition ws2812_rmt_types.hpp:328
constexpr uint32_t kMaxSlackMs
Longest transmit slack Config may ask for, in milliseconds.
Definition ws2812_rmt_types.hpp:337
constexpr uint32_t kMaxSupplyMv
Supplies above this count as 6 V in MinLogicHighMv() (the absolute maximum is 5.3 V).
Definition ws2812_rmt_types.hpp:343
Channel
A colour channel of a pixel.
Definition ws2812_rmt_types.hpp:199
Gamma
The transfer curve applied before brightness (convenience, not a chip feature).
Definition ws2812_rmt_types.hpp:215
constexpr uint32_t kSk6812MinHighMv
The 3.4 V input high level of the SK6812 family at 5 V, in millivolts: the minimum of SK6812RGBW Rev ...
Definition ws2812_rmt_types.hpp:351
constexpr Timing ProfileTiming(Variant variant) noexcept
The window set of a named variant, as nanoseconds. Values are the intersection of the windows of ever...
Definition ws2812_rmt_types.hpp:430
Variant
The timing generation (window set) of the part, chosen by what is printed on or known about it.
Definition ws2812_rmt_types.hpp:176
constexpr size_t kMaxPixels
Largest strip the driver accepts, in pixels.
Definition ws2812_rmt_types.hpp:333
constexpr uint32_t kMaxPeriodNs
Longest period a Custom timing may name, in ns.
Definition ws2812_rmt_types.hpp:326
@ W
White (RGBW parts only).
@ Gamma22
round(255 * (v / 255) ^ 2.2) from a committed table.
@ Ws2812
WS2812 (6 pin): 0.35/0.8/0.7/0.6 us +-150 ns, reset above 50 us.
@ Ws2812bV5
WS2812B V5 and V6, WS2812B-2020, WS2812B-1313, WS2812C-2020: reset above 280 us.
@ Ws2812c
WS2812C (5050) and WS2812-2020: T1L window 220 to 420 ns.
@ Ws2812d
WS2812D-F8: 750 to 1000 ns on T0L and T1H.
@ Custom
Any other part with the same frame: the whole timing is Config::custom.
@ Ws2812bPreV5
WS2812B and WS2812S of 2013 to 2017: reset above 300 us.
@ Sk6812
SK6812 RGB (Rev 01): 0.3/0.9/0.6/0.6 us +-150 ns, reset 80 us.
@ Sk6812Rgbw
SK6812RGBW: 32 bit frame, low inside a frame below 20 us.
@ InvalidArgument
Validate() failed, or a pixel index is out of range.
@ TimingUnreachable
The port's tick rate cannot place any tick count inside a timing window.
The WS2812_RMT driver: everything it declares lives here; internals in detail.
Definition ws2812_rmt.hpp:29
constexpr std::array< uint8_t, 256 > kGamma22
The gamma 2.2 transfer curve for 8 bit values.
Definition ws2812_rmt_gamma.hpp:21
The order of the channels on the wire, first sent first.
Definition ws2812_rmt_types.hpp:268
uint8_t count
3 or 4 channels used.
Definition ws2812_rmt_types.hpp:271
A pixel colour, 8 bit per channel; w is used only by four-channel orders.
Definition ws2812_rmt_types.hpp:229
uint8_t r
Red.
Definition ws2812_rmt_types.hpp:230
Everything EnsureInitialized() uses. The defaults suit a current WS2812B (V5, V6).
Definition ws2812_rmt_types.hpp:509
The value of a call, or why there is none.
Definition ws2812_rmt_types.hpp:102
static constexpr Result Success(T v) noexcept
A successful result.
Definition ws2812_rmt_types.hpp:119
static constexpr Result Failure(Error e) noexcept
A failed result.
Definition ws2812_rmt_types.hpp:127
The wire timing of one window set.
Definition ws2812_rmt_types.hpp:308
Range t1h
1 code, high time.
Definition ws2812_rmt_types.hpp:311
uint32_t max_low_ns
The low time that must stay below inside a frame; 0 = reset_min_ns.
Definition ws2812_rmt_types.hpp:316
uint32_t period_max_ns
Longest bit period; 0 none.
Definition ws2812_rmt_types.hpp:314
uint32_t period_min_ns
Shortest bit period (high plus low); 0 none.
Definition ws2812_rmt_types.hpp:313
Range t0l
0 code, low time.
Definition ws2812_rmt_types.hpp:310
uint32_t reset_min_ns
The low time that must be exceeded to latch.
Definition ws2812_rmt_types.hpp:315
Range t1l
1 code, low time.
Definition ws2812_rmt_types.hpp:312
Range t0h
0 code, high time.
Definition ws2812_rmt_types.hpp:309
What the port needs to send the frame, in ticks of resolution_hz.
Definition ws2812_rmt_types.hpp:354
uint32_t resolution_hz
The tick rate the port reported.
Definition ws2812_rmt_types.hpp:355
Compile-time helpers of the WS2812_RMT driver: exact hook matching and enumeration checks.
The gamma 2.2 table of the WS2812_RMT driver, written by tools/gen_gamma.py.
Public types of the WS2812_RMT driver: errors, Result<T>, colours, the timing profile of every suppor...