mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-09-12 01:24:04 +00:00
[SysVar] Add %c_random%(L,H) system variable
As requested on the [forum](https://www.letscontrolit.com/forum/viewtopic.php?p=73835#p73835)
This commit is contained in:
@@ -362,6 +362,9 @@ The conversion always outputs a string, but not all of these can be converted ba
|
||||
* - Secs to dhms: ``%c_s2dhms%(100000)``
|
||||
- Secs to dhms: ``1d03:46:40``
|
||||
- Seconds to days/hours/minutes/seconds notation
|
||||
* - Random(L,H): ``%c_random%(0, 1)``
|
||||
- Random(L,H): ``0.123``
|
||||
- Generate random number in the given range L ... H (Added: 2025/04/29)
|
||||
* - To HEX: ``%c_2hex%(100000)``
|
||||
- To HEX: ``186A0``
|
||||
- Convert integer value to HEX notation. (Added: 2020/10/07)
|
||||
|
||||
@@ -339,6 +339,7 @@ ESPEASY_RULES_FLOAT_TYPE mapADCtoFloat(ESPEASY_RULES_FLOAT_TYPE float_value,
|
||||
ESPEASY_RULES_FLOAT_TYPE out1,
|
||||
ESPEASY_RULES_FLOAT_TYPE out2)
|
||||
{
|
||||
// FIXME TD-er: When ADC1/2 are equal, shouldn't we just output either out1, out2 or some average of those?
|
||||
if (!approximatelyEqual(adc1, adc2))
|
||||
{
|
||||
const ESPEASY_RULES_FLOAT_TYPE normalized = (float_value - adc1) / (adc2 - adc1);
|
||||
|
||||
@@ -526,6 +526,20 @@ long HwRandom(long howsmall, long howbig) {
|
||||
return HwRandom(diff) + howsmall;
|
||||
}
|
||||
|
||||
ESPEASY_RULES_FLOAT_TYPE HwRandom_f(
|
||||
ESPEASY_RULES_FLOAT_TYPE howsmall,
|
||||
ESPEASY_RULES_FLOAT_TYPE howbig)
|
||||
{
|
||||
if (approximatelyEqual(howsmall, howbig)) {
|
||||
return howsmall;
|
||||
}
|
||||
return mapADCtoFloat(
|
||||
HwRandom(),
|
||||
0, std::numeric_limits<uint32_t>::max(),
|
||||
howsmall, howbig);
|
||||
}
|
||||
|
||||
|
||||
#ifdef ESP8266
|
||||
void readBootCause() {
|
||||
lastBootCause = BOOT_CAUSE_MANUAL_REBOOT;
|
||||
|
||||
@@ -93,6 +93,11 @@ long HwRandom(long howsmall,
|
||||
long howbig);
|
||||
|
||||
|
||||
ESPEASY_RULES_FLOAT_TYPE HwRandom_f(
|
||||
ESPEASY_RULES_FLOAT_TYPE howsmall,
|
||||
ESPEASY_RULES_FLOAT_TYPE howbig);
|
||||
|
||||
|
||||
/********************************************************************************************\
|
||||
Boot information
|
||||
\*********************************************************************************************/
|
||||
|
||||
@@ -1507,6 +1507,12 @@ void parseStandardConversions(String& s, bool useURLencode) {
|
||||
#endif
|
||||
SMART_CONV(F("%c_alt_pres_sea%"), toString(altitudeFromPressure(data.arg1, data.arg2), 2))
|
||||
SMART_CONV(F("%c_sea_pres_alt%"), toString(pressureElevation(data.arg1, data.arg2), 2))
|
||||
|
||||
#if FEATURE_USE_DOUBLE_AS_ESPEASY_RULES_FLOAT_TYPE
|
||||
SMART_CONV(F("%c_random%"), doubleToString(HwRandom_f(data.arg1, data.arg2), 3, true))
|
||||
#else
|
||||
SMART_CONV(F("%c_random%"), floatToString(HwRandom_f(data.arg1, data.arg2), 3, true))
|
||||
#endif
|
||||
#undef SMART_CONV
|
||||
}
|
||||
|
||||
|
||||
@@ -378,6 +378,8 @@ void handle_sysvars() {
|
||||
F("Mins to hcm: %c_m2hcm%(482)"),
|
||||
F("Secs to dhms: %c_s2dhms%(100000)"),
|
||||
|
||||
F("Random: %c_random%(0, 100)"),
|
||||
|
||||
// addFormSeparator(3,
|
||||
F("To HEX: %c_2hex%(100000)"),
|
||||
|
||||
|
||||
+1
-1
@@ -175,7 +175,7 @@ var AnythingElse = [
|
||||
"%ethwifimode%", "%ethconnected%", "%ethduplex%", "%ethspeed%", "%ethstate%", "%ethspeedstate%",
|
||||
//Standard Conversions
|
||||
"%c_w_dir%", "%c_c2f%", "%c_ms2Bft%", "%c_dew_th%", "%c_alt_pres_sea%", "%c_sea_pres_alt%", "%c_cm2imp%", "%c_mm2imp%",
|
||||
"%c_m2day%", "%c_m2dh%", "%c_m2dhm%", "%c_s2dhms%", "%c_2hex%", "%c_u2ip%", "%c_uname%", "%c_uage%", "%c_ubuild%", "%c_ubuildstr%",
|
||||
"%c_m2day%", "%c_m2dh%", "%c_m2dhm%", "%c_s2dhms%", "%c_random%", "%c_2hex%", "%c_u2ip%", "%c_uname%", "%c_uage%", "%c_ubuild%", "%c_ubuildstr%",
|
||||
"%c_uload%", "%c_utype%", "%c_utypestr%",
|
||||
//Variables
|
||||
"var", "int"
|
||||
|
||||
Vendored
+1
-1
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user