Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Functions] Add 'mapc' to constrain values #5203

Merged
merged 5 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion docs/source/Rules/Rules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1425,6 +1425,8 @@ Basic Math Functions
* ``^`` The caret is used as the exponentiation operator for calculating the value of x to the power of y (x\ :sup:`y`).

* ``map(value:fromLow:fromHigh:toLow:toHigh)`` Maps value x in the fromLow/fromHigh range to toLow/toHigh values. Similar to the Arduino map() function. See examples below. (Using a colon as an argument separator to not interfere with regular argument processing)
* ``mapc(value:fromLow:fromHigh:toLow:toHigh)`` same as map, but constrains the result to the fromLow/fromHigh range.


Rules example:

Expand Down Expand Up @@ -1460,7 +1462,7 @@ Called with event ``eventname2=1.234,100``
213379 : Info : ACT : LogEntry,'pow of 1.234^100 = 1353679866.79107'
213382 : Info : pow of 1.234^100 = 1353679866.79107

Examples using the ``map()`` function. Map does not constrain the values within the given range, but uses extrapolation when the input value goes outside the ``fromLow`` / ``fromHigh`` range.
Examples using the ``map()`` & ``mapc()`` function. ``map()`` without the "c" does not constrain the values within the given range, but uses extrapolation when the input value goes outside the ``fromLow`` / ``fromHigh`` range.

Missing values for the map function default to 0.

Expand All @@ -1476,7 +1478,9 @@ Missing values for the map function default to 0.

on eventname3 do
let,1,map(%eventvalue1|10%:0:100:100:0) // Reverse mapping of a value, 0..100 will output 100..0
let,2,mapc(%eventvalue1|10%:0:100:100:0)
LogEntry,'Input value %eventvalue1|10% mapped to: %v1%'
LogEntry,'Input value %eventvalue1|10% mapped to: %v2% and constrained'
endon


Expand Down
14 changes: 11 additions & 3 deletions src/src/Helpers/Rules_calculate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ bool RulesCalculate_t::is_quinary_operator(char c)
{
const UnaryOperator op = static_cast<UnaryOperator>(c);

return op == UnaryOperator::Map;
return op == UnaryOperator::Map || op == UnaryOperator::MapC;
}

CalculateReturnCode RulesCalculate_t::push(ESPEASY_RULES_FLOAT_TYPE value)
Expand Down Expand Up @@ -280,8 +280,13 @@ ESPEASY_RULES_FLOAT_TYPE RulesCalculate_t::apply_quinary_operator(char op,
ESPEASY_RULES_FLOAT_TYPE ret{};
const UnaryOperator qu_op = static_cast<UnaryOperator>(op);

if (UnaryOperator::Map == qu_op) {
return mapADCtoFloat(first, second, third, fourth, fifth);
if (UnaryOperator::Map == qu_op || UnaryOperator::MapC == qu_op) {

// Clamp the result if the operator is MapC
if (qu_op == UnaryOperator::MapC) {
first = constrain(first, min(second, third), max(second, third));;
}
chromoxdor marked this conversation as resolved.
Show resolved Hide resolved
ret = mapADCtoFloat(first, second, third, fourth, fifth);
}
return ret;
}
Expand Down Expand Up @@ -656,6 +661,8 @@ const __FlashStringHelper* toString(UnaryOperator op)
return F("atan_d");
case UnaryOperator::Map:
return F("map");
case UnaryOperator::MapC:
return F("mapc");
}
return F("");
}
Expand Down Expand Up @@ -692,6 +699,7 @@ String RulesCalculate_t::preProces(const String& input)
,UnaryOperator::Tan_d
#endif // if FEATURE_TRIGONOMETRIC_FUNCTIONS_RULES
,UnaryOperator::Map
,UnaryOperator::MapC

};

Expand Down
1 change: 1 addition & 0 deletions src/src/Helpers/Rules_calculate.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ enum class UnaryOperator : uint8_t {
ArcTan, // Arc Tangent (radian)
ArcTan_d, // Arc Tangent (degree)
Map, // Map (value, lowFrom, highFrom, lowTo, highTo) (not really unary...)
MapC, // Map (value, lowFrom, highFrom, lowTo, highTo) and clamp to lowTo/highTo
};

void preProcessReplace(String & input,
Expand Down
2 changes: 1 addition & 1 deletion static/espeasy.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ var pluginDispCmd = [
];
var commonTag = ["On", "Do", "Endon"];
var commonNumber = ["toBin", "toHex", "Constrain", "XOR", "AND:", "OR:", "Ord", "bitRead", "bitSet", "bitClear", "bitWrite", "urlencode"];
var commonMath = ["Log", "Ln", "Abs", "Exp", "Sqrt", "Sq", "Round", "Sin", "Cos", "Tan", "aSin", "aCos", "aTan", "Sin_d", "Cos_d", "Tan_d", "aSin_d", "aCos_d", "aTan_d", "map"];
var commonMath = ["Log", "Ln", "Abs", "Exp", "Sqrt", "Sq", "Round", "Sin", "Cos", "Tan", "aSin", "aCos", "aTan", "Sin_d", "Cos_d", "Tan_d", "aSin_d", "aCos_d", "aTan_d", "map", "mapc"];
var commonWarning = ["delay", "Delay", "ResetFlashWriteCounter"];
var taskSpecifics = [
//Task settings
Expand Down
Loading