xml: Print optimization

Reduce the total number of calls by using printf_P and skipping atoi().
This commit is contained in:
Will Miles 2024-09-07 19:52:30 -04:00
parent 16f61ea96d
commit 4ef583c844

View File

@ -8,63 +8,22 @@
//build XML response to HTTP /win API request //build XML response to HTTP /win API request
void XML_response(Print& dest) void XML_response(Print& dest)
{ {
dest.print(F("<?xml version=\"1.0\" ?><vs><ac>")); dest.printf_P(PSTR("<?xml version=\"1.0\" ?><vs><ac>%d</ac>"), (nightlightActive && nightlightMode > NL_MODE_SET) ? briT : bri);
dest.print((nightlightActive && nightlightMode > NL_MODE_SET) ? briT : bri);
dest.print(F("</ac>"));
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
dest.print("<cl>"); dest.printf_P(PSTR("<cl>%d</cl>"), col[i]);
dest.print(col[i]);
dest.print("</cl>");
} }
for (int i = 0; i < 3; i++) for (int i = 0; i < 3; i++)
{ {
dest.print("<cs>"); dest.printf_P(PSTR("<cs>%d</cs>"), colSec[i]);
dest.print(colSec[i]);
dest.print("</cs>");
} }
dest.print(F("<ns>")); dest.printf_P(PSTR("<ns>%d</ns><nr>%d</nr><nl>%d</nl><nf>%d</nf><nd>%d</nd><nt>%d</nt><fx>%d</fx><sx>%d</sx><ix>%d</ix><fp>%d</fp><wv>%d</wv><ws>%d</ws><ps>%d</ps><cy>%d</cy><ds>%s%s</ds><ss>%d</ss></vs>"),
dest.print(notifyDirect); notifyDirect, receiveGroups!=0, nightlightActive, nightlightMode > NL_MODE_SET, nightlightDelayMins,
dest.print(F("</ns><nr>")); nightlightTargetBri, effectCurrent, effectSpeed, effectIntensity, effectPalette,
dest.print(receiveGroups!=0); strip.hasWhiteChannel() ? col[3] : -1, colSec[3], currentPreset, currentPlaylist >= 0,
dest.print(F("</nr><nl>")); serverDescription, realtimeMode ? PSTR(" (live)") : "",
dest.print(nightlightActive); strip.getFirstSelectedSegId()
dest.print(F("</nl><nf>")); );
dest.print(nightlightMode > NL_MODE_SET);
dest.print(F("</nf><nd>"));
dest.print(nightlightDelayMins);
dest.print(F("</nd><nt>"));
dest.print(nightlightTargetBri);
dest.print(F("</nt><fx>"));
dest.print(effectCurrent);
dest.print(F("</fx><sx>"));
dest.print(effectSpeed);
dest.print(F("</sx><ix>"));
dest.print(effectIntensity);
dest.print(F("</ix><fp>"));
dest.print(effectPalette);
dest.print(F("</fp><wv>"));
if (strip.hasWhiteChannel()) {
dest.print(col[3]);
} else {
dest.print("-1");
}
dest.print(F("</wv><ws>"));
dest.print(colSec[3]);
dest.print(F("</ws><ps>"));
dest.print(currentPreset);
dest.print(F("</ps><cy>"));
dest.print(currentPlaylist >= 0);
dest.print(F("</cy><ds>"));
dest.print(serverDescription);
if (realtimeMode)
{
dest.print(F(" (live)"));
}
dest.print(F("</ds><ss>"));
dest.print(strip.getFirstSelectedSegId());
dest.print(F("</ss></vs>"));
} }
static void extractPin(Print& dest, JsonObject &obj, const char *key) { static void extractPin(Print& dest, JsonObject &obj, const char *key) {
@ -114,17 +73,12 @@ void fillUMPins(Print& dest, JsonObject &mods)
} }
void appendGPIOinfo(Print& dest) { void appendGPIOinfo(Print& dest) {
char nS[8];
// add usermod pins as d.um_p array
dest.print(F("d.um_p=[-1")); // has to have 1 element dest.print(F("d.um_p=[-1")); // has to have 1 element
if (i2c_sda > -1 && i2c_scl > -1) { if (i2c_sda > -1 && i2c_scl > -1) {
dest.print(","); dest.print(itoa(i2c_sda,nS,10)); dest.printf_P(PSTR(",%d,%d"), i2c_sda, i2c_scl);
dest.print(","); dest.print(itoa(i2c_scl,nS,10));
} }
if (spi_mosi > -1 && spi_sclk > -1) { if (spi_mosi > -1 && spi_sclk > -1) {
dest.print(","); dest.print(itoa(spi_mosi,nS,10)); dest.printf_P(PSTR(",%d,%d"), spi_mosi, spi_sclk);
dest.print(","); dest.print(itoa(spi_sclk,nS,10));
} }
// usermod pin reservations will become unnecessary when settings pages will read cfg.json directly // usermod pin reservations will become unnecessary when settings pages will read cfg.json directly
if (requestJSONBufferLock(6)) { if (requestJSONBufferLock(6)) {
@ -147,16 +101,16 @@ void appendGPIOinfo(Print& dest) {
dest.print(SET_F("2,")); // DMX hardcoded pin dest.print(SET_F("2,")); // DMX hardcoded pin
#endif #endif
#if defined(WLED_DEBUG) && !defined(WLED_DEBUG_HOST) #if defined(WLED_DEBUG) && !defined(WLED_DEBUG_HOST)
dest.print(itoa(hardwareTX,nS,10)); dest.print(","); // debug output (TX) pin dest.printf_P(PSTR(",%d"),hardwareTX); // debug output (TX) pin
#endif #endif
//Note: Using pin 3 (RX) disables Adalight / Serial JSON //Note: Using pin 3 (RX) disables Adalight / Serial JSON
#ifdef WLED_USE_ETHERNET #ifdef WLED_USE_ETHERNET
if (ethernetType != WLED_ETH_NONE && ethernetType < WLED_NUM_ETH_TYPES) { if (ethernetType != WLED_ETH_NONE && ethernetType < WLED_NUM_ETH_TYPES) {
for (unsigned p=0; p<WLED_ETH_RSVD_PINS_COUNT; p++) { dest.print(itoa(esp32_nonconfigurable_ethernet_pins[p].pin,nS,10)); dest.print(","); } for (unsigned p=0; p<WLED_ETH_RSVD_PINS_COUNT; p++) { dest.printf(",%d", esp32_nonconfigurable_ethernet_pins[p].pin); }
if (ethernetBoards[ethernetType].eth_power>=0) { dest.print(itoa(ethernetBoards[ethernetType].eth_power,nS,10)); dest.print(","); } if (ethernetBoards[ethernetType].eth_power>=0) { dest.printf(",%d", ethernetBoards[ethernetType].eth_power); }
if (ethernetBoards[ethernetType].eth_mdc>=0) { dest.print(itoa(ethernetBoards[ethernetType].eth_mdc,nS,10)); dest.print(","); } if (ethernetBoards[ethernetType].eth_mdc>=0) { dest.printf(",%d", ethernetBoards[ethernetType].eth_mdc); }
if (ethernetBoards[ethernetType].eth_mdio>=0) { dest.print(itoa(ethernetBoards[ethernetType].eth_mdio,nS,10)); dest.print(","); } if (ethernetBoards[ethernetType].eth_mdio>=0) { dest.printf(",%d", ethernetBoards[ethernetType].eth_mdio); }
switch (ethernetBoards[ethernetType].eth_clk_mode) { switch (ethernetBoards[ethernetType].eth_clk_mode) {
case ETH_CLOCK_GPIO0_IN: case ETH_CLOCK_GPIO0_IN:
case ETH_CLOCK_GPIO0_OUT: case ETH_CLOCK_GPIO0_OUT:
dest.print(SET_F("0")); dest.print(SET_F("0"));
@ -211,27 +165,19 @@ void getSettingsJS(byte subPage, Print& dest)
if (subPage == SUBPAGE_WIFI) if (subPage == SUBPAGE_WIFI)
{ {
char nS[10];
size_t l; size_t l;
dest.print(F("resetWiFi(")); dest.printf_P(PSTR("resetWiFi(%d);"), WLED_MAX_WIFI_COUNT);
dest.print(WLED_MAX_WIFI_COUNT);
dest.print(F(");"));
for (size_t n = 0; n < multiWiFi.size(); n++) { for (size_t n = 0; n < multiWiFi.size(); n++) {
l = strlen(multiWiFi[n].clientPass); l = strlen(multiWiFi[n].clientPass);
char fpass[l+1]; //fill password field with *** char fpass[l+1]; //fill password field with ***
fpass[l] = 0; fpass[l] = 0;
memset(fpass,'*',l); memset(fpass,'*',l);
dest.print(F("addWiFi(\"")); dest.printf_P(PSTR("addWiFi(\"%s\",\",%s\",0x%X,0x%X,0x%X);"),
dest.print(multiWiFi[n].clientSSID); multiWiFi[n].clientSSID,
dest.print(F("\",\"")); fpass,
dest.print(fpass); (uint32_t) multiWiFi[n].staticIP, // explicit cast required as this is a struct
dest.print(F("\",0x")); (uint32_t) multiWiFi[n].staticGW,
dest.print(itoa(multiWiFi[n].staticIP,nS,16)); (uint32_t) multiWiFi[n].staticSN);
dest.print(F(",0x"));
dest.print(itoa(multiWiFi[n].staticGW,nS,16));
dest.print(F(",0x"));
dest.print(itoa(multiWiFi[n].staticSN,nS,16));
dest.print(F(");"));
} }
sappend(dest,'v',SET_F("D0"),dnsAddress[0]); sappend(dest,'v',SET_F("D0"),dnsAddress[0]);
@ -320,16 +266,16 @@ void getSettingsJS(byte subPage, Print& dest)
dest.print(SET_F("d.ledTypes=")); dest.print(BusManager::getLEDTypesJSONString().c_str()); dest.print(";"); dest.print(SET_F("d.ledTypes=")); dest.print(BusManager::getLEDTypesJSONString().c_str()); dest.print(";");
// set limits // set limits
dest.print(F("bLimits(")); dest.printf_P(PSTR("bLimits(%d,%d,%d,%d,%d,%d,%d,%d);"),
dest.print(itoa(WLED_MAX_BUSSES,nS,10)); dest.print(","); WLED_MAX_BUSSES,
dest.print(itoa(WLED_MIN_VIRTUAL_BUSSES,nS,10)); dest.print(","); WLED_MIN_VIRTUAL_BUSSES,
dest.print(itoa(MAX_LEDS_PER_BUS,nS,10)); dest.print(","); MAX_LEDS_PER_BUS,
dest.print(itoa(MAX_LED_MEMORY,nS,10)); dest.print(","); MAX_LED_MEMORY,
dest.print(itoa(MAX_LEDS,nS,10)); dest.print(","); MAX_LEDS,
dest.print(itoa(WLED_MAX_COLOR_ORDER_MAPPINGS,nS,10)); dest.print(","); WLED_MAX_COLOR_ORDER_MAPPINGS,
dest.print(itoa(WLED_MAX_DIGITAL_CHANNELS,nS,10)); dest.print(","); WLED_MAX_DIGITAL_CHANNELS,
dest.print(itoa(WLED_MAX_ANALOG_CHANNELS,nS,10)); WLED_MAX_ANALOG_CHANNELS
dest.print(F(");")); );
sappend(dest,'c',SET_F("MS"),strip.autoSegments); sappend(dest,'c',SET_F("MS"),strip.autoSegments);
sappend(dest,'c',SET_F("CCT"),strip.correctWB); sappend(dest,'c',SET_F("CCT"),strip.correctWB);
@ -403,17 +349,12 @@ void getSettingsJS(byte subPage, Print& dest)
sappend(dest,'c',SET_F("ABL"),BusManager::ablMilliampsMax() || sumMa > 0); sappend(dest,'c',SET_F("ABL"),BusManager::ablMilliampsMax() || sumMa > 0);
sappend(dest,'c',SET_F("PPL"),!BusManager::ablMilliampsMax() && sumMa > 0); sappend(dest,'c',SET_F("PPL"),!BusManager::ablMilliampsMax() && sumMa > 0);
dest.print(F("resetCOM(")); dest.printf_P(PSTR("resetCOM(%d);"), WLED_MAX_COLOR_ORDER_MAPPINGS);
dest.print(itoa(WLED_MAX_COLOR_ORDER_MAPPINGS,nS,10));
dest.print(F(");"));
const ColorOrderMap& com = BusManager::getColorOrderMap(); const ColorOrderMap& com = BusManager::getColorOrderMap();
for (int s = 0; s < com.count(); s++) { for (int s = 0; s < com.count(); s++) {
const ColorOrderMapEntry* entry = com.get(s); const ColorOrderMapEntry* entry = com.get(s);
if (entry == nullptr) break; if (entry == nullptr) break;
dest.print(F("addCOM(")); dest.printf_P(PSTR("addCOM(%d,%d,%d);"), entry->start, entry->len, entry->colorOrder);
dest.print(itoa(entry->start,nS,10)); dest.print(",");
dest.print(itoa(entry->len,nS,10)); dest.print(",");
dest.print(itoa(entry->colorOrder,nS,10)); dest.print(");");
} }
sappend(dest,'v',SET_F("CA"),briS); sappend(dest,'v',SET_F("CA"),briS);
@ -439,11 +380,7 @@ void getSettingsJS(byte subPage, Print& dest)
sappend(dest,'c',SET_F("RM"),rlyMde); sappend(dest,'c',SET_F("RM"),rlyMde);
sappend(dest,'c',SET_F("RO"),rlyOpenDrain); sappend(dest,'c',SET_F("RO"),rlyOpenDrain);
for (int i = 0; i < WLED_MAX_BUTTONS; i++) { for (int i = 0; i < WLED_MAX_BUTTONS; i++) {
dest.print(F("addBtn(")); dest.printf_P(PSTR("addBtn(%d,%d,%d);"), i, btnPin[i], buttonType[i]);
dest.print(itoa(i,nS,10)); dest.print(",");
dest.print(itoa(btnPin[i],nS,10)); dest.print(",");
dest.print(itoa(buttonType[i],nS,10));
dest.print(F(");"));
} }
sappend(dest,'c',SET_F("IP"),disablePullUp); sappend(dest,'c',SET_F("IP"),disablePullUp);
sappend(dest,'v',SET_F("TT"),touchThreshold); sappend(dest,'v',SET_F("TT"),touchThreshold);
@ -528,9 +465,8 @@ void getSettingsJS(byte subPage, Print& dest)
sappends(dest,'s',SET_F("MG"),mqttGroupTopic); sappends(dest,'s',SET_F("MG"),mqttGroupTopic);
sappend(dest,'c',SET_F("BM"),buttonPublishMqtt); sappend(dest,'c',SET_F("BM"),buttonPublishMqtt);
sappend(dest,'c',SET_F("RT"),retainMqttMsg); sappend(dest,'c',SET_F("RT"),retainMqttMsg);
dest.print(F("d.Sf.MD.maxlength=")); dest.print(itoa(MQTT_MAX_TOPIC_LEN,nS,10)); dest.print(F(";")); dest.printf_P(PSTR("d.Sf.MD.maxlength=%d;d.Sf.MG.maxlength=%d;d.Sf.MS.maxlength=%d;"),
dest.print(F("d.Sf.MG.maxlength=")); dest.print(itoa(MQTT_MAX_TOPIC_LEN,nS,10)); dest.print(F(";")); MQTT_MAX_TOPIC_LEN, MQTT_MAX_TOPIC_LEN, MQTT_MAX_SERVER_LEN);
dest.print(F("d.Sf.MS.maxlength=")); dest.print(itoa(MQTT_MAX_SERVER_LEN,nS,10)); dest.print(F(";"));
#else #else
dest.print(F("toggle('MQTT');")); // hide MQTT settings dest.print(F("toggle('MQTT');")); // hide MQTT settings
#endif #endif
@ -608,12 +544,7 @@ void getSettingsJS(byte subPage, Print& dest)
sappend(dest,'v',SET_F("MC"),macroCountdown); sappend(dest,'v',SET_F("MC"),macroCountdown);
sappend(dest,'v',SET_F("MN"),macroNl); sappend(dest,'v',SET_F("MN"),macroNl);
for (unsigned i=0; i<WLED_MAX_BUTTONS; i++) { for (unsigned i=0; i<WLED_MAX_BUTTONS; i++) {
dest.print(F("addRow(")); dest.printf_P(PSTR("addRow(%d,%d,%d,%d);"), i, macroButton[i], macroLongPress[i], macroDoublePress[i]);
dest.print(itoa(i,tm,10)); dest.print(",");
dest.print(itoa(macroButton[i],tm,10)); dest.print(",");
dest.print(itoa(macroLongPress[i],tm,10)); dest.print(",");
dest.print(itoa(macroDoublePress[i],tm,10));
dest.print(F(");"));
} }
char k[4]; char k[4];
@ -647,9 +578,7 @@ void getSettingsJS(byte subPage, Print& dest)
char tmp_buf[128]; char tmp_buf[128];
snprintf_P(tmp_buf,sizeof(tmp_buf),PSTR("WLED %s (build %d)"),versionString,VERSION); snprintf_P(tmp_buf,sizeof(tmp_buf),PSTR("WLED %s (build %d)"),versionString,VERSION);
sappends(dest,'m',SET_F("(\"sip\")[0]"),tmp_buf); sappends(dest,'m',SET_F("(\"sip\")[0]"),tmp_buf);
dest.print(F("sd=\"")); dest.printf_P(PSTR("sd=\"%s\";"), serverDescription);
dest.print(serverDescription);
dest.print(F("\";"));
} }
#ifdef WLED_ENABLE_DMX // include only if DMX is enabled #ifdef WLED_ENABLE_DMX // include only if DMX is enabled
@ -683,19 +612,19 @@ void getSettingsJS(byte subPage, Print& dest)
if (subPage == SUBPAGE_UM) //usermods if (subPage == SUBPAGE_UM) //usermods
{ {
appendGPIOinfo(dest); appendGPIOinfo(dest);
dest.print(F("numM=")); dest.printf_P(PSTR("numM=%d;"), usermods.getModCount());
dest.print(usermods.getModCount());
dest.print(";");
sappend(dest,'v',SET_F("SDA"),i2c_sda); sappend(dest,'v',SET_F("SDA"),i2c_sda);
sappend(dest,'v',SET_F("SCL"),i2c_scl); sappend(dest,'v',SET_F("SCL"),i2c_scl);
sappend(dest,'v',SET_F("MOSI"),spi_mosi); sappend(dest,'v',SET_F("MOSI"),spi_mosi);
sappend(dest,'v',SET_F("MISO"),spi_miso); sappend(dest,'v',SET_F("MISO"),spi_miso);
sappend(dest,'v',SET_F("SCLK"),spi_sclk); sappend(dest,'v',SET_F("SCLK"),spi_sclk);
dest.print(F("addInfo('SDA','")); dest.print(HW_PIN_SDA); dest.print(F("');")); dest.printf_P(PSTR("addInfo('SDA','%d');"
dest.print(F("addInfo('SCL','")); dest.print(HW_PIN_SCL); dest.print(F("');")); "addInfo('SCL','%d');"
dest.print(F("addInfo('MOSI','")); dest.print(HW_PIN_DATASPI); dest.print(F("');")); "addInfo('MOSI','%d');"
dest.print(F("addInfo('MISO','")); dest.print(HW_PIN_MISOSPI); dest.print(F("');")); "addInfo('MISO','%d');"
dest.print(F("addInfo('SCLK','")); dest.print(HW_PIN_CLOCKSPI); dest.print(F("');")); "addInfo('SCLK','%d');"),
HW_PIN_SDA, HW_PIN_SCL, HW_PIN_DATASPI, HW_PIN_MISOSPI, HW_PIN_CLOCKSPI
);
usermods.appendConfigData(dest); usermods.appendConfigData(dest);
} }