FastLED 3.9.15
Loading...
Searching...
No Matches
delay.h
Go to the documentation of this file.
1#pragma once
2
3#ifndef __INC_FL_DELAY_H
4#define __INC_FL_DELAY_H
5
9
10#include "platforms/cycle_type.h"
12#include "fl/stl/int.h"
13#include "fl/stl/type_traits.h"
14// ============================================================================
15// Platform-specific includes via trampoline header
16// ============================================================================
17
18#include "platforms/delay.h" // IWYU pragma: keep
19#include "fl/stl/noexcept.h"
20
21namespace fl {
22
23namespace detail {
24
29constexpr fl::u32 cycles_from_ns(fl::u32 ns, fl::u32 hz) FL_NOEXCEPT {
30 // Round up: cycles = ceil(ns * hz / 1e9)
31 // Using: (ns * hz + 999'999'999) / 1'000'000'000
32 return ((fl::u64)ns * (fl::u64)hz + 999999999UL) / 1000000000UL;
33}
34
38constexpr fl::u32 cycles_from_ns_default(fl::u32 ns) FL_NOEXCEPT {
39 return cycles_from_ns(ns, GET_CPU_FREQUENCY());
40}
41
42} // namespace detail
43
46template<fl::u32 NS>
48 // Delegate to platform-specific implementation with compile-time constant
49 // Platform-specific delayNanoseconds_impl() functions are provided by platforms/delay.h
50 delayNanoseconds_impl(NS);
51}
52
55void delayNanoseconds(fl::u32 ns) FL_NOEXCEPT;
56
60void delayNanoseconds(fl::u32 ns, fl::u32 hz) FL_NOEXCEPT;
61
62// ============================================================================
63// Clock cycle-counted delay loop (delaycycles)
64// ============================================================================
65
71template<cycle_t CYCLES> void delaycycles() FL_NOEXCEPT;
72
75template<cycle_t CYCLES> inline void delaycycles_min1() FL_NOEXCEPT {
77 delaycycles<CYCLES - 1>();
78}
79
80// ============================================================================
81// Millisecond and Microsecond delay wrappers
82// ============================================================================
83
84namespace detail {
85
89void delay_impl(u32 ms, bool run_async = true) FL_NOEXCEPT;
90
91} // namespace detail
92
97template<int Dummy = 0>
98inline void delay(u32 ms, bool run_async = true) FL_NOEXCEPT {
99 (void)Dummy;
100 detail::delay_impl(ms, run_async);
101}
102
105void delayMillis(u32 ms) FL_NOEXCEPT;
106
109void delayMicroseconds(u32 us) FL_NOEXCEPT;
110
113inline void delayUs(u32 us) FL_NOEXCEPT {
115}
116
120inline void delayMs(u32 ms, bool run_async = true) FL_NOEXCEPT {
121 detail::delay_impl(ms, run_async);
122}
123
126template<u32 NS> inline void delayNs() FL_NOEXCEPT {
128}
129
132inline void delayNs(u32 ns) FL_NOEXCEPT {
134}
135
139inline void delayNs(u32 ns, u32 hz) FL_NOEXCEPT {
140 delayNanoseconds(ns, hz);
141}
142
143} // namespace fl
144
145#endif // __INC_FL_DELAY_H
#define NS(_NS)
Convert from nanoseconds to number of clock cycles.
constexpr fl::u32 cycles_from_ns(fl::u32 ns, fl::u32 hz) FL_NOEXCEPT
Convert nanoseconds to CPU cycles.
Definition delay.h:29
constexpr fl::u32 cycles_from_ns_default(fl::u32 ns) FL_NOEXCEPT
Compute cycles using default CPU frequency (compile-time)
Definition delay.h:38
void delay_impl(u32 ms, bool run_async)
Internal delay implementation used by the public fl::delay wrapper.
void delayMs(u32 ms, bool run_async=true) FL_NOEXCEPT
Shorter alias for delay with optional async task pumping.
Definition delay.h:120
void delayUs(u32 us) FL_NOEXCEPT
Shorter alias for delayMicroseconds.
Definition delay.h:113
FASTLED_FORCE_INLINE void delayNanoseconds() FL_NOEXCEPT
Delay for a compile-time constant number of nanoseconds.
Definition delay.h:47
void delaycycles_min1() FL_NOEXCEPT
A variant of delaycycles that will always delay at least one cycle.
Definition delay.h:75
void delay(u32 ms, bool run_async=true) FL_NOEXCEPT
Public delay wrapper that keeps bare Arduino delay() preferred after using fl::delay; while still all...
Definition delay.h:98
void delaycycles< 1 >() FL_NOEXCEPT
Definition delay.cpp.hpp:75
void delayMillis(u32 ms) FL_NOEXCEPT
Delay for a given number of milliseconds (legacy - no async pumping)
void delaycycles() FL_NOEXCEPT
Forward declaration of delaycycles template.
fl::u64 u64
Definition s16x16x4.h:221
void delayMicroseconds(u32 us)
Delay for a given number of microseconds.
void delayNs() FL_NOEXCEPT
Shorter alias for delayNanoseconds (template version)
Definition delay.h:126
Base definition for an LED controller.
Definition crgb.hpp:179
#define FASTLED_FORCE_INLINE
#define FL_NOEXCEPT