Update main.ino
This commit is contained in:
parent
024208e3ac
commit
f40033cf3d
1 changed files with 38 additions and 42 deletions
76
main.ino
76
main.ino
|
@ -26,67 +26,63 @@ const float WAIT_TIME = 150;
|
||||||
class DataStorage {
|
class DataStorage {
|
||||||
private:
|
private:
|
||||||
float data[AMOUNT_DATAPOINTS];
|
float data[AMOUNT_DATAPOINTS];
|
||||||
int cursor;
|
int rIndex;
|
||||||
|
int rCount;
|
||||||
const int MAX_DATA_POINTS;
|
const int MAX_DATA_POINTS;
|
||||||
const String UNIT;
|
const String UNIT;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
DataStorage(String unit, int maxDataPoints)
|
DataStorage(String unit, int maxDataPoints)
|
||||||
: UNIT(unit), MAX_DATA_POINTS(maxDataPoints), cursor(0) {
|
: UNIT(unit), MAX_DATA_POINTS(maxDataPoints), rIndex(0), rCount(0) {
|
||||||
for (int i = 0; i < MAX_DATA_POINTS; i++) {
|
for (int i = 0; i < this->MAX_DATA_POINTS; i++) {
|
||||||
this->data[i] = 0;
|
this->data[i] = 0;
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
|
||||||
float getDataByIndex(int index) {
|
|
||||||
if(index < 0 || index >= this->cursor) return 0;
|
|
||||||
return this->data[index];
|
|
||||||
};
|
|
||||||
|
|
||||||
int getCursor() const {return this->cursor;};
|
|
||||||
|
|
||||||
void addData(float newData) {
|
void addData(float newData) {
|
||||||
if (this->cursor < this->MAX_DATA_POINTS) {
|
this->data[this->rIndex] = newData;
|
||||||
this->data[this->cursor++] = newData; //++cursor, cursor++, know the difference :3
|
this->rIndex = (this->rIndex + 1) % this->MAX_DATA_POINTS;
|
||||||
} else {
|
if (this->rCount < this->MAX_DATA_POINTS) this->rCount++;
|
||||||
for (int i = 0; i < this->MAX_DATA_POINTS - 1; i++) {
|
|
||||||
this->data[i] = this->data[i + 1];
|
|
||||||
}
|
}
|
||||||
this->data[this->MAX_DATA_POINTS - 1] = newData;
|
|
||||||
|
float getDataByIndex(int index) {
|
||||||
|
if(index < 0 || index >= this->rCount) return 0;
|
||||||
|
int idx = (this->rIndex + this->MAX_DATA_POINTS - this->rCount + index) % this->MAX_DATA_POINTS;
|
||||||
|
return this->data[idx];
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
int getCursor() const { return this->rCount; }
|
||||||
|
|
||||||
float getMaxDataPoint() {
|
float getMaxDataPoint() {
|
||||||
if (this->cursor == 0) return 0;
|
if (this->rCount == 0) return 0;
|
||||||
float maxY = this->data[0];
|
float maxY = this->getDataByIndex(0);
|
||||||
for (int i = 1; i < this->cursor; i++) {
|
for (int i = 1; i < this->rCount; i++) {
|
||||||
if (this->data[i] > maxY) maxY = this->data[i];
|
float v = this->getDataByIndex(i);
|
||||||
|
if (v > maxY) maxY = v;
|
||||||
}
|
}
|
||||||
return maxY;
|
return maxY;
|
||||||
};
|
}
|
||||||
|
|
||||||
float getMinDataPoint() {
|
float getMinDataPoint() {
|
||||||
if (this->cursor == 0) return 0;
|
if (this->rCount == 0) return 0;
|
||||||
float minY = this->data[0];
|
float minY = this->getDataByIndex(0);
|
||||||
for (int i = 1; i < this->cursor; i++) {
|
for (int i = 1; i < this->rCount; i++) {
|
||||||
if (this->data[i] < minY) minY = this->data[i];
|
float v = this->getDataByIndex(i);
|
||||||
|
if (v < minY) minY = v;
|
||||||
}
|
}
|
||||||
return minY;
|
return minY;
|
||||||
};
|
}
|
||||||
|
|
||||||
float getAvgDataPoint() {
|
float getAvgDataPoint() {
|
||||||
if (this->cursor == 0) return 0;
|
if (this->rCount == 0) return 0;
|
||||||
float avgY = this->data[0];
|
float sum = 0;
|
||||||
for (int i = 1; i < this->cursor; i++) {
|
for (int i = 0; i < this->rCount; i++) {
|
||||||
avgY += data[i];
|
sum += this->getDataByIndex(i);
|
||||||
};
|
}
|
||||||
return avgY / this->cursor;
|
return sum / this->rCount;
|
||||||
};
|
}
|
||||||
|
|
||||||
String getUnit() {
|
String getUnit() { return this->UNIT; }
|
||||||
return this->UNIT;
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
class Element {
|
class Element {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue