Updated for mbed: Simple library for interfacing to Nokia 5110 LCD display (as found on the SparkFun website).

Fork of N5110 by Craig Evans

Embed: (wiki syntax)

« Back to documentation index

N5110 Class Reference

N5110 Class Reference

Library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed. More...

#include <N5110.h>

Public Member Functions

 N5110 (PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin)
 Create a N5110 object connected to the specified pins.
void init ()
 Initialise display.
void turnOff ()
 Turn off.
void clear ()
 Clears.
void normalMode ()
 Turn on normal video mode (default) Black on white.
void inverseMode ()
 Turn on inverse video mode (default) White on black.
void setBrightness (float brightness)
 Set Brightness.
void printString (const char *str, int x, int y)
 Print String.
void printChar (char c, int x, int y)
 Print Character.
void setPixel (int x, int y)
 Set a Pixel.
void clearPixel (int x, int y)
 Clear a Pixel.
int getPixel (int x, int y)
 Get a Pixel.
void refresh ()
 Refresh display.
void randomiseBuffer ()
 Randomise buffer.
void plotArray (float array[])
 Plot Array.
void drawCircle (int x0, int y0, int radius, int fill)
 Draw Circle.
void drawLine (int x0, int y0, int x1, int y1, int type)
 Draw Line.
void drawRect (int x0, int y0, int width, int height, int fill)
 Draw Rectangle.

Detailed Description

Library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed.

The display is powered from a GPIO pin meaning it can be controlled via software. The LED backlight is also software-controllable (via PWM pin). Can print characters and strings to the display using the included 5x7 font. The library also implements a screen buffer so that individual pixels on the display (84 x 48) can be set, cleared and read. The library can print primitive shapes (lines, circles, rectangles) Acknowledgements to Chris Yan's Nokia_5110 Library.

Revision 1.2

Author:
Craig A. Evans
Date:
17th March 2015
Author:
updated Jeremy Ogus
Date:
10/05/2015

Example:

#include "mbed.h"
#include "N5110.h"

//    VCC,SCE,RST,D/C,MOSI,SCLK,LED
N5110 lcd(p7,p8,p9,p10,p11,p13,p21);
// Can also power (VCC) directly from VOUT (3.3 V) -
// Can give better performance due to current limitation from GPIO pin

int main()
{
    // first need to initialise display
    lcd.init();

    while(1) {

        // these are default settings so not strictly needed
        lcd.normalMode();      // normal colour mode
        lcd.setBrightness(0.5); // put LED backlight on 50%

        // can directly print strings at specified co-ordinates
        lcd.printString("Hello, World!",0,0);

        char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
        // so can display a string of a maximum 14 characters in length
        // or create formatted strings - ensure they aren't more than 14 characters long
        int temperature = 27;
        int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
        // it is important the format specifier ensures the length will fit in the buffer
        if (length <= 14)  // if string will fit on display
            lcd.printString(buffer,0,1);           // display on screen

        float pressure = 1012.3;  // same idea with floats
        length = sprintf(buffer,"P = %.2f mb",pressure);
        if (length <= 14)
            lcd.printString(buffer,0,2);

        // can also print individual characters at specified place
        lcd.printChar('X',5,3);

        // draw a line across the display at y = 40 pixels (origin top-left)
        for (int i = 0; i < WIDTH; i++) {
            lcd.setPixel(i,40);
        }
        // need to refresh display after setting pixels
        lcd.refresh();

        // can also check status of pixels using getPixel(x,y)

        wait(5.0);
        lcd.clear();            // clear display
        lcd.inverseMode();      // invert colours
        lcd.setBrightness(1.0); // put LED backlight on full

        float array[84];

        for (int i = 0; i < 84; i++) {
            array[i] = 0.5 + 0.5*sin(i*2*3.14/84);
        }

        // can also plot graphs - 84 elements only
        // values must be in range 0.0 - 1.0
        lcd.plotArray(array);
        wait(5.0);
        lcd.clear();
        lcd.normalMode();      // normal colour mode back
        lcd.setBrightness(0.5); // put LED backlight on 50%

        // example of drawing lines
        for (int x = 0; x < WIDTH ; x+=10) {
            // x0,y0,x1,y1,type 0-white,1-black,2-dotted
            lcd.drawLine(0,0,x,HEIGHT,2);
        }
        lcd.refresh();   // need to refresh screen after drawing lines

        wait(5.0);
        lcd.clear();

        // example of how to draw circles
        lcd.drawCircle(WIDTH/2,HEIGHT/2,20,1);  // x,y,radius,black fill
        lcd.drawCircle(WIDTH/2,HEIGHT/2,10,2);  // x,y,radius,white fill
        lcd.drawCircle(WIDTH/2,HEIGHT/2,30,0);  // x,y,radius,transparent with outline
        lcd.refresh();   // need to refresh screen after drawing circles

        wait(5.0);
        lcd.clear();

        // example of how to draw rectangles
        //          origin x,y,width,height,type
        lcd.drawRect(10,10,50,30,1);  // filled black rectangle
        lcd.drawRect(15,15,20,10,2);  // filled white rectange (no outline)
        lcd.drawRect(2,2,70,40,0);    // transparent, just outline
        lcd.refresh();   // need to refresh screen after drawing rects


        wait(5.0);
        lcd.clear();

    }
}

Definition at line 163 of file N5110.h.


