Modjo String problem

Discussion in 'UDOO NEO' started by modjo, Sep 13, 2016.

  1. modjo

    modjo Active Member

    Joined:
    Sep 29, 2014
    Messages:
    417
    Likes Received:
    127
    Another issue that i found, create a String object with a float (for example String(3.32)) different from 0 stop the program. If someone can test it please ? For information, i have the lasted arduino library (udoo).
     
  2. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
    Can you give us more info @modjo?
     
  3. modjo

    modjo Active Member

    Joined:
    Sep 29, 2014
    Messages:
    417
    Likes Received:
    127
    Yes, simply if i create a String object form a float that is equal to zero the sketch run :
    Code:
    float myFloat=0;
    String data =String(myFloat);
    But if i define this (or modify the value of the float later--> from adc for example) the sketch compile but the program stop or bug in the card :
    Code:
    float myFloat=3.14;
    String data =String(myFloat);
     
  4. Andrea Rovai

    Andrea Rovai Well-Known Member

    Joined:
    Oct 27, 2014
    Messages:
    1,703
    Likes Received:
    240
  5. Maurice

    Maurice Active Member

    Joined:
    Oct 13, 2015
    Messages:
    394
    Likes Received:
    87
    I'm not a C++ programmer by heart, but afaik there is no float constructor of a string , is there? If you initialize it with 0 I guess it will create a string solely of '\0' aka The Empty String. if you pass 3.14 it will pass it some string starting with character nonsense until somewhere in the memory it will encounter a \0.

    The C++ library seems to contain a to_string method, which might have the function you need. See the C++ reference: http://www.cplusplus.com/reference/string/to_string/

    (I have no idea of which version/subset of C++ UDOO/Arduino is delivering)
     
    Andrea Rovai likes this.
  6. modjo

    modjo Active Member

    Joined:
    Sep 29, 2014
    Messages:
    417
    Likes Received:
    127
    I just want to create a string with a float because i can't send a structure for the moment over serial, see this issue :
    http://www.udoo.org/forum/threads/bug-with-uart-com.4499/
    Otherwise, i just want to use a constructor of Arduino String object that take float in argument ! see here for more details :
    https://www.arduino.cc/en/Tutorial/StringConstructors
    I'm not a fan of string use in programming (take more time to process !!!), but for the moment it's the easiest way to use serial com with A9 (send a packed struct not working ....:mad:)
     
  7. Francesco

    Francesco Active Member

    Joined:
    Jun 23, 2015
    Messages:
    220
    Likes Received:
    110
  8. William Warke

    William Warke New Member

    Joined:
    Oct 5, 2016
    Messages:
    7
    Likes Received:
    3
    snprintf is out. The following code crashes the M4:
    Code:
    double x = 1234.56;
    char buf[128];
    snprintf(buf, 128, "%f", x);
     
  9. William Warke

    William Warke New Member

    Joined:
    Oct 5, 2016
    Messages:
    7
    Likes Received:
    3
    Okay guys, I came up with a solution that has worked for me so far. I modified the printFloat function from AppData\Local\Arduino15\packages\UDOO\hardware\solox\1.6.7\variants\udooneo\Print.cpp to write a double to a char* buffer, which was exactly what I needed. Leaving it here in case anyone finds it useful:

    [Edit: Modified to return pointer to buffer.]
    Code:
    char *doubleToString(double number, uint8_t digits, char *buffer, int bufferLength) {
        int bufIndex = 0;
    
        if (isnan(number)) {
            snprintf(buffer, bufferLength, "nan");
            return buffer;
        }
    
        if (isinf(number)) {
            snprintf(buffer, bufferLength, "inf");
            return buffer;
        }
    
        if (number > 4294967040.0) {
            snprintf(buffer, bufferLength, "ovf");
            return buffer;
        }
    
        if (number <-4294967040.0) {
            snprintf(buffer, bufferLength, "ovf");
            return buffer;
        }
    
        // Handle negative numbers
        if (number < 0.0) {
            buffer[bufIndex++] = '-';
            number = -number;
        }
    
        // Round correctly so that print(1.999, 2) prints as "2.00"
        double rounding = 0.5;
        for (uint8_t i = 0; i < digits; ++i) {
            rounding /= 10.0;
        }
        number += rounding;
    
        // Extract the integer part of the number and print it
        unsigned long int_part = (unsigned long)number;
        double remainder = number - (double)int_part;
       
        char tmp[bufferLength];
        itoa (int_part, tmp, 10);
        for (int i = 0; i < strlen(tmp); i++) {
            buffer[bufIndex++] = tmp[i];
        }
    
        // Print the decimal point, but only if there are digits beyond
        if (digits > 0) {
            buffer[bufIndex++] = '.';
        }
    
        // Extract digits from the remainder one at a time
        while (digits-- > 0) {
            remainder *= 10.0;
            int toPrint = int(remainder);
            itoa (toPrint, tmp, 10);
            buffer[bufIndex++] = tmp[0];
            remainder -= toPrint;
        }
    
        //Null-terminate the string
        buffer[bufIndex] = '\0';
    
        return buffer;
    }
    
    Usage:
    Code:
        double x = -1234.5678;
        char buf[128];
        doubleToString(x, 2, buf, 128);
        Serial0.println(buf);
     
    Last edited: Dec 20, 2016
    modjo and waltervl like this.
  10. modjo

    modjo Active Member

    Joined:
    Sep 29, 2014
    Messages:
    417
    Likes Received:
    127
    @Andrea Rovai , it would be great to integrate this into the official udoo arduino lib i think !
     
  11. Maurice

    Maurice Active Member

    Joined:
    Oct 13, 2015
    Messages:
    394
    Likes Received:
    87
    Not if it then deviates from the standard Arduino libraries.
     

Share This Page