概述

最近公司项目换另一款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 0case '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;#elsecase 'f':case 'F': {float fv = (float)va_arg(*pParamList, double);    // Retrieves the input floating point valueif(fv < 0) _StoreChar(&BufferDesc, '-');          // Judge the signv = abs((int)fv);                                 // Take the positive integer part_PrintInt(&BufferDesc, v, 10u, NumDigits, FieldWidth, FormatFlags);  // According to an integer_StoreChar(&BufferDesc, '.');                                        // Display decimal pointv = abs((int)(fv * 1000));               v = v % 1000;_PrintInt(&BufferDesc, v, 10u, 3, FieldWidth, FormatFlags);          // Display three decimal places}break;#endifdefault: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;
}

二、运行结果:

SEGGER_RTT_printf()函数实现打印浮点、负数-示例相关推荐

  1. 使用JLink SEGGER_RTT_printf()函数实现打印浮点数

    问题: 源码SEGGER_RTT_printf()函数无法打印浮点数. 思路: 修改SEGGER_RTT_printf()函数来达到打印浮点数. 参考文章:https://blog.csdn.net/ ...

  2. php云打印类,PHP应用:PHP云打印类完整示例

    <PHP应用:PHP云打印类完整示例>要点: 本文介绍了PHP应用:PHP云打印类完整示例,希望对您有用.如果有疑问,可以联系我们. 本文实例讲述了PHP云打印类.分享给大家供大家参考,具 ...

  3. 【C语言】函数:实现一个函数,打印乘法口诀表

    文章目录 1.条件概述 2.代码实现 1.条件概述 实现一个函数,打印乘法口诀表,口诀表的行数和列数自己指定,输入9,输出99口诀表,输入12,输出1212的乘法口诀表. 2.代码实现 //实现一个函 ...

  4. linux qt 打印预览控件,Qt实现保存、浏览、预览、打印功能的示例代码

    Qt提供了以文本.图片.HTML等方式来实现对文档的操作,主要用到了QPrinter类和QPainter类,用到了QFileDialog文件窗口.QPrintPreviewDialog预览窗口类和QP ...

  5. RT-Thread 入门学习笔记:vsnprintf来替代rt_vsnprintf来打印浮点

    前言 有童鞋说在gcc下,vsnprintf来替代rt_vsnprintf来打印浮点会引发死机 经过实际验证,没有发现死机 但并不建议直接用vsnprintf来替代rt_vsnprintf,打印浮点多 ...

  6. C语言练习题,三色球抽取,从3个红球,5个白球,6个黑球中任意取出8个作为一组进行输出。在每组中可以没有黑球,但必须要有红球和白球。编程实现以上功能。用函数返回其组合数,在函数中打印每组的组合

    三色球分组 从3个红球,5个白球,6个黑球中任意取出8个作为一组进行输出.在每组中可以没有黑球,但必须要有红球和白球.编程实现以上功能.用函数返回其组合数,在函数中打印每组的组合 函数原型为: int ...

  7. boost::sort模块实现spreadsort 浮点排序示例

    boost::sort模块实现spreadsort 浮点排序示例 实现功能 C++实现代码 实现功能 boost::sort模块实现spreadsort 浮点排序示例 C++实现代码 #include ...

  8. boost::math模块两个 Lambert W 函数的最基本调用示例

    boost::math模块两个 Lambert W 函数的最基本调用示例 实现功能 C++实现代码 实现功能 boost::math模块两个 Lambert W 函数的最基本调用示例 C++实现代码 ...

  9. python函数调用的一般形式_Python的函数的定义与使用示例

    Python培训分享函数的定义与使用示例,结合实例形式分析了Python函数的定义.参数.变量作用域.返回值等相关概念与使用技巧. Python定义函数使用def关键字,一般格式如下: def 函数名 ...

  10. python打印表格_python 6.7 编写printTable()函数表格打印(完整代码)

    编写一个名为printTable()的函数,它接受字符串的列表的列表,将它显示在组织良好的表格中,每列右对齐.假定所有内层列表都包含同样数目的字符串 输入: tableData = [['apple' ...

最新文章

  1. 新遇到的系统编译问题!已解决!
  2. 乾勤科技-智能车竞赛培训计划
  3. OpenCV:详解掩膜mask
  4. 暴力打表之hdu 2089
  5. android文件读写操作布局文件代码,android实现文件读写功能
  6. Sprite Kit 入门教程
  7. 2016 年最值得程序员阅读的开源书:《全栈增长工程师指南》
  8. a标签提交form表单_Web前端开发基础知识,HTML中表单元素的理解
  9. thinkPhp5.1配置常量与输出
  10. 有一定的php基础,求推荐PHP框架,本人有一定PHP基础。
  11. SmartDeblur-图片模糊处理器
  12. dict后缀_词根词缀法记单词之dict
  13. 计算机win7关机重启,电脑win7关机后自动重启如何解决_win7一关机就立马自动重启修复方法-win7之家...
  14. 看电影学英语是学习口语的最佳方式
  15. 【基础算法】Gossip协议
  16. Linux 命令(179)—— nice 命令
  17. 物流快递商家寄件运力查询接口API代码实例
  18. Unity发布WebGL运行Js代码报错:‘Pointer_stringify‘ is obsoleted and will be removed in a future Unity version.
  19. Python防微信撤回
  20. 浅谈如何做一名优秀的WEB前端工程师

热门文章

  1. 我的Verilog HDL学习历程(二) 组合逻辑电路的一个实例:基于EGO1板子
  2. C/C++语言经典、实用、趣味程序设计编程百例精解
  3. U盘文件夹被隐藏能够解决方法
  4. linuxeth0路径_linux 驱动 路径(最新更新)网卡驱动路径
  5. Android 打包AAB+PAD(java篇)
  6. 常微分方程机敏问答简介
  7. c语言595驱动数码管,74hc595驱动数码管程序
  8. Stacer ---- Linux系统优化和监测工具
  9. flashfxp和任务计划自动定时ftp备份
  10. 2021-10-21 FFmpeg截取视频第一帧