Berry provide lightweight options for tasmota.wifi/eth/memory/rtc (#20448)

This commit is contained in:
s-hadinger 2024-01-09 19:49:30 +01:00 committed by GitHub
parent 85fb54fe8d
commit 69d4e323d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 175 additions and 115 deletions

View File

@ -17,6 +17,7 @@ All notable changes to this project will be documented in this file.
- Berry button to dynamically load GPIO Viewer with Berry backend (#20424) - Berry button to dynamically load GPIO Viewer with Berry backend (#20424)
- Berry `debug_panel.tapp` to display real-time heap and wifi rssi - Berry `debug_panel.tapp` to display real-time heap and wifi rssi
- Berry `webserver.header` to read browser sent headers - Berry `webserver.header` to read browser sent headers
- Berry provide lightweight options for `tasmota.wifi/eth/memory/rtc`
### Breaking Changed ### Breaking Changed

View File

@ -8,49 +8,91 @@
/* Insert an nil to a key */ /* Insert an nil to a key */
void be_map_insert_nil(bvm *vm, const char *key) void be_map_insert_nil(bvm *vm, const char *key)
{ {
if (be_ismap(vm, -1)) {
be_pushstring(vm, key); be_pushstring(vm, key);
be_pushnil(vm); be_pushnil(vm);
be_data_insert(vm, -3); be_data_insert(vm, -3);
be_pop(vm, 2); be_pop(vm, 2);
} }
}
/* Insert an int to a key */ /* Insert an int to a key */
// On stack is either:
// Case 1; (-2) map instance, (-1) map
// Case 2; (-2) nil, (-1) string -> if key matches then update (-2)
void be_map_insert_int(bvm *vm, const char *key, bint value) void be_map_insert_int(bvm *vm, const char *key, bint value)
{ {
if (be_ismap(vm, -1)) {
be_pushstring(vm, key); be_pushstring(vm, key);
be_pushint(vm, value); be_pushint(vm, value);
be_data_insert(vm, -3); be_data_insert(vm, -3);
be_pop(vm, 2); be_pop(vm, 2);
} else if (be_isstring(vm, -1)) {
const char * needle = be_tostring(vm, -1);
if (strcmp(key, needle) == 0) {
be_pushint(vm, value);
be_moveto(vm, -1, -3);
be_pop(vm, 1);
}
}
} }
/* Insert an bbool to a key */ /* Insert an bbool to a key */
void be_map_insert_bool(bvm *vm, const char *key, bbool value) void be_map_insert_bool(bvm *vm, const char *key, bbool value)
{ {
if (be_ismap(vm, -1)) {
be_pushstring(vm, key); be_pushstring(vm, key);
be_pushbool(vm, value); be_pushbool(vm, value);
be_data_insert(vm, -3); be_data_insert(vm, -3);
be_pop(vm, 2); be_pop(vm, 2);
} else if (be_isstring(vm, -1)) {
const char * needle = be_tostring(vm, -1);
if (strcmp(key, needle) == 0) {
be_pushbool(vm, value);
be_moveto(vm, -1, -3);
be_pop(vm, 1);
}
}
} }
/* Insert an real to a key */ /* Insert an real to a key */
/* if value == NAN, ignore */ /* if value == NAN, ignore */
void be_map_insert_real(bvm *vm, const char *key, breal value) void be_map_insert_real(bvm *vm, const char *key, breal value)
{ {
if (be_ismap(vm, -1)) {
if (!isnan(value)) { if (!isnan(value)) {
be_pushstring(vm, key); be_pushstring(vm, key);
be_pushreal(vm, value); be_pushreal(vm, value);
be_data_insert(vm, -3); be_data_insert(vm, -3);
be_pop(vm, 2); be_pop(vm, 2);
} }
} else if (be_isstring(vm, -1)) {
const char * needle = be_tostring(vm, -1);
if (strcmp(key, needle) == 0) {
be_pushreal(vm, value);
be_moveto(vm, -1, -3);
be_pop(vm, 1);
}
}
} }
/* Insert an C string to a key */ /* Insert an C string to a key */
void be_map_insert_str(bvm *vm, const char *key, const char *value) void be_map_insert_str(bvm *vm, const char *key, const char *value)
{ {
if (be_ismap(vm, -1)) {
be_pushstring(vm, key); be_pushstring(vm, key);
be_pushstring(vm, value); be_pushstring(vm, value);
be_data_insert(vm, -3); be_data_insert(vm, -3);
be_pop(vm, 2); be_pop(vm, 2);
} else if (be_isstring(vm, -1)) {
const char * needle = be_tostring(vm, -1);
if (strcmp(key, needle) == 0) {
be_pushstring(vm, value);
be_moveto(vm, -1, -3);
be_pop(vm, 1);
}
}
} }
/* Insert list of bytes as individual integers to a key */ /* Insert list of bytes as individual integers to a key */
void be_map_insert_list_uint8(bvm *vm, const char *key, const uint8_t *value, size_t size) void be_map_insert_list_uint8(bvm *vm, const char *key, const uint8_t *value, size_t size)
{ {
if (be_ismap(vm, -1)) {
be_pushstring(vm, key); be_pushstring(vm, key);
be_newobject(vm, "list"); be_newobject(vm, "list");
@ -64,6 +106,7 @@ void be_map_insert_list_uint8(bvm *vm, const char *key, const uint8_t *value, si
be_data_insert(vm, -3); // insert into map, key/value be_data_insert(vm, -3); // insert into map, key/value
be_pop(vm, 2); // pop both key and value be_pop(vm, 2); // pop both key and value
} }
}
/*********************************************************************************************\ /*********************************************************************************************\
* Binary search for dynamic attributes * Binary search for dynamic attributes

View File

@ -111,14 +111,14 @@ class be_class_tasmota (scope: global, name: Tasmota) {
get_option, func(l_getoption) get_option, func(l_getoption)
millis, func(l_millis) millis, func(l_millis)
time_reached, func(l_timereached) time_reached, func(l_timereached)
rtc, func(l_rtc) rtc, static_func(l_rtc)
rtc_utc, func(l_rtc_utc) rtc_utc, func(l_rtc_utc)
time_dump, func(l_time_dump) time_dump, func(l_time_dump)
strftime, func(l_strftime) strftime, func(l_strftime)
strptime, func(l_strptime) strptime, func(l_strptime)
memory, func(l_memory) memory, static_func(l_memory)
wifi, func(l_wifi) wifi, static_func(l_wifi)
eth, func(l_eth) eth, static_func(l_eth)
hostname, func(l_hostname) hostname, func(l_hostname)
yield, func(l_yield) yield, func(l_yield)
delay, func(l_delay) delay, func(l_delay)

View File

@ -171,8 +171,14 @@ extern "C" {
int32_t l_rtc(struct bvm *vm); int32_t l_rtc(struct bvm *vm);
int32_t l_rtc(struct bvm *vm) { int32_t l_rtc(struct bvm *vm) {
int32_t top = be_top(vm); // Get the number of arguments int32_t top = be_top(vm); // Get the number of arguments
if (top == 1) { // no argument (instance only) if (top >= 1 && be_isstring(vm, 1)) { // argument is name
be_pushnil(vm);
be_pushvalue(vm, 1);
// (-2) nil, (-1) string -> if key matches then update (-2)
} else {
be_newobject(vm, "map"); be_newobject(vm, "map");
// (-2) map instance, (-1) map
}
be_map_insert_int(vm, "utc", Rtc.utc_time); be_map_insert_int(vm, "utc", Rtc.utc_time);
be_map_insert_int(vm, "local", Rtc.local_time); be_map_insert_int(vm, "local", Rtc.local_time);
be_map_insert_int(vm, "restart", Rtc.restart_time); be_map_insert_int(vm, "restart", Rtc.restart_time);
@ -180,8 +186,6 @@ extern "C" {
be_pop(vm, 1); be_pop(vm, 1);
be_return(vm); be_return(vm);
} }
be_raise(vm, kTypeError, nullptr);
}
// Berry: tasmota.rtc_utc() -> int // Berry: tasmota.rtc_utc() -> int
// //
@ -196,8 +200,14 @@ extern "C" {
int32_t l_memory(struct bvm *vm); int32_t l_memory(struct bvm *vm);
int32_t l_memory(struct bvm *vm) { int32_t l_memory(struct bvm *vm) {
int32_t top = be_top(vm); // Get the number of arguments int32_t top = be_top(vm); // Get the number of arguments
if (top == 1) { // no argument (instance only) if (top >= 1 && be_isstring(vm, 1)) { // argument is name
be_pushnil(vm);
be_pushvalue(vm, 1);
// (-2) nil, (-1) string -> if key matches then update (-2)
} else {
be_newobject(vm, "map"); be_newobject(vm, "map");
// (-2) map instance, (-1) map
}
be_map_insert_int(vm, "flash", ESP_getFlashChipMagicSize() / 1024); be_map_insert_int(vm, "flash", ESP_getFlashChipMagicSize() / 1024);
be_map_insert_int(vm, "flash_real", ESP.getFlashChipSize() / 1024); be_map_insert_int(vm, "flash_real", ESP.getFlashChipSize() / 1024);
be_map_insert_int(vm, "program", ESP_getSketchSize() / 1024); be_map_insert_int(vm, "program", ESP_getSketchSize() / 1024);
@ -217,16 +227,20 @@ extern "C" {
be_pop(vm, 1); be_pop(vm, 1);
be_return(vm); be_return(vm);
} }
be_raise(vm, kTypeError, nullptr);
}
// Berry: tasmota.wifi() -> map // Berry: tasmota.wifi() -> map
// //
int32_t l_wifi(struct bvm *vm); int32_t l_wifi(struct bvm *vm);
int32_t l_wifi(struct bvm *vm) { int32_t l_wifi(struct bvm *vm) {
int32_t top = be_top(vm); // Get the number of arguments int32_t top = be_top(vm); // Get the number of arguments
if (top == 1) { // no argument (instance only) if (top >= 1 && be_isstring(vm, 1)) { // argument is name
be_pushnil(vm);
be_pushvalue(vm, 1);
// (-2) nil, (-1) string -> if key matches then update (-2)
} else {
be_newobject(vm, "map"); be_newobject(vm, "map");
// (-2) map instance, (-1) map
}
be_map_insert_str(vm, "mac", WiFi.macAddress().c_str()); be_map_insert_str(vm, "mac", WiFi.macAddress().c_str());
be_map_insert_bool(vm, "up", WifiHasIP()); be_map_insert_bool(vm, "up", WifiHasIP());
if (Settings->flag4.network_wifi) { if (Settings->flag4.network_wifi) {
@ -256,16 +270,20 @@ extern "C" {
be_pop(vm, 1); be_pop(vm, 1);
be_return(vm); be_return(vm);
} }
be_raise(vm, kTypeError, nullptr);
}
// Berry: tasmota.eth() -> map // Berry: tasmota.eth() -> map
// //
int32_t l_eth(struct bvm *vm); int32_t l_eth(struct bvm *vm);
int32_t l_eth(struct bvm *vm) { int32_t l_eth(struct bvm *vm) {
int32_t top = be_top(vm); // Get the number of arguments int32_t top = be_top(vm); // Get the number of arguments
if (top == 1) { // no argument (instance only) if (top >= 1 && be_isstring(vm, 1)) { // argument is name
be_pushnil(vm);
be_pushvalue(vm, 1);
// (-2) nil, (-1) string -> if key matches then update (-2)
} else {
be_newobject(vm, "map"); be_newobject(vm, "map");
// (-2) map instance, (-1) map
}
#ifdef USE_ETHERNET #ifdef USE_ETHERNET
be_map_insert_bool(vm, "up", EthernetHasIP()); be_map_insert_bool(vm, "up", EthernetHasIP());
String eth_mac = EthernetMacAddress().c_str(); String eth_mac = EthernetMacAddress().c_str();
@ -291,8 +309,6 @@ extern "C" {
be_pop(vm, 1); be_pop(vm, 1);
be_return(vm); be_return(vm);
} }
be_raise(vm, kTypeError, nullptr);
}
// Berry: tasmota.hostname() -> string // Berry: tasmota.hostname() -> string
// //