Pages: [1]   Go Down
Author Topic: P macro  (Read 736 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Bald-Headed, Middle-Aged Nerd
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}


Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 217
Posts: 13742
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Rob Tillaart

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

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

P gets replaced by whatever it is defined as, so
Code:
P(signMessage) = "This is some text . . . ";
gets changed to
Code:
static const prog_uchar signMessage[] PROGMEM = "This is some text . . . ";
Logged

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 48
Bald-Headed, Middle-Aged Nerd
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#define P(name)   static const prog_uchar name[] PROGMEM

P gets replaced by whatever it is defined as, so
Code:
P(signMessage) = "This is some text . . . ";
gets changed to
Code:
static const prog_uchar signMessage[] PROGMEM = "This is some text . . . ";
Logged

UK
Offline Offline
Faraday Member
**
Karma: 99
Posts: 4153
Where is your SSCCE?!?!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Get 10% off all 4D Systems TFT screens this month: use discount code MAJENKO10

Pages: [1]   Go Up
Jump to: