Lib for the LCD display on mbed lab Board

Dependents:   SprintUSBModemWebsocketTest-LCD-RO iOSAppChat Christmas-LCD led_dimm ... more

Basic information

The LCD on the mbed lab board has 128 x 32 pixels and is connected via spi. It use a ST7565R controller. The spi connection is fast, but it has one drawback - you can't read the display buffer. This is a problem, because each bit reflect a pixel. If you want to set only one bit / pixel, you have to know the status of the other seven bits / pixel. Because of this we have to use a framebuffer (128 * 32 / 8 = 512 Byte). All drawing functions are working on this framebuffer. If you use the LPC1768 based mbed, the dma channel 0 is used to speed up the transfer to the lcd. This information is only relevant if you also want to use the dma controller. You have to switch to a other channel.

There are two update mode. After startup the automode is turned on. This means that the display is automaticly updated after the drawing. For example - if you call the function

lcd.line(x0, y0, x1, y1, 1);

a line from x0,y0 to x1,y1 is drawn inside the framebuffer and after that the framebuffer is copied to the lcd. If you draw more lines, it will be faster to draw all graphics inside the framebuffer and update the lcd only once. To do so, you can use the function :

lcd.set_auto_up(0);

This switch off the autoupdate. If you want to see it, you have to refresh the lcd by calling the function :

lcd.copy_to_lcd();

lcd.set_auto_up(1);

will switch back to auto update mode.

Basic functions

To use the lcd we have to create a lcd object :

C12832_LCD lcd;

There are two drawing modes : NORMAL and XOR. At startup the mode is NORMAL. If you use

lcd.setmode(XOR);

you switch to XOR mode. In this mode a pixel in the frambuffer is inverted if you set it to 1.

lcd.setmode(NORMAL);

switch back to normal mode.

The function :

lcd.invert(1);

will invert the lcd. This is done by the lcd controller. The framebuffer is not changed.

lcd.invert(0);

will switch back.

The function :

lcd.cls();

clear the screen.

The function :

lcd.set_contrast(25);

will set the contrast. The lib start with 23. A value between 10 and 35 will be visible.

Text

To print text you simply have to use the printf function. The output of the stdout is redirected to the lcd.

lcd.printf("temperature : %3.2f F",heat);

The position can be set up with the function :

lcd.locate(x,y);

At startup a 7 pixel font is used. If you want to use a different font you can include the lib http://mbed.org/users/dreschpe/code/LCD_fonts. This lib include four additional fonts. From 6 pixel to 23 pixel. To switch the font use :

lcd.set_font((unsigned char*) Arial_9);

The names of the fonts are : Small_6, Small_7, Arial_9, Arial12x12 and Arial24x23.

The function :

lcd._putc(c);

print the char c on the actual cursor position.

The function :

lcd.character(x, y, c);

print the char c at position x,y.

Graphic

The function :

lcd.line(x0, y0, x1, y1, color);

draw a single pixel line from x0,y0 to x1,y1. If color is 1 : black, 0 : white.

The function :

lcd.rect(x0, y0, x1, y1, color);

draw a single pixel rectangle from x0, y0 to x1, y1. If color is 1 : black, 0 : white.

The function :

lcd.fillrect(x0, y0, x1, y1, color);

draw a filled rectangle from x0, y0 to x1, y1. If color is 1 : black, 0 : white.

The function :

lcd.circle(x, y, r, color);

draw a circle with x,y center and radius r. If color is 1 : black, 0 : white.

The function :

lcd.fillcircle(x, y, r, color);

draw a filled circle with x,y center and radius r. If color is 1 : black, 0 : white.

The function :

lcd.pixel(x, y, color);

set a single pixel at x,y. If color is 1 : black, 0 : white. This function is not updating the lcd ! Even if the autoupdate is on. You have to call lcd.copy_to_lcd() after using this function - or to use a other function with autoupdate afterwards.

mbed rtos

To use the mbed rtos with the lib we have to make the lib thread save. What is the problem ? If different threads are writing to the lcd it can end in troubble. Thread1 is using the pintf("hello mbed") function to print this string to the actual position. After the chars "hel" are printed ,the scheduler is switching to thread2. This thread is writing at a different position on the screen. After that the scheduler is switch back to thread1 and the print function continue. Thread1 did not know that the internal cursor position has changed ....

To protect the access to the lcd we use a Mutex. If a thread has the mutex and a other thread also want it, the second thread has to wait.

Mutex lcd_mutex;  // define the mutex
    //...
lcd_mutex.lock(); // get the mutex or wait

//acccess to the lcd
 
lcd_mutex.unlock(); // free the mutex 

We use this framing to access the lcd.

Test program to show : http://mbed.org/users/dreschpe/code/lab1/

Committer:
sam_grove
Date:
Sun Oct 27 23:16:07 2013 +0000
Revision:
10:8f86576007d6
Parent:
0:4bbc531be6e2
Don't claim stdout by default. Make the user of the library do that.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dreschpe 0:4bbc531be6e2 1 /* mbed TextDisplay Library Base Class
dreschpe 0:4bbc531be6e2 2 * Copyright (c) 2007-2009 sford
dreschpe 0:4bbc531be6e2 3 * Released under the MIT License: http://mbed.org/license/mit
dreschpe 0:4bbc531be6e2 4 *
dreschpe 0:4bbc531be6e2 5 * A common base class for Text displays
dreschpe 0:4bbc531be6e2 6 * To port a new display, derive from this class and implement
dreschpe 0:4bbc531be6e2 7 * the constructor (setup the display), character (put a character
dreschpe 0:4bbc531be6e2 8 * at a location), rows and columns (number of rows/cols) functions.
dreschpe 0:4bbc531be6e2 9 * Everything else (locate, printf, putc, cls) will come for free
dreschpe 0:4bbc531be6e2 10 *
dreschpe 0:4bbc531be6e2 11 * The model is the display will wrap at the right and bottom, so you can
dreschpe 0:4bbc531be6e2 12 * keep writing and will always get valid characters. The location is
dreschpe 0:4bbc531be6e2 13 * maintained internally to the class to make this easy
dreschpe 0:4bbc531be6e2 14 */
dreschpe 0:4bbc531be6e2 15
dreschpe 0:4bbc531be6e2 16 #ifndef MBED_TEXTDISPLAY_H
dreschpe 0:4bbc531be6e2 17 #define MBED_TEXTDISPLAY_H
dreschpe 0:4bbc531be6e2 18
dreschpe 0:4bbc531be6e2 19 #include "mbed.h"
dreschpe 0:4bbc531be6e2 20
dreschpe 0:4bbc531be6e2 21 class TextDisplay : public Stream {
dreschpe 0:4bbc531be6e2 22 public:
dreschpe 0:4bbc531be6e2 23
dreschpe 0:4bbc531be6e2 24 // functions needing implementation in derived implementation class
dreschpe 0:4bbc531be6e2 25 /** Create a TextDisplay interface
dreschpe 0:4bbc531be6e2 26 *
dreschpe 0:4bbc531be6e2 27 * @param name The name used in the path to access the strean through the filesystem
dreschpe 0:4bbc531be6e2 28 */
dreschpe 0:4bbc531be6e2 29 TextDisplay(const char *name = NULL);
dreschpe 0:4bbc531be6e2 30
dreschpe 0:4bbc531be6e2 31 /** output a character at the given position
dreschpe 0:4bbc531be6e2 32 *
dreschpe 0:4bbc531be6e2 33 * @param column column where charater must be written
dreschpe 0:4bbc531be6e2 34 * @param row where character must be written
dreschpe 0:4bbc531be6e2 35 * @param c the character to be written to the TextDisplay
dreschpe 0:4bbc531be6e2 36 */
dreschpe 0:4bbc531be6e2 37 virtual void character(int column, int row, int c) = 0;
dreschpe 0:4bbc531be6e2 38
dreschpe 0:4bbc531be6e2 39 /** return number if rows on TextDisplay
dreschpe 0:4bbc531be6e2 40 * @result number of rows
dreschpe 0:4bbc531be6e2 41 */
dreschpe 0:4bbc531be6e2 42 virtual int rows() = 0;
dreschpe 0:4bbc531be6e2 43
dreschpe 0:4bbc531be6e2 44 /** return number if columns on TextDisplay
dreschpe 0:4bbc531be6e2 45 * @result number of rows
dreschpe 0:4bbc531be6e2 46 */
dreschpe 0:4bbc531be6e2 47 virtual int columns() = 0;
dreschpe 0:4bbc531be6e2 48
dreschpe 0:4bbc531be6e2 49 // functions that come for free, but can be overwritten
dreschpe 0:4bbc531be6e2 50
dreschpe 0:4bbc531be6e2 51 /** redirect output from a stream (stoud, sterr) to display
dreschpe 0:4bbc531be6e2 52 * @param stream stream that shall be redirected to the TextDisplay
dreschpe 0:4bbc531be6e2 53 */
dreschpe 0:4bbc531be6e2 54 virtual bool claim (FILE *stream);
dreschpe 0:4bbc531be6e2 55
dreschpe 0:4bbc531be6e2 56 /** clear screen
dreschpe 0:4bbc531be6e2 57 */
dreschpe 0:4bbc531be6e2 58 virtual void cls();
dreschpe 0:4bbc531be6e2 59 virtual void locate(int column, int row);
dreschpe 0:4bbc531be6e2 60 virtual void foreground(uint16_t colour);
dreschpe 0:4bbc531be6e2 61 virtual void background(uint16_t colour);
dreschpe 0:4bbc531be6e2 62 // putc (from Stream)
dreschpe 0:4bbc531be6e2 63 // printf (from Stream)
dreschpe 0:4bbc531be6e2 64
dreschpe 0:4bbc531be6e2 65 protected:
dreschpe 0:4bbc531be6e2 66
dreschpe 0:4bbc531be6e2 67 virtual int _putc(int value);
dreschpe 0:4bbc531be6e2 68 virtual int _getc();
dreschpe 0:4bbc531be6e2 69
dreschpe 0:4bbc531be6e2 70 // character location
dreschpe 0:4bbc531be6e2 71 uint16_t _column;
dreschpe 0:4bbc531be6e2 72 uint16_t _row;
dreschpe 0:4bbc531be6e2 73
dreschpe 0:4bbc531be6e2 74 // colours
dreschpe 0:4bbc531be6e2 75 uint16_t _foreground;
dreschpe 0:4bbc531be6e2 76 uint16_t _background;
dreschpe 0:4bbc531be6e2 77 char *_path;
dreschpe 0:4bbc531be6e2 78 };
dreschpe 0:4bbc531be6e2 79
dreschpe 0:4bbc531be6e2 80 #endif