Serial.print (value, format) overload for format-string

4,786 views
Skip to first unread message

Nils Springob

unread,
Oct 11, 2013, 12:32:01 PM10/11/13
to Arduino Developer's List
Hi everyone,

what do you think of the idea to overload the print and println methods with a format-string as second parameter?
The format-string would be printf-like, it would be very usefull for some outputs, (fixed digits, trailing zeros, etc..)

Examples:
Serial.print(0x012a, "%04x") -> "012a"
Serial.print(0xab, "%02X") -> "AB"
Serial.print(3.14159265, "%.4f") -> "3.1416"
Serial.print("ab", "%5s") -> " ab"
Serial.print(0x1e, "%08b") -> "00011110"

It would be also possible to add some text:
Serial.print(12.344, "Voltage: %.1f V") -> "Voltage: 12.3 V"

If you think that it would be good, I could do the implementation (without the use of the printf-function-family ;-) ).

Best regards,
Nils

Rob Tillaart

unread,
Oct 11, 2013, 1:06:07 PM10/11/13
to Nils Springob, Arduino Developer's List
Hi Nils,
Thanks for the idea, however in Arduino sketches one can use the sprintf to format data just like printf(). and Serial.print() the string thereafter. By default the float is not supported as it takes quite a lot of memory IIRC, you should google the forum for the details as sprintf is discussed every month more than once. e.g. http://forum.arduino.cc/index.php/topic,124809.0.html

succes,
Rob






--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

Tom Igoe

unread,
Oct 11, 2013, 1:17:34 PM10/11/13
to Nils Springob, Arduino Developer's List
I think something more human-understandable would be better than the rather terse printf modifiers.
> --
> You received this message because you are subscribed to the Google Groups "Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.

Nils Springob

unread,
Oct 11, 2013, 1:41:22 PM10/11/13
to devel...@arduino.cc
Hi Rob,

My idea is from a didactical approach:

1) A beginner learns the output of text by using the print and println functions.
2) The beginner starts with easy (classical) formatting (HEX, DEC, precision etc...)
3) The beginner will learn more complex formatting (my suggested form)
4) Now the beginner is ready to become an expert, learning the formatting (and the pitfalls) with sprintf()

With the suggested approach the programmer does not need to now the details and difficulties of C-Strings.
Another point is the big memory footprint of the C90 conform sprintf implementation, if you dont use the sprintf family the linker don't reserves this memory.
Because the arduino-core is linked as a lib, it would be possible to let the user choose the flavor without any impact on codesize...

Best regards,
Nils
> To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@__arduino.cc.
>
>
> --
> You received this message because you are subscribed to the Google Groups "Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.


--
Nils Springob
Am Denkmal 8, 52223 Stolberg
Tel: +49-2402-997-8391
Mobil: +49-163-6556110

Rob Tillaart

unread,
Oct 11, 2013, 1:53:54 PM10/11/13
to Tom Igoe, Nils Springob, Arduino Developer's List

Easiest I can think of is 2 parameters:
1) format
2) length (bit like float)

something like:
Serial.println(var, HEX, 4);    ==> ABCD  (0x prefix?)
Serial.println(var, DEC, 4);   ==> 1234
Serial.println(var, OCT, 4);   ==> 0123

Serial.println("123456789", LEFT, 20);  // ?


<side track>
Maybe time to include SCI / ENG formatting for floats? so floats larger than maxlong can be printed correctly.
See discussions here 

+ support for divmod10 would improve all print derived classes (and even others that use / and %) 
</side track>


Rob Tillaart

unread,
Oct 11, 2013, 2:08:21 PM10/11/13
to Nils Springob, Arduino Developers
Hi Nils,

