arduino / arduino/ArduinoCore-API
Improved print formatting
- 主要语言
- C++
- 星标
- 306
- 派生
- 150
- PR 合并指标
- 30 天内没有已合并 PR
描述
In the past, there has been talk of improving print formatting. e.g. on the mailinglist [here](https://groups.google.com/a/arduino.cc/d/topic/developers/y7p5wS4ICIY/discussion), [here](https://groups.google.com/a/arduino.cc/d/topic/developers/NWnjeSyuNek/discussion), [here](https://groups.google.com/a/arduino.cc/d/topic/developers/7KpdLDgFsO0/discussion). The bottom line is that there is a need for a way to have more control over print formatting (e.g. leading zeroes) and, IMHO, also ways to apply custom formatting and formatting options.
Note that printf is often suggested as the solution to this need. In the above threads, a lot of the discussion is about his, but there are IMHO sufficient reasons to *not* want to use printf (which I'll not repeat here). Please do *not* add comments here arguing for printf support. Instead, head over to #28 and #32 if you want to make a case for printf.
A related request is to allow printing multiple values in a single print call (e.g. to do things like "Serial.println("Received ", num, " bytes");`).
PR #30 has an implementation for this.
IMHO it would be good to tackle formatting options at the same time as variadic printing, since the implementations are likely related (and doing one without the other might cause compatibility problems in the future).
In the above discussions, I've suggested an approach where printable arguments can be intermixed with formatting directives, putting the formatting after the value (just like the current base and precision arguments). These formatting directives could then be either builtin-directives, or custom directives. For my previous thoughts, see [here](https://groups.google.com/a/arduino.cc/d/msg/developers/7KpdLDgFsO0/W4XGNr_t_kAJ) and [here](https://groups.google.com/a/arduino.cc/d/msg/developers/7KpdLDgFsO0/4FUnoRsppA8J). Since people seemed to like this approach, I did an initial implementation.
I still have a lot of documentation to do, and I'm not quite happy with the way that formatter customization works now, but in the interest of discussion, I've pushed my changes here: https://github.com/arduino/ArduinoCore-avr/blob/formatting/cores/arduino/Print.h For an example sketch that uses this, see https://gist.github.com/matthijskooijman/cbac69e8db907ab0f849775b5a1cac44. Note that this is just the sketch I was using during development, so it's not so much a realistic testcase as well as a way to make sure various things would (not) work. I'm not actually sure if any of this even compiles currently, it's been a while since I worked on this.
In #30, @cousteaulecommandant said:
> Maybe it'd be better to move formatting entirely out of Print.h (except for very simple things, for the sake of backwards compatibility) to a separate "value formatter" class that does all the formatting work, and leave Print::print for simple multi-argument printing.
This might be an interesting avenue to explore. It would mean that (custom/extended) formatting would work differently than the current base/precision formatting options, but if we come up with something significantly better, I guess deprecating the existing system and switching to something new would be ok. Implementing this might very well be simpler than what I did so far (it might even be a matter of creating custom `Printable` instances, though I suspect there might be some performance issues with that approach).
I have no time *right now* to go into this further, but wanted to at least create this issue as a place to collect further discussion.
贡献指南
这个仓库没有索引到贡献指南
调研方向
首先阅读 formatting 分支中 cores/arduino/Print.h 的提议实现,然后检查链接的示例 sketch 以及 issues #28、#30 和 #32 中的相关讨论。当格式化和可变参数打印方案确定并完成实现、文档编写,且验证能够使用示例用例编译时,工作即告完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- cpp
- 领域
- api, embedded-iot
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100