Dear gurus,
As far as I know, Arduino UNO and Mega doest not support snprintf() to convert float to string, while DUE does. So I need some directives to detect which board is using when compiling the sketch.
BR,
Ardypro
Dear gurus,
As far as I know, Arduino UNO and Mega doest not support snprintf() to convert float to string, while DUE does. So I need some directives to detect which board is using when compiling the sketch.
BR,
Ardypro
What have you looked at/searched for? This is a frequent question. Look in avr/io.h.
I'm looking for a macro directive to detect which board is using, so the following code could be compiled whatever board it is:
#if defined DUE_BOARD
snprintf(); // convert float to string for DUE
#else
dtostrf(); //convert float to string for UNO or MEGA
#endif
I'm looking for a macro directive to detect which board is using
The names are not going to be that obvious, but they are listed in the avr/io.h file.
Since dtostrf() works on both, why not use the same code on both?
But it said dtostrf was not declared in this scope on DUE
PaulS:
The names are not going to be that obvious, but they are listed in the avr/io.h file.
Since there are only one board based on ARM, then how can I find the directive for ARM? Names in avr/io.h are all AVR IC.
I have got a tricky way to achieve my requirement as below:
#if defined (__AVR__) || (__avr__)
//for Arduino UNO/Mega based on AVR IC
dtostrf (value, 8, precision, str);
#else
//for Arduino DUE based on ARM IC
snprintf(str,len,"%g",value);
#endif
Thanks go to all of you!
Microprocessors using the ATmega328p (like the Uno), define AVR_ATmega328.
Microprocessors using the ATmega32u4 (like Leonardo), define AVR_ATmega32U4
Microprocessors using the ATtiny85 chip (Trinket, Gemma, Digispark), define AVR_ATtiny85
Microprocessors using the ATmega2560 chip (Mega), define AVR_ATmega2560
Due and DigiX define: SAM3X8E, and arm
Teensy 3.0 defines: MK20DX128, arm, and CORE_TEENSY
Teensy 3.1 defines: MK20DX256, arm, and CORE_TEENSY
But there is floating support in the AVR libC xxprintf() routines.
It is disabled by default and the IDE does not enable it.
If you are using the 1.5x IDE, which you probably are since you are using DUE,
you can go in and change the linker options in the AVR platforms file to enable the xxprintf() floating point
support.
Since your next question probably is how do that....
Edit the platform.txt file down in
{installdir}/hardware/arduino/avr
Change this:
compiler.c.elf.flags=-Os -Wl,--gc-sections
to this:
compiler.c.elf.flags=-Os -Wl,--gc-sections -Wl,-u,vfprintf -lprintf_flt
If you have the extra 2k of program space, it might be worth it to keep the sketch code the
same on all platforms and not have to mess with preprocessor conditionals.
--- bill