赞
踩
最近公司项目换另一款gsensor,用到了浮点数打印。又不想使用串口来打印数据,在此做个笔录,通过修改源码方式实现。
1、在 SEGGER_RTT_printf.c 中 的 int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList)函数,增加以下
case 'f':
case 'F': 语句
- /*********************************************************************
- *
- * SEGGER_RTT_vprintf
- *
- * Function description
- * Stores a formatted string in SEGGER RTT control block.
- * This data is read by the host.
- *
- * Parameters
- * BufferIndex Index of "Up"-buffer to be used. (e.g. 0 for "Terminal")
- * sFormat Pointer to format string
- * pParamList Pointer to the list of arguments for the format string
- *
- * Return values
- * >= 0: Number of bytes which have been stored in the "Up"-buffer.
- * < 0: Error
- */
- int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList) {
- char c;
- SEGGER_RTT_PRINTF_DESC BufferDesc;
- int v;
- unsigned NumDigits;
- unsigned FormatFlags;
- unsigned FieldWidth;
- char acBuffer[SEGGER_RTT_PRINTF_BUFFER_SIZE];
-
- BufferDesc.pBuffer = acBuffer;
- BufferDesc.BufferSize = SEGGER_RTT_PRINTF_BUFFER_SIZE;
- BufferDesc.Cnt = 0u;
- BufferDesc.RTTBufferIndex = BufferIndex;
- BufferDesc.ReturnValue = 0;
-
- do {
- c = *sFormat;
- sFormat++;
- if (c == 0u) {
- break;
- }
- if (c == '%') {
- //
- // Filter out flags
- //
- FormatFlags = 0u;
- v = 1;
- do {
- c = *sFormat;
- switch (c) {
- case '-': FormatFlags |= FORMAT_FLAG_LEFT_JUSTIFY; sFormat++; break;
- case '0': FormatFlags |= FORMAT_FLAG_PAD_ZERO; sFormat++; break;
- case '+': FormatFlags |= FORMAT_FLAG_PRINT_SIGN; sFormat++; break;
- case '#': FormatFlags |= FORMAT_FLAG_ALTERNATE; sFormat++; break;
- default: v = 0; break;
- }
- } while (v);
- //
- // filter out field with
- //
- FieldWidth = 0u;
- do {
- c = *sFormat;
- if ((c < '0') || (c > '9')) {
- break;
- }
- sFormat++;
- FieldWidth = (FieldWidth * 10u) + ((unsigned)c - '0');
- } while (1);
-
- //
- // Filter out precision (number of digits to display)
- //
- NumDigits = 0u;
- c = *sFormat;
- if (c == '.') {
- sFormat++;
- do {
- c = *sFormat;
- if ((c < '0') || (c > '9')) {
- break;
- }
- sFormat++;
- NumDigits = NumDigits * 10u + ((unsigned)c - '0');
- } while (1);
- }
- //
- // Filter out length modifier
- //
- c = *sFormat;
- do {
- if ((c == 'l') || (c == 'h')) {
- sFormat++;
- c = *sFormat;
- } else {
- break;
- }
- } while (1);
- //
- // Handle specifiers
- //
- switch (c) {
- case 'c': {
- char c0;
- v = va_arg(*pParamList, int);
- c0 = (char)v;
- _StoreChar(&BufferDesc, c0);
- break;
- }
- case 'd':
- v = va_arg(*pParamList, int);
- _PrintInt(&BufferDesc, v, 10u, NumDigits, FieldWidth, FormatFlags);
- break;
- case 'u':
- v = va_arg(*pParamList, int);
- _PrintUnsigned(&BufferDesc, (unsigned)v, 10u, NumDigits, FieldWidth, FormatFlags);
- break;
- case 'x':
- case 'X':
- v = va_arg(*pParamList, int);
- _PrintUnsigned(&BufferDesc, (unsigned)v, 16u, NumDigits, FieldWidth, FormatFlags);
- break;
- case 's':
- {
- const char * s = va_arg(*pParamList, const char *);
- do {
- c = *s;
- s++;
- if (c == '\0') {
- break;
- }
- _StoreChar(&BufferDesc, c);
- } while (BufferDesc.ReturnValue >= 0);
- }
- break;
- case 'p':
- v = va_arg(*pParamList, int);
- _PrintUnsigned(&BufferDesc, (unsigned)v, 16u, 8u, 8u, 0u);
- break;
- case '%':
- _StoreChar(&BufferDesc, '%');
- break;
- // Add the ability to output floating point numbers. It has two decimal places by default.
- #if 0
- case 'f':
- case 'F':
- {
- char ch[10]={0};
- const char * s=ch;
- double a = va_arg(*pParamList, double);
- sprintf(ch,"%4.3f",a);
- do{
- c = *s;
- s++;
- if (c == '\0') break;
- _StoreChar(&BufferDesc, c);
- }while(BufferDesc.ReturnValue >= 0);
- }break;
- #else
- case 'f':
- case 'F': {
- float fv = (float)va_arg(*pParamList, double); // Retrieves the input floating point value
- if(fv < 0) _StoreChar(&BufferDesc, '-'); // Judge the sign
- v = abs((int)fv); // Take the positive integer part
- _PrintInt(&BufferDesc, v, 10u, NumDigits, FieldWidth, FormatFlags); // According to an integer
- _StoreChar(&BufferDesc, '.'); // Display decimal point
- v = abs((int)(fv * 1000));
- v = v % 1000;
- _PrintInt(&BufferDesc, v, 10u, 3, FieldWidth, FormatFlags); // Display three decimal places
- }break;
- #endif
- default:
- break;
- }
- sFormat++;
- } else {
- _StoreChar(&BufferDesc, c);
- }
- } while (BufferDesc.ReturnValue >= 0);
-
- if (BufferDesc.ReturnValue > 0) {
- //
- // Write remaining data, if any
- //
- if (BufferDesc.Cnt != 0u) {
- SEGGER_RTT_Write(BufferIndex, acBuffer, BufferDesc.Cnt);
- }
- BufferDesc.ReturnValue += (int)BufferDesc.Cnt;
- }
- return BufferDesc.ReturnValue;
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。