Merge pull request #14350 from s-hadinger/berry_fast_loop

Berry fast loop
This commit is contained in:
s-hadinger 2022-01-09 19:41:58 +01:00 committed by GitHub
commit e5b4bd07b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 1191 additions and 1028 deletions

View File

@ -186,6 +186,7 @@ extern const bcstring be_const_str__end_transmission;
extern const bcstring be_const_str__energy;
extern const bcstring be_const_str__error;
extern const bcstring be_const_str__filename;
extern const bcstring be_const_str__fl;
extern const bcstring be_const_str__global_addr;
extern const bcstring be_const_str__global_def;
extern const bcstring be_const_str__lvgl;
@ -207,6 +208,7 @@ extern const bcstring be_const_str_add;
extern const bcstring be_const_str_add_anim;
extern const bcstring be_const_str_add_cmd;
extern const bcstring be_const_str_add_driver;
extern const bcstring be_const_str_add_fast_loop;
extern const bcstring be_const_str_add_handler;
extern const bcstring be_const_str_add_header;
extern const bcstring be_const_str_add_rule;
@ -221,6 +223,7 @@ extern const bcstring be_const_str_arg;
extern const bcstring be_const_str_arg_X20must_X20be_X20a_X20subclass_X20of_X20lv_obj;
extern const bcstring be_const_str_arg_name;
extern const bcstring be_const_str_arg_size;
extern const bcstring be_const_str_argument_X20must_X20be_X20a_X20function;
extern const bcstring be_const_str_argument_X20must_X20be_X20a_X20list;
extern const bcstring be_const_str_as;
extern const bcstring be_const_str_asin;
@ -362,6 +365,8 @@ extern const bcstring be_const_str_exists;
extern const bcstring be_const_str_exp;
extern const bcstring be_const_str_f;
extern const bcstring be_const_str_false;
extern const bcstring be_const_str_fast_loop;
extern const bcstring be_const_str_fast_loop_enabled;
extern const bcstring be_const_str_file;
extern const bcstring be_const_str_file_X20extension_X20is_X20not_X20_X27_X2Ebe_X27_X20or_X20_X27_X2Ebec_X27;
extern const bcstring be_const_str_files;

File diff suppressed because it is too large Load Diff

View File

