Hi,
With the new F() function, I can see how easy it would be to print from Flash without using PROGMEM.
How can I use this function to print a progmem array too?
Thank you,
RI
Hi,
With the new F() function, I can see how easy it would be to print from Flash without using PROGMEM.
How can I use this function to print a progmem array too?
Thank you,
RI
sorry that i cant help, but where is the info on this function?
But I can't find within any library file where it is referenced.
I wanted to peek and see what it actually does, but I was unable to find it.
this is the only other Arduino reference i can find for F() http://arduino.cc/it/Serial/Print
You can't. You can make a normal PROGMEM array and define your own print function, like this:
void printProgmemArray(prog_uchar* array, byte length) {
for (int i = 0; i < length; i++) {
Serial.print(array); Serial.print(", ");
if (i % 10 == 0) Serial.println();
}
Serial.println();
}
It is defined like this in WString.h:
class __FlashStringHelper;
#define F(string_literal) (reinterpret_cast<__FlashStringHelper *>(PSTR(string_literal)))
newhobby:
With the new F() function, I can see how easy it would be to print from Flash without using PROGMEM.
How can I use this function to print a progmem array too?
Flash is PROGMEM.
i think he new that, he was saying progmem as the function in the code to access the actual progmem(flash)
Well, I know that much
I've been using this for quite a while:
void PROGMEMprint(const prog_char str[])
{
char c;
if(!str) return;
while((c = pgm_read_byte(str++)))
Serial.write(c);
}
The reason I wanted to know about the F() printing PROGMEM arrays too is that my code has several PROGMEM variables defined on my custom library header file that I can use on any of my other library files.
So, my questions now is:
Instead of using this:
const prog_char MyTestVar[] PROGMEM = "Test";
PROGMEMprint(MyTestVar);
If I started using this:
#define MyTestVar F("Test")
Serial.print(MyTestVar);
Which one is the best alternative, keeping in mind that I have code size issues?
I did a simple run test and the PROGMEM route used less code size.
The second example uses less memory because it won't require the PROGMEMprint function.
But your two examples don't do the same thing. In the first, MyTestVar is a character string. In the second it is a manifest constant.
Pete
Well, I don't really understand much the difference of the two you mentioned, but actually the PROGMEM code is the one that was smaller not the F().
The end result is the same for both. I wanted to print from flash and they both do that, right?
So, you seem to understand it much more than me. Which is the best approach?
but actually the PROGMEM code is the one that was smaller not the F().
Can't be. Both examples use the same amount of PROGMEM but your PROGMEM code adds the PROGMEMprint function which isn't required when using F(). So the F() version will use less PROGMEM (unless you left the PROGMEMprint function in the F() program).
Pete
#define MyTestVar F("Test")
void setup()
{
Serial.begin(57600);
Serial.print(MyTestVar);
}
void loop()
{
}
Binary sketch size: 1914 bytes (of a 32256 byte maximum)
const prog_char MyTestVar[] PROGMEM = "Test";
void setup()
{
Serial.begin(57600);
PROGMEMprint(MyTestVar);
}
void loop()
{
}
void PROGMEMprint(const prog_char str[])
{
char c;
if(!str) return;
while((c = pgm_read_byte(str++)))
Serial.write(c);
}
Binary sketch size: 1868 bytes (of a 32256 byte maximum)
Maybe I'm doing something wrong.
Actually you are doing it just right. I had a look at the code which does the internal Serial.print and it will use a few more instructions than what you use because it also generates a sum of every byte that is sent and returns it as the value of the function call. So, actually, your code will be shorter.
Pete
Or you can let printf do your dirty work for you...
const prog_char MyTestVar[] PROGMEM = "Test";
int serial_putc( char c, FILE * )
{
Serial.write( c );
return c;
}
void setup( void )
{
fdevopen( &serial_putc, 0 );
printf_P( PSTR("Hello, %S"), MyTestVar );
}
void loop( void )
{
}