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

Berry fast_loop is now called every 5ms whatever the Sleep value #19436

Merged
merged 1 commit into from
Sep 2, 2023
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tasmota/my_user_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 16 additions & 2 deletions tasmota/tasmota_xdrv_driver/xdrv_52_9_berry.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand All @@ -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;

Expand Down