FastLED 3.10.8
Loading...
Searching...
No Matches
chrono.cpp.hpp
Go to the documentation of this file.
1#include "fl/stl/chrono.h"
2#include "fl/log/log.h"
3#include "fl/stl/mutex.h"
4#include "platforms/time_platform.h"
5
6#ifdef FASTLED_TESTING
7 #include "fl/stl/mutex.h"
8#include "fl/stl/noexcept.h"
9#endif
10
11namespace fl {
12
14
15#ifdef FASTLED_TESTING
16
17namespace {
18 struct TimeProviderState {
20 time_provider_t provider;
21 };
22
23 TimeProviderState& get_time_provider_state() FL_NO_EXCEPT {
24 // millis() may be called by background threads and static destructors
25 // after ordinary function-local statics have been destroyed. Retain this
26 // one bounded state object for the process lifetime so both its mutex and
27 // installed provider remain valid throughout teardown.
28 static TimeProviderState* const state = new TimeProviderState(); // ok bare allocation - intentionally retained for safe process teardown
29 return *state;
30 }
31
32 bool get_injected_millis(fl::u32* value) FL_NO_EXCEPT {
33 TimeProviderState& state = get_time_provider_state();
34 fl::unique_lock<fl::mutex> lock(state.mutex);
35 if (!state.provider) {
36 return false;
37 }
38 *value = state.provider();
39 return true;
40 }
41}
42
43void inject_time_provider(const time_provider_t& provider) FL_NO_EXCEPT {
44 TimeProviderState& state = get_time_provider_state();
45 fl::unique_lock<fl::mutex> lock(state.mutex);
46 state.provider = provider;
47}
48
49void clear_time_provider() FL_NO_EXCEPT {
50 TimeProviderState& state = get_time_provider_state();
51 fl::unique_lock<fl::mutex> lock(state.mutex);
52 state.provider = time_provider_t{}; // Clear the function
53}
54
55// MockTimeProvider implementation
56MockTimeProvider::MockTimeProvider(fl::u32 initial_time) FL_NO_EXCEPT
57 : mCurrentTime(initial_time) {
58}
59
60void MockTimeProvider::advance(fl::u32 milliseconds) FL_NO_EXCEPT {
61 mCurrentTime += milliseconds;
62}
63
64void MockTimeProvider::set_time(fl::u32 milliseconds) FL_NO_EXCEPT {
65 mCurrentTime = milliseconds;
66}
67
68fl::u32 MockTimeProvider::current_time() const FL_NO_EXCEPT {
69 return mCurrentTime;
70}
71
72fl::u32 MockTimeProvider::operator()() const FL_NO_EXCEPT {
73 return mCurrentTime;
74}
75
76#endif // FASTLED_TESTING
77
79
81#if FL_CHRONO_HAS_TEST_TIME_PROVIDER
82 fl::u32 injected_millis = 0;
83 if (get_injected_millis(&injected_millis)) {
84 return injected_millis;
85 }
86#endif
87
88 // Use platform-specific implementation
89 return fl::platforms::millis();
90}
91
93#if FL_CHRONO_HAS_TEST_TIME_PROVIDER
94 fl::u32 injected_millis = 0;
95 if (get_injected_millis(&injected_millis)) {
96 return injected_millis * 1000u;
97 }
98#endif
99
100 return fl::platforms::micros();
101}
102
103namespace {
105 fl::u64 accumulated = 0;
106 fl::u32 last_micros = 0;
107 bool initialized = false;
109 };
110
115}
116
117#if FL_CHRONO_HAS_TEST_TIME_PROVIDER
118void micros64_reset() FL_NO_EXCEPT {
119 Micros64State& state = get_micros64_state();
120 fl::unique_lock<fl::mutex> lock(state.mutex);
121 state.accumulated = 0;
122 state.last_micros = 0;
123 state.initialized = false;
124}
125#endif
126
128 Micros64State& state = get_micros64_state();
129 fl::unique_lock<fl::mutex> lock(state.mutex);
130 const fl::u32 current = fl::micros();
131 if (!state.initialized) {
132 state.accumulated = current;
133 state.last_micros = current;
134 state.initialized = true;
135 return state.accumulated;
136 }
137 state.accumulated += static_cast<fl::u32>(current - state.last_micros);
138 state.last_micros = current;
139 return state.accumulated;
140}
141
145
149
150namespace {
151 // Thread-safe 64-bit millisecond counter state
153 fl::u64 accumulated = 0;
154 fl::u32 last_millis = 0;
155 bool initialized = false;
157 };
158
163}
164
166 Millis64State& state = get_millis64_state();
167 fl::unique_lock<fl::mutex> lock(state.mutex);
168 state.accumulated = 0;
169 state.last_millis = 0;
170 state.initialized = false;
171}
172
174 Millis64State& state = get_millis64_state();
175 fl::u32 current_millis = fl::millis();
176 fl::unique_lock<fl::mutex> lock(state.mutex);
177
178 if (!state.initialized) {
179 // First call, set initial value
180 state.accumulated = current_millis;
181 state.last_millis = current_millis;
182 state.initialized = true;
183 return state.accumulated;
184 }
185
186 // Detect wraparound: current < last means 32-bit counter wrapped
187 // Delta calculation handles wraparound correctly via unsigned arithmetic
188 fl::u32 delta = current_millis - state.last_millis;
189
190 // Accumulate the delta into 64-bit counter
191 state.accumulated += delta;
192
193 // Update last value for next call
194 state.last_millis = current_millis;
195
196 return state.accumulated;
197}
198
199} // namespace fl
TestState state
FastLED chrono implementation - duration types for time measurements.
Centralized logging categories for FastLED hardware interfaces and subsystems.
Platform-independent mutex interface.
Millis64State & get_millis64_state() FL_NO_EXCEPT
Micros64State & get_micros64_state() FL_NO_EXCEPT
duration< fl::i64, fl::milli > milliseconds
Milliseconds - duration with period of 1/1,000 seconds.
Definition chrono.h:106
void millis64_reset() FL_NO_EXCEPT
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
constexpr int type_rank< T >::value
fl::u32 millis() FL_NO_EXCEPT
Universal millisecond timer - returns milliseconds since system startup.
fl::u32 micros() FL_NO_EXCEPT
Universal microsecond timer - returns microseconds since system startup.
fl::u64 millis64() FL_NO_EXCEPT
64-bit millisecond timer - returns milliseconds since system startup without wraparound
fl::u64 micros64() FL_NO_EXCEPT
64-bit monotonic microsecond timer.
fl::platforms::mutex mutex
Definition mutex.h:20
Base definition for an LED controller.
Definition crgb.hpp:179
#define FL_NO_EXCEPT
static time_point now() FL_NO_EXCEPT
Returns the current time point.
microseconds duration
Definition chrono.h:190
fl::chrono::time_point< steady_clock > time_point
Definition chrono.h:193
microseconds duration
Definition chrono.h:206
static time_point now() FL_NO_EXCEPT
Returns the current time point.
fl::chrono::time_point< system_clock > time_point
Definition chrono.h:209