Skip to content

Commit 775faf5

Browse files
authored
Dynamic script name (#7)
Allow selection of script file on webscreen.json
1 parent a5ee0f8 commit 775faf5

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

websocket/dynamic_js.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66
#include "pins_config.h"
77
#include "rm67162.h"
88
#include "lvgl_elk.h" // Contains init_lvgl_display(), init_lv_fs(), etc.
9+
#include "globals.h"
10+
11+
// Initialize with the default script filename
12+
String g_script_filename = "/app.js";
913

1014
void dynamic_js_setup() {
11-
Serial.println("DYNAMIC_JS: Setting up Elk + script.js scenario...");
15+
Serial.println("DYNAMIC_JS: Setting up Elk + script scenario...");
1216

1317
// If needed, mount the SD again (or confirm it’s already mounted):
1418
SD_MMC.setPins(PIN_SD_CLK, PIN_SD_CMD, PIN_SD_D0);

websocket/dynamic_js.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#pragma once
22

3-
// Functions to set up and loop your “dynamic” (Elk + script.js) code
3+
// Functions to set up and loop your “dynamic” (Elk + script) code
44
void dynamic_js_setup();
55
void dynamic_js_loop();

websocket/globals.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#pragma once
2+
#include <Arduino.h>
3+
4+
// Declare the global script filename variable
5+
extern String g_script_filename;

websocket/lvgl_elk.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#include <vector>
1414
#include <utility> // for std::pair
1515

16+
#include "globals.h"
17+
1618
// Global WiFiClient + PubSubClient
1719
static WiFiClient g_wifiClient;
1820
static PubSubClient g_mqttClient(g_wifiClient);
@@ -3250,9 +3252,10 @@ static void elk_task(void *pvParam) {
32503252
// 2) Register bridging
32513253
register_js_functions();
32523254

3253-
// 3) Load & execute your script
3254-
if(!load_and_execute_js_script("/script.js")) {
3255-
Serial.println("Failed to load and execute JavaScript script");
3255+
// 3) Load & execute your script using the filename from the configuration
3256+
if(!load_and_execute_js_script(g_script_filename.c_str())) {
3257+
Serial.printf("Failed to load and execute JavaScript script from %s\n", g_script_filename.c_str());
3258+
// Optionally handle the error
32563259
} else {
32573260
Serial.println("Script executed successfully in elk_task");
32583261
}

websocket/websocket.ino

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
#include "pins_config.h" // For PIN_SD_CMD, etc.
77
#include "fallback.h" // Fallback header
88
#include "dynamic_js.h" // Dynamic (Elk + JS) header
9+
#include "globals.h"
910

1011
#include <ArduinoJson.h>
1112

1213
// Global flag to decide fallback vs dynamic
1314
static bool useFallback = false;
1415

15-
static bool readWiFiConfigJSON(const char* path, String &outSSID, String &outPASS) {
16+
static bool readConfigJSON(const char* path, String &outSSID, String &outPASS, String &outScript) {
1617
File f = SD_MMC.open(path);
1718
if (!f) {
1819
Serial.println("No JSON file");
@@ -28,23 +29,25 @@ static bool readWiFiConfigJSON(const char* path, String &outSSID, String &outPAS
2829
return false;
2930
}
3031

31-
// Extract SSID/PASS from doc
32+
// Extract Wi-Fi settings
3233
outSSID = doc["settings"]["wifi"]["ssid"] | "";
3334
outPASS = doc["settings"]["wifi"]["pass"] | "";
3435
if (outSSID.isEmpty() || outPASS.isEmpty()) {
3536
Serial.println("SSID or PASS empty in JSON");
3637
return false;
3738
}
3839

39-
// Optionally update 'last_read' with current time
40+
// Extract the script filename (default to "app.js" if not provided)
41+
outScript = doc["script"] | "app.js";
42+
43+
// Update 'last_read' if desired
4044
time_t now;
4145
time(&now);
4246
doc["last_read"] = (unsigned long)now;
4347

44-
// Re-serialize and write back to the same file (if you want to save changes)
48+
// Optionally write back the updated JSON
4549
String updated;
4650
serializeJson(doc, updated);
47-
4851
f = SD_MMC.open(path, FILE_WRITE);
4952
if (!f) {
5053
Serial.println("Failed to open JSON for writing");
@@ -70,14 +73,20 @@ void setup() {
7073
}
7174

7275
// Optionally read /webscreen.json for Wi-Fi
73-
String s, p;
74-
if(!readWiFiConfigJSON("/webscreen.json", s, p)) {
76+
String s, p, scriptFile;
77+
if(!readConfigJSON("/webscreen.json", s, p, scriptFile)) {
7578
Serial.println("Failed to read /webscreen.json => fallback");
7679
useFallback = true;
7780
fallback_setup();
7881
return;
7982
}
8083

84+
// Ensure the script filename starts with a '/'
85+
if (!scriptFile.startsWith("/")) {
86+
scriptFile = "/" + scriptFile;
87+
}
88+
g_script_filename = scriptFile; // update global variable
89+
8190
// Connect Wi-Fi
8291
WiFi.mode(WIFI_STA);
8392
WiFi.begin(s.c_str(), p.c_str());
@@ -95,19 +104,22 @@ void setup() {
95104
}
96105
Serial.println("Wi-Fi connected => " + WiFi.localIP().toString());
97106

98-
// Check if /script.js exists
99-
{
100-
File checkF = SD_MMC.open("/script.js");
101-
if(!checkF) {
102-
Serial.println("No script.js => fallback");
103-
useFallback = true;
104-
fallback_setup();
105-
return;
106-
}
107-
checkF.close();
107+
// Use the filename specified in the config; ensure it has a leading '/'
108+
String scriptPath = scriptFile;
109+
if (!scriptPath.startsWith("/")) {
110+
scriptPath = "/" + scriptPath;
111+
}
112+
// Check if the script file exists on the SD card:
113+
File checkF = SD_MMC.open(g_script_filename);
114+
if(!checkF) {
115+
Serial.printf("No %s found => fallback\n", g_script_filename.c_str());
116+
useFallback = true;
117+
fallback_setup();
118+
return;
108119
}
120+
checkF.close();
109121

110-
// We have script.js => run dynamic
122+
// If everything is okay, run the dynamic JS functionality:
111123
useFallback = false;
112124
dynamic_js_setup();
113125
}

0 commit comments

Comments
 (0)