FastLED 3.9.15
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#endif
9
10namespace fl {
11
13
14#ifdef FASTLED_TESTING
15
16namespace {
17 // Thread-safe storage for injected time provider
18 fl::mutex& get_time_mutex() {
19 static fl::mutex mutex;
20 return mutex;
21 }
22
23 time_provider_t& get_time_provider() {
24 static time_provider_t provider;
25 return provider;
26 }
27}
28
29void inject_time_provider(const time_provider_t& provider) {
30 fl::unique_lock<fl::mutex> lock(get_time_mutex());
31 get_time_provider() = provider;
32}
33
34void clear_time_provider() {
35 fl::unique_lock<fl::mutex> lock(get_time_mutex());
36 get_time_provider() = time_provider_t{}; // Clear the function
37}
38
39// MockTimeProvider implementation
40MockTimeProvider::MockTimeProvider(fl::u32 initial_time)
41 : mCurrentTime(initial_time) {
42}
43
44void MockTimeProvider::advance(fl::u32 milliseconds) {
45 mCurrentTime += milliseconds;
46}
47
48void MockTimeProvider::set_time(fl::u32 milliseconds) {
49 mCurrentTime = milliseconds;
50}
51
52fl::u32 MockTimeProvider::current_time() const {
53 return mCurrentTime;
54}
55
56fl::u32 MockTimeProvider::operator()() const {
57 return mCurrentTime;
58}
59
60#endif // FASTLED_TESTING
61
63
64fl::u32 millis() {
65#ifdef FASTLED_TESTING
66 // Check for injected time provider first
67 {
68 fl::unique_lock<fl::mutex> lock(get_time_mutex());
69 const auto& provider = get_time_provider();
70 if (provider) {
71 return provider();
72 }
73 }
74#endif
75
76 // Use platform-specific implementation
77 return fl::platforms::millis();
78}
79
80fl::u32 micros() {
81 // Note: micros() does not support time injection
82 return fl::platforms::micros();
83}
84
85namespace {
86 // Thread-safe 64-bit millisecond counter state
89 fl::u32 last_millis = 0;
90 bool initialized = false;
92 };
93
95 static Millis64State state;
96 return state;
97 }
98}
99
101 Millis64State& state = get_millis64_state();
102 fl::unique_lock<fl::mutex> lock(state.mutex);
103 state.accumulated = 0;
104 state.last_millis = 0;
105 state.initialized = false;
106}
107
109 Millis64State& state = get_millis64_state();
110 fl::u32 current_millis = fl::millis();
111 fl::unique_lock<fl::mutex> lock(state.mutex);
112
113 if (!state.initialized) {
114 // First call, set initial value
115 state.accumulated = current_millis;
116 state.last_millis = current_millis;
117 state.initialized = true;
118 return state.accumulated;
119 }
120
121 // Detect wraparound: current < last means 32-bit counter wrapped
122 // Delta calculation handles wraparound correctly via unsigned arithmetic
123 fl::u32 delta = current_millis - state.last_millis;
124
125 // Accumulate the delta into 64-bit counter
126 state.accumulated += delta;
127
128 // Update last value for next call
129 state.last_millis = current_millis;
130
131 return state.accumulated;
132}
133
134} // 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.
duration< fl::i64, fl::milli > milliseconds
Milliseconds - duration with period of 1/1,000 seconds.
Definition chrono.h:103
void millis64_reset()
fl::u32 millis()
Universal millisecond timer - returns milliseconds since system startup.
fl::u32 micros()
Universal microsecond timer - returns microseconds since system startup.
fl::u64 millis64()
64-bit millisecond timer - returns milliseconds since system startup without wraparound
fl::u64 u64
Definition s16x16x4.h:221
fl::platforms::mutex mutex
Definition mutex.h:20
Base definition for an LED controller.
Definition crgb.hpp:179