diff --git a/CHANGELOG.md b/CHANGELOG.md index d73601d5f..c5fac9cb0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file. - Command ``SetOption129 1`` to enable split total energy results (#13030) - Commands ``EnergyTotal``, ``EnergyToday`` and ``EnergyYesterday`` to (re)set energy values - Commands ``EnergyUsage`` and ``EnergyExport`` to (re)set energy usage and export values +- Berry add module ``import persist`` ### Breaking Changed - ESP32 LVGL updated to v8.0.2 diff --git a/lib/libesp32/Berry/default/be_modtab.c b/lib/libesp32/Berry/default/be_modtab.c index 44737b5ba..aa795dbff 100644 --- a/lib/libesp32/Berry/default/be_modtab.c +++ b/lib/libesp32/Berry/default/be_modtab.c @@ -24,6 +24,7 @@ be_extern_native_module(introspect); be_extern_native_module(strict); /* Tasmota specific */ +be_extern_native_module(persist); be_extern_native_module(light); be_extern_native_module(gpio); be_extern_native_module(energy); @@ -80,6 +81,7 @@ BERRY_LOCAL const bntvmodule* const be_module_table[] = { /* user-defined modules register start */ &be_native_module(path), + &be_native_module(persist), &be_native_module(gpio), #ifdef USE_LIGHT &be_native_module(light), diff --git a/lib/libesp32/Berry/default/be_persist_lib.c b/lib/libesp32/Berry/default/be_persist_lib.c new file mode 100644 index 000000000..0626c966b --- /dev/null +++ b/lib/libesp32/Berry/default/be_persist_lib.c @@ -0,0 +1,382 @@ +/******************************************************************** + * Tasmota lib + * + * To use: `import power` + * + * read power values + *******************************************************************/ +#include "be_constobj.h" + +/******************************************************************** +** Solidified function: setmember +********************************************************************/ +be_local_closure(setmember, /* name */ + be_nested_proto( + 4, /* nstack */ + 3, /* argc */ + 0, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[ 2]) { /* constants */ + /* K0 */ be_nested_string("p", -183745553, 1), + /* K1 */ be_nested_string("dirty", -1627386213, 5), + }), + (be_nested_const_str("setmember", 1432909441, 9)), + (be_nested_const_str("stdin", -1529146723, 5)), + ( &(const binstruction[ 5]) { /* code */ + 0x880C0100, // 0000 GETMBR R3 R0 K0 + 0x980C0202, // 0001 SETIDX R3 R1 R2 + 0x500C0200, // 0002 LDBOOL R3 1 0 + 0x90020203, // 0003 SETMBR R0 K1 R3 + 0x80000000, // 0004 RET 0 + }) + ) +); +/*******************************************************************/ + + +/******************************************************************** +** Solidified function: save +********************************************************************/ +be_local_closure(save, /* name */ + be_nested_proto( + 8, /* nstack */ + 1, /* argc */ + 0, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[ 8]) { /* constants */ + /* K0 */ be_nested_string("json", 916562499, 4), + /* K1 */ be_nested_string("filename", 1058081160, 8), + /* K2 */ be_nested_string("w", -234078410, 1), + /* K3 */ be_nested_string("write", -1104765092, 5), + /* K4 */ be_nested_string("dump", -631966073, 4), + /* K5 */ be_nested_string("p", -183745553, 1), + /* K6 */ be_nested_string("close", 667630371, 5), + /* K7 */ be_nested_string("dirty", -1627386213, 5), + }), + (be_nested_const_str("save", -855671224, 4)), + (be_nested_const_str("stdin", -1529146723, 5)), + ( &(const binstruction[30]) { /* code */ + 0xA4060000, // 0000 IMPORT R1 K0 + 0x4C080000, // 0001 LDNIL R2 + 0xA802000D, // 0002 EXBLK 0 #0011 + 0x600C0011, // 0003 GETGBL R3 G17 + 0x88100101, // 0004 GETMBR R4 R0 K1 + 0x58140002, // 0005 LDCONST R5 K2 + 0x7C0C0400, // 0006 CALL R3 2 + 0x5C080600, // 0007 MOVE R2 R3 + 0x8C0C0503, // 0008 GETMET R3 R2 K3 + 0x8C140304, // 0009 GETMET R5 R1 K4 + 0x881C0105, // 000A GETMBR R7 R0 K5 + 0x7C140400, // 000B CALL R5 2 + 0x7C0C0400, // 000C CALL R3 2 + 0x8C0C0506, // 000D GETMET R3 R2 K6 + 0x7C0C0200, // 000E CALL R3 1 + 0xA8040001, // 000F EXBLK 1 1 + 0x70020009, // 0010 JMP #001B + 0xAC0C0002, // 0011 CATCH R3 0 2 + 0x70020006, // 0012 JMP #001A + 0x4C140000, // 0013 LDNIL R5 + 0x20140405, // 0014 NE R5 R2 R5 + 0x78160001, // 0015 JMPF R5 #0018 + 0x8C140506, // 0016 GETMET R5 R2 K6 + 0x7C140200, // 0017 CALL R5 1 + 0xB0040604, // 0018 RAISE 1 R3 R4 + 0x70020000, // 0019 JMP #001B + 0xB0080000, // 001A RAISE 2 R0 R0 + 0x500C0000, // 001B LDBOOL R3 0 0 + 0x90020E03, // 001C SETMBR R0 K7 R3 + 0x80000000, // 001D RET 0 + }) + ) +); +/*******************************************************************/ + + +/******************************************************************** +** Solidified function: init +********************************************************************/ +be_local_closure(init, /* name */ + be_nested_proto( + 6, /* nstack */ + 2, /* argc */ + 0, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[ 6]) { /* constants */ + /* K0 */ be_nested_string("filename", 1058081160, 8), + /* K1 */ be_nested_string("_persist.json", 2008425138, 13), + /* K2 */ be_nested_string("p", -183745553, 1), + /* K3 */ be_nested_string("copy", -446502332, 4), + /* K4 */ be_nested_string("load", -435725847, 4), + /* K5 */ be_nested_string("dirty", -1627386213, 5), + }), + (be_nested_const_str("init", 380752755, 4)), + (be_nested_const_str("stdin", -1529146723, 5)), + ( &(const binstruction[20]) { /* code */ + 0x90020101, // 0000 SETMBR R0 K0 K1 + 0x6008000F, // 0001 GETGBL R2 G15 + 0x5C0C0200, // 0002 MOVE R3 R1 + 0x60100013, // 0003 GETGBL R4 G19 + 0x7C080400, // 0004 CALL R2 2 + 0x780A0003, // 0005 JMPF R2 #000A + 0x8C080303, // 0006 GETMET R2 R1 K3 + 0x7C080200, // 0007 CALL R2 1 + 0x90020402, // 0008 SETMBR R0 K2 R2 + 0x70020002, // 0009 JMP #000D + 0x60080013, // 000A GETGBL R2 G19 + 0x7C080000, // 000B CALL R2 0 + 0x90020402, // 000C SETMBR R0 K2 R2 + 0x8C080104, // 000D GETMET R2 R0 K4 + 0x88100102, // 000E GETMBR R4 R0 K2 + 0x88140100, // 000F GETMBR R5 R0 K0 + 0x7C080600, // 0010 CALL R2 3 + 0x50080000, // 0011 LDBOOL R2 0 0 + 0x90020A02, // 0012 SETMBR R0 K5 R2 + 0x80000000, // 0013 RET 0 + }) + ) +); +/*******************************************************************/ + + +/******************************************************************** +** Solidified function: zero +********************************************************************/ +be_local_closure(zero, /* name */ + be_nested_proto( + 2, /* nstack */ + 1, /* argc */ + 0, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[ 2]) { /* constants */ + /* K0 */ be_nested_string("p", -183745553, 1), + /* K1 */ be_nested_string("dirty", -1627386213, 5), + }), + (be_nested_const_str("zero", -1955600541, 4)), + (be_nested_const_str("stdin", -1529146723, 5)), + ( &(const binstruction[ 6]) { /* code */ + 0x60040013, // 0000 GETGBL R1 G19 + 0x7C040000, // 0001 CALL R1 0 + 0x90020001, // 0002 SETMBR R0 K0 R1 + 0x50040200, // 0003 LDBOOL R1 1 0 + 0x90020201, // 0004 SETMBR R0 K1 R1 + 0x80000000, // 0005 RET 0 + }) + ) +); +/*******************************************************************/ + + +/******************************************************************** +** Solidified function: tostring +********************************************************************/ +be_local_closure(tostring, /* name */ + be_nested_proto( + 8, /* nstack */ + 1, /* argc */ + 0, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[ 4]) { /* constants */ + /* K0 */ be_nested_string("string", 398550328, 6), + /* K1 */ be_nested_string("format", -1180859054, 6), + /* K2 */ be_nested_string("", 1553432777, 18), + /* K3 */ be_nested_string("p", -183745553, 1), + }), + (be_nested_const_str("tostring", -1995258651, 8)), + (be_nested_const_str("stdin", -1529146723, 5)), + ( &(const binstruction[11]) { /* code */ + 0xA4060000, // 0000 IMPORT R1 K0 + 0x8C080301, // 0001 GETMET R2 R1 K1 + 0x58100002, // 0002 LDCONST R4 K2 + 0x60140005, // 0003 GETGBL R5 G5 + 0x5C180000, // 0004 MOVE R6 R0 + 0x7C140200, // 0005 CALL R5 1 + 0x60180008, // 0006 GETGBL R6 G8 + 0x881C0103, // 0007 GETMBR R7 R0 K3 + 0x7C180200, // 0008 CALL R6 1 + 0x7C080800, // 0009 CALL R2 4 + 0x80040400, // 000A RET 1 R2 + }) + ) +); +/*******************************************************************/ + + +/******************************************************************** +** Solidified function: member +********************************************************************/ +be_local_closure(member, /* name */ + be_nested_proto( + 5, /* nstack */ + 2, /* argc */ + 0, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[ 2]) { /* constants */ + /* K0 */ be_nested_string("p", -183745553, 1), + /* K1 */ be_nested_string("find", -1108310694, 4), + }), + (be_nested_const_str("member", 719708611, 6)), + (be_nested_const_str("stdin", -1529146723, 5)), + ( &(const binstruction[ 5]) { /* code */ + 0x88080100, // 0000 GETMBR R2 R0 K0 + 0x8C080501, // 0001 GETMET R2 R2 K1 + 0x5C100200, // 0002 MOVE R4 R1 + 0x7C080400, // 0003 CALL R2 2 + 0x80040400, // 0004 RET 1 R2 + }) + ) +); +/*******************************************************************/ + + +/******************************************************************** +** Solidified function: load +********************************************************************/ +be_local_closure(load, /* name */ + be_nested_proto( + 9, /* nstack */ + 1, /* argc */ + 0, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[10]) { /* constants */ + /* K0 */ be_nested_string("json", 916562499, 4), + /* K1 */ be_nested_string("path", -2071507658, 4), + /* K2 */ be_nested_string("exists", 1002329533, 6), + /* K3 */ be_nested_string("filename", 1058081160, 8), + /* K4 */ be_nested_string("r", -150190315, 1), + /* K5 */ be_nested_string("load", -435725847, 4), + /* K6 */ be_nested_string("read", -824204347, 4), + /* K7 */ be_nested_string("close", 667630371, 5), + /* K8 */ be_nested_string("p", -183745553, 1), + /* K9 */ be_nested_string("dirty", -1627386213, 5), + }), + (be_nested_const_str("load", -435725847, 4)), + (be_nested_const_str("stdin", -1529146723, 5)), + ( &(const binstruction[37]) { /* code */ + 0xA4060000, // 0000 IMPORT R1 K0 + 0xA40A0200, // 0001 IMPORT R2 K1 + 0x4C0C0000, // 0002 LDNIL R3 + 0x4C100000, // 0003 LDNIL R4 + 0x8C140502, // 0004 GETMET R5 R2 K2 + 0x881C0103, // 0005 GETMBR R7 R0 K3 + 0x7C140400, // 0006 CALL R5 2 + 0x7816001B, // 0007 JMPF R5 #0024 + 0xA802000D, // 0008 EXBLK 0 #0017 + 0x60140011, // 0009 GETGBL R5 G17 + 0x88180103, // 000A GETMBR R6 R0 K3 + 0x581C0004, // 000B LDCONST R7 K4 + 0x7C140400, // 000C CALL R5 2 + 0x5C0C0A00, // 000D MOVE R3 R5 + 0x8C140305, // 000E GETMET R5 R1 K5 + 0x8C1C0706, // 000F GETMET R7 R3 K6 + 0x7C1C0200, // 0010 CALL R7 1 + 0x7C140400, // 0011 CALL R5 2 + 0x5C100A00, // 0012 MOVE R4 R5 + 0x8C140707, // 0013 GETMET R5 R3 K7 + 0x7C140200, // 0014 CALL R5 1 + 0xA8040001, // 0015 EXBLK 1 1 + 0x70020009, // 0016 JMP #0021 + 0xAC140002, // 0017 CATCH R5 0 2 + 0x70020006, // 0018 JMP #0020 + 0x4C1C0000, // 0019 LDNIL R7 + 0x201C0607, // 001A NE R7 R3 R7 + 0x781E0001, // 001B JMPF R7 #001E + 0x8C1C0707, // 001C GETMET R7 R3 K7 + 0x7C1C0200, // 001D CALL R7 1 + 0xB0040A06, // 001E RAISE 1 R5 R6 + 0x70020000, // 001F JMP #0021 + 0xB0080000, // 0020 RAISE 2 R0 R0 + 0x90021004, // 0021 SETMBR R0 K8 R4 + 0x50140000, // 0022 LDBOOL R5 0 0 + 0x90021205, // 0023 SETMBR R0 K9 R5 + 0x80000000, // 0024 RET 0 + }) + ) +); +/*******************************************************************/ + + +/******************************************************************** +** Solidified class: Persist +********************************************************************/ +be_local_class(Persist, + 3, + NULL, + be_nested_map(10, + ( (struct bmapnode*) &(const bmapnode[]) { + { be_nested_key("filename", 1058081160, 8, -1), be_const_var(0) }, + { be_nested_key("setmember", 1432909441, 9, 7), be_const_closure(setmember_closure) }, + { be_nested_key("save", -855671224, 4, -1), be_const_closure(save_closure) }, + { be_nested_key("dirty", -1627386213, 5, 8), be_const_var(2) }, + { be_nested_key("init", 380752755, 4, -1), be_const_closure(init_closure) }, + { be_nested_key("zero", -1955600541, 4, 6), be_const_closure(zero_closure) }, + { be_nested_key("tostring", -1995258651, 8, 4), be_const_closure(tostring_closure) }, + { be_nested_key("member", 719708611, 6, -1), be_const_closure(member_closure) }, + { be_nested_key("p", -183745553, 1, -1), be_const_var(1) }, + { be_nested_key("load", -435725847, 4, -1), be_const_closure(load_closure) }, + })), + (be_nested_const_str("Persist", 1456346147, 7)) +); + +/******************************************************************** +** Solidified function: _anonymous_ +********************************************************************/ +be_local_closure(_anonymous_, /* name */ + be_nested_proto( + 3, /* nstack */ + 1, /* argc */ + 0, /* varg */ + 0, /* has upvals */ + NULL, /* no upvals */ + 0, /* has sup protos */ + NULL, /* no sub protos */ + 1, /* has constants */ + ( &(const bvalue[ 1]) { /* constants */ + /* K0 */ be_const_class(be_class_Persist), + }), + (be_nested_const_str("_anonymous_", 1957281476, 11)), + (be_nested_const_str("stdin", -1529146723, 5)), + ( &(const binstruction[ 5]) { /* code */ + 0x58040000, // 0000 LDCONST R1 K0 + 0xB4000000, // 0001 CLASS K0 + 0x5C080200, // 0002 MOVE R2 R1 + 0x7C080000, // 0003 CALL R2 0 + 0x80040400, // 0004 RET 1 R2 + }) + ) +); +/*******************************************************************/ + +/* @const_object_info_begin +module persist (scope: global) { + init, closure(_anonymous__closure) +} +@const_object_info_end */ +#include "../generate/be_fixed_persist.h" diff --git a/lib/libesp32/Berry/default/be_tasmotalib.c b/lib/libesp32/Berry/default/be_tasmotalib.c index 556647998..764ec512d 100644 --- a/lib/libesp32/Berry/default/be_tasmotalib.c +++ b/lib/libesp32/Berry/default/be_tasmotalib.c @@ -1412,7 +1412,7 @@ be_local_closure(gc, /* name */ ********************************************************************/ be_local_closure(event, /* name */ be_nested_proto( - 19, /* nstack */ + 20, /* nstack */ 6, /* argc */ 0, /* varg */ 0, /* has upvals */ @@ -1420,7 +1420,7 @@ be_local_closure(event, /* name */ 0, /* has sup protos */ NULL, /* no sub protos */ 1, /* has constants */ - ( &(const bvalue[15]) { /* constants */ + ( &(const bvalue[18]) { /* constants */ /* K0 */ be_nested_string("introspect", 164638290, 10), /* K1 */ be_nested_string("every_50ms", -1911083288, 10), /* K2 */ be_nested_string("run_deferred", 371594696, 12), @@ -1436,89 +1436,96 @@ be_local_closure(event, /* name */ /* K12 */ be_nested_string("format", -1180859054, 6), /* K13 */ be_nested_string("BRY: Exception> '%s' - %s", -2047976332, 25), /* K14 */ be_nested_string("stop_iteration", -121173395, 14), + /* K15 */ be_nested_string("save_before_restart", 1253239338, 19), + /* K16 */ be_nested_string("persist", -377883517, 7), + /* K17 */ be_nested_string("save", -855671224, 4), }), (be_nested_const_str("event", -30355297, 5)), - (be_nested_const_str("Tasmota.be", 825809411, 10)), - ( &(const binstruction[79]) { /* code */ - 0xA41A0000, // 0000 IMPORT R6 K0 - 0x1C1C0301, // 0001 EQ R7 R1 K1 - 0x781E0001, // 0002 JMPF R7 #0005 - 0x8C1C0102, // 0003 GETMET R7 R0 K2 - 0x7C1C0200, // 0004 CALL R7 1 - 0x1C1C0303, // 0005 EQ R7 R1 K3 - 0x781E0006, // 0006 JMPF R7 #000E - 0x8C1C0104, // 0007 GETMET R7 R0 K4 - 0x5C240400, // 0008 MOVE R9 R2 - 0x5C280600, // 0009 MOVE R10 R3 - 0x5C2C0800, // 000A MOVE R11 R4 - 0x7C1C0800, // 000B CALL R7 4 - 0x80040E00, // 000C RET 1 R7 - 0x7002003F, // 000D JMP #004E - 0x1C1C0305, // 000E EQ R7 R1 K5 - 0x781E0004, // 000F JMPF R7 #0015 - 0x8C1C0106, // 0010 GETMET R7 R0 K6 - 0x5C240800, // 0011 MOVE R9 R4 - 0x7C1C0400, // 0012 CALL R7 2 - 0x80040E00, // 0013 RET 1 R7 - 0x70020038, // 0014 JMP #004E - 0x1C1C0307, // 0015 EQ R7 R1 K7 - 0x781E0003, // 0016 JMPF R7 #001B - 0x8C1C0107, // 0017 GETMET R7 R0 K7 - 0x7C1C0200, // 0018 CALL R7 1 - 0x80040E00, // 0019 RET 1 R7 - 0x70020032, // 001A JMP #004E - 0x881C0108, // 001B GETMBR R7 R0 K8 - 0x781E0030, // 001C JMPF R7 #004E - 0x601C0010, // 001D GETGBL R7 G16 - 0x88200108, // 001E GETMBR R8 R0 K8 - 0x7C1C0200, // 001F CALL R7 1 - 0xA8020027, // 0020 EXBLK 0 #0049 - 0x5C200E00, // 0021 MOVE R8 R7 - 0x7C200000, // 0022 CALL R8 0 - 0x8C240D09, // 0023 GETMET R9 R6 K9 - 0x5C2C1000, // 0024 MOVE R11 R8 - 0x5C300200, // 0025 MOVE R12 R1 - 0x7C240600, // 0026 CALL R9 3 - 0x60280004, // 0027 GETGBL R10 G4 - 0x5C2C1200, // 0028 MOVE R11 R9 - 0x7C280200, // 0029 CALL R10 1 - 0x1C28150A, // 002A EQ R10 R10 K10 - 0x782A001B, // 002B JMPF R10 #0048 - 0xA802000E, // 002C EXBLK 0 #003C - 0x5C281200, // 002D MOVE R10 R9 - 0x5C2C1000, // 002E MOVE R11 R8 - 0x5C300400, // 002F MOVE R12 R2 - 0x5C340600, // 0030 MOVE R13 R3 - 0x5C380800, // 0031 MOVE R14 R4 - 0x5C3C0A00, // 0032 MOVE R15 R5 - 0x7C280A00, // 0033 CALL R10 5 - 0x502C0200, // 0034 LDBOOL R11 1 0 - 0x1C2C140B, // 0035 EQ R11 R10 R11 - 0x782E0002, // 0036 JMPF R11 #003A - 0x502C0200, // 0037 LDBOOL R11 1 0 - 0xA8040002, // 0038 EXBLK 1 2 - 0x80041600, // 0039 RET 1 R11 - 0xA8040001, // 003A EXBLK 1 1 - 0x7002000B, // 003B JMP #0048 - 0xAC280002, // 003C CATCH R10 0 2 - 0x70020008, // 003D JMP #0047 - 0xA4321600, // 003E IMPORT R12 K11 - 0x60340001, // 003F GETGBL R13 G1 - 0x8C38190C, // 0040 GETMET R14 R12 K12 - 0x5840000D, // 0041 LDCONST R16 K13 - 0x5C441400, // 0042 MOVE R17 R10 - 0x5C481600, // 0043 MOVE R18 R11 - 0x7C380800, // 0044 CALL R14 4 - 0x7C340200, // 0045 CALL R13 1 - 0x70020000, // 0046 JMP #0048 - 0xB0080000, // 0047 RAISE 2 R0 R0 - 0x7001FFD7, // 0048 JMP #0021 - 0x581C000E, // 0049 LDCONST R7 K14 - 0xAC1C0200, // 004A CATCH R7 1 0 - 0xB0080000, // 004B RAISE 2 R0 R0 - 0x501C0000, // 004C LDBOOL R7 0 0 - 0x80040E00, // 004D RET 1 R7 - 0x80000000, // 004E RET 0 + (be_nested_const_str("input", -103256197, 5)), + ( &(const binstruction[83]) { /* code */ + 0xA41A0000, // 0000 IMPORT R6 K0 + 0x1C1C0301, // 0001 EQ R7 R1 K1 + 0x781E0001, // 0002 JMPF R7 #0005 + 0x8C1C0102, // 0003 GETMET R7 R0 K2 + 0x7C1C0200, // 0004 CALL R7 1 + 0x501C0000, // 0005 LDBOOL R7 0 0 + 0x1C200303, // 0006 EQ R8 R1 K3 + 0x78220006, // 0007 JMPF R8 #000F + 0x8C200104, // 0008 GETMET R8 R0 K4 + 0x5C280400, // 0009 MOVE R10 R2 + 0x5C2C0600, // 000A MOVE R11 R3 + 0x5C300800, // 000B MOVE R12 R4 + 0x7C200800, // 000C CALL R8 4 + 0x80041000, // 000D RET 1 R8 + 0x7002003D, // 000E JMP #004D + 0x1C200305, // 000F EQ R8 R1 K5 + 0x78220004, // 0010 JMPF R8 #0016 + 0x8C200106, // 0011 GETMET R8 R0 K6 + 0x5C280800, // 0012 MOVE R10 R4 + 0x7C200400, // 0013 CALL R8 2 + 0x80041000, // 0014 RET 1 R8 + 0x70020036, // 0015 JMP #004D + 0x1C200307, // 0016 EQ R8 R1 K7 + 0x78220003, // 0017 JMPF R8 #001C + 0x8C200107, // 0018 GETMET R8 R0 K7 + 0x7C200200, // 0019 CALL R8 1 + 0x80041000, // 001A RET 1 R8 + 0x70020030, // 001B JMP #004D + 0x88200108, // 001C GETMBR R8 R0 K8 + 0x7822002E, // 001D JMPF R8 #004D + 0x60200010, // 001E GETGBL R8 G16 + 0x88240108, // 001F GETMBR R9 R0 K8 + 0x7C200200, // 0020 CALL R8 1 + 0xA8020027, // 0021 EXBLK 0 #004A + 0x5C241000, // 0022 MOVE R9 R8 + 0x7C240000, // 0023 CALL R9 0 + 0x8C280D09, // 0024 GETMET R10 R6 K9 + 0x5C301200, // 0025 MOVE R12 R9 + 0x5C340200, // 0026 MOVE R13 R1 + 0x7C280600, // 0027 CALL R10 3 + 0x602C0004, // 0028 GETGBL R11 G4 + 0x5C301400, // 0029 MOVE R12 R10 + 0x7C2C0200, // 002A CALL R11 1 + 0x1C2C170A, // 002B EQ R11 R11 K10 + 0x782E0019, // 002C JMPF R11 #0047 + 0xA802000C, // 002D EXBLK 0 #003B + 0x5C2C1400, // 002E MOVE R11 R10 + 0x5C301200, // 002F MOVE R12 R9 + 0x5C340400, // 0030 MOVE R13 R2 + 0x5C380600, // 0031 MOVE R14 R3 + 0x5C3C0800, // 0032 MOVE R15 R4 + 0x5C400A00, // 0033 MOVE R16 R5 + 0x7C2C0A00, // 0034 CALL R11 5 + 0x5C1C1600, // 0035 MOVE R7 R11 + 0x781E0001, // 0036 JMPF R7 #0039 + 0xA8040001, // 0037 EXBLK 1 1 + 0x7002000E, // 0038 JMP #0048 + 0xA8040001, // 0039 EXBLK 1 1 + 0x7002000B, // 003A JMP #0047 + 0xAC2C0002, // 003B CATCH R11 0 2 + 0x70020008, // 003C JMP #0046 + 0xA4361600, // 003D IMPORT R13 K11 + 0x60380001, // 003E GETGBL R14 G1 + 0x8C3C1B0C, // 003F GETMET R15 R13 K12 + 0x5844000D, // 0040 LDCONST R17 K13 + 0x5C481600, // 0041 MOVE R18 R11 + 0x5C4C1800, // 0042 MOVE R19 R12 + 0x7C3C0800, // 0043 CALL R15 4 + 0x7C380200, // 0044 CALL R14 1 + 0x70020000, // 0045 JMP #0047 + 0xB0080000, // 0046 RAISE 2 R0 R0 + 0x7001FFD9, // 0047 JMP #0022 + 0xA8040001, // 0048 EXBLK 1 1 + 0x70020002, // 0049 JMP #004D + 0x5820000E, // 004A LDCONST R8 K14 + 0xAC200200, // 004B CATCH R8 1 0 + 0xB0080000, // 004C RAISE 2 R0 R0 + 0x1C20030F, // 004D EQ R8 R1 K15 + 0x78220002, // 004E JMPF R8 #0052 + 0xA4222000, // 004F IMPORT R8 K16 + 0x8C241111, // 0050 GETMET R9 R8 K17 + 0x7C240200, // 0051 CALL R9 1 + 0x80040E00, // 0052 RET 1 R7 }) ) ); diff --git a/lib/libesp32/Berry/default/embedded/Tasmota.be b/lib/libesp32/Berry/default/embedded/Tasmota.be index f2a2f20cb..4fbd4c991 100644 --- a/lib/libesp32/Berry/default/embedded/Tasmota.be +++ b/lib/libesp32/Berry/default/embedded/Tasmota.be @@ -291,6 +291,7 @@ class Tasmota import introspect if event_type=='every_50ms' self.run_deferred() end #- first run deferred events -# + var done = false if event_type=='cmd' return self.exec_cmd(cmd, idx, payload) elif event_type=='rule' return self.exec_rules(payload) elif event_type=='gc' return self.gc() @@ -299,16 +300,23 @@ class Tasmota var f = introspect.get(d, event_type) # try to match a function or method with the same name if type(f) == 'function' try - var done = f(d, cmd, idx, payload, raw) - if done == true return true end + done = f(d, cmd, idx, payload, raw) + if done break end except .. as e,m import string print(string.format("BRY: Exception> '%s' - %s", e, m)) end end end - return false end + + # save persist + if event_type=='save_before_restart' + import persist + persist.save() + end + + return done end def add_driver(d) diff --git a/lib/libesp32/Berry/default/embedded/persist.be b/lib/libesp32/Berry/default/embedded/persist.be new file mode 100644 index 000000000..9f5d7fd34 --- /dev/null +++ b/lib/libesp32/Berry/default/embedded/persist.be @@ -0,0 +1,98 @@ +#- persistance module for Berry -# +#- -# +#- To solidify: -# +#- + # load only persis_module and persist_module.init + import solidify + solidify.dump(persist_module.init) + # copy and paste into `be_persist_lib.c` +-# +var persist_module = module("persist") + +persist_module.init = def (m) + + class Persist + var filename + var p + var dirty + + #- persist can be initialized with pre-existing values. The map is not copied so any change will be reflected -# + def init(m) + # print("Persist init") + self.filename = '_persist.json' + if isinstance(m,map) + self.p = m.copy() # need to copy instead? + else + self.p = {} + end + self.load(self.p, self.filename) + self.dirty = false + # print("Persist init") + end + + #- virtual member getter, if a key does not exists return `nil`-# + def member(key) + return self.p.find(key) + end + + #- virtual member setter -# + def setmember(key, value) + self.p[key] = value + self.dirty = true + end + + #- clear all entries -# + def zero() + self.p = {} + self.dirty = true + end + + #- custom tostring -# + def tostring() + import string + return string.format("", classname(self), str(self.p)) + end + + def load() + import json + import path + var f # file object + var val # values loaded from json + + if path.exists(self.filename) + try + f = open(self.filename, "r") + val = json.load(f.read()) + f.close() + except .. as e, m + if f != nil f.close() end + raise e, m + end + self.p = val + self.dirty = false + end + + # print("Loading") + end + + def save() + import json + + var f # file object + try + f = open(self.filename, "w") + f.write(json.dump(self.p)) + f.close() + except .. as e, m + if f != nil f.close() end + raise e, m + end + self.dirty = false + # print("Saving") + end + end + + return Persist() # return an instance of this class +end + +return persist_module diff --git a/lib/libesp32/Berry/generate/be_fixed_persist.h b/lib/libesp32/Berry/generate/be_fixed_persist.h new file mode 100644 index 000000000..2bcedb715 --- /dev/null +++ b/lib/libesp32/Berry/generate/be_fixed_persist.h @@ -0,0 +1,17 @@ +#include "be_constobj.h" + +static be_define_const_map_slots(m_libpersist_map) { + { be_const_key(init, -1), be_const_closure(_anonymous__closure) }, +}; + +static be_define_const_map( + m_libpersist_map, + 1 +); + +static be_define_const_module( + m_libpersist, + "persist" +); + +BE_EXPORT_VARIABLE be_define_const_native_module(persist, NULL); diff --git a/lib/libesp32/Berry/src/be_module.c b/lib/libesp32/Berry/src/be_module.c index afd7e146d..b69eb35cb 100644 --- a/lib/libesp32/Berry/src/be_module.c +++ b/lib/libesp32/Berry/src/be_module.c @@ -254,14 +254,18 @@ static void cache_module(bvm *vm, bstring *name) /* Try to run '()' function of module. Module is already loaded. */ static void module_init(bvm *vm) { - if (be_getmember(vm, -1, "init")) { - /* found, call it with current module as parameter */ - be_pushvalue(vm, -2); - be_call(vm, 1); - /* we don't care about the result */ - be_pop(vm, 1); + if (be_ismodule(vm, -1)) { + if (be_getmember(vm, -1, "init")) { + /* found, call it with current module as parameter */ + be_pushvalue(vm, -2); + be_call(vm, 1); + /* the result of init() is cached and returned */ + be_pop(vm, 1); + be_remove(vm, -2); /* remove initial module */ + } else { + be_pop(vm, 1); + } } - be_pop(vm, 1); } /* load module to vm->top */ @@ -273,9 +277,9 @@ int be_module_load(bvm *vm, bstring *path) if (res == BE_IO_ERROR) res = load_package(vm, path); if (res == BE_OK) { - cache_module(vm, path); /* on first load of the module, try running the '()' function */ module_init(vm); + cache_module(vm, path); } } return res; diff --git a/lib/libesp32/Berry/src/be_parser.c b/lib/libesp32/Berry/src/be_parser.c index cc4ae7969..21d4e9ae2 100644 --- a/lib/libesp32/Berry/src/be_parser.c +++ b/lib/libesp32/Berry/src/be_parser.c @@ -602,12 +602,12 @@ static bproto* funcbody(bparser *parser, bstring *name, int type) return finfo.proto; /* return fully constructed `bproto` */ } -/* anonymous function, build `bproto` object with name `` */ +/* anonymous function, build `bproto` object with name `_anonymous_` */ /* and build a expdesc for the bproto */ static void anon_func(bparser *parser, bexpdesc *e) { bproto *proto; - bstring *name = parser_newstr(parser, ""); + bstring *name = parser_newstr(parser, "_anonymous_"); /* 'def' ID '(' varlist ')' block 'end' */ scan_next_token(parser); /* skip 'def' */ proto = funcbody(parser, name, FUNC_ANONYMOUS); diff --git a/lib/libesp32/Berry/src/be_solidifylib.c b/lib/libesp32/Berry/src/be_solidifylib.c index eebc0ab9e..8e4447a9a 100644 --- a/lib/libesp32/Berry/src/be_solidifylib.c +++ b/lib/libesp32/Berry/src/be_solidifylib.c @@ -47,14 +47,14 @@ static void m_solidify_bvalue(bvm *vm, bvalue * value) #if BE_INTGER_TYPE == 2 logfmt("be_const_int(%lli)", var_toint(value)); #else - logfmt("be_const_int(%i)", var_toint(value)); + logfmt("be_const_int(%li)", var_toint(value)); #endif break; case BE_INDEX: #if BE_INTGER_TYPE == 2 logfmt("be_const_var(%lli)", var_toint(value)); #else - logfmt("be_const_var(%i)", var_toint(value)); + logfmt("be_const_var(%li)", var_toint(value)); #endif break; case BE_REAL: @@ -78,6 +78,9 @@ static void m_solidify_bvalue(bvm *vm, bvalue * value) case BE_CLOSURE: logfmt("be_const_closure(%s_closure)", str(((bclosure*) var_toobj(value))->proto->name)); break; + case BE_CLASS: + logfmt("be_const_class(be_class_%s)", str(((bclass*) var_toobj(value))->name)); + break; default: { char error[64]; @@ -87,6 +90,23 @@ static void m_solidify_bvalue(bvm *vm, bvalue * value) } } +static void m_solidify_subclass(bvm *vm, bclass *cl, int builtins); + +/* solidify any inner class */ +static void m_solidify_proto_inner_class(bvm *vm, bproto *pr, int builtins) +{ + // parse any class in constants to output it first + if (pr->nconst > 0) { + for (int k = 0; k < pr->nconst; k++) { + if (var_type(&pr->ktab[k]) == BE_CLASS) { + // output the class + m_solidify_subclass(vm, (bclass*) var_toobj(&pr->ktab[k]), builtins); + } + } + } +} + + static void m_solidify_proto(bvm *vm, bproto *pr, const char * func_name, int builtins, int indent) { // const char * func_name = str(pr->name); @@ -172,12 +192,15 @@ static void m_solidify_closure(bvm *vm, bclosure *cl, int builtins) be_raise(vm, "internal_error", "Unsupported upvals in closure"); } + int indent = 2; + + m_solidify_proto_inner_class(vm, pr, builtins); + logfmt("\n"); logfmt("/********************************************************************\n"); logfmt("** Solidified function: %s\n", func_name); logfmt("********************************************************************/\n"); - int indent = 2; logfmt("be_local_closure(%s, /* name */\n", func_name); m_solidify_proto(vm, pr, func_name, builtins, indent); @@ -188,8 +211,7 @@ static void m_solidify_closure(bvm *vm, bclosure *cl, int builtins) logfmt("/*******************************************************************/\n\n"); } - -static void m_solidify_class(bvm *vm, bclass *cl, int builtins) +static void m_solidify_subclass(bvm *vm, bclass *cl, int builtins) { const char * class_name = str(cl->name); @@ -251,6 +273,14 @@ static void m_solidify_class(bvm *vm, bclass *cl, int builtins) logfmt(" (be_nested_const_str(\"%s\", %i, %i))\n", class_name, be_strhash(cl->name), str_len(cl->name)); logfmt(");\n"); + +} + + +static void m_solidify_class(bvm *vm, bclass *cl, int builtins) +{ + const char * class_name = str(cl->name); + m_solidify_subclass(vm, cl, builtins); logfmt("/*******************************************************************/\n\n"); logfmt("void be_load_%s_class(bvm *vm) {\n", class_name); diff --git a/tasmota/xdrv_52_7_berry_embedded.ino b/tasmota/xdrv_52_7_berry_embedded.ino index 8949f983a..89ca8e98a 100644 --- a/tasmota/xdrv_52_7_berry_embedded.ino +++ b/tasmota/xdrv_52_7_berry_embedded.ino @@ -26,7 +26,7 @@ \*********************************************************************************************/ const char berry_prog[] = - "" + "import persist " // create a 'ntv' module to allow functions to be registered in a safe namespace // "ntv = module('ntv') "