From 6d0c7713cdd4f00fcc8a097a41440e1a898e41f3 Mon Sep 17 00:00:00 2001 From: Rob Tillaart Date: Tue, 12 Dec 2023 19:47:21 +0100 Subject: [PATCH] 0.2.2 MINMAX --- libraries/MINMAX/CHANGELOG.md | 6 ++- libraries/MINMAX/MINMAX.cpp | 36 +++++++++++---- libraries/MINMAX/MINMAX.h | 10 +++- libraries/MINMAX/README.md | 39 ++++++++++++++-- .../minmax_setResetDefaults.ino | 46 +++++++++++++++++++ libraries/MINMAX/keywords.txt | 1 + libraries/MINMAX/library.json | 2 +- libraries/MINMAX/library.properties | 2 +- 8 files changed, 125 insertions(+), 17 deletions(-) create mode 100644 libraries/MINMAX/examples/minmax_setResetDefaults/minmax_setResetDefaults.ino diff --git a/libraries/MINMAX/CHANGELOG.md b/libraries/MINMAX/CHANGELOG.md index b8fbcdd6..10c2f680 100644 --- a/libraries/MINMAX/CHANGELOG.md +++ b/libraries/MINMAX/CHANGELOG.md @@ -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() diff --git a/libraries/MINMAX/MINMAX.cpp b/libraries/MINMAX/MINMAX.cpp index 52beca01..16dc82b1 100644 --- a/libraries/MINMAX/MINMAX.cpp +++ b/libraries/MINMAX/MINMAX.cpp @@ -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() { + _resetCount = 0; + _minimumDefault = 0; + _maximumDefault = 0; + _callback = NULL; reset(); - _resetCount = 0; - _callback = NULL; } @@ -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; diff --git a/libraries/MINMAX/MINMAX.h b/libraries/MINMAX/MINMAX.h index 2d105e0e..8298b385 100644 --- a/libraries/MINMAX/MINMAX.h +++ b/libraries/MINMAX/MINMAX.h @@ -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); diff --git a/libraries/MINMAX/README.md b/libraries/MINMAX/README.md index a3aeda73..358dc2da 100644 --- a/libraries/MINMAX/README.md +++ b/libraries/MINMAX/README.md @@ -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; diff --git a/libraries/MINMAX/examples/minmax_setResetDefaults/minmax_setResetDefaults.ino b/libraries/MINMAX/examples/minmax_setResetDefaults/minmax_setResetDefaults.ino new file mode 100644 index 00000000..cf228d19 --- /dev/null +++ b/libraries/MINMAX/examples/minmax_setResetDefaults/minmax_setResetDefaults.ino @@ -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 -- diff --git a/libraries/MINMAX/keywords.txt b/libraries/MINMAX/keywords.txt index ffa61890..aa0a71ea 100644 --- a/libraries/MINMAX/keywords.txt +++ b/libraries/MINMAX/keywords.txt @@ -6,6 +6,7 @@ MINMAX KEYWORD1 # Methods and Functions (KEYWORD2) add KEYWORD2 reset KEYWORD2 +setResetDefaults KEYWORD2 addCallBack KEYWORD2 diff --git a/libraries/MINMAX/library.json b/libraries/MINMAX/library.json index 187c5179..0d403ae2 100644 --- a/libraries/MINMAX/library.json +++ b/libraries/MINMAX/library.json @@ -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": "*", diff --git a/libraries/MINMAX/library.properties b/libraries/MINMAX/library.properties index e3cec45a..135c5ad8 100644 --- a/libraries/MINMAX/library.properties +++ b/libraries/MINMAX/library.properties @@ -1,5 +1,5 @@ name=MINMAX -version=0.2.1 +version=0.2.2 author=Rob Tillaart maintainer=Rob Tillaart sentence=MINMAX library for Arduino.