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 `debug_panel.tapp` to display real-time heap and wifi rssi
- Berry `webserver.header` to read browser sent headers
- Berry provide lightweight options for `tasmota.wifi/eth/memory/rtc`
### Breaking Changed

View File

@ -8,49 +8,91 @@
/* Insert an nil to a key */
void be_map_insert_nil(bvm *vm, const char *key)
{
if (be_ismap(vm, -1)) {
be_pushstring(vm, key);
be_pushnil(vm);
be_data_insert(vm, -3);
be_pop(vm, 2);
}
}
/* 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)
{
if (be_ismap(vm, -1)) {
be_pushstring(vm, key);
be_pushint(vm, value);
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 */
void be_map_insert_bool(bvm *vm, const char *key, bbool value)
{
if (be_ismap(vm, -1)) {
be_pushstring(vm, key);
be_pushbool(vm, value);
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 */
/* if value == NAN, ignore */
void be_map_insert_real(bvm *vm, const char *key, breal value)
{
if (be_ismap(vm, -1)) {
if (!isnan(value)) {
be_pushstring(vm, key);
be_pushreal(vm, value);
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 */
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, value);
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 */
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_newobject(vm, "list");
@ -63,6 +105,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_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)
millis, func(l_millis)
time_reached, func(l_timereached)
rtc, func(l_rtc)
rtc, static_func(l_rtc)
rtc_utc, func(l_rtc_utc)
time_dump, func(l_time_dump)
strftime, func(l_strftime)
strptime, func(l_strptime)
memory, func(l_memory)
wifi, func(l_wifi)
eth, func(l_eth)
memory, static_func(l_memory)
wifi, static_func(l_wifi)
eth, static_func(l_eth)
hostname, func(l_hostname)
yield, func(l_yield)
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 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");
// (-2) map instance, (-1) map
}
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);
@ -180,8 +186,6 @@ extern "C" {
be_pop(vm, 1);
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
}
// 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 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");
// (-2) map instance, (-1) map
}
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);
@ -217,16 +227,20 @@ extern "C" {
be_pop(vm, 1);
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
}
// Berry: tasmota.wifi() -> map
//
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
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");
// (-2) map instance, (-1) map
}
be_map_insert_str(vm, "mac", WiFi.macAddress().c_str());
be_map_insert_bool(vm, "up", WifiHasIP());
if (Settings->flag4.network_wifi) {
@ -256,16 +270,20 @@ extern "C" {
be_pop(vm, 1);
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
}
// Berry: tasmota.eth() -> map
//
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
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");
// (-2) map instance, (-1) map
}
#ifdef USE_ETHERNET
be_map_insert_bool(vm, "up", EthernetHasIP());
String eth_mac = EthernetMacAddress().c_str();
@ -291,8 +309,6 @@ extern "C" {
be_pop(vm, 1);
be_return(vm);
}
be_raise(vm, kTypeError, nullptr);
}
// Berry: tasmota.hostname() -> string
//