-
Notifications
You must be signed in to change notification settings - Fork 3
/
scheduler.hpp
67 lines (49 loc) · 1.64 KB
/
scheduler.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* scheduler.hpp
*
* Created on: 11 feb. 2023
* Author: minoseigenheer
*/
#ifndef SCHEDULER_HPP_
#define SCHEDULER_HPP_
#include <stdint.h>
#include <stdio.h>
#include <stdbool.h>
#include "main.h"
using tSchedulerTime = uint32_t;
constexpr tSchedulerTime SchedulerDisabled = -1; // 0xffffffffUL or 0xffffffffffffffffULL
//------------------------------------------------------------------------------
// SyncScheduler is meant to schedule periodic function calls
// for example sending periodic CAN messages
class tSyncScheduler {
protected:
tSchedulerTime NextTime = 0;
uint32_t Offset; // Offset to avoid all calls with similar period trigger in the same cycle.
uint32_t Period;
public:
tSyncScheduler(bool Enable=false, uint32_t _Period=0, uint32_t _Offset=0);
//void init(bool Enable=false, uint32_t _Period=0, uint32_t _Offset=0);
inline void Disable() { if(IsEnabled()) NextTime=SchedulerDisabled; }
inline void Enable() { if(IsDisabled()) UpdateNextTime(); }
inline bool IsDisabled() const { return NextTime==SchedulerDisabled; }
inline bool IsEnabled() const { return NextTime!=SchedulerDisabled; }
inline bool IsTime() {
if(HAL_GetTick() > NextTime) {
UpdateNextTime();
return true;
}
else {
return false;
}
}
inline tSchedulerTime Remaining() { return IsTime()?0:NextTime-HAL_GetTick(); }
void SetPeriod(uint32_t _Period);
void SetOffset(uint32_t _Offset);
void SetPeriodAndOffset(uint32_t _Period, uint32_t _Offset);
uint32_t GetOffset() const;
uint32_t GetPeriod() const;
tSchedulerTime GetNextTime() const;
tSchedulerTime GetLastTime() const;
void UpdateNextTime();
};
#endif /* SCHEDULER_HPP_ */