Tasmota/tasmota/xdrv_52_7_berry_embedded.ino

180 lines
5.4 KiB
Arduino
Raw Normal View History

2021-02-28 19:50:37 +00:00
/*
xdrv_52_3_berry_embedded.ino - Berry scripting language, embedded code
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
/*********************************************************************************************\
* Handlers for Berry calls and async
2021-04-11 14:39:59 +01:00
*
2021-02-28 19:50:37 +00:00
\*********************************************************************************************/
const char berry_prog[] =
""
2021-03-13 21:42:24 +00:00
// create a 'ntv' module to allow functions to be registered in a safe namespace
// "ntv = module('ntv') "
2021-03-13 21:42:24 +00:00
2021-02-28 19:50:37 +00:00
// auto-import modules
// // import alias
2021-04-24 11:11:06 +01:00
#ifdef USE_ENERGY_SENSOR
2021-03-13 21:42:24 +00:00
"import energy "
2021-04-24 11:11:06 +01:00
#endif
2021-02-28 19:50:37 +00:00
// // Force gc and return allocated memory
// "def gc() "
// "import gc "
// "gc.collect() "
// "return gc.allocated() "
// // "end "
// // simple wrapper to load a file
// // prefixes '/' if needed, and simpler to use than `compile()`
// "def load(f) "
// "import string "
// "try "
// // check that the file ends with '.be' of '.bec'
// "var fl = string.split(f,'.') "
// "if (size(fl) <= 1 || (fl[-1] != 'be' && fl[-1] != 'bec')) "
// "raise \"file extension is not '.be' or '.bec'\" "
// "end "
// "var native = f[size(f)-1] == 'c' "
// // add prefix if needed
// "if f[0] != '/' f = '/' + f end "
// // load - works the same for .be and .bec
// "var c = compile(f,'file') "
// // save the compiled bytecode
// "if !native "
// "try "
// "self.save(f+'c', c) "
// "except .. as e "
// "self.log(string.format('BRY: could not save compiled file %s (%s)',f+'c',e)) "
// "end "
// "end "
// // call the compiled code
// "c() "
// "self.log(string.format(\"BRY: sucessfully loaded '%s'\",f)) "
// "except .. as e "
2021-04-11 14:39:59 +01:00
// "raise \"io_error\",string.format(\"Could not load file '%s'\",f) "
// "end "
2021-02-28 19:50:37 +00:00
// "end "
2021-02-28 19:50:37 +00:00
2021-04-11 14:14:10 +01:00
// // Monkey patch `Driver` class - To be continued
// "class Driver2 : Driver "
// "def add_cmd(c, f) "
// "var tasmota = self.get_tasmota() "
// "tasmota.add_cmd(c, / cmd, idx, payload, payload_json -> f(self, cmd, idx, payload, payload_json)) "
// "end "
// "end "
// "Driver = Driver2 "
2021-02-28 19:50:37 +00:00
// Instantiate tasmota object
"tasmota = Tasmota() "
"def log(m,l) tasmota.log(m,l) end "
"def load(f) tasmota.load(f) end "
2021-03-21 09:03:58 +00:00
#ifdef USE_LVGL
// instanciate singleton
// "class lvgl : lvgl_ntv "
// "end "
// "lv = lvgl() "
"import lvgl as lv "
2021-05-06 14:27:49 +01:00
"_lvgl_cb = [ {}, {}, {}, {}, {}, {} ] "
"_lvgl_cb_obj = [ {}, {}, {}, {}, {}, {} ] "
"def _lvgl_cb_dispatch(idx, obj, v1, v2, v3, v4) "
2021-05-10 19:04:11 +01:00
// "import string print(string.format('>>> idx=%i obj=0x%08X v1=%i', idx, obj, v1)) "
2021-05-06 14:27:49 +01:00
"var func = _lvgl_cb[idx].find(obj) "
"var inst = _lvgl_cb_obj[idx].find(obj) "
"if func != nil "
"return func(inst, v1, v2, v3, v4) "
"end "
"return nil "
"end "
// array of 6 callback types, each with key (lv_obj pointer converted to int, closure)
#endif // USE_LVGL
2021-03-21 09:03:58 +00:00
// Wire class
// "class Wire : Wire_ntv "
// // read bytes as `bytes()` object
// "def read_bytes(addr,reg,size) "
// "self._begin_transmission(addr) "
// "self._write(reg) "
// "self._end_transmission(false) "
// "self._request_from(addr,size) "
// "var ret=bytes(size) "
// "while (self._available()) "
// "ret..self._read() "
// "end "
// "return ret "
// "end "
// // write bytes from `bytes` object
// "def write_bytes(addr,reg,b) "
// "self._begin_transmission(addr) "
// "self._write(reg) "
// "self._write(b) "
// "self._end_transmission() "
// "end "
// "end "
2021-03-21 09:03:58 +00:00
2021-04-11 14:14:10 +01:00
#ifdef USE_I2C
"tasmota.wire1 = Wire(1) "
"tasmota.wire2 = Wire(2) "
"wire1 = tasmota.wire1 "
"wire2 = tasmota.wire2 "
2021-04-11 14:14:10 +01:00
#endif // USE_I2C
2021-04-03 17:06:40 +01:00
// auto-import gpio
"import gpio "
2021-04-11 14:14:10 +01:00
2021-04-04 18:36:25 +01:00
#ifdef USE_LIGHT
"import light "
#endif // USE_LIGHT
2021-02-28 19:50:37 +00:00
;
2021-03-13 21:42:24 +00:00
const char berry_autoexec[] =
// load "autoexec.be" using import, which loads either .be or .bec file
2021-04-19 20:07:46 +01:00
"import string "
2021-03-13 21:42:24 +00:00
"try "
2021-04-19 20:13:06 +01:00
"load('autoexec.be') "
2021-04-11 14:39:59 +01:00
"except .. as e,m "
2021-04-19 20:13:06 +01:00
"if e=='io_error' && string.find(m, \"autoexec.be\")>0 "
"log(\"BRY: no autoexec.be\") "
"else "
"log(\"BRY: exception in autoexec.be: \"+e+\": \"+m) "
"end "
2021-03-13 21:42:24 +00:00
"end "
;
2021-05-25 17:53:10 +01:00
const char berry_preinit[] =
// load "autoexec.be" using import, which loads either .be or .bec file
"import string "
"try "
"load('preinit.be') "
"except .. as e,m "
"if e=='io_error' && string.find(m, \"preinit.be\")>0 "
"log(\"BRY: no preinit.be\") "
"else "
"log(\"BRY: exception in preinit.be: \"+e+\": \"+m) "
"end "
"end "
;
2021-02-28 19:50:37 +00:00
#endif // USE_BERRY