Skip to content

Commit 6d0c771

Browse files
committed
0.2.2 MINMAX
1 parent dd21cb1 commit 6d0c771

File tree

8 files changed

+125
-17
lines changed

8 files changed

+125
-17
lines changed

libraries/MINMAX/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,15 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.2.2] - 2023-12-12
10+
- add **void setResetDefaults(float minimum, float maximum)**
11+
- update readme
12+
13+
914
## [0.2.1] - 2023-11-14
1015
- update readme.md
1116
- update examples
1217

13-
1418
## [0.2.0] - 2022-12-07
1519
- fix #6 split in .cpp and .h
1620
- remove obsolete autoReset()

libraries/MINMAX/MINMAX.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: MINMAX.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.2.1
4+
// VERSION: 0.2.2
55
// DATE: 2021-10-14
66
// PURPOSE: MINMAX library - simple peak finder
77
// URL: https://github.com/RobTillaart/MINMAX
@@ -12,9 +12,11 @@
1212

1313
MINMAX::MINMAX()
1414
{
15+
_resetCount = 0;
16+
_minimumDefault = 0;
17+
_maximumDefault = 0;
18+
_callback = NULL;
1519
reset();
16-
_resetCount = 0;
17-
_callback = NULL;
1820
}
1921

2022

@@ -27,35 +29,53 @@ uint8_t MINMAX::add(const float value)
2729
reset();
2830
rv |= MINMAX_RESET_DONE;
2931
}
30-
if ((value < _minimum) || (_count == 0))
32+
// new run and range not adjusted by setResetDefaults()
33+
if ((_count == 0) && (_minimum == 0) && (_maximum == 0))
34+
{
35+
_minimum = _maximum = value;
36+
_lastMin = _lastMax = millis();
37+
rv |= MINMAX_MIN_CHANGED | MINMAX_MAX_CHANGED;
38+
}
39+
if (value < _minimum)
3140
{
3241
_minimum = value;
3342
_lastMin = millis();
3443
rv |= MINMAX_MIN_CHANGED;
3544
}
36-
if ((value > _maximum) || (_count == 0))
45+
if (value > _maximum)
3746
{
3847
_maximum = value;
3948
_lastMax = millis();
4049
rv |= MINMAX_MAX_CHANGED;
4150
}
4251
_count++;
43-
if ((rv != MINMAX_NO_CHANGE) && (_callback != NULL)) _callback();
52+
if ((rv != MINMAX_NO_CHANGE) && (_callback != NULL))
53+
{
54+
_callback();
55+
}
4456
return rv;
4557
}
4658

4759

4860
void MINMAX::reset()
4961
{
5062
_lastValue = 0;
51-
_minimum = 0;
52-
_maximum = 0;
63+
_minimum = _minimumDefault;
64+
_maximum = _maximumDefault;
5365
_count = 0;
5466
_lastMin = 0;
5567
_lastMax = 0;
5668
}
5769

5870

