@@ -44,9 +44,37 @@ static unsigned long lastWiFiReconnectAttempt = 0;
4444/* *****************************************************************************
4545 * A) Elk Memory + Global Instances
4646 ******************************************************************************/
47- #define ELK_HEAP_BYTES (96 * 1024 ) // Increased to 96KB for better long-term stability
48- static uint8_t elk_memory[ELK_HEAP_BYTES ];
47+ #define ELK_HEAP_BYTES (256 * 1024 ) // 256KB in PSRAM for complex scripts
48+ static uint8_t *elk_memory = NULL ;
49+ static size_t elk_memory_size = 0 ;
4950struct js *js = NULL ; // Global Elk instance
51+
52+ // Initialize Elk memory from PSRAM (must be called before js_create)
53+ static bool init_elk_memory () {
54+ if (elk_memory != NULL ) {
55+ return true ; // Already initialized
56+ }
57+
58+ // Try to allocate from PSRAM first
59+ elk_memory = (uint8_t *)ps_malloc (ELK_HEAP_BYTES );
60+ if (elk_memory != NULL ) {
61+ elk_memory_size = ELK_HEAP_BYTES ;
62+ LOGF (" Elk heap allocated in PSRAM: %u KB\n " , ELK_HEAP_BYTES / 1024 );
63+ return true ;
64+ }
65+
66+ // Fallback to regular heap with smaller size
67+ size_t fallback_size = 96 * 1024 ;
68+ elk_memory = (uint8_t *)malloc (fallback_size);
69+ if (elk_memory != NULL ) {
70+ elk_memory_size = fallback_size;
71+ LOGF (" Elk heap allocated in RAM (fallback): %u KB\n " , fallback_size / 1024 );
72+ return true ;
73+ }
74+
75+ LOG (" ERROR: Failed to allocate Elk heap!" );
76+ return false ;
77+ }
5078// Adjust as needed
5179#define MAX_RAM_IMAGES 16
5280
@@ -3272,7 +3300,14 @@ void register_js_functions() {
32723300// K) The elk_task -- runs Elk + bridging in a separate FreeRTOS task
32733301
32743302static void elk_task (void *pvParam) {
3275- js = js_create (elk_memory, sizeof (elk_memory));
3303+ // Initialize Elk memory from PSRAM
3304+ if (!init_elk_memory ()) {
3305+ LOG (" Failed to allocate Elk memory in elk_task" );
3306+ vTaskDelete (NULL );
3307+ return ;
3308+ }
3309+
3310+ js = js_create (elk_memory, elk_memory_size);
32763311 if (!js) {
32773312 LOG (" Failed to initialize Elk in elk_task" );
32783313 // Delete this task if you want
0 commit comments