Merge pull request #2517 from ChuckMash/serial+

Extend Serial functions. Baudrate changeable during runtime and fast LED data retrieval
This commit is contained in:
Christian Schwinne 2022-02-01 20:07:30 +01:00 committed by GitHub
commit 795c515999
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 97 additions and 11 deletions

View File

@ -211,6 +211,10 @@ bool deserializeConfig(JsonObject doc, bool fromFS) {
rlyMde = !relay["rev"];
}
CJSON(serialBaud, hw[F("baud")]);
if (serialBaud < 96 || serialBaud > 15000) serialBaud = 1152;
updateBaudRate(serialBaud *100);
//int hw_status_pin = hw[F("status")]["pin"]; // -1
JsonObject light = doc[F("light")];
@ -630,6 +634,8 @@ void serializeConfig() {
hw_relay["pin"] = rlyPin;
hw_relay["rev"] = !rlyMde;
hw[F("baud")] = serialBaud;
//JsonObject hw_status = hw.createNestedObject("status");
//hw_status["pin"] = -1;

View File

@ -160,7 +160,21 @@ Hue Bridge IP:<br>
<input name="H3" type="number" class="s" min="0" max="255" ><br>
<b>Press the pushlink button on the bridge, after that save this page!</b><br>
(when first connecting)<br>
Hue status: <span class="sip"> Disabled in this build </span><hr>
Hue status: <span class="sip"> Disabled in this build </span>
<h3>Serial</h3>
Baud rate:
<select name=BD>
<option value=1152>115200</option>
<option value=2304>230400</option>
<option value=4608>460800</option>
<option value=5000>500000</option>
<option value=5760>576000</option>
<option value=9216>921600</option>
<option value=10000>1000000</option>
<option value=15000>1500000</option>
</select><br>
<i>Keep at 115200 to use Improv. Some boards may not support high rates.</i>
<hr>
<button type="button" onclick="B()">Back</button><button type="submit">Save</button>
</form>
</body>

View File

@ -287,6 +287,7 @@ void clearEEPROM();
//wled_serial.cpp
void handleSerial();
void updateBaudRate(uint32_t rate);
//wled_server.cpp
bool isIp(String str);

View File

@ -340,8 +340,14 @@ class="s" min="0" max="255"> . <input name="H2" type="number" class="s" min="0"
max="255"> . <input name="H3" type="number" class="s" min="0" max="255"><br><b>
Press the pushlink button on the bridge, after that save this page!</b><br>
(when first connecting)<br>Hue status: <span class="sip">Disabled in this build
</span><hr><button type="button" onclick="B()">Back</button><button
type="submit">Save</button></form></body></html>)=====";
</span><h3>Serial</h3>Baud rate: <select name="BD"><option value="1152">115200
</option><option value="2304">230400</option><option value="4608">460800
</option><option value="5000">500000</option><option value="5760">576000
</option><option value="9216">921600</option><option value="10000">1000000
</option><option value="15000">1500000</option></select><br><i>
Keep at 115200 to use Improv. Some boards may not support high rates.</i><hr>
<button type="button" onclick="B()">Back</button><button type="submit">Save
</button></form></body></html>)=====";
// Autogenerated from wled00/data/settings_time.htm, do not edit!!

View File

@ -661,7 +661,7 @@ void handleIR()
{
if (results.value != 0) // only print results if anything is received ( != 0 )
{
if (!pinManager.isPinAllocated(1)) //GPIO 1 - Serial TX pin
if (!pinManager.isPinAllocated(1) || pinManager.getPinOwner(1) == PinOwner::DebugOut) //GPIO 1 - Serial TX pin
Serial.printf_P(PSTR("IR recv: 0x%lX\n"), (unsigned long)results.value);
}
decodeIR(results.value);

View File

@ -433,9 +433,9 @@ void serializeSegment(JsonObject& root, WS2812FX::Segment& seg, byte id, bool fo
}
char tmpcol[22];
sprintf_P(tmpcol, format, (unsigned)c[0], (unsigned)c[1], (unsigned)c[2], (unsigned)c[3]);
strcat(colstr, i<2 ? strcat_P(tmpcol, PSTR(",")) : tmpcol);
strcat(colstr, i<2 ? strcat(tmpcol, ",") : tmpcol);
}
strcat_P(colstr, PSTR("]"));
strcat(colstr, "]");
root["col"] = serialized(colstr);
root["fx"] = seg.mode;

View File

