|
Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
|
Divider slots over one base period, run in a fixed stage order. More...
Divider slots over one base period, run in a fixed stage order.
The app drives run() once per base period - the 1 kHz timer ISR on hardware, the timer thread on the workbench - and passes the control clock's tick (control/clock.hpp). Each slot runs every Nth base period. Subscribers are told how many ticks elapsed since their slot last ran, read from the clock, so an irregular base period never skews time-constant math: components are told elapsed time and never assume a call rate.
Every run goes through the stages in order, so the update order is enforced by construction:
Within a stage, due subscribers run in subscription order. Apps subscribe; components never self-register. Static storage, no allocation.
Timing. Told its base period in clock ticks (set_timing()), the scheduler counts per slot the runs that came late and the periods that passed with no run, and keeps the worst delay. Each slot is measured against a fixed grid on the clock, one due instant every period (its divider times the base period). The slot's first run after set_timing() anchors the grid. A run's delay is its time past the due instant, and the due instant then advances by whole periods, so a run that is on time does not move the grid. A run later than the tolerance is late, and each whole period in its delay is a skipped period. A run that comes early moves the grid earlier, to that run, and is not late: this is what corrects a grid that was anchored on a delayed run.
The grid is fixed because, on a target whose tick is a hardware timer on the same oscillator as the clock, the two do not drift, so the grid gives a run's true delay behind its timer instant. Measured against the previous run instead, a delay that creeps up within the tolerance at each step would be under-read. A tick source that runs steadily slower than the clock is therefore counted: its delay grows until its runs are late, and a period is counted as skipped each time the delay reaches one.
The counting never changes which slots run or what they are told.
Usage:
| Slots | Number of divider slots, at least 1 |
| Capacity | Maximum subscribers across all slots, at least 1 |
Definition at line 95 of file scheduler.hpp.
#include <scheduler.hpp>
Public Member Functions | |
| Scheduler (const uint16_t(÷rs)[Slots]) | |
| Construct with one divider per slot. | |
| bool | subscribe (Stage stage, uint8_t slot, SubscriberFn fn, void *context) |
| Add a subscriber. | |
| void | set_timing (uint32_t base_period_ticks, uint32_t late_ticks) |
| Give the base period, so late and skipped runs are counted. | |
| template<typename T , void(T::*)(uint32_t) Method> | |
| bool | subscribe (Stage stage, uint8_t slot, T &object) |
Subscribe a member void T::method(uint32_t elapsed_ticks) | |
| void | run (uint32_t now) |
| One run, once per base period. | |
| uint32_t | late_runs (uint8_t slot) const |
| Runs of the slot that came later than the tolerance past their due instant; wraps at 2^32. | |
| uint32_t | skipped_periods (uint8_t slot) const |
| Periods of the slot that passed with no run; wraps at 2^32. | |
| uint32_t | worst_delay_ticks (uint8_t slot) const |
| The longest a run of the slot came past its due instant, since the start or reset_timing() | |
| void | reset_timing () |
| uint8_t | subscriber_count () const |
| Subscribers added. | |
| uint16_t | divider (uint8_t slot) const |
| The slot's divider, or 0 for a slot out of range. | |
Static Public Member Functions | |
| static constexpr uint8_t | slots () |
| Slots. | |
| static constexpr uint8_t | capacity () |
| Maximum subscribers. | |
|
inlineexplicit |
Construct with one divider per slot.
| dividers | Base periods between a slot's runs, per slot (0 is treated as 1) |
Definition at line 104 of file scheduler.hpp.
|
inline |
Add a subscriber.
| stage | Stage it runs in |
| slot | Slot index, 0 to Slots - 1 |
| fn | Callback |
| context | Passed to fn unchanged |
Definition at line 118 of file scheduler.hpp.
Referenced by sbl::control::Scheduler< Slots, Capacity >::subscribe().
|
inline |
Give the base period, so late and skipped runs are counted.
| base_period_ticks | Clock ticks in one base period; 0 turns the counting off |
| late_ticks | A run is late when it comes more than this past its due instant; held below the base period |
Without this call nothing is counted. At init; after a later call each slot's next run anchors its grid again.
Definition at line 135 of file scheduler.hpp.
|
inline |
Subscribe a member void T::method(uint32_t elapsed_ticks)
| T | Object type |
| Method | Member function called with the elapsed ticks |
| stage | Stage it runs in |
| slot | Slot index, 0 to Slots - 1 |
| object | Object the method is called on; it must outlive the scheduler |
Definition at line 154 of file scheduler.hpp.
References sbl::control::Scheduler< Slots, Capacity >::subscribe().
|
inline |
One run, once per base period.
The first call establishes the baseline: every slot runs, told 0.
| now | The control clock's tick (wrapping; unsigned deltas) |
Definition at line 166 of file scheduler.hpp.
References sbl::control::STAGE_COUNT.
|
inline |
Runs of the slot that came later than the tolerance past their due instant; wraps at 2^32.
| slot | Slot index |
Definition at line 207 of file scheduler.hpp.
|
inline |
Periods of the slot that passed with no run; wraps at 2^32.
| slot | Slot index |
Definition at line 214 of file scheduler.hpp.
|
inline |
The longest a run of the slot came past its due instant, since the start or reset_timing()
| slot | Slot index |
Definition at line 221 of file scheduler.hpp.
|
inline |
Zero the counts and the worst delays. From run()'s context, or while it does not run.
Definition at line 224 of file scheduler.hpp.
|
inline |
Subscribers added.
Definition at line 233 of file scheduler.hpp.
|
inline |
The slot's divider, or 0 for a slot out of range.
Definition at line 235 of file scheduler.hpp.
|
inlinestaticconstexpr |
Slots.
Definition at line 237 of file scheduler.hpp.
|
inlinestaticconstexpr |
Maximum subscribers.
Definition at line 239 of file scheduler.hpp.