Skip to content

Commit

Permalink
Merge pull request letscontrolit#5203 from chromoxdor/mapc
Browse files Browse the repository at this point in the history
[Functions] Add 'mapc' to constrain values
  • Loading branch information
TD-er authored Jan 12, 2025
2 parents acce70e + 2e74037 commit 80bb313
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 6 deletions.
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 @@ -78,7 +78,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 @@ -281,8 +281,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));;
}
ret = mapADCtoFloat(first, second, third, fourth, fifth);
}
return ret;
}
Expand Down Expand Up @@ -709,6 +714,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 @@ -745,6 +752,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

0 comments on commit 80bb313

Please sign in to comment.