Skip to content

Commit a24420a

Browse files
committed
Fix wled#4268
1 parent c0875b3 commit a24420a

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

wled00/bus_manager.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <Arduino.h>
66
#include <IPAddress.h>
7+
#include "src/dependencies/network/Network.h" // for isConnected() (& WiFi)
78
#ifdef ARDUINO_ARCH_ESP32
89
#include "driver/ledc.h"
910
#include "soc/ledc_struct.h"
@@ -95,7 +96,7 @@ void Bus::calculateCCT(uint32_t c, uint8_t &ww, uint8_t &cw) {
9596
} else {
9697
cct = (approximateKelvinFromRGB(c) - 1900) >> 5; // convert K (from RGB value) to relative format
9798
}
98-
99+
99100
//0 - linear (CCT 127 = 50% warm, 50% cold), 127 - additive CCT blending (CCT 127 = 100% warm, 100% cold)
100101
if (cct < _cctBlend) ww = 255;
101102
else ww = ((255-cct) * 255) / (255 - _cctBlend);
@@ -692,6 +693,10 @@ BusNetwork::BusNetwork(const BusConfig &bc)
692693
_hasCCT = false;
693694
_UDPchannels = _hasWhite + 3;
694695
_client = IPAddress(bc.pins[0],bc.pins[1],bc.pins[2],bc.pins[3]);
696+
#ifdef ARDUINO_ARCH_ESP32
697+
_hostname = bc.text;
698+
resolveHostname(); // resolve hostname to IP address if needed
699+
#endif
695700
_data = (uint8_t*)d_calloc(_len, _UDPchannels);
696701
_valid = (_data != nullptr);
697702
DEBUGBUS_PRINTF_P(PSTR("%successfully inited virtual strip with type %u and IP %u.%u.%u.%u\n"), _valid?"S":"Uns", bc.type, bc.pins[0], bc.pins[1], bc.pins[2], bc.pins[3]);
@@ -726,6 +731,19 @@ size_t BusNetwork::getPins(uint8_t* pinArray) const {
726731
return 4;
727732
}
728733

734+
#ifdef ARDUINO_ARCH_ESP32
735+
void BusNetwork::resolveHostname() {
736+
static unsigned long nextResolve = 0;
737+
if (Network.isConnected() && millis() > nextResolve && _hostname.length() > 0) {
738+
nextResolve = millis() + 600000; // resolve only every 10 minutes
739+
IPAddress clnt;
740+
if (strlen(cmDNS) > 0) clnt = MDNS.queryHost(_hostname);
741+
else WiFi.hostByName(_hostname.c_str(), clnt);
742+
if (clnt != IPAddress()) _client = clnt;
743+
}
744+
}
745+
#endif
746+
729747
// credit @willmmiles & @netmindz https://github.com/wled/WLED/pull/4056
730748
std::vector<LEDType> BusNetwork::getLEDTypes() {
731749
return {
@@ -911,6 +929,13 @@ void BusManager::on() {
911929
}
912930
}
913931
}
932+
#else
933+
for (auto &bus : busses) if (bus->isVirtual()) {
934+
// virtual/network bus should check for IP change if hostname is specified
935+
// otherwise there are no endpoints to force DNS resolution
936+
BusNetwork &b = static_cast<BusNetwork&>(*bus);
937+
b.resolveHostname();
938+
}
914939
#endif
915940
#ifdef ESP32_DATA_IDLE_HIGH
916941
esp32RMTInvertIdle();

