0.2.2 MINMAX

This commit is contained in:
Rob Tillaart 2023-12-12 19:47:21 +01:00
parent dd21cb1907
commit 6d0c7713cd
8 changed files with 125 additions and 17 deletions

View File

@ -6,11 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).
## [0.2.2] - 2023-12-12
- add **void setResetDefaults(float minimum, float maximum)**
- update readme
## [0.2.1] - 2023-11-14
- update readme.md
- update examples
## [0.2.0] - 2022-12-07
- fix #6 split in .cpp and .h
- remove obsolete autoReset()

View File

@ -1,7 +1,7 @@
//
// FILE: MINMAX.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.1
// VERSION: 0.2.2
// DATE: 2021-10-14
// PURPOSE: MINMAX library - simple peak finder
// URL: https://github.com/RobTillaart/MINMAX
@ -12,9 +12,11 @@
MINMAX::MINMAX()
{
reset();
_resetCount = 0;
_minimumDefault = 0;
_maximumDefault = 0;
_callback = NULL;
reset();
}
@ -27,20 +29,30 @@ uint8_t MINMAX::add(const float value)
reset();
rv |= MINMAX_RESET_DONE;
}
if ((value < _minimum) || (_count == 0))
// new run and range not adjusted by setResetDefaults()
if ((_count == 0) && (_minimum == 0) && (_maximum == 0))
{
_minimum = _maximum = value;
_lastMin = _lastMax = millis();
rv |= MINMAX_MIN_CHANGED | MINMAX_MAX_CHANGED;
}
if (value < _minimum)
{
_minimum = value;
_lastMin = millis();
rv |= MINMAX_MIN_CHANGED;
}
if ((value > _maximum) || (_count == 0))
if (value > _maximum)
{
_maximum = value;
_lastMax = millis();
rv |= MINMAX_MAX_CHANGED;
}
_count++;
if ((rv != MINMAX_NO_CHANGE) && (_callback != NULL)) _callback();
if ((rv != MINMAX_NO_CHANGE) && (_callback != NULL))
{
_callback();
}
return rv;
}
@ -48,14 +60,22 @@ uint8_t MINMAX::add(const float value)
void MINMAX::reset()
{
_lastValue = 0;
_minimum = 0;
_maximum = 0;
_minimum = _minimumDefault;
_maximum = _maximumDefault;
_count = 0;
_lastMin = 0;
_lastMax = 0;
}
void MINMAX::setResetDefaults(float minimum, float maximum)
{
_minimumDefault = minimum;
_maximumDefault = maximum;
}
void MINMAX::addCallBack( void (* func)(void) )
{
_callback = func;

View File

@ -2,7 +2,7 @@
//
// FILE: MINMAX.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.1
// VERSION: 0.2.2
// DATE: 2021-10-14
// PURPOSE: MINMAX library - simple peak finder
// URL: https://github.com/RobTillaart/MINMAX
@ -10,7 +10,7 @@
#include "Arduino.h"
#define MINMAX_LIB_VERSION (F("0.2.1"))
#define MINMAX_LIB_VERSION (F("0.2.2"))
#define MINMAX_NO_CHANGE 0X00
#define MINMAX_MIN_CHANGED 0X01
@ -26,6 +26,8 @@ public:
uint8_t add(const float value);
void reset();
// the reset defaults are both zero.
void setResetDefaults(float minimum, float maximum);
// call back function when there is a change
void addCallBack( void (* func)(void) );
@ -52,8 +54,12 @@ public:
private:
float _lastValue;
float _minimum;
float _maximum;
float _minimumDefault;
float _maximumDefault;
uint32_t _count;
uint32_t _resetCount;
void (* _callback)(void);

View File

@ -44,6 +44,7 @@ Finally the library keeps track when the last peaks occurred.
- **MINMAX()** Constructor,
- **uint8_t add(float value)** add next value. Returns status (bit flags), see table below.
- **void reset()** resets the minimum and maximum to 0.
- **void setResetDefaults(float minimum, float maximum)** sets the default values for minimum and maximum defining an initial range / window.
- **float minimum()** returns last minimum. Can be higher than previous call due to **reset()** or **autoReset()**.
If no call to **add()** is made yet it will return 0.
- **float maximum()** returns last maximum. Can be lower than previous call due to **reset()** or **autoReset()**.
@ -88,6 +89,30 @@ when the minimum or the maximum has changed.
See examples.
#### setResetDefaults()
- **void setResetDefaults(minimum, maximum)** sets the default values for minimum and maximum defining an initial range / window when **reset()** is called.
This will reduce an abundance of new min/max values in the first part of a stream, possibly causing unneeded call back calls.
The constructor sets both to zero (0) by default. The user can now override these values.
There are no default values for the parameters in the function.
The user is responsible and even allowed to set minimum to be larger than maximum.
The new values become active after the call to **reset()**.
The function does not change or reset the **lastMin()** and **lastMax()** timestamps.
Only after **reset()** these are set to 0 again.
Note that with an initial range set, the **lastMin()** and **lastMax()** timestamps
may be zero for quite a while.
Typical code snippet
```cpp
mm.setResetDefaults(-10, 20); // arbitrary values
mm.reset(); // activate them
```
## Obsolete
- **void autoReset(uint32_t count)** obsolete since 0.2.0
@ -107,8 +132,9 @@ The examples show the basic working of the functions.
#### Should
- separate call back for MINMAX_MIN_CHANGED and MINMAX_MAX_CHANGED
- add getLastEvent()?
- consider an (featured) extended class and a (simple) base class.
- separate call back for **MINMAX_MIN_CHANGED** and **MINMAX_MAX_CHANGED**
- add **getLastEvent()**
- add AVG **average()** **sum()**
- like a digital multimeter (DMM)
- **sum()** would be sufficient as average can be derived.
@ -117,11 +143,16 @@ The examples show the basic working of the functions.
- Template class to allow other types
- int32_t uint64_t double etc.
- now you loose precision
- now you loose precision.
- a related class might be **WINDOW(min, max)** that counts and call backs if
a value is out of a predefined range.
- **void setResetDefaults(minimum, maximum, bool adjust = true)** add an adjust flag
that allows / reject adaption of min / max. (extended class).
- define MINMAX_MIN_CHANGED => MINMAX_MIN_EXCEEDED (new or reuse?)
#### Wont (unless)
- thresholds, windowing + triggers (separate class?)
- thresholds, windowing + triggers (separate class!)
- auto-reset after time? (would affect all functions ?)
- need a uint32_t start;
- need a uint32_t threshold;

View File

@ -0,0 +1,46 @@
//
// FILE: minmax_setResetDefaults.ino
// AUTHOR: Rob Tillaart
// PURPOSE: demo
// URL: https://github.com/RobTillaart/minmax
#include "MINMAX.h"
MINMAX mm;
uint32_t start, stop;
void setup()
{
Serial.begin(115200);
Serial.println(__FILE__);
Serial.print("MINMAX_LIB_VERSION: ");
Serial.println(MINMAX_LIB_VERSION);
Serial.println();
mm.setResetDefaults(-100, 100);
mm.reset();
mm.setAutoResetCount(10000);
}
void loop()
{
int r = random(220) - 110;
if (mm.add(r) != MINMAX_NO_CHANGE) // changed minimum or maximum or reset
{
Serial.print(mm.count());
Serial.print("\t");
Serial.print(mm.minimum());
Serial.print("\t");
Serial.print(mm.maximum());
Serial.print("\n");
}
}
// -- END OF FILE --

View File

@ -6,6 +6,7 @@ MINMAX KEYWORD1
# Methods and Functions (KEYWORD2)
add KEYWORD2
reset KEYWORD2
setResetDefaults KEYWORD2
addCallBack KEYWORD2

View File

@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/MINMAX.git"
},
"version": "0.2.1",
"version": "0.2.2",
"license": "MIT",
"frameworks": "*",
"platforms": "*",

View File

@ -1,5 +1,5 @@
name=MINMAX
version=0.2.1
version=0.2.2
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=MINMAX library for Arduino.