Arduino Library when to use Serial.print

This post is just for general information. No need to reply:

In the Arduino Libraries generated by Edge Impulse the Static_Buffer example uses both Serial.print and ```ei_printf```

    Serial.println("Edge Impulse Inferencing Demo");
}

/**
 * @brief      Arduino main function
 */
void loop()
{
    ei_printf("Edge Impulse standalone inferencing (Arduino)\n");

Through a fair bit of trial and error, it looks like ei_printf is for character arrays or more specifically a “const char* s_variable” whereas Serial.print is the more regular Arduino print function. It also looks like these print commands can be used interchangeably, if so, why is ei_printf used?

ei_printf like “printf” allows for specific formatting codes that have been around for ages, whereas Serial.print originally didn’t do formatting terrible well.

Now-a-days Serial.print is fairly powerful, the following complex lines are interchangeable:

      //  ei_printf("    %s: %.5f\n", result.classification[ix].label, result.classification[ix].value);
        Serial.println("   " + String(result.classification[ix].label) + ": " + String(result.classification[ix].value, 5) );

Hey, good question. We need a cross-platform, portable printf implementation that works the same on all targets so we can print debugging information from the library, and have a way to communicate back to the users in a standard way. Printf very often does not support half the functions (%f is often missing), routes to the wrong location, or there is no working printf (like on Arduino, where they roll their own). For this we have ei_printf which is a pluggable function that works on all targets.

Now-a-days Serial.print is fairly powerful, the following complex lines are interchangeable:

I’m 99% sure this only works on certain targets, because there is no standard Arduino library.

1 Like

Good point, that explains why the mixed code.