Merge pull request #13994 from s-hadinger/berry_minor_cleaning

Berry minor cleaning
This commit is contained in:
s-hadinger 2021-12-10 21:56:17 +01:00 committed by GitHub
commit 227ae7b016
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 49 additions and 30 deletions

View File

@ -25,6 +25,11 @@
#include "re1.5.h" #include "re1.5.h"
/*********************************************************************************************\
* Logging for Tasmota Berry console
*
* We need to declare the the log class first since it is used in structure
\*********************************************************************************************/
#define BERRY_CONSOLE_CMD_DELIMITER "\x01" #define BERRY_CONSOLE_CMD_DELIMITER "\x01"
class Log_line { class Log_line {
@ -71,6 +76,12 @@ public:
LList<Log_line> log; LList<Log_line> log;
}; };
/*********************************************************************************************\
* Berry global structure
*
\*********************************************************************************************/
class BerryLog;
class BerrySupport { class BerrySupport {
public: public:
bvm *vm = nullptr; // berry vm bvm *vm = nullptr; // berry vm
@ -84,5 +95,4 @@ public:
}; };
BerrySupport berry; BerrySupport berry;
#endif // USE_BERRY #endif // USE_BERRY

View File

@ -1,5 +1,5 @@
/* /*
xdrv_52_3_berry_native.ino - Berry scripting language, native fucnctions xdrv_52_1_berry_native.ino - Berry scripting language, native fucnctions
Copyright (C) 2021 Stephan Hadinger, Berry language by Guan Wenliang https://github.com/Skiars/berry Copyright (C) 2021 Stephan Hadinger, Berry language by Guan Wenliang https://github.com/Skiars/berry
@ -45,6 +45,11 @@ extern "C" {
* Responds to virtual constants * Responds to virtual constants
\*********************************************************************************************/ \*********************************************************************************************/
extern "C" { extern "C" {
// Clear all elements on the stack
void be_pop_all(bvm *vm) {
be_pop(vm, be_top(vm)); // clear Berry stack
}
#include "be_exec.h" #include "be_exec.h"
#include "be_debug.h" #include "be_debug.h"
void be_dumpstack(bvm *vm) { void be_dumpstack(bvm *vm) {
@ -259,12 +264,27 @@ extern "C" {
/*********************************************************************************************\ /*********************************************************************************************\
* Generalized callbacks * Generalized callbacks
* *
* Warning, the following expect all parameters to be 32 bits wide
\*********************************************************************************************/ \*********************************************************************************************/
extern "C" { extern "C" {
/*********************************************************************************************\
* Callback structures
*
* We allow 4 parameters, or 3 if method (first arg is `self`)
* This could be extended if needed
\*********************************************************************************************/
typedef int32_t (*berry_callback_t)(int32_t v0, int32_t v1, int32_t v2, int32_t v3); typedef int32_t (*berry_callback_t)(int32_t v0, int32_t v1, int32_t v2, int32_t v3);
extern void BerryDumpErrorAndClear(bvm *vm, bool berry_console); extern void BerryDumpErrorAndClear(bvm *vm, bool berry_console);
/*********************************************************************************************\
* Callback structures
*
* We allow 4 parameters, or 3 if method (first arg is `self`)
* This could be extended if needed
\*********************************************************************************************/
int32_t call_berry_cb(int32_t num, int32_t v0, int32_t v1, int32_t v2, int32_t v3) { int32_t call_berry_cb(int32_t num, int32_t v0, int32_t v1, int32_t v2, int32_t v3) {
// call berry cb dispatcher // call berry cb dispatcher
int32_t ret = 0; int32_t ret = 0;
@ -285,6 +305,7 @@ extern "C" {
ret = be_pcall(berry.vm, 6); // 5 arguments ret = be_pcall(berry.vm, 6); // 5 arguments
if (ret != 0) { if (ret != 0) {
BerryDumpErrorAndClear(berry.vm, false); // log in Tasmota console only BerryDumpErrorAndClear(berry.vm, false); // log in Tasmota console only
be_pop_all(berry.vm); // clear Berry stack
return 0; return 0;
} }
be_pop(berry.vm, 6); be_pop(berry.vm, 6);
@ -353,10 +374,6 @@ extern "C" {
}; };
} }
#define LV_OBJ_CLASS "lv_obj"
#define LV_MODULE "lvgl" // name of the lvgl module
/*********************************************************************************************\ /*********************************************************************************************\
* Automatically parse Berry stack and call the C function accordingly * Automatically parse Berry stack and call the C function accordingly
* *
@ -424,7 +441,7 @@ int32_t be_convert_single_elt(bvm *vm, int32_t idx, const char * arg_type = null
const void * func = be_tocomptr(vm, -6); const void * func = be_tocomptr(vm, -6);
be_pop(vm, 6); be_pop(vm, 6);
// berry_log_P("func=%p", func); // berry_log_C("func=%p", func);
return (int32_t) func; return (int32_t) func;
} else { } else {
be_raise(vm, kTypeError, "Closure expected for callback type"); be_raise(vm, kTypeError, "Closure expected for callback type");
@ -445,7 +462,7 @@ int32_t be_convert_single_elt(bvm *vm, int32_t idx, const char * arg_type = null
type_ok = type_ok || (ret == 0 && arg_type_len != 1); // or NULL is accepted for an instance type_ok = type_ok || (ret == 0 && arg_type_len != 1); // or NULL is accepted for an instance
if (!type_ok) { if (!type_ok) {
berry_log_P("Unexpected argument type '%c', expected '%s'", provided_type, arg_type); berry_log_C("Unexpected argument type '%c', expected '%s'", provided_type, arg_type);
} }
return ret; return ret;
} }
@ -475,14 +492,14 @@ int32_t be_convert_single_elt(bvm *vm, int32_t idx, const char * arg_type = null
// Stack: class_of_idx, class_of_target (or nil) // Stack: class_of_idx, class_of_target (or nil)
if (class_found) { if (class_found) {
if (!be_isderived(vm, -2)) { if (!be_isderived(vm, -2)) {
berry_log_P("Unexpected class type '%s', expected '%s'", be_classname(vm, idx), arg_type); berry_log_C("Unexpected class type '%s', expected '%s'", be_classname(vm, idx), arg_type);
} }
} else { } else {
berry_log_P("Unable to find class '%s' (%d)", arg_type, arg_type_len); berry_log_C("Unable to find class '%s' (%d)", arg_type, arg_type_len);
} }
be_pop(vm, 2); be_pop(vm, 2);
} else if (arg_type[0] != '.') { } else if (arg_type[0] != '.') {
berry_log_P("Unexpected instance type '%s', expected '%s'", be_classname(vm, idx), arg_type); berry_log_C("Unexpected instance type '%s', expected '%s'", be_classname(vm, idx), arg_type);
} }
return ret; return ret;

View File

@ -167,7 +167,7 @@ void be_check_arg_type(bvm *vm, int32_t arg_start, int32_t argc, const char * ar
// check if we are missing arguments // check if we are missing arguments
if (arg_type != nullptr && arg_type[arg_idx] != 0) { if (arg_type != nullptr && arg_type[arg_idx] != 0) {
berry_log_P("Missing arguments, remaining type '%s'", &arg_type[arg_idx]); berry_log_C("Missing arguments, remaining type '%s'", &arg_type[arg_idx]);
} }
} }
@ -777,7 +777,7 @@ extern "C" {
int lv0_register_button_encoder(bvm *vm) { int lv0_register_button_encoder(bvm *vm) {
int32_t argc = be_top(vm); // Get the number of arguments int32_t argc = be_top(vm); // Get the number of arguments
bool inverted = false; bool inverted = false;
// berry_log_P("lv0_register_button_encoder argc=%d inverted=%d", argc, be_tobool(vm, 1)); // berry_log_C("lv0_register_button_encoder argc=%d inverted=%d", argc, be_tobool(vm, 1));
if (argc >= 1) { if (argc >= 1) {
inverted = be_tobool(vm, 1); // get the inverted flag inverted = be_tobool(vm, 1); // get the inverted flag
} }
@ -794,7 +794,7 @@ extern "C" {
lvbe.btn[1].set_inverted(inverted); lvbe.btn[1].set_inverted(inverted);
lvbe.btn[2].set_gpio(btn2); lvbe.btn[2].set_gpio(btn2);
lvbe.btn[2].set_inverted(inverted); lvbe.btn[2].set_inverted(inverted);
berry_log_P(D_LOG_LVGL "Button Rotary encoder using GPIOs %d,%d,%d%s", btn0, btn1, btn2, inverted ? " (inverted)" : ""); berry_log_C(D_LOG_LVGL "Button Rotary encoder using GPIOs %d,%d,%d%s", btn0, btn1, btn2, inverted ? " (inverted)" : "");
lv_indev_drv_init(&lvbe.indev_drv); lv_indev_drv_init(&lvbe.indev_drv);
lvbe.indev_drv.type = LV_INDEV_TYPE_ENCODER; lvbe.indev_drv.type = LV_INDEV_TYPE_ENCODER;
@ -837,7 +837,7 @@ extern "C" {
} }
bool state = lvbe.btn[i].clear_state_changed(); bool state = lvbe.btn[i].clear_state_changed();
data->state = state ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL; data->state = state ? LV_INDEV_STATE_PR : LV_INDEV_STATE_REL;
// berry_log_P("Button event key %d state %d,%d", data->key, state, data->state); // berry_log_C("Button event key %d state %d,%d", data->key, state, data->state);
break; break;
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
xdrv_52_3_berry_native.ino - Berry scripting language, native fucnctions xdrv_52_3_berry_tasmota.ino - Berry scripting language, native fucnctions
Copyright (C) 2021 Stephan Hadinger, Berry language by Guan Wenliang https://github.com/Skiars/berry Copyright (C) 2021 Stephan Hadinger, Berry language by Guan Wenliang https://github.com/Skiars/berry
@ -614,17 +614,4 @@ extern "C" {
} }
} }
void berry_log_P(const char * berry_buf, ...) {
// To save stack space support logging for max text length of 128 characters
char log_data[LOGSZ];
va_list arg;
va_start(arg, berry_buf);
uint32_t len = ext_vsnprintf_P(log_data, LOGSZ-3, berry_buf, arg);
va_end(arg);
if (len+3 > LOGSZ) { strcat(log_data, "..."); } // Actual data is more
berry_log(log_data);
}
#endif // USE_BERRY #endif // USE_BERRY

View File

@ -126,7 +126,6 @@ void BerryDumpErrorAndClear(bvm *vm, bool berry_console) {
} else { } else {
be_dumpstack(vm); be_dumpstack(vm);
} }
be_pop(vm, top);
} }
// void callBerryMqttData(void) { // void callBerryMqttData(void) {
@ -226,6 +225,7 @@ int32_t callBerryEventDispatcher(const char *type, const char *cmd, int32_t idx,
BrTimeoutReset(); BrTimeoutReset();
if (ret != 0) { if (ret != 0) {
BerryDumpErrorAndClear(vm, false); // log in Tasmota console only BerryDumpErrorAndClear(vm, false); // log in Tasmota console only
be_pop_all(berry.vm); // clear Berry stack
return ret; return ret;
} }
be_pop(vm, 5); be_pop(vm, 5);
@ -332,17 +332,20 @@ void BerryInit(void) {
ret_code1 = be_loadstring(berry.vm, berry_prog); ret_code1 = be_loadstring(berry.vm, berry_prog);
if (ret_code1 != 0) { if (ret_code1 != 0) {
BerryDumpErrorAndClear(berry.vm, false); BerryDumpErrorAndClear(berry.vm, false);
be_pop_all(berry.vm); // clear Berry stack
break; break;
} }
// AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_BERRY "Berry code loaded, RAM used=%u"), be_gc_memcount(berry.vm)); // AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_BERRY "Berry code loaded, RAM used=%u"), be_gc_memcount(berry.vm));
ret_code2 = be_pcall(berry.vm, 0); ret_code2 = be_pcall(berry.vm, 0);
if (ret_code1 != 0) { if (ret_code1 != 0) {
BerryDumpErrorAndClear(berry.vm, false); BerryDumpErrorAndClear(berry.vm, false);
be_pop_all(berry.vm); // clear Berry stack
break; break;
} }
// AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_BERRY "Berry code ran, RAM used=%u"), be_gc_memcount(berry.vm)); // AddLog(LOG_LEVEL_DEBUG, PSTR(D_LOG_BERRY "Berry code ran, RAM used=%u"), be_gc_memcount(berry.vm));
if (be_top(berry.vm) > 1) { if (be_top(berry.vm) > 1) {
BerryDumpErrorAndClear(berry.vm, false); BerryDumpErrorAndClear(berry.vm, false);
be_pop_all(berry.vm); // clear Berry stack
} else { } else {
be_pop(berry.vm, 1); be_pop(berry.vm, 1);
} }
@ -387,6 +390,7 @@ void BrLoad(const char * script_name) {
BrTimeoutStart(); BrTimeoutStart();
if (be_pcall(berry.vm, 1) != 0) { if (be_pcall(berry.vm, 1) != 0) {
BerryDumpErrorAndClear(berry.vm, false); BerryDumpErrorAndClear(berry.vm, false);
be_pop_all(berry.vm); // clear Berry stack
return; return;
} }
BrTimeoutReset(); BrTimeoutReset();
@ -492,6 +496,7 @@ void BrREPLRun(char * cmd) {
} }
if (BE_EXCEPTION == ret_code) { if (BE_EXCEPTION == ret_code) {
BerryDumpErrorAndClear(berry.vm, true); BerryDumpErrorAndClear(berry.vm, true);
be_pop_all(berry.vm); // clear Berry stack
// be_dumpstack(berry.vm); // be_dumpstack(berry.vm);
// char exception_s[120]; // char exception_s[120];
// ext_snprintf_P(exception_s, sizeof(exception_s), PSTR("%s: %s"), be_tostring(berry.vm, -2), be_tostring(berry.vm, -1)); // ext_snprintf_P(exception_s, sizeof(exception_s), PSTR("%s: %s"), be_tostring(berry.vm, -2), be_tostring(berry.vm, -1));