-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRunSpeed.h
113 lines (90 loc) · 1.74 KB
/
RunSpeed.h
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// RunSpeed.h
#ifndef _RUNSPEED_h
#define _RUNSPEED_h
#include <Arduino.h>
#include <Print.h>
#include <Statistics.h>
#include "TimeOut.h"
class RunSpeed
{
struct Part
{
const char* name;
Average time;
};
// ReSharper disable once CppInconsistentNaming
static constexpr byte RunSpeed_MAXPARTS = 8;
Average loop;
Part parts [RunSpeed_MAXPARTS] = {};
unsigned long loopTime, startTime;
unsigned part;
public:
explicit RunSpeed ()
: loopTime(0), startTime (0), part (0)
{
}
void clear ()
{
loop.clear ();
for (auto &part : parts)
part.time.clear ();
}
void add (const char* name)
{
for (auto &part : parts)
{
if (part.name == nullptr)
{
part.name = name;
return;
}
}
}
void start ()
{
const auto now = millis ();
// Handle loop time, not possible first time.
if (loopTime != 0)
{
const auto time = now-loopTime;
if (time > 1)
loop.add (time);
}
loopTime = now;
// Start measuring loop parts.
startTime = now;
part = 0;
}
void next ()
{
done (part++);
}
void done (byte part)
{
const auto time = millis ()-startTime;
// Ignore too less times without action calls.
if (time > 1)
parts[part].time.add (time);
startTime = millis ();
}
void print (Print& out)
{
out.println ("Speed:");
out.print ("Loop ");
out.println (loop.getValue ());
for (const auto &part : parts)
{
// Ignore parts too fast.
if (part.time.getSamples () == 0)
continue;
// End when no name.
if (part.name == nullptr)
return;
out.print (part.name);
out.print (' ');
out.print (part.time.getValue ());
out.println ();
}
}
};
#endif