71+
void MINMAX::setResetDefaults(float minimum, float maximum)
72+
{
73+
_minimumDefault = minimum;
74+
_maximumDefault = maximum;
75+
}
76+
77+
78+
5979
void MINMAX::addCallBack( void (* func)(void) )
6080
{
6181
_callback = func;

libraries/MINMAX/MINMAX.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
//
33
// FILE: MINMAX.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.2.1
5+
// VERSION: 0.2.2
66
// DATE: 2021-10-14
77
// PURPOSE: MINMAX library - simple peak finder
88
// URL: https://github.com/RobTillaart/MINMAX
99

1010

1111
#include "Arduino.h"
1212

13-
#define MINMAX_LIB_VERSION (F("0.2.1"))
13+
#define MINMAX_LIB_VERSION (F("0.2.2"))
1414

1515
#define MINMAX_NO_CHANGE 0X00
1616
#define MINMAX_MIN_CHANGED 0X01
@@ -26,6 +26,8 @@ class MINMAX
2626

2727
uint8_t add(const float value);
2828
void reset();
29+
// the reset defaults are both zero.
30+
void setResetDefaults(float minimum, float maximum);
2931

3032
// call back function when there is a change
3133
void addCallBack( void (* func)(void) );
@@ -52,8 +54,12 @@ class MINMAX
5254

5355
private:
5456
float _lastValue;
57+
5558
float _minimum;
5659
float _maximum;
60+
float _minimumDefault;
61+
float _maximumDefault;
62+
5763
uint32_t _count;
5864
uint32_t _resetCount;
5965
void (* _callback)(void);

libraries/MINMAX/README.md

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Finally the library keeps track when the last peaks occurred.
4444
- **MINMAX()** Constructor,
4545
- **uint8_t add(float value)** add next value. Returns status (bit flags), see table below.
4646
- **void reset()** resets the minimum and maximum to 0.
47+
- **void setResetDefaults(float minimum, float maximum)** sets the default values for minimum and maximum defining an initial range / window.
4748
- **float minimum()** returns last minimum. Can be higher than previous call due to **reset()** or **autoReset()**.
4849
If no call to **add()** is made yet it will return 0.
4950
- **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.
8889
See examples.
8990

9091

92+
#### setResetDefaults()
93+
94+
- **void setResetDefaults(minimum, maximum)** sets the default values for minimum and maximum defining an initial range / window when **reset()** is called.
95+
This will reduce an abundance of new min/max values in the first part of a stream, possibly causing unneeded call back calls.
96+
97+
The constructor sets both to zero (0) by default. The user can now override these values.
98+
There are no default values for the parameters in the function.
99+
The user is responsible and even allowed to set minimum to be larger than maximum.
100+
The new values become active after the call to **reset()**.
101+
102+
The function does not change or reset the **lastMin()** and **lastMax()** timestamps.
103+
Only after **reset()** these are set to 0 again.
104+
105+
Note that with an initial range set, the **lastMin()** and **lastMax()** timestamps
106+
may be zero for quite a while.
107+
108+
Typical code snippet
109+
110+
```cpp
111+
mm.setResetDefaults(-10, 20); // arbitrary values
112+
mm.reset(); // activate them
113+
```
114+
115+
91116
## Obsolete
92117

93118
- **void autoReset(uint32_t count)** obsolete since 0.2.0
@@ -107,8 +132,9 @@ The examples show the basic working of the functions.
107132

108133
#### Should
109134

110-
- separate call back for MINMAX_MIN_CHANGED and MINMAX_MAX_CHANGED
111-
- add getLastEvent()?
135+
- consider an (featured) extended class and a (simple) base class.
136+
- separate call back for **MINMAX_MIN_CHANGED** and **MINMAX_MAX_CHANGED**
137+
- add **getLastEvent()**
112138
- add AVG **average()** **sum()**
113139
- like a digital multimeter (DMM)
114140
- **sum()** would be sufficient as average can be derived.
@@ -117,11 +143,16 @@ The examples show the basic working of the functions.
117143

118144
- Template class to allow other types
119145
- int32_t uint64_t double etc.
120-
- now you loose precision
146+
- now you loose precision.
147+
- a related class might be **WINDOW(min, max)** that counts and call backs if
148+
a value is out of a predefined range.
149+
- **void setResetDefaults(minimum, maximum, bool adjust = true)** add an adjust flag
150+
that allows / reject adaption of min / max. (extended class).
151+
- define MINMAX_MIN_CHANGED => MINMAX_MIN_EXCEEDED (new or reuse?)
121152

122153
#### Wont (unless)
123154

124-
- thresholds, windowing + triggers (separate class?)
155+
- thresholds, windowing + triggers (separate class!)
125156
- auto-reset after time? (would affect all functions ?)
126157
- need a uint32_t start;
127158
- need a uint32_t threshold;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// FILE: minmax_setResetDefaults.ino
3+
// AUTHOR: Rob Tillaart
4+
// PURPOSE: demo
5+
// URL: https://github.com/RobTillaart/minmax
6+
7+
8+
#include "MINMAX.h"
9+
10+
11+
MINMAX mm;
12+
13+
uint32_t start, stop;
14+
15+
16+
void setup()
17+
{
18+
Serial.begin(115200);
19+
Serial.println(__FILE__);
20+
Serial.print("MINMAX_LIB_VERSION: ");
21+
Serial.println(MINMAX_LIB_VERSION);
22+
Serial.println();
23+
24+
mm.setResetDefaults(-100, 100);
25+
mm.reset();
26+
mm.setAutoResetCount(10000);
27+
}
28+
29+
30+
void loop()
31+
{
32+
int r = random(220) - 110;
33+
34+
if (mm.add(r) != MINMAX_NO_CHANGE) // changed minimum or maximum or reset
35+
{
36+
Serial.print(mm.count());
37+
Serial.print("\t");
38+
Serial.print(mm.minimum());
39+
Serial.print("\t");
40+
Serial.print(mm.maximum());
41+
Serial.print("\n");
42+
}
43+
}
44+
45+
46+
// -- END OF FILE --

libraries/MINMAX/keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ MINMAX KEYWORD1
66
# Methods and Functions (KEYWORD2)
77
add KEYWORD2
88
reset KEYWORD2
9+
setResetDefaults KEYWORD2
910

1011
addCallBack KEYWORD2
1112

libraries/MINMAX/library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"type": "git",
1616
"url": "https://github.com/RobTillaart/MINMAX.git"
1717
},
18-
"version": "0.2.1",
18+
"version": "0.2.2",
1919
"license": "MIT",
2020
"frameworks": "*",
2121
"platforms": "*",

libraries/MINMAX/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=MINMAX
2-
version=0.2.1
2+
version=0.2.2
33
author=Rob Tillaart <[email protected]>
44
maintainer=Rob Tillaart <[email protected]>
55
sentence=MINMAX library for Arduino.

0 commit comments

Comments
 (0)