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/

Revision:
1:66dd8afbfd06
Parent:
0:4bbc531be6e2
Child:
3:468cdccff7af
--- a/C12832_lcd.h	Tue Oct 16 14:05:57 2012 +0000
+++ b/C12832_lcd.h	Tue Oct 16 17:50:37 2012 +0000
@@ -19,7 +19,7 @@
 #include "GraphicsDisplay.h"
 
 
-/** optional Defines : 
+/** optional Defines :
   * #define debug_lcd  1  enable infos to PC_USB
   */
 
@@ -35,10 +35,10 @@
 #define DMA_DEST_SSP1_TX        (2UL << 6)
 #define DMA_DEST_SSP0_TX        (0UL << 6)
 
- /** Draw mode
-   * NORMAl 
-   * XOR set pixel by xor the screen
-   */  
+/** Draw mode
+  * NORMAl
+  * XOR set pixel by xor the screen
+  */
 enum {NORMAL,XOR};
 
 class C12832_LCD : public GraphicsDisplay
@@ -101,95 +101,103 @@
       *
       */
     void line(int x0, int y0, int x1, int y1, int colour);
-    
+
     /** draw a rect
-   *
-   * @param x0,y0 top left corner
-   * @param x1,y1 down right corner
-   * @param color 1 set pixel ,0 erase pixel
-   *                                                   *
-   */    
-  void rect(int x0, int y0, int x1, int y1, int colour);
-      
- /** draw a filled rect
-   *
-   * @param x0,y0 top left corner
-   * @param x1,y1 down right corner
-   * @param color 1 set pixel ,0 erase pixel
-   *
-   */    
-  void fillrect(int x0, int y0, int x1, int y1, int colour);   
-    
-    /** copy display buffer to lcd 
+    *
+    * @param x0,y0 top left corner
+    * @param x1,y1 down right corner
+    * @param color 1 set pixel ,0 erase pixel
+    *                                                   *
+    */
+    void rect(int x0, int y0, int x1, int y1, int colour);
+
+    /** draw a filled rect
+      *
+      * @param x0,y0 top left corner
+      * @param x1,y1 down right corner
+      * @param color 1 set pixel ,0 erase pixel
       *
       */
-        
+    void fillrect(int x0, int y0, int x1, int y1, int colour);
+
+    /** copy display buffer to lcd
+      *
+      */
+
     void copy_to_lcd(void);
 
     /** set the orienation of the screen
-     *
-     */
+      *
+      */
+
+    //void set_orientation(unsigned int o);
 
-    void set_orientation(unsigned int o);
-
-/** set the contrast of the screen
-     *
-     */
+    /** set the contrast of the screen
+      *
+      * @param o contrast 0-63
+      */
 
     void set_contrast(unsigned int o);
-    
-/** invert the screen 
-  *
-  * @param o = 0 normal, 1 invert
-  */     
+
+    /** read the contrast level
+      *
+      */
+    unsigned int get_contrast(void);
+
+
+
+    /** invert the screen
+      *
+      * @param o = 0 normal, 1 invert
+      */
     void invert(unsigned int o);
-    
-/** clear the screen 
-   *
-   */      
-   virtual void cls(void);
-   
-/** set the drawing mode
-  *  
-  * @param mode NORMAl or XOR
-  */
-   
-void setmode(int mode);
-   
-   int columns(void);
-    
-  /** calculate the max number of columns
-   *
-   * @returns max column
-   * depends on actual font size
-   *
-   */   
-  int rows(void);
-    
-  /** put a char on the screen
-   *
-   * @param value char to print
-   * @returns printed char
-   *
-   */
-  int _putc(int value);
-    
-  /** draw a character on given position out of the active font to the LCD
-   *
-   * @param x x-position of char (top left) 
-   * @param y y-position
-   * @param c char to print
-   *
-   */    
-  virtual void character(int x, int y, int c);
-  
-  /** setup cursor position
-   *
-   * @param x x-position (top left)
-   * @param y y-position 
-   */   
-  void locate(int x, int y);
-   
+
+    /** clear the screen
+       *
+       */
+    virtual void cls(void);
+
+    /** set the drawing mode
+      *
+      * @param mode NORMAl or XOR
+      */
+
+    void setmode(int mode);
+
+    int columns(void);
+
+    /** calculate the max number of columns
+     *
+     * @returns max column
+     * depends on actual font size
+     *
+     */
+    int rows(void);
+
+    /** put a char on the screen
+     *
+     * @param value char to print
+     * @returns printed char
+     *
+     */
+    int _putc(int value);
+
+    /** draw a character on given position out of the active font to the LCD
+     *
+     * @param x x-position of char (top left)
+     * @param y y-position
+     * @param c char to print
+     *
+     */
+    virtual void character(int x, int y, int c);
+
+    /** setup cursor position
+     *
+     * @param x x-position (top left)
+     * @param y y-position
+     */
+    void locate(int x, int y);
+
 
     /** Vars     */
     SPI _spi;
@@ -200,20 +208,20 @@
     unsigned int draw_mode;
 
 
- /** select the font to use
-   *
-   * @param f pointer to font array 
-   *                                                                              
-   *   font array can created with GLCD Font Creator from http://www.mikroe.com
-   *   you have to add 4 parameter at the beginning of the font array to use: 
-   *   - the number of byte / char
-   *   - the vertial size in pixel
-   *   - the horizontal size in pixel
-   *   - the number of byte per vertical line
-   *   you also have to change the array to char[]
-   *
-   */  
-  void set_font(unsigned char* f);
+    /** select the font to use
+      *
+      * @param f pointer to font array
+      *
+      *   font array can created with GLCD Font Creator from http://www.mikroe.com
+      *   you have to add 4 parameter at the beginning of the font array to use:
+      *   - the number of byte / char
+      *   - the vertial size in pixel
+      *   - the horizontal size in pixel
+      *   - the number of byte per vertical line
+      *   you also have to change the array to char[]
+      *
+      */
+    void set_font(unsigned char* f);
 
 
 protected:
@@ -255,13 +263,14 @@
       *
       */
     void wr_cmd(unsigned char value);
-    
+
     void wr_cnt(unsigned char cmd);
 
     unsigned int orientation;
     unsigned int char_x;
     unsigned int char_y;
     unsigned char buffer[512];
+    unsigned int contrast;
 
 };