Go Down

Topic: P macro (Read 792 times) previous topic - next topic

BobW

The code below produces this for output:

This is some text . . .
This is some more text.


The "P Macro" seems to be an integral part of the Arduino "language".
Note that no libraries are included in this sketch.

I'm not exacly sure why this sketch works (i.e. the P part)

Where can I learn more about "P" ?
All I know came from studying the Webduino library.
A basic web seach has not produced any useful results for me.

As usual, sorry if this question is too basic.

Thanks,
Bob W


Code: [Select]
#define P(name)   static const prog_uchar name[] PROGMEM

P(signMessage) = "This is some text . . . ";


void setup()
{
 Serial.begin(9600);
 printP(signMessage);
 printCRLF();
 P(signMessage) = "This is some more text.";
 printP(signMessage);
}


void loop() {
}



void printP(const prog_uchar *str)
{
 // copy data out of program memory into local storage, write out in
 // chunks of 32 bytes to avoid extra short TCP/IP packets
 uint8_t buffer[32];
 size_t bufferEnd = 0;

 while (buffer[bufferEnd++] = pgm_read_byte(str++))
 {
   if (bufferEnd == 32)
   {
     Serial.write(buffer, 32);
     bufferEnd = 0;
   }
 }

 // write out everything left but trailing NUL
 if (bufferEnd > 1)
   Serial.write(buffer, bufferEnd - 1);
}



void printCRLF()
{
 Serial.write((const uint8_t *)"\r\n", 2);
}




robtillaart

I do know the F macro e.g. - http://arduino.cc/forum/index.php?topic=110307.0 -
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

majenko

P is a macro, defined at the top of your sketch

Code: [Select]

#define P(name)   static const prog_uchar name[] PROGMEM


P gets replaced by whatever it is defined as, so
Code: [Select]

P(signMessage) = "This is some text . . . ";

gets changed to
Code: [Select]

static const prog_uchar signMessage[] PROGMEM = "This is some text . . . ";
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

lloyddean

Please be ware that the IDE will automatically, depending upon the version, add an include of "WProgram.h" or "Arduino.h" if you don not.

BobW

I just inferred that the P was something special because the IDE makes the "P" in the define line of the sketch RED in color, as if it were a keyword.   

I got suckered by this.

This does not happen if I change name of macro to "Q", or "R".
Why would this be ?

Bob

PS:  What I am trying to accomplish is to send a big block of HTML to my Arduino Web Server, without using many individual client.print("html code") lines.







P is a macro, defined at the top of your sketch

Code: [Select]

#define P(name)   static const prog_uchar name[] PROGMEM


P gets replaced by whatever it is defined as, so
Code: [Select]

P(signMessage) = "This is some text . . . ";

gets changed to
Code: [Select]

static const prog_uchar signMessage[] PROGMEM = "This is some text . . . ";


majenko


I just inferred that the P was something special because the IDE makes the "P" in the define line of the sketch RED in color, as if it were a keyword.   

I got suckered by this.

This does not happen if I change name of macro to "Q", or "R".
Why would this be ?

Because P will be defined in one of the many keywords.txt files?
Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Go Up