2021-10-14 08:54:11 -04:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: MINMAX.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-11-17 11:36:35 -05:00
|
|
|
// VERSION: 0.1.3
|
2021-10-14 08:54:11 -04:00
|
|
|
// DATE: 2021-10-14
|
|
|
|
// PURPOSE: MINMAX library - simple peak finder
|
2022-11-17 11:36:35 -05:00
|
|
|
|
2021-10-14 08:54:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
2022-11-17 11:36:35 -05:00
|
|
|
#define MINMAX_LIB_VERSION (F("0.1.3"))
|
2021-12-21 13:57:20 -05:00
|
|
|
|
|
|
|
#define MINMAX_NO_CHANGE 0X00
|
|
|
|
#define MINMAX_MIN_CHANGED 0X01
|
|
|
|
#define MINMAX_MAX_CHANGED 0X02
|
|
|
|
#define MINMAX_RESET_DONE 0X80
|
2021-10-14 08:54:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
class MINMAX
|
|
|
|
{
|
2021-11-08 15:19:42 -05:00
|
|
|
public:
|
2021-10-14 08:54:11 -04:00
|
|
|
|
2021-11-08 15:19:42 -05:00
|
|
|
MINMAX()
|
|
|
|
{
|
|
|
|
reset();
|
|
|
|
_resetCount = 0;
|
2022-11-17 11:36:35 -05:00
|
|
|
_callback = NULL;
|
2021-11-08 15:19:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-12-21 13:57:20 -05:00
|
|
|
uint8_t add(const float value)
|
2021-11-08 15:19:42 -05:00
|
|
|
{
|
2021-12-21 13:57:20 -05:00
|
|
|
uint8_t rv = MINMAX_NO_CHANGE;
|
2022-11-17 11:36:35 -05:00
|
|
|
_lastValue = value;
|
2021-11-08 15:19:42 -05:00
|
|
|
if ((_resetCount != 0) && (_resetCount == _count))
|
2021-10-14 08:54:11 -04:00
|
|
|
{
|
|
|
|
reset();
|
2021-12-21 13:57:20 -05:00
|
|
|
rv |= MINMAX_RESET_DONE;
|
2021-10-14 08:54:11 -04:00
|
|
|
}
|
2021-11-08 15:19:42 -05:00
|
|
|
if ((value < _minimum) || (_count == 0))
|
2021-10-14 08:54:11 -04:00
|
|
|
{
|
2021-11-08 15:19:42 -05:00
|
|
|
_minimum = value;
|
2022-11-17 11:36:35 -05:00
|
|
|
_lastMin = millis();
|
2021-12-21 13:57:20 -05:00
|
|
|
rv |= MINMAX_MIN_CHANGED;
|
2021-10-14 08:54:11 -04:00
|
|
|
}
|
2021-11-08 15:19:42 -05:00
|
|
|
if ((value > _maximum) || (_count == 0))
|
2021-10-14 08:54:11 -04:00
|
|
|
{
|
2021-11-08 15:19:42 -05:00
|
|
|
_maximum = value;
|
2022-11-17 11:36:35 -05:00
|
|
|
_lastMax = millis();
|
2021-12-21 13:57:20 -05:00
|
|
|
rv |= MINMAX_MAX_CHANGED;
|
2021-10-14 08:54:11 -04:00
|
|
|
}
|
2021-11-08 15:19:42 -05:00
|
|
|
_count++;
|
2022-11-17 11:36:35 -05:00
|
|
|
if ((rv != MINMAX_NO_CHANGE) && (_callback != NULL)) _callback();
|
2021-11-08 15:19:42 -05:00
|
|
|
return rv;
|
|
|
|
}
|
2021-10-14 08:54:11 -04:00
|
|
|
|
|
|
|
|
2021-11-08 15:19:42 -05:00
|
|
|
void reset()
|
|
|
|
{
|
2022-11-17 11:36:35 -05:00
|
|
|
_lastValue = 0;
|
2021-11-08 15:19:42 -05:00
|
|
|
_minimum = 0;
|
|
|
|
_maximum = 0;
|
|
|
|
_count = 0;
|
2022-11-17 11:36:35 -05:00
|
|
|
_lastMin = 0;
|
|
|
|
_lastMax = 0;
|
2021-11-08 15:19:42 -05:00
|
|
|
}
|
2021-10-14 08:54:11 -04:00
|
|
|
|
|
|
|
|
2022-11-17 11:36:35 -05:00
|
|
|
void addCallBack( void (* func)(void) )
|
2021-11-08 15:19:42 -05:00
|
|
|
{
|
2022-11-17 11:36:35 -05:00
|
|
|
_callback = func;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
void autoReset(uint32_t count) // obsolete 0.2.0
|
|
|
|
{
|
|
|
|
setAutoResetCount(count);
|
2021-11-08 15:19:42 -05:00
|
|
|
};
|
2021-12-21 13:57:20 -05:00
|
|
|
|
|
|
|
|
2021-11-08 15:19:42 -05:00
|
|
|
void setAutoResetCount(uint32_t count)
|
|
|
|
{
|
|
|
|
_resetCount = count;
|
|
|
|
};
|
2021-10-14 08:54:11 -04:00
|
|
|
|
|
|
|
|
2021-11-08 15:19:42 -05:00
|
|
|
uint32_t getAutoResetCount()
|
|
|
|
{
|
|
|
|
return _resetCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-11-17 11:36:35 -05:00
|
|
|
float lastValue() { return _lastValue; };
|
2021-11-08 15:19:42 -05:00
|
|
|
float minimum() { return _minimum; };
|
|
|
|
float maximum() { return _maximum; };
|
|
|
|
uint32_t count() { return _count; };
|
2022-11-17 11:36:35 -05:00
|
|
|
uint32_t lastMin() { return _lastMin; };
|
|
|
|
uint32_t lastMax() { return _lastMax; };
|
2021-11-08 15:19:42 -05:00
|
|
|
|
|
|
|
|
|
|
|
private:
|
2022-11-17 11:36:35 -05:00
|
|
|
float _lastValue;
|
2021-11-08 15:19:42 -05:00
|
|
|
float _minimum;
|
|
|
|
float _maximum;
|
|
|
|
uint32_t _count;
|
|
|
|
uint32_t _resetCount;
|
2022-11-17 11:36:35 -05:00
|
|
|
void (* _callback)(void);
|
|
|
|
|
|
|
|
uint32_t _lastMin;
|
|
|
|
uint32_t _lastMax;
|
2021-10-14 08:54:11 -04:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2021-11-08 15:19:42 -05:00
|
|
|
|
2021-10-14 08:54:11 -04:00
|
|
|
// -- END OF FILE --
|
2021-11-08 15:19:42 -05:00
|
|
|
|