computourist / RFM69-MQTT-client

Arduino - RFM69 based sensors and MQTT gateway
GNU General Public License v2.0
81 stars 27 forks source link

Loop is not necessary #17

Closed jrbenito closed 4 years ago

jrbenito commented 7 years ago

hi,

In the lines 326 and below of RFM_MQTT_GW_25.ino there is:

    dtostrf(mes.fltVal, 10,2, buff_mess);
    while (buff_mess[0] == 32) {                // remove any leading spaces
        for (int i =0; i<strlen(buff_mess); i++) {
        buff_mess[i] = buff_mess[i+1];
        }
    }
    }

The loop is being used to remove leading spaces in the buffer, however, there will be leading spaces only if the number converted by dtostrf functions is smaller than 10 digits. Hence, imagining the smallest number possible being 3 digits long, then seven white spaces would be found before the actual number.

However, AVR Docs says:

Conversion is done in the format "[-]d.ddd". The minimum field width of the output string (including the possible '.' and the possible sign for negative values) is given in width, and prec determines the number of digits after the decimal sign. width is signed value, negative for left adjustment.

So, the second parameter is the minimum width a number can take after conversion. Therefore, if lines 326 is changed to dtostrf(mes.fltVal, 3,2, buff_mess);, there will be no leading spaces hence loop is not necessary. Yet, the buffer need to be large enough to accommodate converted number, so, the number 3 is the minimum width and not maximum. It is unlikely a node generating quantities that have minimum 10 digits long.

computourist commented 6 years ago

Thanks for the feedback. I will consider it for a next release.