-
Notifications
You must be signed in to change notification settings - Fork 0
/
progressmeter.h
115 lines (96 loc) · 2.84 KB
/
progressmeter.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
114
115
#ifndef PROGRESSMETER_H
#define PROGRESSMETER_H
/* An abstract progress indicator. It provides the core of a progress indicator
* that prints the current state at predetermined time intervals.
*/
class ProgressIndicator
{
public:
using Milliseconds = unsigned long;
using TimePoint = Milliseconds;
ProgressIndicator(float initial_, float target_) :
initial(0), target(0)
{
reset(initial_, target_);
}
virtual ~ProgressIndicator() = default;
// Print the current progress if either enough time has elapsed from the
// previous printing or the forcePrint parameter is true.
void print(float value, bool forcePrint = false)
{
TimePoint now = millis();
if (!forcePrint && now < previousPrint + printPeriod)
{
return;
}
previousPrint = now;
printProgress(value);
}
// Set new initial and target values.
void reset(float initial_, float target_)
{
initial = initial_;
target = target_;
previousPrint = millis() - printPeriod;
}
// Finalize the output (for example, by printing a final newline).
virtual void finalize() = 0;
protected:
// Do the actual printing.
virtual void printProgress(float value) = 0;
float initial;
float target;
TimePoint previousPrint;
static constexpr Milliseconds printPeriod{100};
};
// An ASCII progress bar.
class BarIndicator : public ProgressIndicator
{
public:
using ProgressIndicator::ProgressIndicator;
virtual void finalize()
{
Serial.write('\n');
}
private:
virtual void printProgress(float value)
{
constexpr size_t stringLength = 32;
char formattedValue[stringLength];
snprintf(formattedValue, stringLength, "\r\033[K%4d.%02d ", int(value), int(100 * (value - int(value))));
Serial.write(formattedValue);
int position = round(length * (value - initial)/(target - initial));
position = min(max(position, 0), length - 1);
for (int i = 0; i < length; ++i)
{
Serial.write(
i < position ? '=' :
i == position ? '>' :
'-');
}
}
// Length of the bar in characters.
static constexpr int length = 30;
};
/* An indicator with simple numeric output suitable for further processing.
* It outputs lines with the format:
*
* <current value> <progress percent>
*/
class PercentIndicator : public ProgressIndicator
{
public:
using ProgressIndicator::ProgressIndicator;
virtual void finalize() {}
private:
virtual void printProgress(float value)
{
constexpr size_t stringLength = 32;
char formatted[stringLength];
snprintf(formatted, stringLength, "%4d.%02d %d\r\n",
int(value), int(100 * (value - int(value))),
(int)round(100 * (value - initial)/(target - initial)));
Serial.write(formatted);
}
};
#endif // PROGRESSMETER_H