diff --git a/wled00/FX.cpp b/wled00/FX.cpp index 364760fd..e6639c11 100644 --- a/wled00/FX.cpp +++ b/wled00/FX.cpp @@ -42,6 +42,7 @@ uint16_t WS2812FX::mode_static(void) { * Custom mode. Executes WebAssembly fx() function */ uint16_t WS2812FX::mode_custom(void) { + if (SEGENV.call == 0) wasmfx.init(); wasmfx.run(); return FRAMETIME; } diff --git a/wled00/FX.h b/wled00/FX.h index a861fc79..360aa7a6 100644 --- a/wled00/FX.h +++ b/wled00/FX.h @@ -380,12 +380,16 @@ class WS2812FX { */ void resetIfRequired() { if (_requiresReset) { - next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0; + next_time = 0; step = 0; call = 0; aux0 = 0; aux1 = 0; deallocateData(); _requiresReset = false; } } + bool isResetRequired() { + return _requiresReset; + } + /** * Flags that before the next effect is calculated, * the internal segment state should be reset. @@ -630,8 +634,7 @@ class WS2812FX { setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w = 0), show(void), setColorOrder(uint8_t co), - setPixelSegment(uint8_t n), - initWasm(void); + setPixelSegment(uint8_t n); bool isRgbw = false, diff --git a/wled00/FX_fcn.cpp b/wled00/FX_fcn.cpp index 91836ed9..1680e827 100644 --- a/wled00/FX_fcn.cpp +++ b/wled00/FX_fcn.cpp @@ -144,6 +144,7 @@ void WS2812FX::service() { // reset the segment runtime data if needed, called before isActive to ensure deleted // segment's buffers are cleared + if (SEGENV.isResetRequired()) wasmfx.end(); SEGENV.resetIfRequired(); if (!SEGMENT.isActive()) continue; @@ -1085,8 +1086,4 @@ uint32_t WS2812FX::gamma32(uint32_t color) return ((w << 24) | (r << 16) | (g << 8) | (b)); } -void WS2812FX::initWasm() { - wasmfx.init(); -} - WS2812FX* WS2812FX::instance = nullptr; \ No newline at end of file diff --git a/wled00/fcn_declare.h b/wled00/fcn_declare.h index daf13723..1fcd368c 100644 --- a/wled00/fcn_declare.h +++ b/wled00/fcn_declare.h @@ -252,6 +252,7 @@ void userLoop(); //wasm.cpp void wasmInit(); void wasmRun(); +void wasmEnd(); class WASMFX { public: @@ -263,6 +264,10 @@ class WASMFX { wasmRun(); } + void end() { + wasmEnd(); + } + uint32_t now(); uint32_t speed(); uint32_t intensity(); diff --git a/wled00/wasm.cpp b/wled00/wasm.cpp index 9c2e07c0..d439aae4 100644 --- a/wled00/wasm.cpp +++ b/wled00/wasm.cpp @@ -222,10 +222,6 @@ void wasm_task(void*) if (result) FATAL("Func", result); Serial.println(F("WASM init success!")); - - // Should not arrive here - - //while (true) {} } void wasmInit() @@ -261,10 +257,16 @@ void wasmRun() { Serial.print(info.message); Serial.println(")"); if (info.file && strlen(info.file) && info.line) { - Serial.print("At "); - Serial.print(info.file); - Serial.print(":"); - Serial.println(info.line); + Serial.print("At "); + Serial.print(info.file); + Serial.print(":"); + Serial.println(info.line); } + } } + +void wasmEnd() { + if (module) m3_FreeModule(module); module = nullptr; + if (runtime) m3_FreeRuntime(runtime); runtime = nullptr; + if (env) m3_FreeEnvironment(env); env = nullptr; } \ No newline at end of file diff --git a/wled00/wled.cpp b/wled00/wled.cpp index 36cbf1d8..05b060cb 100644 --- a/wled00/wled.cpp +++ b/wled00/wled.cpp @@ -313,7 +313,6 @@ void WLED::setup() if (!pinManager.isPinAllocated(STATUSLED)) pinMode(STATUSLED, OUTPUT); #endif - wasmfx.init(); DEBUG_PRINTLN(F("Initializing strip")); beginStrip();