@ -309,6 +309,10 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
hueStoreAllowed = true;
reconnectHue();
#endif
t = request->arg(F("BD")).toInt();
if (t >= 96 && t <= 15000) serialBaud = t;
updateBaudRate(serialBaud *100);
}
//TIME

View File

@ -370,6 +370,8 @@ WLED_GLOBAL bool hueApplyBri _INIT(true);
WLED_GLOBAL bool hueApplyColor _INIT(true);
#endif
WLED_GLOBAL uint16_t serialBaud _INIT(1152); // serial baud rate, multiply by 100
// Time CONFIG
WLED_GLOBAL bool ntpEnabled _INIT(false); // get internet time. Only required if you use clock overlays or time-activated macros
WLED_GLOBAL bool useAMPM _INIT(false); // 12h/24h clock format

View File

@ -19,6 +19,21 @@ enum class AdaState {
TPM2_Header_CountLo,
};
uint16_t currentBaud = 1152; //default baudrate 115200 (divided by 100)
void updateBaudRate(uint32_t rate){
uint16_t rate100 = rate/100;
if (rate100 == currentBaud || rate100 < 96) return;
currentBaud = rate100;
if (!pinManager.isPinAllocated(1) || pinManager.getPinOwner(1) == PinOwner::DebugOut){
Serial.print(F("Baud is now ")); Serial.println(rate);
}
Serial.flush();
Serial.begin(rate);
}
void handleSerial()
{
if (pinManager.isPinAllocated(3)) return;
@ -31,6 +46,8 @@ void handleSerial()
static byte red = 0x00;
static byte green = 0x00;
uint16_t nBytes = 0;
while (Serial.available() > 0)
{
yield();
@ -46,6 +63,41 @@ void handleSerial()
return;
} else if (next == 'v') {
Serial.print("WLED"); Serial.write(' '); Serial.println(VERSION);
} else if (next == 0xB0) {updateBaudRate( 115200);
} else if (next == 0xB1) {updateBaudRate( 230400);
} else if (next == 0xB2) {updateBaudRate( 460800);
} else if (next == 0xB3) {updateBaudRate( 500000);
} else if (next == 0xB4) {updateBaudRate( 576000);
} else if (next == 0xB5) {updateBaudRate( 921600);
} else if (next == 0xB6) {updateBaudRate(1000000);
} else if (next == 0xB7) {updateBaudRate(1500000);
} else if (next == 'l') { //RGB(W) LED data return as JSON array. Slow, but easy to use on the other end.
if (!pinManager.isPinAllocated(1) || pinManager.getPinOwner(1) == PinOwner::DebugOut){
uint16_t used = strip.getLengthTotal();
Serial.write('[');
for (uint16_t i=0; i<used; i+=1) {
Serial.print(strip.getPixelColor(i));
if (i != used-1) Serial.write(',');
}
Serial.println("]");
}
} else if (next == 'L') { //RGB LED data returned as bytes in tpm2 format. Faster, and slightly less easy to use on the other end.
if (!pinManager.isPinAllocated(1) || pinManager.getPinOwner(1) == PinOwner::DebugOut) {
Serial.write(0xC9); Serial.write(0xDA);
uint16_t used = strip.getLengthTotal();
uint16_t len = used*3;
Serial.write((len << 8) & 0xFF);
Serial.write( len & 0xFF);
for (uint16_t i=0; i < used; i++) {
uint32_t c = strip.getPixelColor(i);
Serial.write(qadd8(W(c), R(c))); //R, add white channel to RGB channels as a simple RGBW -> RGB map
Serial.write(qadd8(W(c), G(c))); //G
Serial.write(qadd8(W(c), B(c))); //B
}
Serial.write(0x36); Serial.write('\n');
}
} else if (next == '{') { //JSON API
bool verboseResponse = false;
#ifdef WLED_USE_DYNAMIC_JSON
@ -61,7 +113,7 @@ void handleSerial()
}
verboseResponse = deserializeState(doc.as<JsonObject>());
//only send response if TX pin is unused for other purposes
if (verboseResponse && !pinManager.isPinAllocated(1)) {
if (verboseResponse && (!pinManager.isPinAllocated(1) || pinManager.getPinOwner(1) == PinOwner::DebugOut)) {
doc.clear();
JsonObject state = doc.createNestedObject("state");
serializeState(state);

View File

@ -551,6 +551,7 @@ void getSettingsJS(byte subPage, char* dest)
sappends('m',SET_F("(\"sip\")[0]"),hueErrorString);
#endif
sappend('v',SET_F("BD"),serialBaud);
}
if (subPage == 5)