Pages: [1]   Go Down
Author Topic: printf() Command on Graphic LCD?  (Read 2153 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible to use the printf command to print text to a graphical LCD? I will be using the ks0108 library.
Logged

Seattle, WA USA
Online Online
Brattain Member
*****
Karma: 613
Posts: 49303
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is it possible to use the printf command to print text to a graphical LCD?
No, but you can create a character array, and use sprintf to format the text in the buffer, and then print that buffer.
Logged

Central Europe
Offline Offline
Edison Member
*
Karma: 7
Posts: 1220
Use the Source, Luke.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is it possible to use the printf command to print text to a graphical LCD?
No, but you can create a character array, and use sprintf to format the text in the buffer, and then print that buffer.

And if you use this solution, make sure you read the fine print about the included floating point libraries. If you want to use printf/sprintf to format floats, this won't work because of the limited math library included. The reason for this is - as so often on micro-controllers - memory constraints.

Korman
Logged

Seattle, Wa
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, you can use printf() if you initialize it with fdevopen() first.

This very simple example will work for printing on a single line.  You could extend it to keep a line buffer, interpret CR & LF, and scroll the screen up.

Code:
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

int lcd_putc( char c, FILE * )
{
  lcd.write( c );
  return 0;
}

void setup(void)
{
  ...
  fdevopen( &lcd_putc, NULL );
  ...
}
Logged

Dallas, TX USA
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it possible to use the printf command to print text to a graphical LCD? I will be using the ks0108 library.

Yes you can!
If you use the latest library (glcd v3 beta).
Use the latest glcd v3 library instead of the older ks0108 library
as it has many improvements including printf support.

I totally detest the print class functions so
I included real printf() support in the glcd library.
It is documented - see the included HTML documentation
(click on glcd/doc/GLCDref.htm in the library)
It is part of the gText class.

So you can do things like:

GLCD.Printf("hello world\n");

It is just that simple.

The  glcd library also supports user definable scrollable text areas so text
will automatically wrap and scoll. The direction of scrolling is
configurable as well.

If you create custom text areas, you can then have portions of
the display scroll independently while other areas of the display remain
untouched.

So you could say create a text area that is the bottom half of the display
and then output text to it while the top half remains in place.
You will see this behavior in the demo sketch.
What isn't in the demo sketch is the use of the Printf() function.
But to use it all you have to do is use Printf instead of print.

So when you see things like
GLCD.print("blah") or textArea.print("blah") you can also use
GLCD.Printf("blah") or textArea.Printf("blah")

It calls the standard library printf functions so all formatting types
are supported.
That said, by default floating point is not supported.
(This is documented in the glcd documentation)
You have to link against another library to get floating point support
and the IDE does not allow you to set linker options so you are kind
of SOA on floating support in the library.

But you can do format widths so things like printing a 4 digit number
that is filled with zeros, i.e like a counter, is as easy as:

GLCD.Printf("%04d", number);

I will say that if you are concerned about code space,
the printf support is 1.8k and if you use the printf support
try to avoid using the print class formatting routines so you
can save the space of those routines.
i.e. pick one method of formatting and stick with it.

I'm about to release an update to the glcd library and will be including an
example sketch that uses the printf functionality as well as some other
updates.

But yes printf support is built into the glcd v3 beta that has been
available for about 6 months now.

--- bill


Logged

Dallas, TX USA
Offline Offline
Faraday Member
**
Karma: 67
Posts: 2702
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

One other note about the use of printf
While it all works, you have to be careful with using up too much RAM space.
The issue is that when declaring a string like a printf formatting string,
the data for that string is stored in the data section.
All data section members end up not only in FLASH but also in RAM.
So each printf formatting string will consume RAM.

There are some interesting work arounds for transparently moving the
strings off to flash, but they are not very compatible with C++

As I update the library, I'll probably add in some "magic" to make it easier
to avoid the unneeded use of RAM.

But for now, in the glcd v3 beta just be aware that if you use the
Printf function, each formatting string will consume RAM.
If that is an issue, you can use Printf_P instead, but then you have to declare your string
as a string that lands in flash. While doable, it is a bit clumsy.

--- bill
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Wow, I was not expecting so much help and support from everybody out there! Just wanted to give a big thanks to everyone for the helf as I began my first Arduino project. 

I have succesfully hooked up a graphical LCD and #included the ks0108 library. I have also figured out how to print text and clear the screen and use several of the functoins in the library which has been a big help.

As far as space goes, a simple program is already using 7k of the total 32k availiable, so space is definately going to be an issue.  I suppose I should start a new thread for this, but I wil need to figure out how to do data logging on non volitile storage such as an SD card..

Thanks again for al of the help, much appreciated!
Logged

Pages: [1]   Go Up
Jump to: