Pages: [1]   Go Down
Author Topic: P macro  (Read 455 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: 170
Posts: 12487
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: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

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: 92
Posts: 3969
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

Why not visit my eBay shop? http://stores.ebay.co.uk/Majenko-Technologies
Replacement for the Arduino IDE: UECIDE - Proper serial terminal, graphing facilities, plugins, overhauled internals.
Java isn't bad in itself, but it has enabled morons to write programs.

Pages: [1]   Go Up
Jump to: