Go Down

Topic: What does the F() do exactly? (Read 8432 times) previous topic - next topic

liudr

Thanks for finding out the answer mmcp42. I guess F() is a part of the solution to SRAM problem but eventually becomes part of the FLASH problem. Does anyone know how to turn on optimization option for the compiler while using arduino ide?

florinc

Quote
I believe it's just a technique to avoid using "words" that people might already have in their program
who in their right mind would use variables like __fred?!?

Right. One would have to count (underscores) instead of read (words) :)

mmcp42

this is a bit klunky, but might help
(like I said, who in their right mind actually types __ :) )

Code: [Select]

__FlashStringHelper *myStrings[3];

void setup(void)
{
  Serial.begin(19200);
  myStrings[0] = F("abcdefg");
  myStrings[1] = F("xbcdefg");
}
void loop(void)
{
  Serial.println(myStrings[0]);
  Serial.println(myStrings[1]);
  delay(1000);
}
there are only 10 types of people
them that understands binary
and them that doesn't

johnwasser

I think you can even initialize the array at compile time rather than run time.
Code: [Select]

__FlashStringHelper *myStrings[] = {F("abcdefg"), F("xbcdefg")};

void setup(void)
{
  Serial.begin(19200);
}
void loop(void)
{
  Serial.println(myStrings[0]);
  Serial.println(myStrings[1]);
  delay(1000);
}
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

mmcp42

there are only 10 types of people
them that understands binary
and them that doesn't

ninja2



Could this F() syntax be used in place of rather cumbersome:
    strcpy_P(buffer, (char*)pgm_read_word(&(str_table[index])));


This may help...
http://arduiniana.org/libraries/flash/


can you clarify the origin of the F() syntax, is it from Mikal Hart's Flash library, or is it part of C or the standard IDE ?

Coding Badly

can you clarify the origin of the F() syntax, is it from Mikal Hart's Flash library


That's how I was introduced to it.

ninja2

so:
(and this may seem a very dumb question)

if (I don't include the flash library in my sketch)
  { F() will not work? ;}
endif

mromani

This sketch compiles and runs, so the answer seems to be no, one doesn't have to (explicitly) include Flash library to use F().

Code: [Select]

// F test

void setup() {
    Serial.begin(9600);
   
    Serial.println(F("STRINGA 1"));
    Serial.println(F("STRINGA 2"));
}

void loop() {
}


ninja2

doh! I could've done that ... excellent way to resolve my query, thanks mromani


kb3dow

Could someone please help me out with this, I am unable to get the F() macro compiled on FreeBSD.
I am using avr-gcc and the arduino 1.0 codebase.
The following line
Serial << F("This is a test string");
throws up an error by the compiler which says

flash_streaming.cpp:210:13: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away qualifiers

I apologize for cross posting, but I already asked the same question in an earlier post - but didn't make any headway
http://arduino.cc/forum/index.php?topic=95572.new

The command I am issuing is -
avr-gcc tst.cpp  -mmcu=atmega328p -DF_CPU=16000000UL -Os -w -Wl,--gc-sections -ffunction-sections -fdata-sections -DARDUINO=100  -I /home/rr/code/arduino//include/arduino/ -I . -c -o tst.o

mkwired


Serial << F("This is a test string");


Try
Serial.print( F("This is a test string") );
or
Serial.println( F("This is a test string") );

kb3dow

It has nothing to do with the << operator, even Serial.print(F("Hello World")) has the same problem.
Serial.print(F("Hello World"));
gives
tst.cpp:19:18: error: reinterpret_cast from type 'const char*' to type '__FlashStringHelper*' casts away qualifiers

I have a medium sized multi-file sketch compiling and functioning using avr-gcc, so there is no problem in general with compiling/linking etc. But this particular issue has me puzzled.

mkwired

What version of avr-gcc are you using?

Go Up