Pages: 1 [2] 3   Go Down
Author Topic: What does the F() do exactly?  (Read 5991 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged


0
Offline Offline
Edison Member
*
Karma: 8
Posts: 1411
Arduino rocks
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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) smiley
Logged

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
__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);
}
Logged

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 202
Posts: 8711
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think you can even initialize the array at compile time rather than run time.
Code:
__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);
}
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Leighton Buzzard, UK
Offline Offline
Edison Member
*
Karma: 21
Posts: 1339
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

smiley
Logged

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

Adelaide, South Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 144
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ?
Logged

Global Moderator
Dallas
Online Online
Shannon Member
*****
Karma: 200
Posts: 12778
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Adelaide, South Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 144
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

0
Offline Offline
God Member
*****
Karma: 2
Posts: 596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
// F test

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

void loop() {
}
Logged

Adelaide, South Australia
Offline Offline
Full Member
***
Karma: 0
Posts: 144
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
God Member
*****
Karma: 2
Posts: 596
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 238
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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") );
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

USA
Offline Offline
Full Member
***
Karma: 0
Posts: 238
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

What version of avr-gcc are you using?
Logged

Pages: 1 [2] 3   Go Up
Jump to: