当前位置:   article > 正文

SEGGER_RTT_printf()函数实现打印浮点、负数-示例_segger_rtt_printf打印浮点数

segger_rtt_printf打印浮点数

概述

最近公司项目换另一款gsensor,用到了浮点数打印。又不想使用串口来打印数据,在此做个笔录,通过修改源码方式实现。

一、修改源码

1、在 SEGGER_RTT_printf.c 中 的 int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList)函数,增加以下
 case 'f':
 case 'F': 语句

  1. /*********************************************************************
  2. *
  3. * SEGGER_RTT_vprintf
  4. *
  5. * Function description
  6. * Stores a formatted string in SEGGER RTT control block.
  7. * This data is read by the host.
  8. *
  9. * Parameters
  10. * BufferIndex Index of "Up"-buffer to be used. (e.g. 0 for "Terminal")
  11. * sFormat Pointer to format string
  12. * pParamList Pointer to the list of arguments for the format string
  13. *
  14. * Return values
  15. * >= 0: Number of bytes which have been stored in the "Up"-buffer.
  16. * < 0: Error
  17. */
  18. int SEGGER_RTT_vprintf(unsigned BufferIndex, const char * sFormat, va_list * pParamList) {
  19. char c;
  20. SEGGER_RTT_PRINTF_DESC BufferDesc;
  21. int v;
  22. unsigned NumDigits;
  23. unsigned FormatFlags;
  24. unsigned FieldWidth;
  25. char acBuffer[SEGGER_RTT_PRINTF_BUFFER_SIZE];
  26. BufferDesc.pBuffer = acBuffer;
  27. BufferDesc.BufferSize = SEGGER_RTT_PRINTF_BUFFER_SIZE;
  28. BufferDesc.Cnt = 0u;
  29. BufferDesc.RTTBufferIndex = BufferIndex;
  30. BufferDesc.ReturnValue = 0;
  31. do {
  32. c = *sFormat;
  33. sFormat++;
  34. if (c == 0u) {
  35. break;
  36. }
  37. if (c == '%') {
  38. //
  39. // Filter out flags
  40. //
  41. FormatFlags = 0u;
  42. v = 1;
  43. do {
  44. c = *sFormat;
  45. switch (c) {
  46. case '-': FormatFlags |= FORMAT_FLAG_LEFT_JUSTIFY; sFormat++; break;
  47. case '0': FormatFlags |= FORMAT_FLAG_PAD_ZERO; sFormat++; break;
  48. case '+': FormatFlags |= FORMAT_FLAG_PRINT_SIGN; sFormat++; break;
  49. case '#': FormatFlags |= FORMAT_FLAG_ALTERNATE; sFormat++; break;
  50. default: v = 0; break;
  51. }
  52. } while (v);
  53. //
  54. // filter out field with
  55. //
  56. FieldWidth = 0u;
  57. do {
  58. c = *sFormat;
  59. if ((c < '0') || (c > '9')) {
  60. break;
  61. }
  62. sFormat++;
  63. FieldWidth = (FieldWidth * 10u) + ((unsigned)c - '0');
  64. } while (1);
  65. //
  66. // Filter out precision (number of digits to display)
  67. //
  68. NumDigits = 0u;
  69. c = *sFormat;
  70. if (c == '.') {
  71. sFormat++;
  72. do {
  73. c = *sFormat;
  74. if ((c < '0') || (c > '9')) {
  75. break;
  76. }
  77. sFormat++;
  78. NumDigits = NumDigits * 10u + ((unsigned)c - '0');
  79. } while (1);
  80. }
  81. //
  82. // Filter out length modifier
  83. //
  84. c = *sFormat;
  85. do {
  86. if ((c == 'l') || (c == 'h')) {
  87. sFormat++;
  88. c = *sFormat;
  89. } else {
  90. break;
  91. }
  92. } while (1);
  93. //
  94. // Handle specifiers
  95. //
  96. switch (c) {
  97. case 'c': {
  98. char c0;
  99. v = va_arg(*pParamList, int);
  100. c0 = (char)v;
  101. _StoreChar(&BufferDesc, c0);
  102. break;
  103. }
  104. case 'd':
  105. v = va_arg(*pParamList, int);
  106. _PrintInt(&BufferDesc, v, 10u, NumDigits, FieldWidth, FormatFlags);
  107. break;
  108. case 'u':
  109. v = va_arg(*pParamList, int);
  110. _PrintUnsigned(&BufferDesc, (unsigned)v, 10u, NumDigits, FieldWidth, FormatFlags);
  111. break;
  112. case 'x':
  113. case 'X':
  114. v = va_arg(*pParamList, int);
  115. _PrintUnsigned(&BufferDesc, (unsigned)v, 16u, NumDigits, FieldWidth, FormatFlags);
  116. break;
  117. case 's':
  118. {
  119. const char * s = va_arg(*pParamList, const char *);
  120. do {
  121. c = *s;
  122. s++;
  123. if (c == '\0') {
  124. break;
  125. }
  126. _StoreChar(&BufferDesc, c);
  127. } while (BufferDesc.ReturnValue >= 0);
  128. }
  129. break;
  130. case 'p':
  131. v = va_arg(*pParamList, int);
  132. _PrintUnsigned(&BufferDesc, (unsigned)v, 16u, 8u, 8u, 0u);
  133. break;
  134. case '%':
  135. _StoreChar(&BufferDesc, '%');
  136. break;
  137. // Add the ability to output floating point numbers. It has two decimal places by default.
  138. #if 0
  139. case 'f':
  140. case 'F':
  141. {
  142. char ch[10]={0};
  143. const char * s=ch;
  144. double a = va_arg(*pParamList, double);
  145. sprintf(ch,"%4.3f",a);
  146. do{
  147. c = *s;
  148. s++;
  149. if (c == '\0') break;
  150. _StoreChar(&BufferDesc, c);
  151. }while(BufferDesc.ReturnValue >= 0);
  152. }break;
  153. #else
  154. case 'f':
  155. case 'F': {
  156. float fv = (float)va_arg(*pParamList, double); // Retrieves the input floating point value
  157. if(fv < 0) _StoreChar(&BufferDesc, '-'); // Judge the sign
  158. v = abs((int)fv); // Take the positive integer part
  159. _PrintInt(&BufferDesc, v, 10u, NumDigits, FieldWidth, FormatFlags); // According to an integer
  160. _StoreChar(&BufferDesc, '.'); // Display decimal point
  161. v = abs((int)(fv * 1000));
  162. v = v % 1000;
  163. _PrintInt(&BufferDesc, v, 10u, 3, FieldWidth, FormatFlags); // Display three decimal places
  164. }break;
  165. #endif
  166. default:
  167. break;
  168. }
  169. sFormat++;
  170. } else {
  171. _StoreChar(&BufferDesc, c);
  172. }
  173. } while (BufferDesc.ReturnValue >= 0);
  174. if (BufferDesc.ReturnValue > 0) {
  175. //
  176. // Write remaining data, if any
  177. //
  178. if (BufferDesc.Cnt != 0u) {
  179. SEGGER_RTT_Write(BufferIndex, acBuffer, BufferDesc.Cnt);
  180. }
  181. BufferDesc.ReturnValue += (int)BufferDesc.Cnt;
  182. }
  183. return BufferDesc.ReturnValue;
  184. }

二、运行结果:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/435975
推荐阅读
相关标签
  

闽ICP备14008679号