Personally (I'm not the voice of Arduino here) I would go as fast as possible to stage 4 Then the programmer knows what is possible and can use it or not. I think it would overload the user if there was an additional intermediate version of formatting. Learning the ins/outs of printf fits on 2 pages => http://www.cplusplus.com/reference/cstdio/printf/

I would like if we had an overloaded int called hex that when printed would automagically output "ABCD"
flavours hex8_t  hex16_t  hex24_t  hex32_t hex64_t 

if you want something printed as a hex make it a hex (under the hood all are ints)

yes we should also have:  bin1_t bin8_t bin16_t bin32_t... it is more OO I think

(my opinion, not necessary the opinion of Arduino)
Rob



To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.


--
Nils Springob
Am Denkmal 8, 52223 Stolberg
Tel: +49-2402-997-8391
Mobil: +49-163-6556110
--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.

Tom Igoe

unread,
Oct 11, 2013, 2:10:34 PM10/11/13
to Rob Tillaart, Nils Springob, Arduino Developers
Can anyone explain to me what the _t on the end of variable names in C means?  Been programming for 20-odd years, still no clue.

t.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.

Rob Tillaart

unread,
Oct 11, 2013, 2:19:44 PM10/11/13
to Tom Igoe, Nils Springob, Arduino Developers
it means type (afaik) and yes it should be obvious from the place where it is used that it is a type.

Tom Igoe

unread,
Oct 11, 2013, 2:58:02 PM10/11/13
to Rob Tillaart, Nils Springob, Arduino Developers
It is not obvious. And the words "should be obvious" express perfectly the mentality that I am trying to avoid in thinking about Arduino. "Should be obvious" makes someone to whom it is not obvious feel like an idiot. I don't want users to feel like idiots just because they don't know a thing.

The _t on the end doesn't seem to add anything in terms of human understanding to the type names, at first glance. Is it there to help the compiler out?

t.

Álvaro Lopes

unread,
Oct 11, 2013, 3:05:34 PM10/11/13
to Tom Igoe, Rob Tillaart, Nils Springob, Arduino Developers
On 11/10/13 19:58, Tom Igoe wrote:
> It is not obvious. And the words "should be obvious" express perfectly the mentality that I am trying to avoid in thinking about Arduino. "Should be obvious"
> makes someone to whom it is not obvious feel like an idiot. I don't want users to feel like idiots just because they don't know a thing.
>
> The _t on the end doesn't seem to add anything in terms of human understanding to the type names, at first glance. Is it there to help the compiler out?

uint8_t, uint16_t, uint32_t ?

Denotes a type. It might not be useful in the beginning, but later on you'll get used to it and will help you a lot discerning from "raw" words like "byte" and
"word". The latter does not mean anything useful at all.

Using simple conventions make your work more understandable in the long run. I don't actually get why most are against it. Using a "_t" for types is just like
using camelCase for functions. Does not add much except making your code easy to understand and easier to maintain.

Alvie

David Mellis

unread,
Oct 11, 2013, 3:18:30 PM10/11/13
to Álvaro Lopes, Tom Igoe, Rob Tillaart, Nils Springob, Arduino Developers
I like the idea of adding an extra parameter to Serial.print() to specify the number of digits. But I think we should leave out the full printf() syntax.


Knight, Dave

unread,
Oct 11, 2013, 5:14:09 PM10/11/13
to Nils Springob, Arduino Developer's List
My life would be much simpler if the Arduino sprintf %f format specification worked!
 


--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+unsubscribe@arduino.cc.



--
"Gun control empowers the wolves, and creates more sheep!"
              Dan Bongino, Former US Secret Service Agent



Rob Tillaart

unread,
Oct 11, 2013, 5:41:01 PM10/11/13
to Tom Igoe, Nils Springob, Arduino Developers
I agree Tom, that's why I used "should be". as it is not obvious, a person has to think twice (or more) to understand.

There are many related questions:
- Why do we use int instead of integer? char instead of character?  long? how long? long long? long what?
I think historically even for source code there was little storage space, so every byte count. Think that is partly the reason why types often have short names. Also because they are used often so short names => faster typing (in the meaning of keyboard presses ;) and the compiler can parse short tokens faster than long ones. And those compilers were not that fast.

These basic C types (int long char) had a drawback, they were compiler/platform specific (most of them), that paved the way for more descriptive name conventions like "u int 8 type" or uint8_t, These types are platform independent and their behaviour therefore more predictive. And that predictability is why users feel like idiots or not. In the end usability is expectation management, do functions variables code do what people expect?

back to _t:
The _t is not needed for the compiler as ui8 should be enough to define unsigned integer of 8 bits. For the compiler it would even be enough to have only a single char/token but then the human looses its mnemonic. So there is a balance to minimize for the compiler and readability for the user. If you want to see an strange (imho) looking language google "J language"

Finally one remark, the basic type I miss the most often is the unsigned float, but that's another story.


Todd Krein

unread,
Oct 11, 2013, 8:55:53 PM10/11/13
to Rob Tillaart, Tom Igoe, Nils Springob, Arduino Developers

It means “A type-def’d type”.

 

Back in the day, when folks first started using typedef en-mass (which I think was about the time C++ started taking off), people got confused by the syntax, esp if there was a compiler error. One didn’t know if “ui8” was a typedef, a #define, a macro, etc… Some folks use an “_p” to mean a typedef pointer to that type, i.e. Unsigned char * would be “ui8_p”.

 

It comes from the same mindset that used to require that all global variables were of the form “gGlobalName”, and constants were “kCONSTANTNAME”.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.



--
Nils Springob
Am Denkmal 8, 52223 Stolberg
Tel: +49-2402-997-8391
Mobil: +49-163-6556110



--
You received this message because you are subscribed to the Google Groups "Developers" group.

To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.

 

 

--
You received this message because you are subscribed to the Google Groups "Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to developers+...@arduino.cc.

Reply all
Reply to author
Forward
0 new messages