Constructor & Destructor Documentation

N5110 ( PinName  pwrPin,
PinName  scePin,
PinName  rstPin,
PinName  dcPin,
PinName  mosiPin,
PinName  sclkPin,
PinName  ledPin 
)

Create a N5110 object connected to the specified pins.

@ pwr Pin connected to Vcc on the LCD display (pin 1)

Parameters:
scePin connected to chip enable (pin 3)
rstPin connected to reset (pin 4)
dcPin connected to data/command select (pin 5)
mosiPin connected to data input (MOSI) (pin 6)
sclkPin connected to serial clock (SCLK) (pin 7)
ledPin connected to LED backlight (must be PWM) (pin 8)

Definition at line 11 of file N5110.cpp.


Member Function Documentation

void clear (  )

Clears.

Clears the screen.

Definition at line 267 of file N5110.cpp.

void clearPixel ( int  x,
int  y 
)

Clear a Pixel.

This function clears pixel in the display. A call to refresh() must be made to update the display to reflect the change in pixels.

Parameters:
x- the x co-ordinate of the pixel (0 to 83)
y- the y co-ordinate of the pixel (0 to 47)

Definition at line 169 of file N5110.cpp.

void drawCircle ( int  x0,
int  y0,
int  radius,
int  fill 
)

Draw Circle.

This function draws a circle at the specified origin with specified radius to the display. Uses the midpoint circle algorithm.

See also:
http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
Parameters:
x0- x-coordinate of centre
y0- y-coordinate of centre
radius- radius of circle in pixels
fill- 0 transparent (w/outline), 1 filled black, 2 filled white (wo/outline)

Definition at line 302 of file N5110.cpp.

void drawLine ( int  x0,
int  y0,
int  x1,
int  y1,
int  type 
)

Draw Line.

This function draws a line between the specified points using linear interpolation.

Parameters:
x0- x-coordinate of first point
y0- y-coordinate of first point
x1- x-coordinate of last point
y1- y-coordinate of last point
type- 0 white,1 black,2 dotted

Definition at line 343 of file N5110.cpp.

void drawRect ( int  x0,
int  y0,
int  width,
int  height,
int  fill 
)

Draw Rectangle.

This function draws a rectangle.

Parameters:
x0- x-coordinate of origin (top-left)
y0- y-coordinate of origin (top-left)
width- width of rectangle
height- height of rectangle
fill- 0 transparent (w/outline), 1 filled black, 2 filled white (wo/outline)

Definition at line 391 of file N5110.cpp.

int getPixel ( int  x,
int  y 
)

Get a Pixel.

This function gets the status of a pixel in the display.

Parameters:
x- the x co-ordinate of the pixel (0 to 83)
y- the y co-ordinate of the pixel (0 to 47)
Returns:
0 - pixel is clear non-zero - pixel is set

Definition at line 177 of file N5110.cpp.

void init (  )

Initialise display.

Powers up the display and turns on backlight (50% brightness default). Sets the display up in horizontal addressing mode and with normal video mode.

Definition at line 27 of file N5110.cpp.

void inverseMode (  )

Turn on inverse video mode (default) White on black.

Definition at line 59 of file N5110.cpp.

void normalMode (  )

Turn on normal video mode (default) Black on white.

Definition at line 52 of file N5110.cpp.

void plotArray ( float  array[] )

Plot Array.

This function plots a one-dimensional array on the display.

Parameters:
array[]- y values of the plot. Values should be normalised in the range 0.0 to 1.0. First 84 plotted.

Definition at line 285 of file N5110.cpp.

void printChar ( char  c,
int  x,
int  y 
)

Print Character.

Sends a character to the display. Printed at the specified location. Character is cut-off after the 83rd pixel.

Parameters:
c- the character to print. Can print ASCII as so printChar('C').
x- the column number (0 to 83)
y- the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row

Definition at line 222 of file N5110.cpp.

void printString ( const char *  str,
int  x,
int  y 
)

Print String.

Prints a string of characters to the display. String is cut-off after the 83rd pixel.

Parameters:
x- the column number (0 to 83)
y- the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row

Definition at line 239 of file N5110.cpp.

void randomiseBuffer (  )

Randomise buffer.

This function fills the buffer with random data. Can be used to test the display. A call to refresh() must be made to update the display to reflect the change in pixels. The seed is not set and so the generated pattern will probably be the same each time. TODO: Randomise the seed - maybe using the noise on the AnalogIn pins.

Definition at line 210 of file N5110.cpp.

void refresh (  )

Refresh display.

This functions refreshes the display to reflect the current data in the buffer.

Definition at line 189 of file N5110.cpp.

void setBrightness ( float  brightness )

Set Brightness.

Sets brightness of LED backlight.

Parameters:
brightness- float in range 0.0 to 1.0

Definition at line 90 of file N5110.cpp.

void setPixel ( int  x,
int  y 
)

Set a Pixel.

This function sets a pixel in the display. A call to refresh() must be made to update the display to reflect the change in pixels.

Parameters:
x- the x co-ordinate of the pixel (0 to 83)
y- the y co-ordinate of the pixel (0 to 47)

Definition at line 161 of file N5110.cpp.

void turnOff (  )

Turn off.

Powers down the display and turns of the backlight. Needs to be reinitialised before being re-used.

Definition at line 73 of file N5110.cpp.