3#include "ustd_platform.h"
7#define SENSOR_VALUE_INVALID -999999.0
52 unsigned int noVals = 0;
53 unsigned int smoothInterval;
54 unsigned int pollTimeSec;
59 double lastVal = SENSOR_VALUE_INVALID;
64 : smoothInterval{smoothInterval}, pollTimeSec{pollTimeSec}, eps{eps} {
88 meanVal = (meanVal * noVals + (*pvalue)) / (noVals + 1);
89 if (noVals < smoothInterval) {
92 double delta = lastVal - meanVal;
94 delta = (-1.0) * delta;
96 if (delta > eps || first) {
103 if (pollTimeSec != 0) {
104 if (
timeDiff(last, millis()) > pollTimeSec * 1000L) {
126 double tval = (double)*plvalue;
129 *plvalue = (long)tval;
140 lastVal = SENSOR_VALUE_INVALID;
144 void update(
unsigned int _smoothInterval = 5,
int unsigned _pollTimeSec = 60,
158 smoothInterval = _smoothInterval;
159 pollTimeSec = _pollTimeSec;
215 ustd::array<T_FLOAT> x, y;
216 T_FLOAT minX, minY, maxX, maxY;
221 bool _extrapolate =
false) {
231 extrapolate = _extrapolate;
233 for (
unsigned int i = 0; i < count; i++) {
236 if (px[i] <= x[len - 1])
238 if (py[i] == y[len - 1])
242 dir = (py[i] > y[len - 1]);
243 if ((py[i] > y[len - 1]) != dir)
248 if (i == 0 || minX > x[len])
250 if (i == 0 || minY > y[len])
252 if (i == 0 || maxX < x[len])
254 if (i == 0 || maxY < y[len])
260 static T_FLOAT
min(ustd::array<T_FLOAT> ar) {
264 T_FLOAT minVal = 0.0;
265 for (
unsigned int i = 0; i < ar.length(); i++) {
266 if (i == 0 || ar[i] < minVal)
272 static T_FLOAT
max(ustd::array<T_FLOAT> ar) {
276 T_FLOAT maxVal = 0.0;
277 for (
unsigned int i = 0; i < ar.length(); i++) {
278 if (i == 0 || ar[i] > maxVal)
284 static void rescale(ustd::array<T_FLOAT> *par, T_FLOAT *pminX, T_FLOAT *pmaxX, T_FLOAT newMin,
301 T_FLOAT newMinX, newMaxX;
302 unsigned int len = (*par).length();
303 if (len < 2 || *pminX == *pmaxX)
306 dx = (*pmaxX - *pminX);
307 T_FLOAT ndx = newMax - newMin;
308 for (
unsigned int i = 0; i < len; i++) {
309 T_FLOAT xi = (*par)[i];
310 (*par)[i] = (xi - *pminX) / dx * ndx + newMin;
311 if (i == 0 || newMinX > xi)
313 if (i == 0 || newMaxX < xi)
326 rescale(&x, &minX, &maxX, newMin, newMax);
334 rescale(&y, &minY, &maxY, newMin, newMax);
337 static int linsearch(ustd::array<T_FLOAT> &ar, T_FLOAT x) {
346 int a = 0, b = ar.length() - 1, n;
363 T_FLOAT dx1, dx2, dy;
375 return y[0] - dy / dx2 * dx1;
382 dx1 = x[len - 1] - x[len];
383 dx2 = xi - x[len - 1];
384 dy = y[len - 1] - y[len - 2];
385 return y[len - 1] + dy / dx1 * dx2;
391 dx1 = x[n] - x[n + 1];
393 dy = y[n + 1] - y[n];
394 float yi = y[n] - dy / dx1 * dx2;
muwerk numericFunction class
Definition sensors.h:213
static int linsearch(ustd::array< T_FLOAT > &ar, T_FLOAT x)
Definition sensors.h:337
void rescaleY(T_FLOAT newMin, T_FLOAT newMax)
Definition sensors.h:328
static void rescale(ustd::array< T_FLOAT > *par, T_FLOAT *pminX, T_FLOAT *pmaxX, T_FLOAT newMin, T_FLOAT newMax)
Definition sensors.h:284
T_FLOAT interpol(T_FLOAT xi)
Definition sensors.h:359
T_FLOAT operator()(T_FLOAT x)
Definition sensors.h:398
static T_FLOAT min(ustd::array< T_FLOAT > ar)
Definition sensors.h:260
static T_FLOAT max(ustd::array< T_FLOAT > ar)
Definition sensors.h:272
void rescaleX(T_FLOAT newMin, T_FLOAT newMax)
Definition sensors.h:320
numericFunction(const T_FLOAT px[], const T_FLOAT py[], unsigned int count, bool _extrapolate=false)
Definition sensors.h:220
muwerk sensorprocessor class
Definition sensors.h:50
void reset()
Definition sensors.h:134
bool filter(long *plvalue)
Definition sensors.h:115
void update(unsigned int _smoothInterval=5, int unsigned _pollTimeSec=60, double _eps=0.1)
Definition sensors.h:144
bool filter(double *pvalue)
Definition sensors.h:77
sensorprocessor(unsigned int smoothInterval=5, int unsigned pollTimeSec=60, double eps=0.1)
Definition sensors.h:62
The muwerk namespace.
Definition console.h:15
unsigned long timeDiff(unsigned long first, unsigned long second)
Definition muwerk.h:44