7#ifndef SBL_CONTROL_SCHEDULER_HPP_
8#define SBL_CONTROL_SCHEDULER_HPP_
25using SubscriberFn = void (*)(
void* context, uint32_t elapsed_ticks);
94template<u
int8_t Slots, u
int8_t Capacity = 16>
96 static_assert(Slots > 0,
"Scheduler needs at least one slot");
97 static_assert(Capacity > 0,
"Scheduler needs subscriber capacity");
105 for (uint8_t s = 0; s < Slots; ++s) {
106 divider_[s] = dividers[s] ? dividers[s] : 1;
119 if (count_ >= Capacity || slot >= Slots || fn ==
nullptr) {
122 subs_[count_++] = {fn, context, stage, slot};
135 void set_timing(uint32_t base_period_ticks, uint32_t late_ticks) {
137 (base_period_ticks > 0 && late_ticks >= base_period_ticks) ? base_period_ticks - 1 : late_ticks;
138 for (uint8_t s = 0; s < Slots; ++s) {
139 period_[s] = base_period_ticks * divider_[s];
140 scheduled_[s] =
false;
153 template<
typename T,
void (T::*Method)(u
int32_t)>
156 stage, slot, [](
void* c, uint32_t elapsed) { (
static_cast<T*
>(c)->*Method)(elapsed); }, &object);
168 for (uint8_t s = 0; s < Slots; ++s) {
174 bool due[Slots] = {};
175 uint32_t elapsed[Slots] = {};
176 for (uint8_t s = 0; s < Slots; ++s) {
177 due[s] = (phase_[s] == 0);
180 elapsed[s] = now - last_[s];
182 if (period_[s] != 0) {
186 if (++phase_[s] >= divider_[s]) {
193 for (uint8_t i = 0; i < count_; ++i) {
194 const Subscriber& sub = subs_[i];
195 if (sub.stage == stage && due[sub.slot]) {
196 sub.fn(sub.context, elapsed[sub.slot]);
207 uint32_t
late_runs(uint8_t slot)
const {
return slot < Slots ? late_[slot] : 0; }
214 uint32_t
skipped_periods(uint8_t slot)
const {
return slot < Slots ? skipped_[slot] : 0; }
221 uint32_t
worst_delay_ticks(uint8_t slot)
const {
return slot < Slots ? worst_delay_[slot] : 0; }
225 for (uint8_t s = 0; s < Slots; ++s) {
235 uint16_t
divider(uint8_t slot)
const {
return slot < Slots ? divider_[slot] : 0; }
237 static constexpr uint8_t
slots() {
return Slots; }
239 static constexpr uint8_t
capacity() {
return Capacity; }
244 void* context =
nullptr;
245 Stage stage = Stage::Update;
250 void time_run(uint8_t s, uint32_t now) {
251 if (!scheduled_[s]) {
252 scheduled_[s] =
true;
253 due_[s] = now + period_[s];
256 const int32_t delay =
static_cast<int32_t
>(now - due_[s]);
259 due_[s] = now + period_[s];
263 const uint32_t behind =
static_cast<uint32_t
>(delay);
264 const uint32_t whole = behind >= period_[s] ? behind / period_[s] : 0;
265 due_[s] += (whole + 1) * period_[s];
266 if (behind > late_tolerance_) {
268 skipped_[s] += whole;
270 if (behind > worst_delay_[s]) {
271 worst_delay_[s] = behind;
275 Subscriber subs_[Capacity]{};
276 uint16_t divider_[Slots]{};
277 uint16_t phase_[Slots]{};
278 uint32_t last_[Slots]{};
279 uint32_t period_[Slots]{};
280 uint32_t due_[Slots]{};
281 uint32_t late_[Slots]{};
282 uint32_t skipped_[Slots]{};
283 uint32_t worst_delay_[Slots]{};
284 uint32_t late_tolerance_ = 0;
285 bool scheduled_[Slots]{};
287 bool started_ =
false;
Divider slots over one base period, run in a fixed stage order.
static constexpr uint8_t capacity()
Maximum subscribers.
static constexpr uint8_t slots()
Slots.
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()
bool subscribe(Stage stage, uint8_t slot, T &object)
Subscribe a member void T::method(uint32_t elapsed_ticks)
bool subscribe(Stage stage, uint8_t slot, SubscriberFn fn, void *context)
Add a subscriber.
Scheduler(const uint16_t(÷rs)[Slots])
Construct with one divider per slot.
uint16_t divider(uint8_t slot) const
The slot's divider, or 0 for a slot out of range.
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.
void set_timing(uint32_t base_period_ticks, uint32_t late_ticks)
Give the base period, so late and skipped runs are counted.
void run(uint32_t now)
One run, once per base period.
uint32_t skipped_periods(uint8_t slot) const
Periods of the slot that passed with no run; wraps at 2^32.
uint8_t subscriber_count() const
Subscribers added.
The control stack: scheduler, event queue, merge, CC mapping, calibration.
void(*)(void *context, uint32_t elapsed_ticks) SubscriberFn
constexpr uint8_t STAGE_COUNT