Skip to content

Commit 28c716f

Browse files
committed
fixed some issues, shortened html/java code
- reverting name to `linked_remote` - ESPNow remote list is now hidden if unchecked - shortened java script function names and variables to save flash - removed now obsolete settings in xml.cpp - correct checking of valid hex string for remote list in java script
1 parent 05000c8 commit 28c716f

File tree

3 files changed

+46
-41
lines changed

3 files changed

+46
-41
lines changed

wled00/cfg.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
3939
#ifndef WLED_DISABLE_ESPNOW
4040
CJSON(enableESPNow, nw[F("espnow")]);
4141
linked_remotes.clear();
42-
JsonArray lrem = nw[F("lrem")]; // array of MAC addresses
42+
JsonArray lrem = nw[F("linked_remote")]; // array of MAC addresses
4343
if (!lrem.isNull()) {
4444
for (size_t i = 0; i < lrem.size(); i++) {
4545
std::array<char, 13> entry{};
@@ -739,7 +739,7 @@ void serializeConfig(JsonObject root) {
739739
JsonObject nw = root.createNestedObject("nw");
740740
#ifndef WLED_DISABLE_ESPNOW
741741
nw[F("espnow")] = enableESPNow;
742-
JsonArray lrem = nw.createNestedArray(F("lrem"));
742+
JsonArray lrem = nw.createNestedArray(F("linked_remote"));
743743
for (size_t i = 0; i < linked_remotes.size(); i++) {
744744
lrem.add(linked_remotes[i].data());
745745
}

wled00/data/settings_wifi.htm

+42-33
Original file line numberDiff line numberDiff line change
@@ -136,68 +136,75 @@
136136
getLoc();
137137
loadJS(getURL('/settings/s.js?p=1'), false); // If we set async false, file is loaded and executed, then next statement is processed
138138
if (loc) d.Sf.action = getURL('/settings/wifi');
139+
setTimeout(tE, 500); // wait for DOM to load before calling tE()
139140
}
140141

141-
var remoteCount = 0;
142-
// Initialize empty list for remotes
143-
function resetRemotes() {
144-
d.getElementById('rmlist').innerHTML = '';
145-
remoteCount = 0;
146-
updateRMArray();
142+
var rC = 0; // remote count
143+
// toggle visibility of ESP-NOW remote list based on checkbox state
144+
function tE() {
145+
const e = d.querySelector('input[name="RE"]').checked;
146+
const l = gId('rlc'); // remote list container
147+
// keep the hidden input with MAC addresses, only toggle visibility of the list UI
148+
if (l) l.style.display = e ? 'block' : 'none';
147149
}
148-
149-
// Add a Remote MAC to the list
150-
function addRemote(id, mac) {
151-
if (!mac || mac === 'None') return;
152-
if (remoteCount >= 10) return;
153-
let inputs = d.getElementById('rmlist').getElementsByTagName('input');
150+
// reset remotes: initialize empty list (called from xml.cpp)
151+
function rstR() {
152+
gId('rml').innerHTML = ''; // clear remote list
153+
rC = 0;
154+
uR(); // update remote list
155+
}
156+
// add remote MAC to the list
157+
function aR(id, mac) {
158+
if (!/^[0-9A-F]{12}$/i.test(mac)) return; // check for valid hex string
159+
if (rC >= 10) return;
160+
let inputs = gId('rml').getElementsByTagName('input');
154161
for (let i = 0; i < inputs.length; i++) {
155162
if (inputs[i].value === mac) return;
156163
}
157-
let list = d.getElementById('rmlist');
158-
let row = d.createElement('div');
159-
let inp = d.createElement('input');
164+
let list = gId('rml');
165+
let row = cE('div');
166+
let inp = cE('input');
160167
inp.type = 'text';
161168
inp.name = id;
162169
inp.value = mac;
163170
inp.maxLength = 12;
164171
inp.minLength = 12;
165-
inp.onchange = updateRMArray;
172+
inp.onchange = uR;
166173
row.appendChild(inp);
167174
let btn = d.createElement('button');
168175
btn.type = 'button';
169176
btn.className = 'btn btn-xs';
170177
btn.innerText = 'Remove';
171178
btn.onclick = function() {
172-
removeRemote(row);
179+
rR(row);
173180
};
174181
row.appendChild(btn);
175182
list.appendChild(row);
176-
remoteCount++;
177-
updateRMArray();
183+
rC++;
184+
uR();
178185
}
179-
// Remove a remote from the list
180-
function removeRemote(element) {
181-
element.remove();
182-
updateRMArray();
186+
// remove a remote from the list
187+
function rR(e) {
188+
e.remove();
189+
uR();
183190
}
184-
// Update hidden input with JSON array of MACs
185-
function updateRMArray() {
191+
// update remote list
192+
function uR() {
186193
let macs = [];
187-
let inputs = d.getElementById('rmlist').getElementsByTagName('input');
194+
let inputs = gId('rml').getElementsByTagName('input');
188195
for (let i = 0; i < inputs.length; i++) {
189196
if (inputs[i].value && inputs[i].value !== '') {
190197
macs.push(inputs[i].value);
191198
}
192199
}
193-
d.getElementById('rmacs').value = JSON.stringify(macs);
200+
gId('rmacs').value = JSON.stringify(macs);
194201
return true; // Allow form submission to continue
195202
}
196203
</script>
197204
<style>@import url("style.css");</style>
198205
</head>
199206
<body onload="S()">
200-
<form id="form_s" name="Sf" method="post" onsubmit="return updateRMArray()">
207+
<form id="form_s" name="Sf" method="post" onsubmit="return uR()">
201208
<div class="toprow">
202209
<div class="helpB"><button type="button" onclick="H('features/settings/#wifi-settings')">?</button></div>
203210
<button type="button" onclick="B()">Back</button><button type="submit">Save & Connect</button><hr>
@@ -258,13 +265,15 @@ <h3>ESP-NOW Wireless</h3>
258265
<i class="warn">This firmware build does not include ESP-NOW support.<br></i>
259266
</div>
260267
<div id="ESPNOW">
261-
Enable ESP-NOW: <input type="checkbox" name="RE"><br>
268+
Enable ESP-NOW: <input type="checkbox" name="RE" onchange="tE()"><br>
262269
<i>Listen for events over ESP-NOW<br>
263270
Keep disabled if not using a remote or wireless sync, increases power consumption.<br></i>
264-
Last device seen: <span class="rlid">None</span>
265-
<button type="button" class="btn btn-xs" onclick="addRemote('RM'+remoteCount,this.parentElement.querySelector('.rlid').textContent)">Add</button><br>
266-
Linked MACs:<br>
267-
<div id="rmlist">
271+
<div id="rlc">
272+
Last device seen: <span class="rlid" id="ld">None</span>
273+
<button type="button" class="btn btn-xs" onclick="aR('RM'+rC,gId('ld').textContent)">Add</button><br>
274+
Linked MACs:<br>
275+
<div id="rml">
276+
</div>
268277
</div>
269278
<input type="hidden" name="RMA" id="rmacs">
270279
</div>

wled00/xml.cpp

+2-6
Original file line numberDiff line numberDiff line change
@@ -216,9 +216,9 @@ void getSettingsJS(byte subPage, Print& settingsScript)
216216

217217
#ifndef WLED_DISABLE_ESPNOW
218218
printSetFormCheckbox(settingsScript,PSTR("RE"),enableESPNow);
219-
settingsScript.printf_P(PSTR("resetRemotes();"));
219+
settingsScript.printf_P(PSTR("rstR();")); // reset remote list
220220
for (size_t i = 0; i < linked_remotes.size(); i++) {
221-
settingsScript.printf_P(PSTR("addRemote(\"RM%u\",\"%s\");"), i, linked_remotes[i].data());
221+
settingsScript.printf_P(PSTR("aR(\"RM%u\",\"%s\");"), i, linked_remotes[i].data()); // add remote to list
222222
}
223223
#else
224224
//hide remote settings if not compiled
@@ -261,10 +261,6 @@ void getSettingsJS(byte subPage, Print& settingsScript)
261261
#ifndef WLED_DISABLE_ESPNOW
262262
if (strlen(last_signal_src) > 0) { //Have seen an ESP-NOW Remote
263263
printSetClassElementHTML(settingsScript,PSTR("rlid"),0,last_signal_src);
264-
} else if (!enableESPNow) {
265-
printSetClassElementHTML(settingsScript,PSTR("rlid"),0,(char*)F("(Enable ESP-NOW to listen)"));
266-
} else {
267-
printSetClassElementHTML(settingsScript,PSTR("rlid"),0,(char*)F("None"));
268264
}
269265
#endif
270266
}

0 commit comments

Comments
 (0)