2021-10-14 08:54:11 -04:00
|
|
|
#pragma once
|
|
|
|
//
|
|
|
|
// FILE: MINMAX.h
|
|
|
|
// AUTHOR: Rob Tillaart
|
2021-12-21 13:57:20 -05:00
|
|
|
// VERSION: 0.1.2
|
2021-10-14 08:54:11 -04:00
|
|
|
// DATE: 2021-10-14
|
|
|
|
// PURPOSE: MINMAX library - simple peak finder
|
|
|
|
//
|
|
|
|
// HISTORY:
|
|
|
|
// 0.1.0 2021-10-14 initial version
|
2021-11-08 15:19:42 -05:00
|
|
|
// 0.1.1 2021-11-09 update readme, parameters
|
|
|
|
// add getAutoResetCount()
|
|
|
|
// rename setAutoResetCount()
|
2021-12-21 13:57:20 -05:00
|
|
|
// 0.1.2 2021-12-21 update library.json, license, minor edits
|
|
|
|
// defined constants
|
2021-10-14 08:54:11 -04:00
|
|
|
|
|
|
|
|
|
|
|
#include "Arduino.h"
|
|
|
|
|
2021-12-21 13:57:20 -05:00
|
|
|
#define MINMAX_LIB_VERSION (F("0.1.2"))
|
|
|
|
|
|
|
|
#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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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;
|
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;
|
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;
|
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++;
|
|
|
|
return rv;
|
|
|
|
}
|
2021-10-14 08:54:11 -04:00
|
|
|
|
|
|
|
|
2021-11-08 15:19:42 -05:00
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
_minimum = 0;
|
|
|
|
_maximum = 0;
|
|
|
|
_count = 0;
|
|
|
|
}
|
2021-10-14 08:54:11 -04:00
|
|
|
|
|
|
|
|
2021-11-08 15:19:42 -05:00
|
|
|
void autoReset(uint32_t count) // obsolete 0.2.0
|
|
|
|
{
|
|
|
|
_resetCount = count;
|
|
|
|
};
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
float minimum() { return _minimum; };
|
|
|
|
float maximum() { return _maximum; };
|
|
|
|
uint32_t count() { return _count; };
|
|
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
float _minimum;
|
|
|
|
float _maximum;
|
|
|
|
uint32_t _count;
|
|
|
|
uint32_t _resetCount;
|
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
|
|
|
|