Tasmota/tasmota/xdrv_52_0_berry_struct.ino

89 lines
2.8 KiB
Arduino
Raw Normal View History

2021-03-20 17:44:35 +00:00
/*
xdrv_52_0_berry_struct.ino - Berry scripting language, native fucnctions
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 <LList.h>
2021-03-20 17:44:35 +00:00
2021-11-25 21:57:37 +00:00
#include "re1.5.h"
#define BERRY_CONSOLE_CMD_DELIMITER "\x01"
2021-11-29 20:51:46 +00:00
class Log_line {
public:
Log_line() : log_line(nullptr) {}
~Log_line() {
if (log_line != nullptr) {
berry_free(log_line);
}
}
char * getBuffer() { return log_line; }
char * allocate(size_t size) {
if (log_line != nullptr) {
berry_free(log_line);
}
log_line = (char*) berry_malloc(size);
return log_line;
}
char * log_line;
};
// typedef LList_elt<Log_line> log_elt; // store the string after the header to avoid double allocation if we had used char*
2021-03-20 17:44:35 +00:00
class BerryLog {
public:
inline bool isEmpty(void) const { return log.isEmpty(); }
2021-11-29 20:51:46 +00:00
LList_elt<Log_line> * addString(const char * s, const char * prefix = nullptr, const char * suffix = nullptr) {
2021-03-20 17:44:35 +00:00
if (suffix == nullptr) { suffix = ""; }
if (prefix == nullptr) { prefix = ""; }
if (s == nullptr) { s = ""; }
size_t s_len = strlen_P(s) + strlen_P(prefix) + strlen_P(suffix);
if (0 == s_len) { return nullptr; } // do nothing
2021-11-29 20:51:46 +00:00
LList_elt<Log_line> * log_elt = new LList_elt<Log_line>();
log_elt->val().allocate(s_len + 1);
snprintf_P(log_elt->val().getBuffer(), s_len+1, PSTR("%s%s%s"), prefix, s, suffix);
log.addToLast(log_elt);
return log_elt;
2021-03-20 17:44:35 +00:00
}
void reset(void) {
log.reset();
}
2021-11-29 20:51:46 +00:00
LList<Log_line> log;
2021-03-20 17:44:35 +00:00
};
class BerrySupport {
public:
bvm *vm = nullptr; // berry vm
2021-10-19 19:14:31 +01:00
int32_t timeout = 0; // Berry heartbeat timeout, preventing code to run for too long. `0` means not enabled
2021-03-20 17:44:35 +00:00
bool rules_busy = false; // are we already processing rules, avoid infinite loop
bool autoexec_done = false; // do we still need to load 'autoexec.be'
bool repl_active = false; // is REPL running (activates log recording)
// output log is stored as a LinkedList of buffers
// and active only when a REPL command is running
BerryLog log;
};
BerrySupport berry;
#endif // USE_BERRY