Browse Source

calculate pressure trend

ir
Hendrik Langer 5 years ago
parent
commit
997a6522a0
  1. 77
      src/SensorHistory.cpp
  2. 26
      src/SensorHistory.h
  3. 24
      src/main.cpp

77
src/SensorHistory.cpp

@ -0,0 +1,77 @@
#include "SensorHistory.h"
#include <Arduino.h>
//static const char *TAG = "SensorHistory";
SensorHistory::SensorHistory(const int size) {
_size = size;
_values = (float*) malloc(_size * sizeof(float));
clear();
}
SensorHistory::~SensorHistory()
{
if (_values != NULL) free(_values);
}
void SensorHistory::clear(void) {
_index = 0;
_cnt = 0;
for (int i = 0; i < _size; i++)
{
_values[i] = 0.0; // keeps addValue simpler
}
}
void SensorHistory::addValue(const float value) {
if (_values == NULL) return;
_values[_index] = value;
_index = (_index+1) % _size;
if (_cnt < _size) _cnt++;
}
float SensorHistory::getAverage(void) const {
if (_cnt == 0) return NAN;
float sum = 0;
for (int i = 0; i < _cnt; i++) {
sum += _values[i];
}
return sum / _cnt;
}
float SensorHistory::getMin(void) const {
if (_cnt == 0) return NAN;
float min = _values[0];
for (int i = 0; i < _cnt; i++) {
if (_values[i] < min) min = _values[i];
}
return min;
}
float SensorHistory::getMax(void) const {
if (_cnt == 0) return NAN;
float max = _values[0];
for (int i = 0; i < _cnt; i++) {
if (_values[i] > max) max = _values[i];
}
return max;
}
float SensorHistory::getElement(int index) const {
if (_cnt == 0) return NAN;
index = (_index-1 - index) % _size;
if (index < 0) index = _size+index;
if (index >= _cnt) return NAN;
return _values[index];
}
float SensorHistory::getFirst(void) const {
if (_cnt < _size) return _values[0];
return getElement(-1);
}

26
src/SensorHistory.h

@ -0,0 +1,26 @@
#ifndef _SENSOR_HISTORY_H
#define _SENSOR_HISTORY_H
class SensorHistory {
public:
explicit SensorHistory(const int);
~SensorHistory();
void clear();
void addValue(const float);
float getAverage() const;
float getMin() const;
float getMax() const;
float getElement(int) const;
float getFirst() const;
int getSize() const { return _size; };
int getCount() const { return _cnt; };
protected:
int _size;
int _index;
int _cnt;
float* _values;
};
#endif /* _SENSOR_HISTORY_H */

24
src/main.cpp

@ -31,6 +31,8 @@
#include "network/XD0OTA.h"
#include "network/XD0MQTT.h"
#include "SensorHistory.h"
#include "icons.h"
extern "C" {
@ -66,6 +68,8 @@ struct __attribute__((packed)) sensor_readings_t {
sensor_readings_t sensors_a4cf1211c3e4, sensors_246f28d1fa5c, sensors_246f28d1a080, sensors_30aea47b0568;
SensorHistory history_pressure(30);
uint32_t lastDisplayUpdate = 0;
bool bme280_active = false;
bool bme680_active = false;
@ -148,6 +152,8 @@ void getSensorMeasurements() {
sensor_readings.temperature_min = sensor_readings.temperature;
}
history_pressure.addValue(sensor_readings.pressure / 100.0F);
if (uv_active) {
sensor_readings.uvi = uv.readUVI();
sensor_readings.uva = uv.readUVA();
@ -264,6 +270,24 @@ void displayValues() {
display.setFont(&FreeSansBold9pt7b);
display.setCursor(135,y_offset+40);
display.printf("%.1f", sensor_readings.pressure / 100.0F);
float pressure_diff = history_pressure.getElement(0) - history_pressure.getFirst();
display.setFont(NULL);
display.setCursor(135,y_offset+45);
if (isnan(pressure_diff) || history_pressure.getCount() < history_pressure.getSize()) {
} else if (pressure_diff > -20 && pressure_diff < -1.1) {
display.print("Trend: --");
} else if (pressure_diff < -0.2) {
display.print("Trend: -");
} else if (pressure_diff < 0.2) {
display.print("Trend: =");
} else if (pressure_diff < 1.1) {
display.print("Trend: +");
} else if (pressure_diff < 20) {
display.print("Trend: ++");
} else {
display.print("?");
}
// Other
display.drawRect(195,y_offset+10,56,122-10,GxEPD_BLACK);

Loading…
Cancel
Save