Artim
API documentation
Loading...
Searching...
No Matches
Artim.h
1/*
2 Arduino Runtime Control and Measurement.
3 Copyright (C) 2021 Thomas Wendel
4 https://github.com/thweit00/Artim
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19#ifndef ARTIM_H
20#define ARTIM_H
21
22#include <Arduino.h>
23
24
25// Array size for histogram data.
26// Current implementation is fixed to 10.
27#define ARTIM_NUM_HIST (10)
28
30class Artim {
31 private:
32
33 // ------------- private members -------------
34
35 // Loop time of main loop in ms. Artim will suspend execution
36 // at the end of the loop and wait till loopTime is expired.
37 unsigned int loopTime;
38
39 // Private variable that holds the start time of the loop in ms.
40 // Used for runtime calculation.
41 unsigned long loopStartTime;
42
43 // Private variable that holds the end time of the loop in ms.
44 // Used for runtime calculation.
45 unsigned long loopEndTime;
46
47 // Private variable that holds the calculation result (ms) for the
48 // wait time that needs to be applied to meet the loopTime.
49 // Can be negative if a loop overrun happened (execution took longer
50 // than the configured time)
51 long loopWaitTime;
52
53 // Private counter that holds the number of load samples (number of executions)
54 // Basis for average calculation
55 unsigned long numLoadSamples;
56
57 // Load average in % (valid for current power cycle)
58 float loadAvg;
59
60 // Load minimum in % (valid for current power cycle)
61 float loadMin;
62
63 // Load maximum in % (valid for current power cycle)
64 float loadMax;
65
66 // Historgram data array. Will be updated each cycle.
67 // Each element represents a load range (0%-9%, 10% - 19%, 20% - 29%, ...)
68 // E.g. if current runtime is 21%, the counter at index 2 will be incremented.
69 unsigned int loadHistogram[ARTIM_NUM_HIST];
70
71 // Overload counter. Same input as histogram array but will be incremented
72 // each time a loop overrun was detected (negative wait time).
73 unsigned int overloadHistogram;
74
75 // Private flag used to start average calculation
76 boolean firstCall;
77
78 // ------------- private operations -------------
79
80 // Private operation to calculate load of the last loop.
81 // Called at the beginning of each loop (called by loopBegin).
82 void calcStat(void);
83
84 // Private operation to calculate the wait time.
85 // Called at the end of each loop (called by loopEnd).
86 void calcWait(void);
87
88 // Private operation to execute the wait operation (delay)
89 // based on the wait time calculated before (called by loopEnd).
90 void waitNow(void);
91
92
93 public:
94
95 // ------------- public operations -------------
98 Artim(unsigned int loopTime_ms);
99
103 void loopBegin(void);
104
108 void loopEnd(void);
109
113 float getAvgLoad(void);
114
119 float getMinLoad(void);
120
125 float getMaxLoad(void);
126
144 unsigned int getLoadHist(byte rangeIdx);
145
152 unsigned int getOverloadHist(void);
153};
154
155
156#endif // ARTIM_H
157
Arduino Runtime Control and Measurement class.
Definition: Artim.h:30
float getAvgLoad(void)
API function that provides the average load in %.
Definition: Artim.cpp:131
unsigned int getLoadHist(byte rangeIdx)
API function that provides the histogram data for a range provided as input parameter.
Definition: Artim.cpp:149
void loopEnd(void)
loopEnd needs to be invoked as very last operation of the loop.
Definition: Artim.cpp:44
float getMinLoad(void)
API function that provides the minimum load in %.
Definition: Artim.cpp:137
float getMaxLoad(void)
API function that provides the maximum load in %.
Definition: Artim.cpp:143
unsigned int getOverloadHist(void)
API function that provides the overload historgram data.
Definition: Artim.cpp:165
void loopBegin(void)
loopBegin needs to be invoked as very first operation of the loop.
Definition: Artim.cpp:36