From 33c949097fa5740e486256bf3970eff9c67c11e2 Mon Sep 17 00:00:00 2001 From: Ewoud Date: Wed, 6 Nov 2024 17:52:13 +0100 Subject: [PATCH 1/3] Refactor pushToConsole: for all LEDOS and support external printf --- src/ESPLiveScript.h | 44 +++++++++++++-------------- src/NodeToken.h | 20 +----------- src/asm_parser.h | 58 ++++++----------------------------- src/execute.h | 74 +++++++++++++-------------------------------- src/functionlib.h | 37 +++++++++++++++++++++++ 5 files changed, 91 insertions(+), 142 deletions(-) diff --git a/src/ESPLiveScript.h b/src/ESPLiveScript.h index b5b72a4..186edd4 100644 --- a/src/ESPLiveScript.h +++ b/src/ESPLiveScript.h @@ -247,17 +247,17 @@ class Parser parse(); if (Error.error) { - pushToConsole(Error.error_message.c_str(), true); + pushToConsoleForce("%s\r\n", Error.error_message.c_str()); return results; } - pushToConsole("***********PARSING DONE*********"); + pushToConsole("***********PARSING DONE*********\r\n"); updateMem(); buildParents(&program); #ifdef __TEST_DEBUG prettyPrint(&program, ""); #endif program.visitNode(); - pushToConsole("***********COMPILING DONE*********"); + pushToConsole("***********COMPILING DONE*********\r\n"); updateMem(); displayStat(); main_script.clear(); @@ -283,10 +283,10 @@ class Parser updateMem(); displayStat(); - pushToConsole("***********AFTER CLEAN*********"); + pushToConsole("***********AFTER CLEAN*********\r\n"); #ifndef __TEST_DEBUG - pushToConsole("***********CREATE EXECUTABLE*********"); + pushToConsole("***********CREATE EXECUTABLE*********\r\n"); executable _executecmd = createExectutable(&header, &content, __parser_debug); results.setExecutable(_executecmd); content.clear(); @@ -299,7 +299,7 @@ class Parser // exeExist = false; // Serial.printf(termColor.Red); - pushToConsole(_executecmd.error.error_message.c_str(), true); + pushToConsoleForce("%s\r\n", _executecmd.error.error_message.c_str()); } #endif @@ -2642,7 +2642,7 @@ void kill(Console *cons, vector args) } else { - pushToConsole("Nothing is currently running.", true); + pushToConsoleForce("Nothing is currently running.\r\n"); } } else @@ -2699,7 +2699,7 @@ void run(Console *cons, vector args) { if (SCExecutable.isRunning()) { - LedOS.pushToConsole("Something Already running kill it first ..."); + pushToConsole("Something Already running kill it first ...\r\n"); kill(cons, args); } SCExecutable.executeAsTask("main", _args); @@ -2710,7 +2710,7 @@ void run(Console *cons, vector args) /* if (progToRun > scExecutables.size()) { - LedOS.pushToConsole("No executable ...", true); + pushToConsoleForce("No executable ...\r\n"); } else { @@ -2752,7 +2752,7 @@ void parseasm(Console *cons, vector args) { vector d; d.push_back("main"); - LedOS.pushToConsole("***********START RUN *********"); + pushToConsole("***********START RUN *********\r\n"); run(cons, d); if (cons->cmode == keyword) { @@ -2762,31 +2762,31 @@ void parseasm(Console *cons, vector args) } else { - LedOS.pushToConsole("***********START RUN*********"); - LedOS.pushToConsole("Execution asm ...", true); + pushToConsole("***********START RUN*********\r\n"); + pushToConsoleForce("Execution asm ...\r\n"); executeBinary("main", executecmd); - LedOS.pushToConsole("Execution done.", true, true); + pushToConsoleForce("Execution done.\r\n"); } } else { exeExist = false; // Serial.printf(termColor.Red); - LedOS.pushToConsole(executecmd.error.error_message.c_str()); + pushToConsole("%s\r\n", executecmd.error.error_message.c_str()); // Serial.printf(config.ESC_RESET); } */ } void compile_c(Console *cons, vector args) { - pushToConsole("Compiling ...", true); + pushToConsoleForce("Compiling ...\r\n"); Executable _scExec = p.parse_c(&cons->script); if (_scExec.exeExist) { _scExec.name = cons->filename; scriptRuntime.addExe(_scExec); - pushToConsole(string_format("Compiling done. Handle number:%d", scExecutables.size()), true); + pushToConsoleforce("Compiling done. Handle number:%d\r\n", scExecutables.size()); } } void free(Console *cons, vector args) @@ -2808,14 +2808,14 @@ void parse_c(Console *cons, vector args) { if (SCExecutable.isRunning()) { - LedOS.pushToConsole("Something Already running kill it first ..."); + pushToConsole("Something Already running kill it first ...\r\n"); vector k; kill(cons, k); } bool othercore = false; SCExecutable.free(); - LedOS.pushToConsole("Compiling ...", true); + pushToConsoleForce("Compiling ...\r\n"); if (args.size() > 0) { if (args[0].compare("&") != 0) @@ -2833,7 +2833,7 @@ void parse_c(Console *cons, vector args) { vector d; // d.push_back("main"); - LedOS.pushToConsole("***********START RUN *********"); + pushToConsole("***********START RUN *********\r\n"); run(cons, d); if (cons->cmode == keyword) @@ -2844,10 +2844,10 @@ void parse_c(Console *cons, vector args) } else { - LedOS.pushToConsole("Start program", true); + pushToConsoleForce("Start program\r\n"); SCExecutable.execute("main"); // executeBinary("main", executecmd); - LedOS.pushToConsole("Execution done.", true); + pushToConsoleForce("Execution done.\r\n"); } } @@ -3113,7 +3113,7 @@ void artiPrintfln(char const *format, ...) va_end(argp); } void showError(int line, uint32_t size, uint32_t got) { - pushToConsole(string_format("Overflow error max size: %d got %d", size, got), true); + pushToConsoleForce("Overflow error max size: %d got %d\r\n", size, got); } class INIT_PARSER { diff --git a/src/NodeToken.h b/src/NodeToken.h index a61c0e3..3d99554 100644 --- a/src/NodeToken.h +++ b/src/NodeToken.h @@ -10,24 +10,6 @@ using namespace std; #include "tokenizer.h" -void pushToConsole(string str, bool force) -{ -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole(str, force); -#else -#ifndef __TEST_DEBUG - Serial.printf("%s\r\n", str.c_str()); -#else - printf("%s\r\n", str.c_str()); -#endif -#endif -} - -void pushToConsole(string str) -{ - pushToConsole(str, false); -} - #define _START_2 32 #define _STACK_SIZE (_START_2 + 6 * 4) string __globalscript; @@ -135,7 +117,7 @@ void updateMem() } void displayStat() { - pushToConsole(string_format("max used memory: %ld maxstack:%ld started %d free mem:%ld consumed %ld time:%dms", __maxMemUsage, __MaxStackMemory, __startmem, esp_get_free_heap_size(), __startmem - esp_get_free_heap_size(), (__endtime - __starttime) / 240000)); + pushToConsole("max used memory: %ld maxstack:%ld started %d free mem:%ld consumed %ld time:%dms\r\n", __maxMemUsage, __MaxStackMemory, __startmem, esp_get_free_heap_size(), __startmem - esp_get_free_heap_size(), (__endtime - __starttime) / 240000); } string _numToBytes(uint32_t __num) { diff --git a/src/asm_parser.h b/src/asm_parser.h index 0e76b63..c8a4bd2 100644 --- a/src/asm_parser.h +++ b/src/asm_parser.h @@ -1193,12 +1193,7 @@ error_message_struct parseASM(Text *_header, Text *_content, parsedLines *asm_pa // printf("taille instr %d\r\n",_instr_size); _instr_size = (_instr_size / 8) * 8 + 8; -#ifdef __CONSOLE_ESP32 - string _d = string_format("Creation of an %d bytes binary and %d bytes data", _instr_size, _size); - LedOS.pushToConsole(_d); -#else - printf("Creation of an %d bytes binary and %d bytes data\r\n", _instr_size, _size); -#endif + pushToConsole("Creation of an %d bytes binary and %d bytes data\r\n", _instr_size, _size); tmp_exec = (uint8_t *)malloc(_instr_size); if (_size > 0) @@ -1208,12 +1203,7 @@ error_message_struct parseASM(Text *_header, Text *_content, parsedLines *asm_pa _data_size=(_size / 4) * 4 + 4; } // printf("her:\r\n"); -#ifdef __CONSOLE_ESP32 - string d = string_format("Parsing %d assembly lines ... ", _header->size() + _content->size()); - LedOS.pushToConsole(d); -#else - printf("Parsing %d assembly lines ...\r\n ", _header->size() + _content->size()); -#endif + pushToConsole("Parsing %d assembly lines ...\r\n ", _header->size() + _content->size()); int size = _header->size(); int tmp_size = size; @@ -1435,12 +1425,7 @@ void printparsdAsm(uint32_t start_address, parsedLines *asm_parsed) void flagLabel32aligned(parsedLines *asm_parsed) { // return; -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("Flag label(s) to align ... "); - -#else - printf("Flag label(s) to align ... "); -#endif + pushToConsole("Flag label(s) to align ...\r\n"); // uint32_t add = 0; // vector::iterator it = (*asm_parsed).begin(); for (vector::iterator it = asm_parsed->begin(); it != asm_parsed->end(); it++) @@ -1455,24 +1440,13 @@ void flagLabel32aligned(parsedLines *asm_parsed) } } } -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("Done. "); - -#else - printf("Done."); -#endif - // printf("Done.\r\n"); + pushToConsole("Done.\r\n"); } error_message_struct calculateJump(parsedLines *asm_parsed) { -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("alculating jumps ..."); - -#else - //printf("Calculating jumps ... "); -#endif + pushToConsole("Calculating jumps ...\r\n"); error_message_struct error; error.error = 0; @@ -1510,12 +1484,7 @@ error_message_struct calculateJump(parsedLines *asm_parsed) error_message_struct calculateJump(uint8_t *exec, parsedLines *asm_parsed) { -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("alculating jumps 2..."); - -#else - // printf("Calculating jumps2 ... "); -#endif + pushToConsole("Calculating jumps2...\r\n"); error_message_struct error; error.error = 0; @@ -1709,22 +1678,15 @@ executable createExectutable(Text *_header, Text *_content, bool display) void executeBinaryAsm(uint32_t *j, uint32_t *c) { -#ifdef __CONSOLE_ESP32 - string s = string_format("Executing asm code @%x", j); - LedOS.pushToConsole(s, false); -#else - // printf("Executing asm code ...\r\n"); -#endif + pushToConsole("Executing asm code ...\r\n"); + asm volatile("l32i a10,%1,0\n\t" "l32i a15,%0,0\n\t" "callx8 a15" : : "r"(j), "r"(c) :); -#ifdef __CONSOLE_ESP32 - // LedOS.pushToConsole("Execution Done.",true); -#else - // printf("Execution Done.\n"); -#endif + + // pushToConsoleForce("Execution Done.\r\n"); // free(exec); } diff --git a/src/execute.h b/src/execute.h index 2ee19fb..c8a86d1 100644 --- a/src/execute.h +++ b/src/execute.h @@ -342,11 +342,7 @@ class Executable #ifndef __TEST_DEBUG if (_isRunning) { -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("Something Already running kill it first ...", true); -#else - Serial.printf("Something Already running kill it first ...\r\n"); -#endif + pushToConsoleForce("Something Already running kill it first ...\r\n"); kill(); } @@ -368,11 +364,7 @@ class Executable #ifndef __TEST_DEBUG if (_isRunning and !isHalted) { -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("Halting the program ...", true); -#else - Serial.printf("Halting the program...\r\n"); -#endif + pushToConsoleForce("Halting the program ...\r\n", true); resetSync = true; runningPrograms.freeSync(); @@ -387,11 +379,7 @@ class Executable vTaskDelay(10); // vTaskDelay(20); -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("Program Halted.", true); -#else - Serial.printf("Program Halted.\r\n"); -#endif + pushToConsoleForce("Program Halted.\r\n"); } // freeBinary(&_executecmd); @@ -418,11 +406,7 @@ class Executable #ifndef __TEST_DEBUG if (_isRunning) { -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("Stopping the program ...", true); -#else - Serial.printf("Stopping the program...\r\n"); -#endif + pushToConsoleForce("Stopping the program ...\r\n"); // printf("old mask %d\r\n",runningPrograms.getMask()); toResetSync = true; while (!toResetSync) @@ -446,11 +430,7 @@ class Executable runningPrograms.postkill(); // vTaskDelay(20); -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("Program stopped.", true); -#else - Serial.printf("Program stopped.\r\n"); -#endif + pushToConsoleForce("Program stopped.\r\n"); vTaskDelay(10); runningPrograms.removeHandle(__run_handle_index); // printf("new mask %d\r\n",runningPrograms.getMask()); @@ -504,11 +484,7 @@ class Executable if (__run_handle_index == 9999) { -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("too many programs at once", true); -#else - Serial.printf("too many programs at once\r\n"); -#endif + pushToConsoleForce("too many programs at once\r\n"); } string taskname; if (name.compare("Unknow") == 0) @@ -517,19 +493,11 @@ class Executable taskname = string_format("%s_%d", name.c_str(), __run_handle_index); xTaskCreateUniversal(_run_task, taskname.c_str(), 4096 * 2, this, 3, (TaskHandle_t *)runningPrograms.getHandleByIndex(__run_handle_index), core); -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("Execution on going CTRL + k to stop", true); -#else - Serial.printf("Execution on going CTRL + k to stop\r\n"); -#endif + pushToConsoleForce("Execution on going CTRL + k to stop\r\n"); } else { -#ifdef __CONSOLE_ESP32 - LedOS.pushToConsole("Nothing to execute.", true); -#else - Serial.printf("Nothing to execute\r\n"); -#endif + pushToConsoleForce("Nothing to execute\r\n"); } #endif return __run_handle_index; @@ -544,11 +512,11 @@ class Executable if (exeExist) { uint32_t memb = esp_get_free_heap_size(); - printf("Free memory before:%ld\r\n", esp_get_free_heap_size()); + pushToConsole("Free memory before:%ld\r\n", esp_get_free_heap_size()); freeBinary(&_executecmd); _isRunning = false; uint32_t mema = esp_get_free_heap_size(); - printf("Free memory after:%ld freed:%ld\r\n", mema, mema - memb); + pushToConsole("Free memory after:%ld freed:%ld\r\n", mema, mema - memb); } exeExist = false; @@ -567,7 +535,7 @@ class Executable error_message_struct res = executeBinary("@_" + prog, _executecmd, 9999, args); if (res.error) { - pushToConsole(res.error_message, true); + pushToConsoleForce("%s\r\n", res.error_message.c_str()); } #endif } @@ -583,7 +551,7 @@ class Executable error_message_struct res = executeBinary("@_" + prog, _executecmd, 9999, args); if (res.error) { - pushToConsole(res.error_message, true); + pushToConsoleForce("%s\r\n", res.error_message.c_str()); } #endif } @@ -604,7 +572,7 @@ class Executable } else { - pushToConsole(string_format("Wrong core number %d should be 0 or 1", core)); + pushToConsole(core, "Wrong core number %d should be 0 or 1\r\n"); //force=core } #endif } @@ -650,7 +618,7 @@ static void _run_task(void *pvParameters) error_message_struct res = executeBinary(exec->df.args[0], exec->df.exe, exec->__run_handle_index, exec->args); if (res.error) { - pushToConsole(res.error_message, true); + pushToConsoleForce("%s\r\n", res.error_message.c_str()); } } else @@ -658,10 +626,10 @@ static void _run_task(void *pvParameters) error_message_struct res = executeBinary("@_main", exec->df.exe, exec->__run_handle_index, exec->args); if (res.error) { - pushToConsole(res.error_message, true); + pushToConsoleForce("%s\r\n", res.error_message.c_str()); } } - pushToConsole("Execution done.", true); + pushToConsoleForce("Execution done.\r\n"); // exec->__run_handle= NULL; exec->_isRunning = false; runningPrograms.removeHandle(exec->__run_handle_index); @@ -712,7 +680,7 @@ class _ScriptRuntime } else { - pushToConsole("please add a name to the executable", true); + pushToConsoleForce("please add a name to the executable\r\n"); } } void addExe(Executable df, string name) @@ -729,7 +697,7 @@ class _ScriptRuntime return &_scExecutables[i]; } } - pushToConsole(string_format("Executable %s not found", name.c_str()), true); + pushToConsoleForce("Executable %s not found\r\n", name.c_str()); return NULL; } void execute(string name) @@ -857,9 +825,9 @@ class _ScriptRuntime { if ((*it).name.compare(name) == 0) { - // pushToConsole("get it"); + // pushToConsole("get it\r\n"); _scExecutables.erase(it); - // pushToConsole("get it2"); + // pushToConsole("get it2\r\n"); return; } } @@ -880,7 +848,7 @@ class _ScriptRuntime { for (int i = 0; i < _scExecutables.size(); i++) { - pushToConsole(string_format(" %2d | %12s isRunning:%d", i + 1, _scExecutables[i].name.c_str(), _scExecutables[i].isRunning()), true); + pushToConsoleForce(" %2d | %12s isRunning:%d\r\n", i + 1, _scExecutables[i].name.c_str(), _scExecutables[i].isRunning()); } } diff --git a/src/functionlib.h b/src/functionlib.h index 4cd7b09..2f305ac 100644 --- a/src/functionlib.h +++ b/src/functionlib.h @@ -4,6 +4,43 @@ #ifndef __FUNCTION_LIB #define __FUNCTION_LIB +//pushToConsole with force argument +void pushToConsole(bool force, const char * format, ...) { + va_list args; + va_start(args, format); + + #ifdef __CONSOLE_ESP32 + LedOS.pushToConsole(string_format(format, args), force); + #elif defined(EXTPRINTF) + EXTPRINTF(format, args); + #else + #ifndef __TEST_DEBUG + Serial.printf(format, args); + #else + printf(format, args); + #endif + #endif + + va_end(args); +} + +//pushToConsole no force (default) +void pushToConsole(const char * format, ...) { + va_list args; + va_start(args, format); + pushToConsole(false, format, args); + va_end(args); +} + +//pushToConsole using force +void pushToConsoleForce(const char * format, ...) { + va_list args; + va_start(args, format); + pushToConsole(true, format, args); + va_end(args); +} + + list add_on; //string division=""; string _sync="\ From 3e32cd753519d974326d7f8047455414046ac382 Mon Sep 17 00:00:00 2001 From: Ewoud Date: Fri, 8 Nov 2024 16:52:59 +0100 Subject: [PATCH 2/3] typo --- src/ESPLiveScript.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ESPLiveScript.h b/src/ESPLiveScript.h index 186edd4..1464807 100644 --- a/src/ESPLiveScript.h +++ b/src/ESPLiveScript.h @@ -2786,7 +2786,7 @@ void compile_c(Console *cons, vector args) _scExec.name = cons->filename; scriptRuntime.addExe(_scExec); - pushToConsoleforce("Compiling done. Handle number:%d\r\n", scExecutables.size()); + pushToConsoleForce("Compiling done. Handle number:%d\r\n", scExecutables.size()); } } void free(Console *cons, vector args) From 661220c4e91aedd4edf077cdf34cbc4fae1e5309 Mon Sep 17 00:00:00 2001 From: Ewoud Date: Sun, 17 Nov 2024 14:57:34 +0100 Subject: [PATCH 3/3] Add string_format to pushToConsole for c_str() --- src/ESPLiveScript.h | 6 +++--- src/execute.h | 10 +++++----- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ESPLiveScript.h b/src/ESPLiveScript.h index 1464807..c43a889 100644 --- a/src/ESPLiveScript.h +++ b/src/ESPLiveScript.h @@ -247,7 +247,7 @@ class Parser parse(); if (Error.error) { - pushToConsoleForce("%s\r\n", Error.error_message.c_str()); + pushToConsoleForce(string_format("%s\r\n", Error.error_message.c_str()).c_str()); return results; } pushToConsole("***********PARSING DONE*********\r\n"); @@ -299,7 +299,7 @@ class Parser // exeExist = false; // Serial.printf(termColor.Red); - pushToConsoleForce("%s\r\n", _executecmd.error.error_message.c_str()); + pushToConsoleForce(string_format("%s\r\n", _executecmd.error.error_message.c_str()).c_str()); } #endif @@ -2772,7 +2772,7 @@ void parseasm(Console *cons, vector args) { exeExist = false; // Serial.printf(termColor.Red); - pushToConsole("%s\r\n", executecmd.error.error_message.c_str()); + pushToConsole(string_format("%s\r\n", executecmd.error.error_message.c_str()).c_str()); // Serial.printf(config.ESC_RESET); } */ diff --git a/src/execute.h b/src/execute.h index c8a86d1..ee764d6 100644 --- a/src/execute.h +++ b/src/execute.h @@ -535,7 +535,7 @@ class Executable error_message_struct res = executeBinary("@_" + prog, _executecmd, 9999, args); if (res.error) { - pushToConsoleForce("%s\r\n", res.error_message.c_str()); + pushToConsoleForce(string_format("%s\r\n", res.error_message.c_str()).c_str()); } #endif } @@ -551,7 +551,7 @@ class Executable error_message_struct res = executeBinary("@_" + prog, _executecmd, 9999, args); if (res.error) { - pushToConsoleForce("%s\r\n", res.error_message.c_str()); + pushToConsoleForce(string_format("%s\r\n", res.error_message.c_str()).c_str()); } #endif } @@ -618,7 +618,7 @@ static void _run_task(void *pvParameters) error_message_struct res = executeBinary(exec->df.args[0], exec->df.exe, exec->__run_handle_index, exec->args); if (res.error) { - pushToConsoleForce("%s\r\n", res.error_message.c_str()); + pushToConsoleForce(string_format("%s\r\n", res.error_message.c_str()).c_str()); } } else @@ -626,7 +626,7 @@ static void _run_task(void *pvParameters) error_message_struct res = executeBinary("@_main", exec->df.exe, exec->__run_handle_index, exec->args); if (res.error) { - pushToConsoleForce("%s\r\n", res.error_message.c_str()); + pushToConsoleForce(string_format("%s\r\n", res.error_message.c_str()).c_str()); } } pushToConsoleForce("Execution done.\r\n"); @@ -697,7 +697,7 @@ class _ScriptRuntime return &_scExecutables[i]; } } - pushToConsoleForce("Executable %s not found\r\n", name.c_str()); + pushToConsoleForce(string_format("Executable %s not found\r\n", name.c_str()).c_str()); return NULL; } void execute(string name)