@ -1,93 +1,96 @@
#include "be_constobj.h"
static be_define_const_map_slots(be_class_tasmota_map) {
{ be_const_key(run_deferred, -1), be_const_closure(Tasmota_run_deferred_closure) },
{ be_const_key(publish, -1), be_const_func(l_publish) },
{ be_const_key(try_rule, 5), be_const_closure(Tasmota_try_rule_closure) },
{ be_const_key(_settings_ptr, -1), be_const_comptr(&Settings) },
{ be_const_key(get_free_heap, 47), be_const_func(l_getFreeHeap) },
{ be_const_key(eth, 56), be_const_func(l_eth) },
{ be_const_key(set_power, -1), be_const_func(l_setpower) },
{ be_const_key(exec_rules, -1), be_const_closure(Tasmota_exec_rules_closure) },
{ be_const_key(i2c_enabled, -1), be_const_func(l_i2cenabled) },
{ be_const_key(chars_in_string, 55), be_const_closure(Tasmota_chars_in_string_closure) },
{ be_const_key(hs2rgb, -1), be_const_closure(Tasmota_hs2rgb_closure) },
{ be_const_key(time_str, 0), be_const_closure(Tasmota_time_str_closure) },
{ be_const_key(set_light, -1), be_const_closure(Tasmota_set_light_closure) },
{ be_const_key(response_append, 1), be_const_func(l_respAppend) },
{ be_const_key(gen_cb, -1), be_const_closure(Tasmota_gen_cb_closure) },
{ be_const_key(remove_driver, 50), be_const_closure(Tasmota_remove_driver_closure) },
{ be_const_key(event, -1), be_const_closure(Tasmota_event_closure) },
{ be_const_key(exec_cmd, 2), be_const_closure(Tasmota_exec_cmd_closure) },
{ be_const_key(rtc, 12), be_const_func(l_rtc) },
{ be_const_key(read_sensors, -1), be_const_func(l_read_sensors) },
{ be_const_key(resp_cmnd_str, -1), be_const_func(l_respCmndStr) },
{ be_const_key(set_timer, -1), be_const_closure(Tasmota_set_timer_closure) },
{ be_const_key(resp_cmnd_error, -1), be_const_func(l_respCmndError) },
{ be_const_key(exec_tele, -1), be_const_closure(Tasmota_exec_tele_closure) },
{ be_const_key(_global_def, -1), be_const_comptr(&be_tasmota_global_struct) },
{ be_const_key(cmd_res, -1), be_const_var(0) },
{ be_const_key(_global_addr, -1), be_const_comptr(&TasmotaGlobal) },
{ be_const_key(add_driver, 14), be_const_closure(Tasmota_add_driver_closure) },
{ be_const_key(_timers, -1), be_const_var(1) },
{ be_const_key(add_rule, -1), be_const_closure(Tasmota_add_rule_closure) },
{ be_const_key(resp_cmnd_failed, -1), be_const_func(l_respCmndFailed) },
{ be_const_key(remove_rule, 9), be_const_closure(Tasmota_remove_rule_closure) },
{ be_const_key(web_send, 16), be_const_func(l_webSend) },
{ be_const_key(resp_cmnd, -1), be_const_func(l_respCmnd) },
{ be_const_key(remove_timer, 10), be_const_closure(Tasmota_remove_timer_closure) },
{ be_const_key(memory, 52), be_const_func(l_memory) },
{ be_const_key(global, 71), be_const_var(2) },
{ be_const_key(find_op, -1), be_const_closure(Tasmota_find_op_closure) },
{ be_const_key(yield, 60), be_const_func(l_yield) },
{ be_const_key(resolvecmnd, -1), be_const_func(l_resolveCmnd) },
{ be_const_key(get_option, -1), be_const_func(l_getoption) },
{ be_const_key(kv, 43), be_const_closure(Tasmota_kv_closure) },
{ be_const_key(wire_scan, -1), be_const_closure(Tasmota_wire_scan_closure) },
{ be_const_key(wifi, 37), be_const_func(l_wifi) },
{ be_const_key(_cmd, -1), be_const_func(l_cmd) },
{ be_const_key(gc, -1), be_const_closure(Tasmota_gc_closure) },
{ be_const_key(get_power, -1), be_const_func(l_getpower) },
{ be_const_key(get_light, -1), be_const_closure(Tasmota_get_light_closure) },
{ be_const_key(time_dump, -1), be_const_func(l_time_dump) },
{ be_const_key(settings, -1), be_const_var(3) },
{ be_const_key(cmd, 62), be_const_closure(Tasmota_cmd_closure) },
{ be_const_key(read_sensors, 20), be_const_func(l_read_sensors) },
{ be_const_key(response_append, 50), be_const_func(l_respAppend) },
{ be_const_key(try_rule, 74), be_const_closure(Tasmota_try_rule_closure) },
{ be_const_key(eth, -1), be_const_func(l_eth) },
{ be_const_key(cmd, -1), be_const_closure(Tasmota_cmd_closure) },
{ be_const_key(find_key_i, -1), be_const_closure(Tasmota_find_key_i_closure) },
{ be_const_key(rtc, -1), be_const_func(l_rtc) },
{ be_const_key(remove_driver, 64), be_const_closure(Tasmota_remove_driver_closure) },
{ be_const_key(load, -1), be_const_closure(Tasmota_load_closure) },
{ be_const_key(strptime, -1), be_const_func(l_strptime) },
{ be_const_key(_ccmd, -1), be_const_var(4) },
{ be_const_key(find_key_i, 25), be_const_closure(Tasmota_find_key_i_closure) },
{ be_const_key(delay, -1), be_const_func(l_delay) },
{ be_const_key(time_reached, -1), be_const_func(l_timereached) },
{ be_const_key(_settings_def, -1), be_const_comptr(&be_tasmota_settings_struct) },
{ be_const_key(save, 36), be_const_func(l_save) },
{ be_const_key(millis, -1), be_const_func(l_millis) },
{ be_const_key(wire2, -1), be_const_var(5) },
{ be_const_key(wire1, 72), be_const_var(6) },
{ be_const_key(web_send_decimal, -1), be_const_func(l_webSendDecimal) },
{ be_const_key(scale_uint, 51), be_const_func(l_scaleuint) },
{ be_const_key(get_switch, -1), be_const_func(l_getswitch) },
{ be_const_key(_debug_present, -1), be_const_var(7) },
{ be_const_key(publish_result, -1), be_const_func(l_publish_result) },
{ be_const_key(_drivers, -1), be_const_var(8) },
{ be_const_key(_settings_ptr, -1), be_const_comptr(&Settings) },
{ be_const_key(set_light, -1), be_const_closure(Tasmota_set_light_closure) },
{ be_const_key(_fl, 34), be_const_var(0) },
{ be_const_key(set_power, -1), be_const_func(l_setpower) },
{ be_const_key(global, 73), be_const_var(1) },
{ be_const_key(cmd_res, -1), be_const_var(2) },
{ be_const_key(find_op, 7), be_const_closure(Tasmota_find_op_closure) },
{ be_const_key(get_power, -1), be_const_func(l_getpower) },
{ be_const_key(add_rule, -1), be_const_closure(Tasmota_add_rule_closure) },
{ be_const_key(fast_loop, -1), be_const_closure(Tasmota_fast_loop_closure) },
{ be_const_key(exec_tele, 71), be_const_closure(Tasmota_exec_tele_closure) },
{ be_const_key(i2c_enabled, -1), be_const_func(l_i2cenabled) },
{ be_const_key(remove_cmd, 10), be_const_closure(Tasmota_remove_cmd_closure) },
{ be_const_key(millis, 18), be_const_func(l_millis) },
{ be_const_key(publish, 28), be_const_func(l_publish) },
{ be_const_key(_drivers, -1), be_const_var(3) },
{ be_const_key(resp_cmnd, -1), be_const_func(l_respCmnd) },
{ be_const_key(time_dump, -1), be_const_func(l_time_dump) },
{ be_const_key(delay, 61), be_const_func(l_delay) },
{ be_const_key(hs2rgb, -1), be_const_closure(Tasmota_hs2rgb_closure) },
{ be_const_key(memory, -1), be_const_func(l_memory) },
{ be_const_key(gen_cb, 0), be_const_closure(Tasmota_gen_cb_closure) },
{ be_const_key(add_cmd, -1), be_const_closure(Tasmota_add_cmd_closure) },
{ be_const_key(strftime, -1), be_const_func(l_strftime) },
{ be_const_key(log, -1), be_const_func(l_logInfo) },
{ be_const_key(arch, -1), be_const_func(l_arch) },
{ be_const_key(resp_cmnd_done, 74), be_const_func(l_respCmndDone) },
{ be_const_key(_rules, 38), be_const_var(9) },
{ be_const_key(remove_cmd, -1), be_const_closure(Tasmota_remove_cmd_closure) },
{ be_const_key(get_option, -1), be_const_func(l_getoption) },
{ be_const_key(chars_in_string, 60), be_const_closure(Tasmota_chars_in_string_closure) },
{ be_const_key(init, -1), be_const_closure(Tasmota_init_closure) },
{ be_const_key(wd, 53), be_const_var(10) },
{ be_const_key(_timers, -1), be_const_var(4) },
{ be_const_key(_global_addr, -1), be_const_comptr(&TasmotaGlobal) },
{ be_const_key(wd, -1), be_const_var(5) },
{ be_const_key(exec_cmd, -1), be_const_closure(Tasmota_exec_cmd_closure) },
{ be_const_key(wire_scan, -1), be_const_closure(Tasmota_wire_scan_closure) },
{ be_const_key(_global_def, 58), be_const_comptr(&be_tasmota_global_struct) },
{ be_const_key(resp_cmnd_failed, 15), be_const_func(l_respCmndFailed) },
{ be_const_key(set_timer, 47), be_const_closure(Tasmota_set_timer_closure) },
{ be_const_key(web_send_decimal, -1), be_const_func(l_webSendDecimal) },
{ be_const_key(_debug_present, 3), be_const_var(6) },
{ be_const_key(settings, -1), be_const_var(7) },
{ be_const_key(_ccmd, -1), be_const_var(8) },
{ be_const_key(remove_rule, -1), be_const_closure(Tasmota_remove_rule_closure) },
{ be_const_key(exec_rules, 69), be_const_closure(Tasmota_exec_rules_closure) },
{ be_const_key(strftime, -1), be_const_func(l_strftime) },
{ be_const_key(add_driver, 2), be_const_closure(Tasmota_add_driver_closure) },
{ be_const_key(kv, 49), be_const_closure(Tasmota_kv_closure) },
{ be_const_key(time_reached, 43), be_const_func(l_timereached) },
{ be_const_key(scale_uint, -1), be_const_func(l_scaleuint) },
{ be_const_key(remove_timer, -1), be_const_closure(Tasmota_remove_timer_closure) },
{ be_const_key(run_deferred, 46), be_const_closure(Tasmota_run_deferred_closure) },
{ be_const_key(arch, -1), be_const_func(l_arch) },
{ be_const_key(strptime, -1), be_const_func(l_strptime) },
{ be_const_key(wire1, 5), be_const_var(9) },
{ be_const_key(time_str, -1), be_const_closure(Tasmota_time_str_closure) },
{ be_const_key(wifi, -1), be_const_func(l_wifi) },
{ be_const_key(_cmd, 19), be_const_func(l_cmd) },
{ be_const_key(resp_cmnd_error, -1), be_const_func(l_respCmndError) },
{ be_const_key(web_send, -1), be_const_func(l_webSend) },
{ be_const_key(_rules, -1), be_const_var(10) },
{ be_const_key(add_fast_loop, -1), be_const_closure(Tasmota_add_fast_loop_closure) },
{ be_const_key(_settings_def, -1), be_const_comptr(&be_tasmota_settings_struct) },
{ be_const_key(get_switch, -1), be_const_func(l_getswitch) },
{ be_const_key(publish_result, -1), be_const_func(l_publish_result) },
{ be_const_key(resp_cmnd_str, -1), be_const_func(l_respCmndStr) },
{ be_const_key(log, 37), be_const_func(l_logInfo) },
{ be_const_key(save, 11), be_const_func(l_save) },
{ be_const_key(yield, -1), be_const_func(l_yield) },
{ be_const_key(resolvecmnd, -1), be_const_func(l_resolveCmnd) },
{ be_const_key(resp_cmnd_done, -1), be_const_func(l_respCmndDone) },
{ be_const_key(get_light, 4), be_const_closure(Tasmota_get_light_closure) },
{ be_const_key(get_free_heap, -1), be_const_func(l_getFreeHeap) },
{ be_const_key(wire2, -1), be_const_var(11) },
{ be_const_key(event, 32), be_const_closure(Tasmota_event_closure) },
};
static be_define_const_map(
be_class_tasmota_map,
77
80
);
BE_EXPORT_VARIABLE be_define_const_class(
be_class_tasmota,
11,
12,
NULL,
Tasmota
);

