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,61 +8,104 @@
/* 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)
{ {
be_pushstring(vm, key); if (be_ismap(vm, -1)) {
be_pushnil(vm); be_pushstring(vm, key);
be_data_insert(vm, -3); be_pushnil(vm);
be_pop(vm, 2); be_data_insert(vm, -3);
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)
{ {
be_pushstring(vm, key); if (be_ismap(vm, -1)) {
be_pushint(vm, value); be_pushstring(vm, key);
be_data_insert(vm, -3); be_pushint(vm, value);
be_pop(vm, 2); be_data_insert(vm, -3);
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)
{ {
be_pushstring(vm, key); if (be_ismap(vm, -1)) {
be_pushbool(vm, value); be_pushstring(vm, key);
be_data_insert(vm, -3); be_pushbool(vm, value);
be_pop(vm, 2); be_data_insert(vm, -3);
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 (!isnan(value)) { if (be_ismap(vm, -1)) {
be_pushstring(vm, key); if (!isnan(value)) {
be_pushreal(vm, value); be_pushstring(vm, key);
be_data_insert(vm, -3); be_pushreal(vm, value);
be_pop(vm, 2); be_data_insert(vm, -3);
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)
{ {
be_pushstring(vm, key); if (be_ismap(vm, -1)) {
be_pushstring(vm, value); be_pushstring(vm, key);
be_data_insert(vm, -3); be_pushstring(vm, value);
be_pop(vm, 2); be_data_insert(vm, -3);
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)
{ {
be_pushstring(vm, key); if (be_ismap(vm, -1)) {
be_pushstring(vm, key);
be_newobject(vm, "list"); be_newobject(vm, "list");
for (uint32_t i=0; i < size; i++) { for (uint32_t i=0; i < size; i++) {
be_pushint(vm, value[i]); be_pushint(vm, value[i]);
be_data_push(vm, -2); be_data_push(vm, -2);
be_pop(vm, 1); be_pop(vm, 1);
}
be_pop(vm, 1); // now list is on top
be_data_insert(vm, -3); // insert into map, key/value
be_pop(vm, 2); // pop both key and value
} }
be_pop(vm, 1); // now list is on top
be_data_insert(vm, -3); // insert into map, key/value
be_pop(vm, 2); // pop both key and value
} }
/*********************************************************************************************\ /*********************************************************************************************\

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,16 +171,20 @@ 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");
be_map_insert_int(vm, "utc", Rtc.utc_time); // (-2) map instance, (-1) map
be_map_insert_int(vm, "local", Rtc.local_time);
be_map_insert_int(vm, "restart", Rtc.restart_time);
be_map_insert_int(vm, "timezone", Rtc.time_timezone);
be_pop(vm, 1);
be_return(vm);
} }
be_raise(vm, kTypeError, nullptr); be_map_insert_int(vm, "utc", Rtc.utc_time);
be_map_insert_int(vm, "local", Rtc.local_time);
be_map_insert_int(vm, "restart", Rtc.restart_time);
be_map_insert_int(vm, "timezone", Rtc.time_timezone);
be_pop(vm, 1);
be_return(vm);
} }
// Berry: tasmota.rtc_utc() -> int // Berry: tasmota.rtc_utc() -> int
@ -196,28 +200,32 @@ 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");
be_map_insert_int(vm, "flash", ESP_getFlashChipMagicSize() / 1024); // (-2) map instance, (-1) map
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_free", ESP_getFreeSketchSpace() / 1024);
be_map_insert_int(vm, "heap_free", ESP_getFreeHeap() / 1024);
be_map_insert_int(vm, "frag", ESP_getHeapFragmentation());
// give info about stack size
be_map_insert_int(vm, "stack_size", SET_ESP32_STACK_SIZE / 1024);
be_map_insert_real(vm, "stack_low", ((float)uxTaskGetStackHighWaterMark(nullptr)) / 1024);
if (UsePSRAM()) {
be_map_insert_int(vm, "psram", ESP.getPsramSize() / 1024);
be_map_insert_int(vm, "psram_free", ESP.getFreePsram() / 1024);
}
// IRAM information
int32_t iram_free = (int32_t)heap_caps_get_free_size(MALLOC_CAP_32BIT) - (int32_t)heap_caps_get_free_size(MALLOC_CAP_8BIT);
be_map_insert_int(vm, "iram_free", iram_free / 1024);
be_pop(vm, 1);
be_return(vm);
} }
be_raise(vm, kTypeError, nullptr); be_map_insert_int(vm, "flash", ESP_getFlashChipMagicSize() / 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_free", ESP_getFreeSketchSpace() / 1024);
be_map_insert_int(vm, "heap_free", ESP_getFreeHeap() / 1024);
be_map_insert_int(vm, "frag", ESP_getHeapFragmentation());
// give info about stack size
be_map_insert_int(vm, "stack_size", SET_ESP32_STACK_SIZE / 1024);
be_map_insert_real(vm, "stack_low", ((float)uxTaskGetStackHighWaterMark(nullptr)) / 1024);
if (UsePSRAM()) {
be_map_insert_int(vm, "psram", ESP.getPsramSize() / 1024);
be_map_insert_int(vm, "psram_free", ESP.getFreePsram() / 1024);
}
// IRAM information
int32_t iram_free = (int32_t)heap_caps_get_free_size(MALLOC_CAP_32BIT) - (int32_t)heap_caps_get_free_size(MALLOC_CAP_8BIT);
be_map_insert_int(vm, "iram_free", iram_free / 1024);
be_pop(vm, 1);
be_return(vm);
} }
// Berry: tasmota.wifi() -> map // Berry: tasmota.wifi() -> map
@ -225,38 +233,42 @@ extern "C" {
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");
be_map_insert_str(vm, "mac", WiFi.macAddress().c_str()); // (-2) map instance, (-1) map
be_map_insert_bool(vm, "up", WifiHasIP());
if (Settings->flag4.network_wifi) {
int32_t rssi = WiFi.RSSI();
bool show_rssi = false;
#ifdef USE_IPV6
String ipv6_addr = WifiGetIPv6Str();
if (ipv6_addr != "") {
be_map_insert_str(vm, "ip6", ipv6_addr.c_str());
show_rssi = true;
}
ipv6_addr = WifiGetIPv6LinkLocalStr();
if (ipv6_addr != "") {
be_map_insert_str(vm, "ip6local", ipv6_addr.c_str());
show_rssi = true;
}
#endif // USE_IPV6
if (static_cast<uint32_t>(WiFi.localIP()) != 0) {
be_map_insert_str(vm, "ip", IPAddress((uint32_t)WiFi.localIP()).toString().c_str()); // quick fix for IPAddress bug
show_rssi = true;
}
if (show_rssi) {
be_map_insert_int(vm, "rssi", rssi);
be_map_insert_int(vm, "quality", WifiGetRssiAsQuality(rssi));
}
}
be_pop(vm, 1);
be_return(vm);
} }
be_raise(vm, kTypeError, nullptr); be_map_insert_str(vm, "mac", WiFi.macAddress().c_str());
be_map_insert_bool(vm, "up", WifiHasIP());
if (Settings->flag4.network_wifi) {
int32_t rssi = WiFi.RSSI();
bool show_rssi = false;
#ifdef USE_IPV6
String ipv6_addr = WifiGetIPv6Str();
if (ipv6_addr != "") {
be_map_insert_str(vm, "ip6", ipv6_addr.c_str());
show_rssi = true;
}
ipv6_addr = WifiGetIPv6LinkLocalStr();
if (ipv6_addr != "") {
be_map_insert_str(vm, "ip6local", ipv6_addr.c_str());
show_rssi = true;
}
#endif // USE_IPV6
if (static_cast<uint32_t>(WiFi.localIP()) != 0) {
be_map_insert_str(vm, "ip", IPAddress((uint32_t)WiFi.localIP()).toString().c_str()); // quick fix for IPAddress bug
show_rssi = true;
}
if (show_rssi) {
be_map_insert_int(vm, "rssi", rssi);
be_map_insert_int(vm, "quality", WifiGetRssiAsQuality(rssi));
}
}
be_pop(vm, 1);
be_return(vm);
} }
// Berry: tasmota.eth() -> map // Berry: tasmota.eth() -> map
@ -264,34 +276,38 @@ extern "C" {
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();
if (eth_mac != "00:00:00:00:00:00") { if (eth_mac != "00:00:00:00:00:00") {
be_map_insert_str(vm, "mac", eth_mac.c_str()); be_map_insert_str(vm, "mac", eth_mac.c_str());
} }
if (static_cast<uint32_t>(EthernetLocalIP()) != 0) { if (static_cast<uint32_t>(EthernetLocalIP()) != 0) {
be_map_insert_str(vm, "ip", IPAddress((uint32_t)EthernetLocalIP()).toString().c_str()); // quick fix for IPAddress bug be_map_insert_str(vm, "ip", IPAddress((uint32_t)EthernetLocalIP()).toString().c_str()); // quick fix for IPAddress bug
} }
#ifdef USE_IPV6 #ifdef USE_IPV6
String ipv6_addr = EthernetGetIPv6Str(); String ipv6_addr = EthernetGetIPv6Str();
if (ipv6_addr != "") { if (ipv6_addr != "") {
be_map_insert_str(vm, "ip6", ipv6_addr.c_str()); be_map_insert_str(vm, "ip6", ipv6_addr.c_str());
} }
ipv6_addr = EthernetGetIPv6LinkLocalStr(); ipv6_addr = EthernetGetIPv6LinkLocalStr();
if (ipv6_addr != "") { if (ipv6_addr != "") {
be_map_insert_str(vm, "ip6local", ipv6_addr.c_str()); be_map_insert_str(vm, "ip6local", ipv6_addr.c_str());
} }
#endif // USE_IPV6 #endif // USE_IPV6
#else // USE_ETHERNET #else // USE_ETHERNET
be_map_insert_bool(vm, "up", bfalse); be_map_insert_bool(vm, "up", bfalse);
#endif // USE_ETHERNET #endif // USE_ETHERNET
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