Tool Libs
UMIT-TIROL Institute of Automation and Control Engineering library collection
 
Loading...
Searching...
No Matches
timed.h
Go to the documentation of this file.
1
5#pragma once
6#include "schedule.h"
7namespace Schedule {
26namespace Recurring {
31 uint32_t next{}, time{}, dt{};
32 Schedulable(uint32_t dt) : dt{dt} { }
34 bool schedule(uint32_t now) override {
35 if (now < next) return false;
36 next += dt;
37 time = now;
38 return true;
39 }
40 void reset() { next = 0; }
41 };
43 struct Func: public Schedulable {
45 using Call = void (*)(uint32_t, uint32_t);
46 Call func;
47 Func(Call f, uint32_t dt): Schedulable(dt), func(f) { }
48 void call() override { return func(time, dt); }
49 };
50
52 template<typename T>
53 struct Method : public Schedulable {
55 using Call = void (T::*)(uint32_t, uint32_t);
56 T* base;
57 Call method;
58 Method(T* b, Call m, uint32_t dt)
59 : Schedulable(dt), base(b), method(m) { }
60 void call() override {
61 return (base->*method)(time, dt);
62 }
63 };
66
68 void every(uint32_t dt_ms, typename Func::Call func) {
69 if (dt_ms) list.append(new Func{func, dt_ms});
70 }
72 template<typename T>
73 void every(uint32_t dt_ms, T& base,
74 typename Method<T>::Call method) {
75 if (dt_ms) list.append(new Method<T>{&base, method, dt_ms});
76 }
83 void reset() {
84 for (auto &c: list) {
85 reinterpret_cast<Schedulable *>(c)->reset();
86 }
87 }
88};
89}}
namespace wrapping all functionality to do with scheduling
Definition evented.h:7
Copyright (c) 2023 IACE.
recurringly callable function wrapper
Definition timed.h:43
void call() override
this method is called to run the Schedulable
Definition timed.h:48
void(*)(uint32_t, uint32_t) Call
signature of recurringly callable function
Definition timed.h:45
recurringly callable method wrapper
Definition timed.h:53
void(T::*)(uint32_t, uint32_t) Call
signature of recurringly callable method
Definition timed.h:55
void call() override
this method is called to run the Schedulable
Definition timed.h:60
registry of recurring calls
Definition timed.h:65
void reset()
reset calling times of all registered functions
Definition timed.h:83
void every(uint32_t dt_ms, typename Func::Call func)
register a function to be called every dt_ms
Definition timed.h:68
void every(uint32_t dt_ms, T &base, typename Method< T >::Call method)
register a method to be called every dt_ms
Definition timed.h:73
recurring Schedulable keeps track of time in order to make recurring scheduling work
Definition timed.h:30
bool schedule(uint32_t now) override
only schedule if dt ms passed since last run
Definition timed.h:34
a registry of Schedulable which can be given to the Scheduler for actual scheduling of calls
Definition schedule.h:36
represents an object that can be scheduled for later calling
Definition schedule.h:20