View File

@ -631,6 +631,95 @@ be_local_closure(Tasmota_remove_driver, /* name */
);
/*******************************************************************/
/********************************************************************
** Solidified function: fast_loop
********************************************************************/
be_local_closure(Tasmota_fast_loop, /* name */
be_nested_proto(
5, /* nstack */
1, /* argc */
0, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
1, /* has constants */
( &(const bvalue[ 3]) { /* constants */
/* K0 */ be_nested_str(_fl),
/* K1 */ be_const_int(0),
/* K2 */ be_const_int(1),
}),
&be_const_str_fast_loop,
&be_const_str_solidified,
( &(const binstruction[15]) { /* code */
0x88040100, // 0000 GETMBR R1 R0 K0
0x5C080200, // 0001 MOVE R2 R1
0x740A0000, // 0002 JMPT R2 #0004
0x80000400, // 0003 RET 0
0x58080001, // 0004 LDCONST R2 K1
0x600C000C, // 0005 GETGBL R3 G12
0x5C100200, // 0006 MOVE R4 R1
0x7C0C0200, // 0007 CALL R3 1
0x140C0403, // 0008 LT R3 R2 R3
0x780E0003, // 0009 JMPF R3 #000E
0x940C0202, // 000A GETIDX R3 R1 R2
0x7C0C0000, // 000B CALL R3 0
0x00080502, // 000C ADD R2 R2 K2
0x7001FFF6, // 000D JMP #0005
0x80000000, // 000E RET 0
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: add_fast_loop
********************************************************************/
be_local_closure(Tasmota_add_fast_loop, /* name */
be_nested_proto(
5, /* nstack */
2, /* argc */
0, /* varg */
0, /* has upvals */
NULL, /* no upvals */
0, /* has sup protos */
NULL, /* no sub protos */
1, /* has constants */
( &(const bvalue[ 8]) { /* constants */
/* K0 */ be_nested_str(_fl),
/* K1 */ be_nested_str(function),
/* K2 */ be_nested_str(value_error),
/* K3 */ be_nested_str(argument_X20must_X20be_X20a_X20function),
/* K4 */ be_nested_str(global),
/* K5 */ be_nested_str(fast_loop_enabled),
/* K6 */ be_const_int(1),
/* K7 */ be_nested_str(push),
}),
&be_const_str_add_fast_loop,
&be_const_str_solidified,
( &(const binstruction[18]) { /* code */
0x88080100, // 0000 GETMBR R2 R0 K0
0x740A0002, // 0001 JMPT R2 #0005
0x60080012, // 0002 GETGBL R2 G18
0x7C080000, // 0003 CALL R2 0
0x90020002, // 0004 SETMBR R0 K0 R2
0x60080004, // 0005 GETGBL R2 G4
0x5C0C0200, // 0006 MOVE R3 R1
0x7C080200, // 0007 CALL R2 1
0x20080501, // 0008 NE R2 R2 K1
0x780A0000, // 0009 JMPF R2 #000B
0xB0060503, // 000A RAISE 1 K2 K3
0x88080104, // 000B GETMBR R2 R0 K4
0x900A0B06, // 000C SETMBR R2 K5 K6
0x88080100, // 000D GETMBR R2 R0 K0
0x8C080507, // 000E GETMET R2 R2 K7
0x5C100200, // 000F MOVE R4 R1
0x7C080400, // 0010 CALL R2 2
0x80000000, // 0011 RET 0
})
)
);
/*******************************************************************/
/********************************************************************
** Solidified function: event
@ -1987,6 +2076,7 @@ void be_load_tasmota_ntvlib(bvm *vm)
/* @const_object_info_begin
class be_class_tasmota (scope: global, name: Tasmota) {
_fl, var
_rules, var
_timers, var
_ccmd, var
@ -2047,6 +2137,8 @@ class be_class_tasmota (scope: global, name: Tasmota) {
i2c_enabled, func(l_i2cenabled)
fast_loop, closure(Tasmota_fast_loop_closure)
add_fast_loop, closure(Tasmota_add_fast_loop_closure)
cmd, closure(Tasmota_cmd_closure)
chars_in_string, closure(Tasmota_chars_in_string_closure)
find_key_i, closure(Tasmota_find_key_i_closure)

View File

@ -17,6 +17,7 @@ end
tasmota = nil
class Tasmota
var _fl # list of fast_loop registered closures
var _rules
var _timers
var _ccmd
@ -426,6 +427,29 @@ class Tasmota
return true
end
# fast_loop() is a trimmed down version of event() called at every Tasmota loop iteration
# it is optimized to be as fast as possible and reduce overhead
# there is no introspect, closures must be registered directly
def fast_loop()
var fl = self._fl
if !fl return end # fast exit if no closure is registered (most common case)
# iterate and call each closure
var i = 0
while i < size(fl)
# note: this is not guarded in try/except for performance reasons. The inner function must not raise exceptions
fl[i]()
i += 1
end
end
def add_fast_loop(cl)
if !self._fl self._fl = [] end
if type(cl) != 'function' raise "value_error", "argument must be a function" end
self.global.fast_loop_enabled = 1 # enable fast_loop at global level: `TasmotaGlobal.fast_loop_enabled = true`
self._fl.push(cl)
end
def event(event_type, cmd, idx, payload, raw)
import introspect
import string

View File

@ -213,6 +213,10 @@ struct TasmotaGlobal_t {
#else
char log_buffer[LOG_BUFFER_SIZE]; // Log buffer in DRAM
#endif // PIO_FRAMEWORK_ARDUINO_MMU_CACHE16_IRAM48_SECHEAP_SHARED
#ifdef USE_BERRY
bool berry_fast_loop_enabled = false; // is Berry fast loop enabled, i.e. control is passed at each loop iteration
#endif // USE_BERRY
} TasmotaGlobal;
TSettings* Settings = nullptr;

View File

@ -33,10 +33,11 @@ extern "C" {
extern const be_ctypes_structure_t be_tasmota_global_struct = {
sizeof(TasmotaGlobal), /* size in bytes */
2, /* number of elements */
3, /* number of elements */
nullptr,
(const be_ctypes_structure_item_t[2]) {
(const be_ctypes_structure_item_t[3]) {
{ "devices_present", offsetof(TasmotaGlobal_t, devices_present), 0, 0, ctypes_u8, 0 },
{ "fast_loop_enabled", offsetof(TasmotaGlobal_t, berry_fast_loop_enabled), 0, 0, ctypes_u8, 0 },
{ "sleep", offsetof(TasmotaGlobal_t, sleep), 0, 0, ctypes_u8, 0 },
}};

View File

@ -199,6 +199,29 @@ int32_t callBerryEventDispatcher(const char *type, const char *cmd, int32_t idx,
return ret;
}
// Simplified version of event loop. Just call `tasmota.fast_loop()`
void callBerryFastLoop(void) {
bvm *vm = berry.vm;
if (nullptr == vm) { return; }
if (be_getglobal(vm, "tasmota")) {
if (be_getmethod(vm, -1, "fast_loop")) {
be_pushvalue(vm, -2); // add instance as first arg
BrTimeoutStart();
int32_t ret = be_pcall(vm, 1);
if (ret != 0) {
be_error_pop_all(berry.vm); // clear Berry stack
}
BrTimeoutReset();
be_pop(vm, 1);
}
be_pop(vm, 1); // remove method
}
be_pop(vm, 1); // remove instance object
be_pop(vm, be_top(vm)); // clean
}
/*********************************************************************************************\
* VM Observability
\*********************************************************************************************/
@ -751,6 +774,9 @@ bool Xdrv52(uint8_t function)
BrLoad("autoexec.be"); // run autoexec.be at first tick, so we know all modules are initialized
berry.autoexec_done = true;
}
if (TasmotaGlobal.berry_fast_loop_enabled) { // call only if enabled at global level
callBerryFastLoop(); // call `tasmota.fast_loop()` optimized for minimal performance impact
}
break;
// Berry wide commands and events