mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
0.2.2 MINMAX
This commit is contained in:
parent
dd21cb1907
commit
6d0c7713cd
@ -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/).
|
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
|
## [0.2.1] - 2023-11-14
|
||||||
- update readme.md
|
- update readme.md
|
||||||
- update examples
|
- update examples
|
||||||
|
|
||||||
|
|
||||||
## [0.2.0] - 2022-12-07
|
## [0.2.0] - 2022-12-07
|
||||||
- fix #6 split in .cpp and .h
|
- fix #6 split in .cpp and .h
|
||||||
- remove obsolete autoReset()
|
- remove obsolete autoReset()
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//
|
//
|
||||||
// FILE: MINMAX.cpp
|
// FILE: MINMAX.cpp
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.2.1
|
// VERSION: 0.2.2
|
||||||
// DATE: 2021-10-14
|
// DATE: 2021-10-14
|
||||||
// PURPOSE: MINMAX library - simple peak finder
|
// PURPOSE: MINMAX library - simple peak finder
|
||||||
// URL: https://github.com/RobTillaart/MINMAX
|
// URL: https://github.com/RobTillaart/MINMAX
|
||||||
@ -12,9 +12,11 @@
|
|||||||
|
|
||||||
MINMAX::MINMAX()
|
MINMAX::MINMAX()
|
||||||
{
|
{
|
||||||
|
_resetCount = 0;
|
||||||
|
_minimumDefault = 0;
|
||||||
|
_maximumDefault = 0;
|
||||||
|
_callback = NULL;
|
||||||
reset();
|
reset();
|
||||||
_resetCount = 0;
|
|
||||||
_callback = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -27,20 +29,30 @@ uint8_t MINMAX::add(const float value)
|
|||||||
reset();
|
reset();
|
||||||
rv |= MINMAX_RESET_DONE;
|
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;
|
_minimum = value;
|
||||||
_lastMin = millis();
|
_lastMin = millis();
|
||||||
rv |= MINMAX_MIN_CHANGED;
|
rv |= MINMAX_MIN_CHANGED;
|
||||||
}
|
}
|
||||||
if ((value > _maximum) || (_count == 0))
|
if (value > _maximum)
|
||||||
{
|
{
|
||||||
_maximum = value;
|
_maximum = value;
|
||||||
_lastMax = millis();
|
_lastMax = millis();
|
||||||
rv |= MINMAX_MAX_CHANGED;
|
rv |= MINMAX_MAX_CHANGED;
|
||||||
}
|
}
|
||||||
_count++;
|
_count++;
|
||||||
if ((rv != MINMAX_NO_CHANGE) && (_callback != NULL)) _callback();
|
if ((rv != MINMAX_NO_CHANGE) && (_callback != NULL))
|
||||||
|
{
|
||||||
|
_callback();
|
||||||
|
}
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,14 +60,22 @@ uint8_t MINMAX::add(const float value)
|
|||||||
void MINMAX::reset()
|
void MINMAX::reset()
|
||||||
{
|
{
|
||||||
_lastValue = 0;
|
_lastValue = 0;
|
||||||
_minimum = 0;
|
_minimum = _minimumDefault;
|
||||||
_maximum = 0;
|
_maximum = _maximumDefault;
|
||||||
_count = 0;
|
_count = 0;
|
||||||
_lastMin = 0;
|
_lastMin = 0;
|
||||||
_lastMax = 0;
|
_lastMax = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void MINMAX::setResetDefaults(float minimum, float maximum)
|
||||||
|
{
|
||||||
|
_minimumDefault = minimum;
|
||||||
|
_maximumDefault = maximum;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void MINMAX::addCallBack( void (* func)(void) )
|
void MINMAX::addCallBack( void (* func)(void) )
|
||||||
{
|
{
|
||||||
_callback = func;
|
_callback = func;
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
//
|
//
|
||||||
// FILE: MINMAX.h
|
// FILE: MINMAX.h
|
||||||
// AUTHOR: Rob Tillaart
|
// AUTHOR: Rob Tillaart
|
||||||
// VERSION: 0.2.1
|
// VERSION: 0.2.2
|
||||||
// DATE: 2021-10-14
|
// DATE: 2021-10-14
|
||||||
// PURPOSE: MINMAX library - simple peak finder
|
// PURPOSE: MINMAX library - simple peak finder
|
||||||
// URL: https://github.com/RobTillaart/MINMAX
|
// URL: https://github.com/RobTillaart/MINMAX
|
||||||
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
#include "Arduino.h"
|
#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_NO_CHANGE 0X00
|
||||||
#define MINMAX_MIN_CHANGED 0X01
|
#define MINMAX_MIN_CHANGED 0X01
|
||||||
@ -26,6 +26,8 @@ public:
|
|||||||
|
|
||||||
uint8_t add(const float value);
|
uint8_t add(const float value);
|
||||||
void reset();
|
void reset();
|
||||||
|
// the reset defaults are both zero.
|
||||||
|
void setResetDefaults(float minimum, float maximum);
|
||||||
|
|
||||||
// call back function when there is a change
|
// call back function when there is a change
|
||||||
void addCallBack( void (* func)(void) );
|
void addCallBack( void (* func)(void) );
|
||||||
@ -52,8 +54,12 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
float _lastValue;
|
float _lastValue;
|
||||||
|
|
||||||
float _minimum;
|
float _minimum;
|
||||||
float _maximum;
|
float _maximum;
|
||||||
|
float _minimumDefault;
|
||||||
|
float _maximumDefault;
|
||||||
|
|
||||||
uint32_t _count;
|
uint32_t _count;
|
||||||
uint32_t _resetCount;
|
uint32_t _resetCount;
|
||||||
void (* _callback)(void);
|
void (* _callback)(void);
|
||||||
|
@ -44,6 +44,7 @@ Finally the library keeps track when the last peaks occurred.
|
|||||||
- **MINMAX()** Constructor,
|
- **MINMAX()** Constructor,
|
||||||
- **uint8_t add(float value)** add next value. Returns status (bit flags), see table below.
|
- **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 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()**.
|
- **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.
|
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()**.
|
- **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.
|
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
|
## Obsolete
|
||||||
|
|
||||||
- **void autoReset(uint32_t count)** obsolete since 0.2.0
|
- **void autoReset(uint32_t count)** obsolete since 0.2.0
|
||||||
@ -107,8 +132,9 @@ The examples show the basic working of the functions.
|
|||||||
|
|
||||||
#### Should
|
#### Should
|
||||||
|
|
||||||
- separate call back for MINMAX_MIN_CHANGED and MINMAX_MAX_CHANGED
|
- consider an (featured) extended class and a (simple) base class.
|
||||||
- add getLastEvent()?
|
- separate call back for **MINMAX_MIN_CHANGED** and **MINMAX_MAX_CHANGED**
|
||||||
|
- add **getLastEvent()**
|
||||||
- add AVG **average()** **sum()**
|
- add AVG **average()** **sum()**
|
||||||
- like a digital multimeter (DMM)
|
- like a digital multimeter (DMM)
|
||||||
- **sum()** would be sufficient as average can be derived.
|
- **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
|
- Template class to allow other types
|
||||||
- int32_t uint64_t double etc.
|
- 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)
|
#### Wont (unless)
|
||||||
|
|
||||||
- thresholds, windowing + triggers (separate class?)
|
- thresholds, windowing + triggers (separate class!)
|
||||||
- auto-reset after time? (would affect all functions ?)
|
- auto-reset after time? (would affect all functions ?)
|
||||||
- need a uint32_t start;
|
- need a uint32_t start;
|
||||||
- need a uint32_t threshold;
|
- need a uint32_t threshold;
|
||||||
|
@ -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 --
|
@ -6,6 +6,7 @@ MINMAX KEYWORD1
|
|||||||
# Methods and Functions (KEYWORD2)
|
# Methods and Functions (KEYWORD2)
|
||||||
add KEYWORD2
|
add KEYWORD2
|
||||||
reset KEYWORD2
|
reset KEYWORD2
|
||||||
|
setResetDefaults KEYWORD2
|
||||||
|
|
||||||
addCallBack KEYWORD2
|
addCallBack KEYWORD2
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
"type": "git",
|
"type": "git",
|
||||||
"url": "https://github.com/RobTillaart/MINMAX.git"
|
"url": "https://github.com/RobTillaart/MINMAX.git"
|
||||||
},
|
},
|
||||||
"version": "0.2.1",
|
"version": "0.2.2",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"frameworks": "*",
|
"frameworks": "*",
|
||||||
"platforms": "*",
|
"platforms": "*",
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
name=MINMAX
|
name=MINMAX
|
||||||
version=0.2.1
|
version=0.2.2
|
||||||
author=Rob Tillaart <rob.tillaart@gmail.com>
|
author=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
|
||||||
sentence=MINMAX library for Arduino.
|
sentence=MINMAX library for Arduino.
|
||||||
|
Loading…
Reference in New Issue
Block a user