How do I implement the snprintf() functionality using EZ-USB FX3 SDK?
For implementing snprintf(), the CyU3PDebugStringPrint() API function can be used. This is a simpler version of the snprintf routine, which prints formatted debug information into a user-provided buffer.
Note that this function only supports the 'c', 'd', 'u', 'x' and 's' conversion specifications. It also does not support precision or type modifiers. This API function makes use of the following parameters in its definition:
uint8_t *buffer, /**< Buffer into which the data is to be printed. */
uint16_t maxLength, /**< Buffer size. */
char *fmt, /**< Format specifier string. */
... /**< Variable argument list. */
The return values for this API can be either CY_U3P_SUCCESS if the print to buffer is successful, or CY_U3P_ERROR_BAD_ARGUMENT if the length of the formatted string exceeds maxLength.
Suppose the requirement is to store a formatted message into a user-defined buffer. For example, the message “He has ‘x’ books.” need to be stored into a user-provided buffer and ‘x’ is a variable. Then, the following code snippet can be used:
uint8_t n =5;
CyU3PDebugStringPrint ((uint8_t*)c1, SIZE_STRING, "He has %d books.\n", n);
This code snippet will store the string “He has 5 books.” into the buffer c1. This can be displayed in Tera Term using the CyU3PDebugPrint(); API function.
The snapshot of output obtained in Tera Term is shown below:
Note: The example project Gpioapp, which comes along with THE FX3 SDK has been modified to implement the example described above. This project is attached as a .zip file along with this article.