Pages: [1]   Go Down
Author Topic: Receiving strings in F() as parameters.  (Read 378 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 38
Don't eat yellow snow.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi

As part of a bigger project, I want to save my programs' general output strings to a text file on the SD card instead of sending it down Serial all the time. Doing this is straightforward:

Code:
#include <SPI.h> // for SD storage
#include <SD.h> // for SD storage
File blackBox;

void addToBlackBox(char* in)
{
  blackBox = SD.open("BB.TXT", FILE_WRITE);
  blackBox.println(in);
  blackBox.close(); 
}

This function works for well enough, but I need to save memory because of my elaborate program. The idea that seemed the simplest was to put all the non-changing strings I wanted on the SD card in F(), the flash memory. So I tried this:

Code:
addToBlackBox(F("hello"));

, but the compiler complained about my parameter type in addToBlackBox(), claiming that it should be a const_FlashStringHelper* . So I changed it to that in the parameters, but then the compiler complained even more, claiming that "variable or field 'addToBlackBox' declared void". I tried poking around the class files of classes that had built-in print()s and println()s, but I couldn't really find out why println() doesn't care about whether it's arguments are in F() or not. Googling it didn't help much either, since I could find little documentation on F() itself.

Please share some experience. Maybe I should approach the entire situation differently? How can I allow addToBlackBox() to accept its' argument from the flash memory?

PS I'm using a Seeeduino Stalker V2.3, and I got my libraries for SD from here:
https://code.google.com/p/sdfatlib/downloads/detail?name=sdfatlib20130313.zip&can=2&q=.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 148
Posts: 6104
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This worked for me. I just did what it told me to do.
Code:
void addToBlackBox(const __FlashStringHelper* in)
{
  blackBox = SD.open("BB.TXT", FILE_WRITE);
  blackBox.println(in);
  blackBox.close(); 
}
BTW, that is a double underscore preceeding FlashStringHelper.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 631
Posts: 50123
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
but I couldn't really find out why println() doesn't care about whether it's arguments are in F() or not.
Because the Print class overloads print() and println() to take strings in SRAM or strings in flash memory (those wrapped in F()).

Quote
How can I allow addToBlackBox() to accept its' argument from the flash memory?
You need two versions - one defined each way, as in the Print class.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 38
Don't eat yellow snow.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks guys! My mistake was extremely dumb. I left out a space when I inserted the new data type, so the compiler didn't recognize it as valid data type. damn typo
Logged

Pages: [1]   Go Up
Jump to: