forked from ValdemarOrn/Teensy4i2s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2s_timers.cpp
89 lines (70 loc) · 1.88 KB
/
i2s_timers.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "i2s_timers.h"
float Timers::TimeAvg[Timers::TIMER_COUNT];
float Timers::TimePeak[Timers::TIMER_COUNT];
float Timers::TimeMax[Timers::TIMER_COUNT];
int Timers::TimeFrameStart = 0;
float Timers::TimeFramePeriod = 1333.33f; // 64 sample blocks at 48khz
void Timers::Lap(uint8_t timerIndex)
{
// don't allow external update of total timer
if (timerIndex >= TIMER_TOTAL)
return;
LapInner(timerIndex);
}
void Timers::LapInner(uint8_t timerIndex)
{
if (timerIndex >= TIMER_COUNT)
return;
int val = float(micros() - TimeFrameStart);
TimeAvg[timerIndex] = 0.995 * TimeAvg[timerIndex] + 0.005 * val;
TimePeak[timerIndex] = 0.995 * TimePeak[timerIndex];
if (val > TimePeak[timerIndex])
TimePeak[timerIndex] = val;
if (val > TimeMax[timerIndex])
TimeMax[timerIndex] = val;
}
float Timers::GetAvg(uint8_t timerIndex)
{
if (timerIndex >= TIMER_COUNT)
return -1;
return Timers::TimeAvg[timerIndex];
}
float Timers::GetPeak(uint8_t timerIndex)
{
if (timerIndex >= TIMER_COUNT)
return -1;
return Timers::TimePeak[timerIndex];
}
float Timers::GetMax(uint8_t timerIndex)
{
if (timerIndex >= TIMER_COUNT)
return -1;
return Timers::TimeMax[timerIndex];
}
void Timers::Clear(uint8_t timerIndex)
{
if (timerIndex >= TIMER_COUNT)
return;
Timers::TimeAvg[timerIndex] = 0;
Timers::TimePeak[timerIndex] = 0;
Timers::TimeMax[timerIndex] = 0;
}
float Timers::GetAvgPeriod()
{
return TimeFramePeriod;
}
float Timers::GetCpuLoad()
{
return Timers::TimeAvg[Timers::TIMER_TOTAL] / TimeFramePeriod;
}
void Timers::ResetFrame()
{
if (TimeFrameStart == 0)
{
TimeFrameStart = micros();
return;
}
int oldStart = TimeFrameStart;
TimeFrameStart = micros();
TimeFramePeriod = 0.995f * TimeFramePeriod + 0.005f * (TimeFrameStart - oldStart);
}