FastLED 3.9.15
Loading...
Searching...
No Matches

◆ validateEdgeTiming()

bool validateEdgeTiming ( fl::span< const fl::EdgeTime > edges,
size_t edge_count,
fl::span< const PinToggle > expected_pattern,
uint32_t tolerance_percent )

Validate captured edge timings against expected pattern.

Prints edge timing data, validates that edges alternate HIGH/LOW correctly, and checks that timing values match the expected pattern within tolerance.

Parameters
edgesCaptured edge timing data
edge_countNumber of edges captured
expected_patternExpected pin toggle pattern to validate against
tolerance_percentTolerance for timing validation (e.g., 15 for ±15%)
Returns
true if validation passes (edges alternate correctly and timing matches), false otherwise

Definition at line 60 of file test.cpp.

63 {
64
65 FL_WARN("[TEST] Captured " << edge_count << " edges");
66
67 if (edge_count == 0) {
68 FL_ERROR("No edges captured!");
69 return false;
70 }
71
72 // Print edge timings
73 FL_WARN("[TEST] Edge timings:");
74 for (size_t i = 0; i < edge_count; i++) {
75 FL_WARN(" [" << i << "] " << (edges[i].high ? "HIGH" : "LOW ")
76 << " " << edges[i].ns << "ns (" << (edges[i].ns / 1000) << "us)");
77 }
78
79 // Validate edge alternation
80 bool alternation_valid = true;
81 for (size_t i = 1; i < edge_count; i++) {
82 if (edges[i].high == edges[i-1].high) {
83 FL_ERROR("Sequential " << (edges[i].high ? "HIGH" : "LOW")
84 << " values at indices " << (i-1) << " and " << i
85 << " - edges should alternate HIGH/LOW");
86 alternation_valid = false;
87 }
88 }
89
90 // Validate timing accuracy against expected pattern
91 bool timing_valid = true;
92 FL_WARN("[TEST] Validating timing accuracy (±" << tolerance_percent << "% tolerance):");
93
94 // Expected edge count is expected_pattern.size() - 1 because the last edge
95 // ends with timeout (no subsequent transition to measure duration)
96 size_t expected_edge_count = expected_pattern.size() - 1;
97 if (edge_count != expected_edge_count) {
98 FL_WARN("WARNING: Edge count mismatch - expected " << expected_edge_count
99 << ", got " << edge_count);
100 timing_valid = false;
101 }
102
103 size_t compare_count = edge_count < expected_edge_count ? edge_count : expected_edge_count;
104 for (size_t i = 0; i < compare_count; i++) {
105 uint32_t expected_us = expected_pattern[i].delay_us;
106 uint32_t actual_us = edges[i].ns / 1000;
107 bool expected_high = expected_pattern[i].is_high;
108 bool actual_high = edges[i].high;
109
110 // Calculate tolerance range
111 uint32_t tolerance_us = (expected_us * tolerance_percent) / 100;
112 uint32_t min_us = expected_us - tolerance_us;
113 uint32_t max_us = expected_us + tolerance_us;
114
115 // Validate timing and level
116 bool timing_ok = (actual_us >= min_us) && (actual_us <= max_us);
117 bool level_ok = (expected_high == actual_high);
118
119 if (timing_ok && level_ok) {
120 FL_WARN(" [" << i << "] ✓ " << (actual_high ? "HIGH" : "LOW ")
121 << " " << actual_us << "us (expected " << expected_us
122 << "us ±" << tolerance_us << "us)");
123 } else {
124 if (!level_ok) {
125 FL_WARN(" [" << i << "] ✗ Level mismatch: expected "
126 << (expected_high ? "HIGH" : "LOW") << ", got "
127 << (actual_high ? "HIGH" : "LOW"));
128 timing_valid = false;
129 }
130 if (!timing_ok) {
131 FL_WARN(" [" << i << "] ✗ Timing out of range: " << actual_us
132 << "us (expected " << expected_us << "us ±" << tolerance_us
133 << "us, range: " << min_us << "-" << max_us << "us)");
134 timing_valid = false;
135 }
136 }
137 }
138
139 // Validate results
140 if (!alternation_valid) {
141 FL_ERROR("Edge timings are not properly alternating");
142 FL_ERROR("Expected pattern: HIGH, LOW, HIGH, LOW, ...");
143 FL_ERROR("Actual pattern contains sequential identical states");
144 return false;
145 } else if (!timing_valid) {
146 FL_ERROR("Captured edge timings do not match expected pattern");
147 FL_ERROR("Check timing accuracy and tolerance settings");
148 return false;
149 } else if (edge_count >= 5) {
150 FL_WARN("[TEST] ✓ PASS: Captured " << edge_count << " edges with proper alternation");
151 FL_WARN("[TEST] ✓ PASS: All timing values match expected pattern within tolerance");
152 FL_WARN("[TEST] ✓ RX channel working correctly!");
153 return true;
154 } else {
155 FL_WARN("WARNING: Only captured " << edge_count << " edges (expected >=5)");
156 return false;
157 }
158}
constexpr fl::size size() const FL_NOEXCEPT
Definition span.h:458
#define FL_WARN(X)
Definition log.h:276
#define FL_ERROR(X)
Definition log.h:219
fl::u32 uint32_t
Definition s16x16x4.h:219

References FL_ERROR, FL_WARN, and fl::span< T, Extent >::size().

Referenced by loop().

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