2021-03-13 21:42:24 +00:00
|
|
|
/*
|
2021-12-10 20:48:06 +00:00
|
|
|
xdrv_52_1_berry_native.ino - Berry scripting language, native fucnctions
|
2021-03-13 21:42:24 +00:00
|
|
|
|
|
|
|
Copyright (C) 2021 Stephan Hadinger, Berry language by Guan Wenliang https://github.com/Skiars/berry
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef USE_BERRY
|
|
|
|
|
|
|
|
#include <berry.h>
|
|
|
|
#include <Wire.h>
|
|
|
|
|
|
|
|
const char kTypeError[] PROGMEM = "type_error";
|
2021-05-28 21:37:06 +01:00
|
|
|
const char kInternalError[] PROGMEM = "intenal_error";
|
2021-03-13 21:42:24 +00:00
|
|
|
|
2021-05-28 22:13:19 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* LVGL top level virtual members
|
|
|
|
*
|
|
|
|
* Responds to virtual constants
|
|
|
|
\*********************************************************************************************/
|
2021-03-13 21:42:24 +00:00
|
|
|
extern "C" {
|
2021-12-12 17:56:11 +00:00
|
|
|
#include "be_vm.h"
|
|
|
|
// Call error handler and pop all from stack
|
|
|
|
void be_error_pop_all(bvm *vm);
|
|
|
|
void be_error_pop_all(bvm *vm) {
|
|
|
|
if (vm->obshook != NULL) (*vm->obshook)(vm, BE_OBS_PCALL_ERROR);
|
2021-12-10 20:48:06 +00:00
|
|
|
be_pop(vm, be_top(vm)); // clear Berry stack
|
|
|
|
}
|
|
|
|
|
2021-04-19 07:40:11 +01:00
|
|
|
#include "be_exec.h"
|
2021-10-26 20:58:21 +01:00
|
|
|
#include "be_debug.h"
|
2021-03-13 21:42:24 +00:00
|
|
|
void be_dumpstack(bvm *vm) {
|
|
|
|
int32_t top = be_top(vm);
|
2022-01-10 17:37:28 +00:00
|
|
|
AddLog(LOG_LEVEL_DEBUG, "BRY: top=%d", top);
|
2021-10-26 20:58:21 +01:00
|
|
|
be_tracestack(vm);
|
2021-03-13 21:42:24 +00:00
|
|
|
for (uint32_t i = 1; i <= top; i++) {
|
|
|
|
const char * tname = be_typename(vm, i);
|
|
|
|
const char * cname = be_classname(vm, i);
|
2021-04-03 18:53:52 +01:00
|
|
|
if (be_isstring(vm, i)) {
|
|
|
|
cname = be_tostring(vm, i);
|
|
|
|
}
|
2021-03-13 21:42:24 +00:00
|
|
|
AddLog(LOG_LEVEL_INFO, "BRY: stack[%d] = type='%s' (%s)", i, (tname != nullptr) ? tname : "", (cname != nullptr) ? cname : "");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// convert to unsigned 8 bits
|
|
|
|
static uint8_t to_u8(int32_t v) {
|
|
|
|
if (v < 0) { return 0; }
|
|
|
|
if (v > 0xFF) { return 0xFF; }
|
|
|
|
return v;
|
|
|
|
}
|
2021-09-13 12:44:39 +01:00
|
|
|
|
2021-04-19 07:40:11 +01:00
|
|
|
int32_t member_find(bvm *vm, const char *key, int32_t default_value) {
|
|
|
|
int32_t ret = default_value;
|
|
|
|
if (be_getmember(vm, -1, key)) {
|
|
|
|
if (be_isint(vm, -1)) {
|
|
|
|
ret = be_toint(vm, -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
be_pop(vm, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
2021-03-13 21:42:24 +00:00
|
|
|
static bool map_find(bvm *vm, const char *key)
|
|
|
|
{
|
|
|
|
be_getmethod(vm, -1, "find"); // look for "find" method of "Map" instance
|
|
|
|
be_pushvalue(vm, -2); // put back instance as first argument (implicit instance)
|
|
|
|
be_pushstring(vm, key); // push string as second argument
|
|
|
|
be_call(vm, 2); // call wirn 2 parameters (implicit instance and key)
|
|
|
|
be_pop(vm, 2); // pop 2 arguments, the function is replaced by result
|
|
|
|
return !be_isnil(vm, -1); // true if not 'nil'
|
|
|
|
}
|
|
|
|
static int32_t get_list_size(bvm *vm) {
|
|
|
|
be_getmethod(vm, -1, "size"); // look for "size" method of "list" instance
|
|
|
|
be_pushvalue(vm, -2); // put back instance as first argument (implicit instance)
|
|
|
|
be_call(vm, 1); // call wirn 2 parameters (implicit instance and key)
|
|
|
|
int32_t ret = be_toint(vm, -2);
|
|
|
|
be_pop(vm, 2); // pop 1 argument and return value
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
// get item number `index` from list, index must be valid or raises an exception
|
|
|
|
static void get_list_item(bvm *vm, int32_t index) {
|
|
|
|
be_getmethod(vm, -1, "item"); // look for "size" method of "list" instance
|
|
|
|
be_pushvalue(vm, -2); // put back instance as first argument (implicit instance)
|
|
|
|
be_pushint(vm, index);
|
|
|
|
// be_dumpstack(vm);
|
|
|
|
be_call(vm, 2); // call wirn 2 parameters (implicit instance and key)
|
|
|
|
be_pop(vm, 2); // pop 2 arguments and return value
|
|
|
|
}
|
2021-11-05 18:25:50 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 19:14:31 +01:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Manage timeout for Berry code
|
|
|
|
*
|
|
|
|
\*********************************************************************************************/
|
|
|
|
void BrTimeoutStart(void) {
|
|
|
|
berry.timeout = millis() + USE_BERRY_TIMEOUT;
|
|
|
|
if (0 == berry.timeout) {
|
|
|
|
berry.timeout = 1; // rare case when value accidentally computes to zero
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrTimeoutYield(void) {
|
|
|
|
if (0 != berry.timeout) {
|
|
|
|
BrTimeoutStart();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BrTimeoutReset(void) {
|
|
|
|
berry.timeout = 0; // remove timer
|
|
|
|
}
|
2021-05-06 09:46:17 +01:00
|
|
|
|
2021-03-13 21:42:24 +00:00
|
|
|
#endif // USE_BERRY
|