rcbench
Servo and ESC test bench: panel, IOmcu and shared C
Loading...
Searching...
No Matches
tone.h
Go to the documentation of this file.
1
112
113#pragma once
114
115#include <stdbool.h>
116#include <stddef.h>
117#include <stdint.h>
118
119#ifdef __cplusplus
120extern "C" {
121#endif
122
124#define TONE_BLOCK 4u
129#define TONE_OUTLIER_PCT 45u
131#define TONE_BEEP_QUEUE 16u
132
138#define TONE_WINDOW_US_MAX 100000u
139#define TONE_GAP_US_MAX 1000000u
140#define TONE_GLITCH_NS_MAX 10000u
141#define TONE_HOLD_NS_MAX 100000u
142
144typedef struct {
145 uint32_t tick_hz;
146 uint32_t window_us;
147 uint32_t f_min_hz;
148 uint32_t f_max_hz;
157 uint32_t glitch_ns;
164 uint32_t hold_ns;
170 uint32_t gap_us;
175 uint32_t split_pct;
176 uint32_t min_periods;
179} tone_cfg_t;
180
182typedef struct {
183 uint64_t t;
184 bool level;
186
188typedef struct {
189 uint64_t index;
190 bool present;
191 float freq_hz;
192 uint32_t periods;
193 uint32_t bursts;
195
202
203typedef struct {
204 uint64_t start;
205 uint64_t end;
206 float freq_hz;
212 uint32_t bursts;
213 uint32_t periods;
214 uint8_t flags;
216
218typedef struct {
219 uint32_t edges;
220 uint32_t glitches;
221 uint32_t out_of_order;
222 uint32_t beeps;
227 uint32_t rejected;
228 uint32_t lost;
230
232typedef struct {
233 uint64_t p;
234 uint64_t s_open;
235 uint64_t e_before;
236 bool good;
238
240typedef struct {
241 tone_cfg_t c;
242 uint64_t win_ticks, per_min, per_max, car_max, glitch, gap;
243 uint64_t start_low;
249 uint64_t spacing;
250
251 bool seen;
252 uint64_t now;
254 uint64_t last_edge;
255 bool high;
256 uint64_t last_rise,
258
259 /* The run: bursts with no silence of gap between them. */
260 bool in_run;
261 uint64_t car_low;
262 uint64_t car_high;
263 uint64_t car_rise;
266 uint64_t burst_start, burst_e_before;
267
268 /* The beep under way. */
269 uint64_t b_start;
270 uint64_t b_sum;
271 uint32_t b_n, b_bursts;
272 uint8_t b_flags;
274 uint32_t n_pend;
275
276 /* The window under way, and the last one finished. */
277 uint64_t w_index, w_end, w_sum;
278 uint32_t w_n, w_bursts;
279 bool have_window;
280 tone_window_t window;
281
283 uint32_t q_head, q_len;
284 tone_stats_t st;
285} tone_t;
286
295void tone_cfg_defaults(tone_cfg_t *c, uint32_t tick_hz);
296
319bool tone_init(tone_t *d, const tone_cfg_t *c);
320
328void tone_edge(tone_t *d, uint64_t t, bool level);
329
337void tone_feed(tone_t *d, const tone_edge_t *e, size_t n);
338
345void tone_advance(tone_t *d, uint64_t now);
346
354void tone_flush(tone_t *d);
355
363bool tone_window(const tone_t *d, tone_window_t *w);
364
373
380bool tone_busy(const tone_t *d);
381
388const tone_stats_t *tone_stats(const tone_t *d);
389
400uint64_t tone_ns_ticks(uint64_t tick_hz, uint64_t ns);
401
409uint64_t tone_ticks_us(const tone_t *d, uint64_t ticks);
410
411/* ------------------------------------------------------------ hold-off */
412
437typedef struct {
438 uint64_t hold;
439 bool line;
440 bool pending;
441 uint64_t fall;
442 uint64_t out_t;
444
453void tone_holdoff_init(tone_holdoff_t *h, uint32_t tick_hz, uint32_t hold_ns);
454
465size_t tone_holdoff_edge(tone_holdoff_t *h, uint64_t t, bool level,
466 tone_edge_t out[2]);
467
477size_t tone_holdoff_advance(tone_holdoff_t *h, uint64_t now,
478 tone_edge_t out[1]);
479
490uint64_t tone_holdoff_horizon(const tone_holdoff_t *h, uint64_t now);
491
492#ifdef __cplusplus
493}
494#endif
One beep as the page reported it.
Definition tone_link.h:193
uint8_t flags
Definition tone_link.h:200
float carrier_hz
The carrier its run was chopped at; 0 when no rise came at the carrier rate.
Definition tone.h:211
uint32_t periods
Definition tone.h:213
uint16_t bursts
Definition tone_link.h:198
uint64_t start
Definition tone.h:204
float freq_hz
Definition tone.h:206
uint64_t end
Definition tone.h:205
Detector settings, as given to tone_init().
Definition tone.h:144
uint32_t min_periods
Definition tone.h:176
uint32_t window_min_periods
Definition tone.h:177
uint32_t f_min_hz
Definition tone.h:147
uint32_t glitch_ns
A low shorter than this, ns, is no low; 1 to 10000.
Definition tone.h:157
uint32_t split_pct
TONE_BLOCK periods this far from the beep's mean, in percent, start a new beep; 0 splits only on sile...
Definition tone.h:175
uint32_t window_us
Definition tone.h:146
uint32_t gap_us
No edge for this long ends a beep, us, at most TONE_GAP_US_MAX; at least the period of f_min_hz,...
Definition tone.h:170
uint32_t hold_ns
The capture's hold-off (tone_holdoff_t), ns, 0 to TONE_HOLD_NS_MAX; 0 none.
Definition tone.h:164
uint32_t f_max_hz
Definition tone.h:148
uint32_t carrier_min_hz
Rise intervals up to this carrier's period are chopping inside a burst.
Definition tone.h:155
uint32_t tick_hz
Definition tone.h:145
One edge: the tick it happened at and the level after it.
Definition tone.h:182
The capture's hold-off: the rule the PIO program applies before an edge reaches the detector,...
Definition tone.h:437
uint64_t fall
Definition tone.h:441
bool line
Definition tone.h:439
uint64_t hold
Definition tone.h:438
bool pending
Definition tone.h:440
uint64_t out_t
Definition tone.h:442
A period waiting for the pitch check.
Definition tone.h:232
uint64_t s_open
Definition tone.h:234
bool good
Definition tone.h:236
uint64_t p
Definition tone.h:233
uint64_t e_before
Definition tone.h:235
Counters of the detector, read with tone_stats().
Definition tone.h:218
uint32_t edges
Definition tone.h:219
uint32_t lost
Definition tone.h:228
uint32_t beeps
Definition tone.h:222
uint32_t glitches
Definition tone.h:220
uint32_t rejected
Runs that made no beep: too few periods, or a mean outside the tone range.
Definition tone.h:227
uint32_t out_of_order
Definition tone.h:221
Detector state. The fields belong to tone.c.
Definition tone.h:240
bool have_edge
Definition tone.h:253
bool seen
Definition tone.h:251
uint64_t last_fall
Definition tone.h:257
uint64_t car_rise
Definition tone.h:263
bool rise_started
Definition tone.h:265
uint64_t start_low
Definition tone.h:243
uint64_t spacing
The shortest time between two burst starts that makes a counted period, ticks: the most of half the s...
Definition tone.h:249
uint64_t car_low
Definition tone.h:261
uint64_t car_high
Definition tone.h:262
uint64_t now
Definition tone.h:252
uint64_t last_edge
Definition tone.h:254
uint64_t last_rise
Definition tone.h:256
bool high
Definition tone.h:255
One window of window_us.
Definition tone.h:188
float freq_hz
Definition tone.h:191
uint32_t periods
Definition tone.h:192
bool present
Definition tone.h:190
uint64_t index
Definition tone.h:189
uint32_t bursts
Definition tone.h:193
uint64_t tone_ns_ticks(uint64_t tick_hz, uint64_t ns)
ns as ticks of a tick_hz clock, rounded up: the conversion of every time setting.
Definition tone.c:49
uint64_t tone_ticks_us(const tone_t *d, uint64_t ticks)
ticks of the detector's clock in microseconds, rounded down.
Definition tone.c:701
#define TONE_BEEP_QUEUE
Beeps held until the caller takes them.
Definition tone.h:131
void tone_edge(tone_t *d, uint64_t t, bool level)
One edge at tick t, level after it.
Definition tone.c:619
size_t tone_holdoff_edge(tone_holdoff_t *h, uint64_t t, bool level, tone_edge_t out[2])
One edge at t; the edges reported, 0 to 2, into out in time order: a held fall whose low lasted the h...
Definition tone.c:721
size_t tone_holdoff_advance(tone_holdoff_t *h, uint64_t now, tone_edge_t out[1])
No edge up to now: a held fall whose low has lasted the hold-off by then, into out; 0 or 1.
Definition tone.c:757
void tone_flush(tone_t *d)
End the beep under way at its last edge, as a silence would, and forget the line's level,...
Definition tone.c:656
#define TONE_BLOCK
Periods compared at once for a pitch change.
Definition tone.h:124
void tone_advance(tone_t *d, uint64_t now)
No edge up to tick now: a silence ends a beep, a window closes.
Definition tone.c:648
tone_beep_flag_t
Definition tone.h:196
@ TONE_BEEP_BEFORE_CHANGE
Ended at a pitch change, with no silence after it.
Definition tone.h:200
@ TONE_BEEP_AFTER_CHANGE
Began at a pitch change, with no silence before it.
Definition tone.h:198
void tone_feed(tone_t *d, const tone_edge_t *e, size_t n)
n edges in time order.
Definition tone.c:638
bool tone_next_beep(tone_t *d, tone_beep_t *b)
Take the oldest beep reported; false with none waiting.
Definition tone.c:680
bool tone_busy(const tone_t *d)
A run of bursts is under way.
Definition tone.c:691
const tone_stats_t * tone_stats(const tone_t *d)
The counters of the detector.
Definition tone.c:696
void tone_cfg_defaults(tone_cfg_t *c, uint32_t tick_hz)
Defaults for a clock of tick_hz: 8000us windows, 400 to 6500Hz, carrier from 7kHz,...
Definition tone.c:16
uint64_t tone_holdoff_horizon(const tone_holdoff_t *h, uint64_t now)
now less the hold-off, at least 0, and never before the latest edge handed out: every edge before it ...
Definition tone.c:771
bool tone_window(const tone_t *d, tone_window_t *w)
The last window finished; false before the first.
Definition tone.c:671
void tone_holdoff_init(tone_holdoff_t *h, uint32_t tick_hz, uint32_t hold_ns)
hold_ns of a clock of tick_hz, rounded up to whole ticks; the line starts low.
Definition tone.c:712
bool tone_init(tone_t *d, const tone_cfg_t *c)
Start from nothing.
Definition tone.c:71