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/

C12832_lcd.h

Committer:
sam_grove
Date:
2013-10-27
Revision:
10:8f86576007d6
Parent:
7:0f5a3b0f3cab

File content as of revision 10:8f86576007d6:

/* mbed library for the mbed Lab Board  128*32 pixel LCD
 * use C12832 controller
 * Copyright (c) 2012 Peter Drescher - DC2PD
 * Released under the MIT License: http://mbed.org/license/mit
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#ifndef C12832_H
#define C12832_H

#include "mbed.h"
#include "GraphicsDisplay.h"


/** optional Defines :
  * #define debug_lcd  1  enable infos to PC_USB
  */

// some defines for the DMA use
#define DMA_CHANNEL_ENABLE      1
#define DMA_TRANSFER_TYPE_M2P   (1UL << 11)
#define DMA_CHANNEL_TCIE        (1UL << 31)
#define DMA_CHANNEL_SRC_INC     (1UL << 26)
#define DMA_MASK_IE             (1UL << 14)
#define DMA_MASK_ITC            (1UL << 15)
#define DMA_SSP1_TX             (1UL << 2)
#define DMA_SSP0_TX             (0)
#define DMA_DEST_SSP1_TX        (2UL << 6)
#define DMA_DEST_SSP0_TX        (0UL << 6)

/** Draw mode
  * NORMAl
  * XOR set pixel by xor the screen
  */
enum {NORMAL,XOR};

/** Bitmap
 */
struct Bitmap{
    int xSize;
    int ySize;
    int Byte_in_Line;
    char* data;
    };

class C12832_LCD : public GraphicsDisplay
{
public:
    /** Create a C12832_LCD object connected to SPI1
      *
      */

    C12832_LCD(const char* name = "LCD");

    /** Get the width of the screen in pixel
      *
      * @param
      * @returns width of screen in pixel
      *
      */
    virtual int width();

    /** Get the height of the screen in pixel
     *
     * @returns height of screen in pixel
     *
     */
    virtual int height();

    /** Draw a pixel at x,y black or white
     *
     * @param x horizontal position
     * @param y vertical position
     * @param colour ,1 set pixel ,0 erase pixel
     */
    virtual void pixel(int x, int y,int colour);

    /** draw a circle
      *
      * @param x0,y0 center
      * @param r radius
      * @param colour ,1 set pixel ,0 erase pixel
      *
      */
    void circle(int x, int y, int r, int colour);

    /** draw a filled circle
     *
     * @param x0,y0 center
     * @param r radius
     * @param color ,1 set pixel ,0 erase pixel
     *
     * use circle with different radius,
     * can miss some pixel
     */
    void fillcircle(int x, int y, int r, int colour);

    /** draw a 1 pixel line
      *
      * @param x0,y0 start point
      * @param x1,y1 stop point
      * @param color ,1 set pixel ,0 erase pixel
      *
      */
    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
      *
      */

    void copy_to_lcd(void);

    /** set the orienation of the screen
      *
      */

    //void set_orientation(unsigned int o);

    /** set the contrast of the screen
      *
      * @param o contrast 0-63
      */

    void set_contrast(unsigned int o);

    /** 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);

    virtual int columns(void);

    /** calculate the max number of columns
     *
     * @returns max column
     * depends on actual font size
     *
     */
    virtual int rows(void);

    /** put a char on the screen
     *
     * @param value char to print
     * @returns printed char
     *
     */
    virtual 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
     */
    virtual void locate(int x, int y);
    
    /** setup auto update of screen 
      *
      * @param up 1 = on , 0 = off
      * if switched off the program has to call copy_to_lcd() 
      * to update screen from framebuffer
      */
    void set_auto_up(unsigned int up);

    /** get status of the auto update function
      *
      *  @returns if auto update is on
      */
    unsigned int get_auto_up(void);

    /** Vars     */
    SPI _spi;
    DigitalOut _reset;
    DigitalOut _A0;
    DigitalOut _CS;
    unsigned char* font;
    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);
    
    /** print bitmap to buffer
      *
      * @param bm Bitmap in flash
      * @param x  x start
      * @param y  y start 
      *
      */

    void print_bm(Bitmap bm, int x, int y);

protected:

    /** draw a horizontal line
      *
      * @param x0 horizontal start
      * @param x1 horizontal stop
      * @param y vertical position
      * @param ,1 set pixel ,0 erase pixel
      *
      */
    void hline(int x0, int x1, int y, int colour);

    /** draw a vertical line
     *
     * @param x horizontal position
     * @param y0 vertical start
     * @param y1 vertical stop
     * @param ,1 set pixel ,0 erase pixel
     */
    void vline(int y0, int y1, int x, int colour);

    /** Init the C12832 LCD controller
     *
     */
    void lcd_reset();

    /** Write data to the LCD controller
     *
     * @param dat data written to LCD controller
     *
     */
    void wr_dat(unsigned char value);

    /** Write a command the LCD controller
      *
      * @param cmd: command to be written
      *
      */
    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;
    unsigned int auto_up;

};




#endif