# xprintf_demo **Repository Path**: IsYourGod/xprintf_demo ## Basic Information - **Project Name**: xprintf_demo - **Description**: xprintf - 嵌入式字符串函数   xprintf 是一个紧凑的字符串 I/O 库。 它非常适用于程序存储器不足以用于常规 printf 函数的微型微控制器。 推荐的用途是:将格式化的字符串写入 LCD 或 UART 以及用于调试/维护控制台。 xprintf demo . - **Primary Language**: C - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 4 - **Created**: 2021-11-26 - **Last Updated**: 2021-11-26 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ### xprintf_demo ### xprintf - 嵌入式字符串函数 - xprintf 是一个紧凑的字符串 I/O 库。 它非常适用于程序存储器不足以用于常规 printf 函数的微型微控制器。 推荐的用途是:将格式化的字符串写入 LCD 或 UART 以及用于调试/维护控制台。 - 可以使用配置选项配置 xprintf 以减小模块大小。 - 下表显示了 Cortex-M3 (gcc -Os) 中代码大小的示例。 long long 和 float 需要 C99 或更高版本。 - ![输入图片说明](https://images.gitee.com/uploads/images/2021/0610/163342_79c5ef9c_9231046.png "A1.png") ### 应用接口 - 嵌入式字符串函数提供以下函数。 ### 输出 ``` /*----------------------------------------------/ / xputc - Put a character /----------------------------------------------*/ void xputc ( int chr /* A character to be output (0-255) */ ); void xfputc ( void(*func)(int), /* Pointer to the output function */ int chr /* Character to be output (0-255) */ ); /*----------------------------------------------/ / xputs - Put a null-terminated string /----------------------------------------------*/ void xputs ( const char* str /* Pointer to the null-terminated string to be output */ ); void xfputs ( void(*func)(int), /* Pointer to the output function */ const char* str /* Pointer to the null-terminated string to be output */ ); /*----------------------------------------------/ / xprintf - Formatted string output /----------------------------------------------*/ void xprintf ( /* Put a formatted string to the default device */ const char* fmt, /* Pointer to the null-terminated format string */ ... /* Optional arguments... */ ); void xfprintf ( /* Put a formatted string to the specified device */ void(*func)(int), /* Pointer to the output function */ const char* fmt, /* Pointer to the null-terminated format string */ ... /* Optional arguments... */ ); void xsprintf ( /* Put a formatted string to the memory */ char* buff, /* Pointer to the buffer to store output string */ const char* fmt, /* Pointer to the null-terminated format string */ ... /* Optional arguments... */ ); /*----------------------------------------------/ / put_dump - Put a line of binary dump /----------------------------------------------*/ void put_dump ( const void* buff, /* Pointer to the data to be displayed */ unsigned long adr, /* Heading address */ int cnt, /* Number of items to be displayed */ int width /* Size of item (1, 2 or 4) */ ); ``` - 格式化控制指令是标准库的一个子集,如下所示: ` %[flag][width][precision][size]type` - flag: - 填充选项。 A - 指定左对齐。 0 指定零填充。 默认设置是右对齐和空格填充。 - width: - 字段的最小宽度,1-99 或 *。 如果生成的字符串宽度小于指定值,则其余字段用空格或零填充。 * 指定值来自 int 类型的参数。 - precision - 指定小数位数或字符串的最大宽度,.0-.99 或 .*。 如果省略 number,则与 .0 相同。 数字默认设置为 6,字符串无限制。 - size - 指定整数参数的大小,l(long) 和 ll(long long)。 如果 sizeof (long) == sizeof (int) 为真(这是典型的 32 位系统),则可以省略长整数参数的前缀 l。 整数参数的默认大小为 int ,浮点参数始终假定为双精度。 - type - 指定输出格式的类型和参数,如下所示。 生成字符串的长度假设 int 为 32 位。 - ![输入图片说明](https://images.gitee.com/uploads/images/2021/0610/163415_1c5944ab_9231046.png "A2.png") ### 例子: ``` xprintf("%d", 1234); /* "1234" */ xprintf("%6d,%3d%%", -200, 5); /* " -200, 5%" */ xprintf("%-6u", 100); /* "100 " */ xprintf("%ld", 12345678); /* "12345678" */ xprintf("%llu", 0x100000000); /* "4294967296" */ xprintf("%lld", -1LL); /* "-1" */ xprintf("%04x", 0xA3); /* "00a3" */ xprintf("%08lX", 0x123ABC); /* "00123ABC" */ xprintf("%016b", 0x550F); /* "0101010100001111" */ xprintf("%*d", 6, 100); /* "100" */ xprintf("%s", "abcdefg"); /* "abcdefg" */ xprintf("%5s", "abc"); /* " abc" */ xprintf("%-5s", "abc"); /* "abc " */ xprintf("%.5s", "abcdefg"); /* "abcde" */ xprintf("%-5.2s", "abcdefg"); /* "ab" */ xprintf("%c", 'a'); /* "a" */ xprintf("%12f", 10.0); /* " 10.000000" */ xprintf("%.4E", 123.45678); /* "1.2346E+02" */ ``` ### 输入 ``` /*----------------------------------------------/ / xgets - Get a line from the input device /----------------------------------------------*/ int xgets ( /* 0:End of stream, 1:A line arrived */ char* buff, /* Pointer to the buffer to input */ int len /* Buffer length */ ); /*----------------------------------------------/ / xatoi - Get a value of integer string /----------------------------------------------*/ /* "123 -5 0x3ff 0b1111 0377 1.5 " ^ 1st call returns 123 and next ptr ^ 2nd call returns -5 and next ptr ^ 3rd call returns 1023 and next ptr ^ 4th call returns 15 and next ptr ^ 5th call returns 255 and next ptr ^ 6th call fails and returns 0 */ int xatoi ( /* 0:Failed, 1:Succeeded */ char** str, /* Pointer to pointer to the string */ long* res /* Pointer to the valiable to store the value */ ); /*----------------------------------------------/ / xatof - Get a value of floating point string /----------------------------------------------*/ /* "123 -5.75 .6 +8.88E+5 1e-6 . " ^ 1st call returns 1.23e2 and next ptr ^ 2nd call returns -5.75e0 and next ptr ^ 3rd call returns 6e-1 and next ptr ^ 4th call returns 8.88e5 and next ptr ^ 5th call returns 1e-6 and next ptr ^ 6th call fails and returns 0 */ int xatof ( /* 0:Failed, 1:Succeded */ char** str, /* Pointer to pointer to the string */ double* res /* Pointer to the valiable to store the value */ ); ``` ### 设备输入/输出功能 - 输出函数是用户提供的回调函数,用于将字节写入输出设备。其地址应设置为模块中的函数指针xfunc_output,默认输出设备。通常,此函数将字节放入 UART、LCD 或某些输出设备。输出函数从 xputc() 回调。有一个宏可以轻松设置。 - 例如,当将 void uart1_putc (uint8_t chr) 附加到模块时, xdev_out(uart1_putc) 就可以了。如果输出函数有多个参数或没有简单的输出函数,则需要另一个封装函数。 xfputc()、xfputs()、xfprintf() 和 xsprintf() 使用其参数覆盖默认输出设备。 - 输入函数是用户提供的回调函数,用于从输入设备读取字节。它的地址应该设置为函数指针 xfunc_input,默认输入设备。有一个宏 xdev_in() 可以轻松设置它。 - 例如xdev_in(uart1_getc)。xfgets() 函数使用其参数覆盖默认输入设备。输入函数从 xgets() 函数回调。通常,输入函数从输入设备或文件中读取一个字节。当输入设备报告流结束时,输入函数应返回-1。xgets() 函数以零中止,应用程序将能够检测到它。 ``` /* 向输出设备写入一个字节 */ void output_func ( int chr /* 要写入的字节 */ ); /* 从输入设备读取一个字节 */ int input_func (void); /* 返回 0 到 255 作为读取字符,-1 作为 EOF */ ```