wled00/bus_manager.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,10 @@ class BusNetwork : public Bus {
342342
size_t getBusSize() const override { return sizeof(BusNetwork) + (isOk() ? _len * _UDPchannels : 0); }
343343
void show() override;
344344
void cleanup();
345+
#ifdef ARDUINO_ARCH_ESP32
346+
void resolveHostname();
347+
const String getCustomText() const override { return _hostname; }
348+
#endif
345349

346350
static std::vector<LEDType> getLEDTypes();
347351

@@ -351,6 +355,9 @@ class BusNetwork : public Bus {
351355
uint8_t _UDPchannels;
352356
bool _broadcastLock;
353357
uint8_t *_data;
358+
#ifdef ARDUINO_ARCH_ESP32
359+
String _hostname;
360+
#endif
354361
};
355362

356363

wled00/data/settings_leds.htm

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
maxL = l; // maxL - max LEDs (will serve to determine ESP >1664 == ESP32)
5353
maxCO = o; // maxCO - max Color Order mappings
5454
}
55+
function is8266() { return maxA == 5 && maxD == 3; } // NOTE: see const.h
56+
function is32() { return maxA == 16 && maxD == 16; } // NOTE: see const.h
57+
function isC3() { return maxA == 6 && maxD == 2; } // NOTE: see const.h
58+
function isS2() { return maxA == 8 && maxD == 12 && maxV == 4; } // NOTE: see const.h
59+
function isS3() { return maxA == 8 && maxD == 12 && maxV == 6; } // NOTE: see const.h
5560
function pinsOK() {
5661
var ok = true;
5762
var nList = d.Sf.querySelectorAll("#mLC input[name^=L]");
@@ -271,7 +276,7 @@
271276
gRGBW |= hasW(t); // RGBW checkbox
272277
gId("co"+n).style.display = (isVir(t) || isAna(t)) ? "none":"inline"; // hide color order for PWM
273278
gId("dig"+n+"w").style.display = (isDig(t) && hasW(t)) ? "inline":"none"; // show swap channels dropdown
274-
gId("dig"+n+"w").querySelector("[data-opt=CCT]").disabled = !hasCCT(t); // disable WW/CW swapping
279+
gId("dig"+n+"w").querySelector("[data-opt=CCT]").disabled = !hasCCT(t); // disable WW/CW swapping
275280
if (!(isDig(t) && hasW(t))) d.Sf["WO"+n].value = 0; // reset swapping
276281
gId("dig"+n+"c").style.display = (isAna(t)) ? "none":"inline"; // hide count for analog
277282
gId("dig"+n+"r").style.display = (isVir(t)) ? "none":"inline"; // hide reversed for virtual
@@ -281,6 +286,8 @@
281286
gId("dig"+n+"l").style.display = (isD2P(t) || isPWM(t)) ? "inline":"none"; // bus clock speed / PWM speed (relative) (not On/Off)
282287
gId("rev"+n).innerHTML = isAna(t) ? "Inverted output":"Reversed"; // change reverse text for analog else (rotated 180°)
283288
//gId("psd"+n).innerHTML = isAna(t) ? "Index:":"Start:"; // change analog start description
289+
gId("net"+n+"h").style.display = isNet(t) && !is8266() ? "block" : "none"; // show host field for network types except on ESP8266
290+
if (!isNet(t) || is8266()) d.Sf["HS"+n].value = ""; // cleart host field if not network type or ESP8266
284291
});
285292
// display global white channel overrides
286293
gId("wc").style.display = (gRGBW) ? 'inline':'none';
@@ -463,6 +470,7 @@
463470
<span id="p2d${s}"></span><input type="number" name="L2${s}" class="s" onchange="UI();pinUpd(this);"/>
464471
<span id="p3d${s}"></span><input type="number" name="L3${s}" class="s" onchange="UI();pinUpd(this);"/>
465472
<span id="p4d${s}"></span><input type="number" name="L4${s}" class="s" onchange="UI();pinUpd(this);"/>
473+
<div id="net${s}h" class="hide">Host: <input type="text" name="HS${s}" maxlength="32" pattern="[a-zA-Z0-9_\\-]*" onchange="UI()"/>.local</div>
466474
<div id="dig${s}r" style="display:inline"><br><span id="rev${s}">Reversed</span>: <input type="checkbox" name="CV${s}"></div>
467475
<div id="dig${s}s" style="display:inline"><br>Skip first LEDs: <input type="number" name="SL${s}" min="0" max="255" value="0" oninput="UI()"></div>
468476
<div id="dig${s}f" style="display:inline"><br><span id="off${s}">Off Refresh</span>: <input id="rf${s}" type="checkbox" name="RF${s}"></div>
@@ -479,7 +487,7 @@
479487
if (type.t != undefined && type.t != "") {
480488
opt.setAttribute('data-type', type.t);
481489
}
482-
sel.appendChild(opt);
490+
sel.appendChild(opt);
483491
}
484492
}
485493
});
@@ -586,7 +594,7 @@
586594
var cs = false;
587595
for (var i=1; i < gEBCN("iST").length; i++) {
588596
var s = chrID(i);
589-
var p = chrID(i-1); // cover edge case 'A' previous char being '9'
597+
var p = chrID(i-1); // cover edge case 'A' previous char being '9'
590598
var v = parseInt(gId("ls"+p).value) + parseInt(gN("LC"+p).value);
591599
if (v != parseInt(gId("ls"+s).value)) {cs = true; startsDirty[i] = true;}
592600
}
@@ -617,7 +625,7 @@
617625

618626
function receivedText(e) {
619627
let lines = e.target.result;
620-
let c = JSON.parse(lines);
628+
let c = JSON.parse(lines);
621629
if (c.hw) {
622630
if (c.hw.led) {
623631
// remove all existing outputs

0 commit comments

Comments
 (0)