diff --git a/CHANGELOG.md b/CHANGELOG.md index 778d93f3abf1..4c00d70aa9bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. ### Breaking Changed ### Changed +- Berry fast_loop is now called every 5ms whatever the Sleep value ### Fixed - PCF8574 mode 1 with base relays exception 3/28 regression from v12.4.0.4 (#19408) diff --git a/tasmota/my_user_config.h b/tasmota/my_user_config.h index 007959931d8b..191df8c9d613 100644 --- a/tasmota/my_user_config.h +++ b/tasmota/my_user_config.h @@ -1128,6 +1128,7 @@ #define USE_BERRY_TIMEOUT 4000 // Timeout in ms, will raise an exception if running time exceeds this timeout #define USE_BERRY_PSRAM // Allocate Berry memory in PSRAM if PSRAM is connected - this might be slightly slower but leaves main memory intact #define USE_BERRY_IRAM // Allocate some data structures in IRAM (which is ususally unused) when possible and if no PSRAM is available + #define USE_BERRY_FAST_LOOP_SLEEP_MS 5 // Minimum time in milliseconds to before calling again `tasmota.fast_loop()`, a smaller value will consume more CPU (min 1ms) // #define USE_BERRY_DEBUG // Compile Berry bytecode with line number information, makes exceptions easier to debug. Adds +8% of memory consumption for compiled code // #define UBE_BERRY_DEBUG_GC // Print low-level GC metrics // #define USE_BERRY_INT64 // Add 64 bits integer support (+1.7KB Flash) diff --git a/tasmota/tasmota_xdrv_driver/xdrv_52_9_berry.ino b/tasmota/tasmota_xdrv_driver/xdrv_52_9_berry.ino index d63e8728c042..e0a75607155c 100644 --- a/tasmota/tasmota_xdrv_driver/xdrv_52_9_berry.ino +++ b/tasmota/tasmota_xdrv_driver/xdrv_52_9_berry.ino @@ -169,11 +169,20 @@ int32_t callBerryEventDispatcher(const char *type, const char *cmd, int32_t idx, } // Simplified version of event loop. Just call `tasmota.fast_loop()` -void callBerryFastLoop(void) { +// `every_5ms` is a flag to wait at least 5ms between calss to `tasmota.fast_loop()` +void callBerryFastLoop(bool every_5ms) { + static uint32_t fast_loop_last_call = 0; bvm *vm = berry.vm; if (nullptr == vm) { return; } + uint32_t now = millis(); + if (every_5ms) { + if (!TimeReached(fast_loop_last_call + USE_BERRY_FAST_LOOP_SLEEP_MS /* 5ms */)) { return; } + } + fast_loop_last_call = now; + + // TODO - can we make this dereferencing once for all? if (be_getglobal(vm, "tasmota")) { if (be_getmethod(vm, -1, "fast_loop")) { be_pushvalue(vm, -2); // add instance as first arg @@ -758,6 +767,11 @@ bool Xdrv52(uint32_t function) bool result = false; switch (function) { + case FUNC_SLEEP_LOOP: + if (TasmotaGlobal.berry_fast_loop_enabled) { // call only if enabled at global level + callBerryFastLoop(true); // call `tasmota.fast_loop()` optimized for minimal performance impact + } + break; case FUNC_LOOP: if (!berry.autoexec_done) { // we generate a synthetic event `autoexec` @@ -779,7 +793,7 @@ bool Xdrv52(uint32_t function) } } if (TasmotaGlobal.berry_fast_loop_enabled) { // call only if enabled at global level - callBerryFastLoop(); // call `tasmota.fast_loop()` optimized for minimal performance impact + callBerryFastLoop(false); // call `tasmota.fast_loop()` optimized for minimal performance impact } break;