Pages: [1]   Go Down
Author Topic: How does lcd.print() in LiquidCrystal work?  (Read 1402 times)
0 Members and 1 Guest are viewing this topic.
Western New York, USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 4382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Could someone please explain how the lcd.print() function operates?  I know the process starts with this line in LiquidCrystal.h:  "class LiquidCrystal : public Print {"

I found this statement by mikalhart:
Quote
If you look at the declaration for class LiquidCrystal, you'll see that it inherits from class Print.  When you call lcd.print, you are actually calling Print::print, which calls LiquidCrystal::send
on the forum in: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1228578130/0 but to me it might just as well be written in zorkonian by a grue.

Thanks, floresta
« Last Edit: April 22, 2009, 06:46:55 pm by floresta » Logged

Brooklyn, NY
Offline Offline
Jr. Member
**
Karma: 0
Posts: 92
Webduino / RGB LED Shield
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is using a feature of C++ called class inheritance.  You'll need to find a good introduction to that language to really understand this.  The page at http://www.cplusplus.com/doc/tutorial/inheritance/ gives some good information, but you'll need to read the earlier parts of that site to get the background needed to understand that article.
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 4382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank's for the reply but it isn't quite what I wanted.  I already knew that it used "class inheritance", the tipoff was the phrase "it inherits from class Print".  That phrase had previously led me to the cplusplus site where I found even more jargon than in the original post.  

Quote
You'll need to find a good introduction to that language to really understand this.
Unfortunately an "introduction" probably won't do the trick and anything beyond that seems to rapidly fill with jargon.

Since the only place in the LiquidCrystal library where the word "print" is mentioned is here:

#include "Print.h"
class LiquidCrystal : public Print {


in LiquidCrystal.h this must be where I am supposed to see that "it inherits from class Print".  To me this is one of those "clearly we see" or "this is inherently obvious to the most casual observer" statements that I seem to remember from my university days.

I worked through the Arduino Library tutorial but that example is very simple and doesn't use (and therefore doesn't explain) some of the techniques used in the LiquidCrystal library.  One of the techniques that the example doesn't use is "class inheritance" which is what prompted my post.

The Library tutorial ends with this paragraph:  
Quote
That's all for now but I'll probably write an advanced library tutorial soon. In the meantime, if you have any problems or suggestions, please post them to the Software Development forum.
Well folks - here I am!  This isn't a suggestion so it must be a problem.  Can someone enlighten me.

Thanks again,

floresta
Logged

Austin, TX USA
Offline Offline
God Member
*****
Karma: 5
Posts: 997
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Let me give it a try.

Let's say you have class Print that contains members functions a, b, and c.  If you have an object of type "Print", you can call those functions with the syntax p.a(), p.b(), p.c(), etc.  So far so good?

Let's further say, without going into detail, that you have another class LiquidCrystal that inherits from class Print.  That means, roughly, that every LiquidCrystal object is also a Print object.  So a LiquidCrystal object can call Print member functions a, b, and c in addition to whatever functions are defined in LiquidCrystal.

That's why you can write lcd.print(), even though the LiquidCrystal class doesn't (directly) provide a print() method.  It's provided by Print!

The magic in this particular design is that the Print class's print() (eventually) calls the mysterious "write" function to "write" a byte.  But wait a second!  Print doesn't have a "write" function.  Where does "write" get defined?!  Answer: back down in the LiquidCrystal class.  

Just as LiquidCrystal relies on Print to provide the print() function, Print relies on LiquidCrystal to provide the write() function.  LiquidCrystal's "write" function is where all the smarts about how to write to an LCD live.

All the other classes that derive from Print provide a write() function too.  That's why

lcd.print() -> prints to an LCD (LiquidCrystal)
Serial.print() -> prints to a serial port (HardwareSerial)
nss.print() -> prints to a software serial port (NewSoftSerial)
eth.print() -> prints to an ethernet port (Ethernet)
str.print() -> prints to a string (PString)

Mikal
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 4382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Mikal - that's exactly what I was looking for!  

floresta
Logged

Pages: [1]   Go Up
Jump to: