-
Notifications
You must be signed in to change notification settings - Fork 5
/
Histogram.cpp
92 lines (78 loc) · 2.05 KB
/
Histogram.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
90
91
#include "Histogram.h"
#include <EEPROM.h>
#include <math.h>
Histogram::Histogram() {
lastAddMillis = millis();
clear();
}
void Histogram::clear()
{
memset(intervalHistogram, 0, sizeof(intervalHistogram));
}
void Histogram::addNow()
{
unsigned long currentTime = millis();
unsigned long elapsedTime = currentTime - lastAddMillis;
lastAddMillis = currentTime;
float logValue = log(elapsedTime);
logValue = constrain(logValue, HISTOGRAM_MIN, HISTOGRAM_MAX);
int index = (int)((logValue-HISTOGRAM_MIN)/HISTOGRAM_INC);
intervalHistogram[index] += 1;
}
void Histogram::print()
{
Serial.begin(28800);
Serial.println("## Key: interval (ms)");
Serial.println("## Value: number of revolutions"
" taking this interval or greater");
char buffer[10];
dtostrf(METERS_PER_REVOLUTION, 0, sizeof(buffer)-3, buffer);
Serial.println(String("METERS_PER_REVOLUTION <- ") + String(buffer));
Serial.println("intervalData <- list(");
Serial.print("intervals=c(");
for(int i = 0; i < HISTOGRAM_SIZE; i++)
{
if (i != 0) { Serial.print(", "); }
float logValue = i*HISTOGRAM_INC + HISTOGRAM_MIN;
float interval = exp(logValue);
Serial.print(interval);
}
Serial.println("),");
Serial.print("counts=c(");
for(int i = 0; i < HISTOGRAM_SIZE; i++)
{
if (i != 0) { Serial.print(", "); }
Serial.print(intervalHistogram[i]);
}
Serial.println(")");
Serial.println(")");
Serial.end();
}
void Histogram::restore()
{
int address;
byte lowerValue, upperValue;
for(int i = 0; i < HISTOGRAM_SIZE; i++)
{
address = i*2;
lowerValue = EEPROM.read(address);
upperValue = EEPROM.read(address+1);
intervalHistogram[i] = (upperValue << 8) | lowerValue;
}
}
void Histogram::save()
{
byte upperByte, lowerByte;
unsigned int lowerMask = 0xFF;
unsigned int upperMask = 0xFF00;
int address;
for(int i = 0; i < HISTOGRAM_SIZE; i++)
{
unsigned int histogramValue = intervalHistogram[i];
address = i*2;
upperByte = (histogramValue & upperMask) >> 8;
EEPROM.write(address+1, upperByte);
lowerByte = histogramValue & lowerMask;
EEPROM.write(address, lowerByte);
}
}