From b03af79ffd148ec906b8b464de59f8d3576d4be4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Fri, 22 Sep 2017 15:26:02 +0200 Subject: [PATCH] Align left dtostrf output --- code/espurna/dht.ino | 4 ++-- code/espurna/ds18b20.ino | 4 ++-- code/espurna/emon.ino | 2 +- code/espurna/hlw8012.ino | 2 +- code/espurna/v9261f.ino | 10 +++++----- code/espurna/web.ino | 7 +++---- 6 files changed, 14 insertions(+), 15 deletions(-) diff --git a/code/espurna/dht.ino b/code/espurna/dht.ino index 5dd5b199..8655b20c 100644 --- a/code/espurna/dht.ino +++ b/code/espurna/dht.ino @@ -34,7 +34,7 @@ void dhtSetup() { #if WEB_SUPPORT apiRegister(DHT_TEMPERATURE_TOPIC, DHT_TEMPERATURE_TOPIC, [](char * buffer, size_t len) { - dtostrf(_dhtTemperature, len-1, 1, buffer); + dtostrf(_dhtTemperature, 1-len, 1, buffer); }); apiRegister(DHT_HUMIDITY_TOPIC, DHT_HUMIDITY_TOPIC, [](char * buffer, size_t len) { snprintf_P(buffer, len, PSTR("%d"), _dhtHumidity); @@ -68,7 +68,7 @@ void dhtLoop() { char temperature[6]; char humidity[6]; - dtostrf(t, 4, 1, temperature); + dtostrf(t, 1-sizeof(temperature), 1, temperature); itoa((unsigned int) h, humidity, 10); DEBUG_MSG_P(PSTR("[DHT] Temperature: %s%s\n"), temperature, (tmpUnits == TMP_CELSIUS) ? "ºC" : "ºF"); diff --git a/code/espurna/ds18b20.ino b/code/espurna/ds18b20.ino index a7a7ff74..ebe98160 100644 --- a/code/espurna/ds18b20.ino +++ b/code/espurna/ds18b20.ino @@ -40,7 +40,7 @@ void dsSetup() { #if WEB_SUPPORT apiRegister(DS18B20_TEMPERATURE_TOPIC, DS18B20_TEMPERATURE_TOPIC, [](char * buffer, size_t len) { - dtostrf(_dsTemperature, len-1, 1, buffer); + dtostrf(_dsTemperature, 1-len, 1, buffer); }); #endif @@ -89,7 +89,7 @@ void dsLoop() { else _dsIsConnected = true; - dtostrf(t, 5, 1, _dsTemperatureStr); + dtostrf(t, 1-sizeof(_dsTemperatureStr), 1, _dsTemperatureStr); DEBUG_MSG_P(PSTR("[DS18B20] Temperature: %s%s\n"), getDSTemperatureStr(), diff --git a/code/espurna/emon.ino b/code/espurna/emon.ino index 160e698b..c6507f7a 100644 --- a/code/espurna/emon.ino +++ b/code/espurna/emon.ino @@ -116,7 +116,7 @@ void powerMonitorSetup() { apiRegister(EMON_CURRENT_TOPIC, EMON_CURRENT_TOPIC, [](char * buffer, size_t len) { if (_emonReady) { - dtostrf(_emonCurrent, len-1, 3, buffer); + dtostrf(_emonCurrent, 1-len, 3, buffer); } else { buffer = NULL; } diff --git a/code/espurna/hlw8012.ino b/code/espurna/hlw8012.ino index 928311bf..b8a2b8e3 100644 --- a/code/espurna/hlw8012.ino +++ b/code/espurna/hlw8012.ino @@ -170,7 +170,7 @@ void hlw8012Setup() { }); apiRegister(HLW8012_CURRENT_TOPIC, HLW8012_CURRENT_TOPIC, [](char * buffer, size_t len) { if (_hlwReady) { - dtostrf(_hlwCurrent, len-1, 3, buffer); + dtostrf(_hlwCurrent, 1-len, 3, buffer); } else { buffer = NULL; } diff --git a/code/espurna/v9261f.ino b/code/espurna/v9261f.ino index 5bc7334f..c244ebfc 100644 --- a/code/espurna/v9261f.ino +++ b/code/espurna/v9261f.ino @@ -193,7 +193,7 @@ void v9261fSetup() { snprintf_P(buffer, len, PSTR("%d"), _v9261f_power); }); apiRegister(HLW8012_CURRENT_TOPIC, HLW8012_CURRENT_TOPIC, [](char * buffer, size_t len) { - dtostrf(_v9261f_current, len-1, 3, buffer); + dtostrf(_v9261f_current, 1-len, 3, buffer); }); apiRegister(HLW8012_VOLTAGE_TOPIC, HLW8012_VOLTAGE_TOPIC, [](char * buffer, size_t len) { snprintf_P(buffer, len, PSTR("%d"), _v9261f_voltage); @@ -232,11 +232,11 @@ void v9261fLoop() { JsonObject& root = jsonBuffer.createObject(); char buf_current[10]; - dtostrf(getCurrent(), 6, 3, buf_current); + dtostrf(getCurrent(), 1-sizeof(buf_current), 3, buf_current); root["powVisible"] = 1; root["powActivePower"] = getActivePower(); - root["powCurrent"] = String(ltrim(buf_current)); + root["powCurrent"] = String(buf_current); root["powVoltage"] = getVoltage(); root["powApparentPower"] = getApparentPower(); root["powReactivePower"] = getReactivePower(); @@ -262,11 +262,11 @@ void v9261fLoop() { unsigned int voltage = sum_voltage / count; double current = sum_current / count; char buf_current[10]; - dtostrf(current, 6, 3, buf_current); + dtostrf(current, 1-sizeof(buf_current), 3, buf_current); unsigned int apparent = sqrt(power * power + reactive * reactive); double energy_delta = (double) power * V9261F_REPORT_INTERVAL / 1000.0 / 3600.0; char buf_energy[10]; - dtostrf(energy_delta, 6, 3, buf_energy); + dtostrf(energy_delta, 1-sizeof(buf_energy), 3, buf_energy); unsigned int factor = 100 * ((double) power / apparent); // Report values to MQTT broker diff --git a/code/espurna/web.ino b/code/espurna/web.ino index eed32a45..1b2cf4ed 100644 --- a/code/espurna/web.ino +++ b/code/espurna/web.ino @@ -809,7 +809,6 @@ ArRequestHandlerFunction _bindAPI(unsigned int apiID) { // Get response from callback char value[API_BUFFER_SIZE]; (api.getFn)(value, API_BUFFER_SIZE); - char *p = ltrim(value); // The response will be a 404 NOT FOUND if the resource is not available if (!value) { @@ -817,15 +816,15 @@ ArRequestHandlerFunction _bindAPI(unsigned int apiID) { request->send(404); return; } - DEBUG_MSG_P(PSTR("[API] Sending response '%s'\n"), p); + DEBUG_MSG_P(PSTR("[API] Sending response '%s'\n"), value); // Format response according to the Accept header if (_asJson(request)) { char buffer[64]; - snprintf_P(buffer, sizeof(buffer), PSTR("{ \"%s\": %s }"), api.key, p); + snprintf_P(buffer, sizeof(buffer), PSTR("{ \"%s\": %s }"), api.key, value); request->send(200, "application/json", buffer); } else { - request->send(200, "text/plain", p); + request->send(200, "text/plain", value); } };