Class used to interface with the Nokia N5110 LCD.

Fork of N5110 by Craig Evans

Embed: (wiki syntax)

« Back to documentation index

N5110 Class Reference

N5110 Class Reference

N5110 Class. More...

#include <N5110.h>

Public Member Functions

 N5110 (PinName const pwrPin, PinName const scePin, PinName const rstPin, PinName const dcPin, PinName const mosiPin, PinName const sclkPin, PinName const ledPin)
 Create a N5110 object connected to the specified pins.
 N5110 (PinName const scePin, PinName const rstPin, PinName const dcPin, PinName const mosiPin, PinName const sclkPin, PinName const ledPin)
 Create a N5110 object connected to the specified pins (Vcc to +3V3)
 ~N5110 ()
 Free allocated memory when object goes out of scope.
void init ()
 Initialise display.
void turnOff ()
 Turn off.
void clear ()
 Clear.
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 const brightness)
 Set Brightness.
void printString (char const *str, unsigned int const x, unsigned int const y)
 Print String.
void printChar (char const c, unsigned int const x, unsigned int const y)
 Print Character.
void setPixel (unsigned int const x, unsigned int const y, bool const state=true)
 Set a Pixel.
void clearPixel (unsigned int const x, unsigned int const y)
 Clear a Pixel.
int getPixel (unsigned int const x, unsigned int const y) const
 Get a Pixel.
void refresh ()
 Refresh display.
void randomiseBuffer ()
 Randomise buffer.
void plotArray (float const array[])
 Plot Array.
void drawCircle (unsigned int const x0, unsigned int const y0, unsigned int const radius, FillType const fill)
 Draw Circle.
void drawLine (unsigned int const x0, unsigned int const y0, unsigned int const x1, unsigned int const y1, unsigned int const type)
 Draw Line.
void drawRect (unsigned int const x0, unsigned int const y0, unsigned int const width, unsigned int const height, FillType const fill)
 Draw Rectangle.

Detailed Description

N5110 Class.

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.3

Author:
Craig A. Evans
Date:
7th February 2017
#include "mbed.h"
#include "N5110.h"

//      rows,cols
int sprite[8][5] =   {
    { 0,0,1,0,0 },
    { 0,1,1,1,0 },
    { 0,0,1,0,0 },
    { 0,1,1,1,0 },
    { 1,1,1,1,1 },
    { 1,1,1,1,1 },
    { 1,1,0,1,1 },
    { 1,1,0,1,1 },
};

//    VCC,SCE,RST,D/C,MOSI,SCLK,LED
//N5110 lcd(p7,p8,p9,p10,p11,p13,p21);  // LPC1768 - pwr from GPIO
N5110 lcd(p8,p9,p10,p11,p13,p21);  // LPC1768 - powered from +3V3 - JP1 in 2/3 position
//N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11);  // K64F - pwr from 3V3

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%

        lcd.clear();
        // x origin, y origin, rows, cols, sprite
        lcd.drawSprite(20,6,8,5,(int *)sprite);
        lcd.refresh();
        wait(5.0);

        lcd.clear(); // clear buffer at start of every loop
        // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display)
        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 (assuming printing at x=0)
            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 or writing strings
        lcd.refresh();
        wait(5.0);

        for(int i = 0; i < 3; i++) {
            for(int j = 0; j < 5; j++) {
                printf("%d,%d = %d\n",i,j,glyph[i][j]);
            }
        }



        // can check status of pixel using getPixel(x,y);
        lcd.clear();  // clear buffer
        lcd.setPixel(2,2);  // set random pixel in buffer
        lcd.refresh();
        wait(1.0);

        int pixel_to_test = lcd.getPixel(2,2);

        if ( pixel_to_test ) {
            lcd.printString("2,2 is set",0,4);
        }

        // this one shouldn't be set
        pixel_to_test = lcd.getPixel(3,3);

        if ( pixel_to_test == 0 ) {
            lcd.printString("3,3 is clear",0,5);
        }

        lcd.refresh();
        wait(4.0);

        lcd.clear();            // clear buffer
        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);
        lcd.refresh();
        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();  // refresh after drawing shapes
        wait(5.0);


        lcd.clear();
        // example of how to draw circles
        lcd.drawCircle(WIDTH/2,HEIGHT/2,20,FILL_BLACK);  // x,y,radius,black fill
        lcd.drawCircle(WIDTH/2,HEIGHT/2,10,FILL_WHITE);  // x,y,radius,white fill
        lcd.drawCircle(WIDTH/2,HEIGHT/2,30,FILL_TRANSPARENT);  // x,y,radius,transparent with outline
        lcd.refresh();  // refresh after drawing shapes
        wait(5.0);

        lcd.clear();
        // example of how to draw rectangles
        //          origin x,y,width,height,type
        lcd.drawRect(10,10,50,30,FILL_BLACK);  // filled black rectangle
        lcd.drawRect(15,15,20,10,FILL_WHITE);  // filled white rectange (no outline)
        lcd.drawRect(2,2,70,40,FILL_TRANSPARENT);    // transparent, just outline
        lcd.refresh();  // refresh after drawing shapes
        wait(5.0);

    }
}

Definition at line 205 of file N5110.h.


Constructor & Destructor Documentation

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

Create a N5110 object connected to the specified pins.

Parameters:
pwrPin connected to Vcc on the LCD display (pin 1)
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 7 of file N5110.cpp.

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

Create a N5110 object connected to the specified pins (Vcc to +3V3)

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 26 of file N5110.cpp.

~N5110 (  )

Free allocated memory when object goes out of scope.

Definition at line 41 of file N5110.cpp.


Member Function Documentation

void clear (  )

Clear.

Clears the screen buffer.

Definition at line 306 of file N5110.cpp.

void clearPixel ( unsigned int const   x,
unsigned int const   y 
)

Clear a Pixel.

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

This function clears pixel in the screen buffer

Definition at line 204 of file N5110.cpp.

void drawCircle ( unsigned int const   x0,
unsigned int const   y0,
unsigned int const   radius,
FillType const   fill 
)

Draw Circle.

This function draws a circle at the specified origin with specified radius in the screen buffer 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- fill-type for the shape

Definition at line 324 of file N5110.cpp.

void drawLine ( unsigned int const   x0,
unsigned int const   y0,
unsigned int const   x1,
unsigned int const   y1,
unsigned int const   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 367 of file N5110.cpp.

void drawRect ( unsigned int const   x0,
unsigned int const   y0,
unsigned int const   width,
unsigned int const   height,
FillType const   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- fill-type for the shape

Definition at line 419 of file N5110.cpp.

int getPixel ( unsigned int const   x,
unsigned int const   y 
) const

Get a Pixel.

This function gets the status of a pixel in the screen buffer.

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 1 - pixel is set

Definition at line 213 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 56 of file N5110.cpp.

void inverseMode (  )

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

Definition at line 87 of file N5110.cpp.

void normalMode (  )

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

Definition at line 81 of file N5110.cpp.

void plotArray ( float const   array[] )

Plot Array.

This function plots a one-dimensional array in the buffer.

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 312 of file N5110.cpp.

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

Print Character.

Sends a character to the screen buffer. 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 263 of file N5110.cpp.

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

Print String.

Prints a string of characters to the screen buffer. 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 281 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 251 of file N5110.cpp.

void refresh (  )

Refresh display.

This functions sends the screen buffer to the display.

Definition at line 232 of file N5110.cpp.

void setBrightness ( float const  brightness )

Set Brightness.

Sets brightness of LED backlight.

Parameters:
brightness- float in range 0.0 to 1.0

Definition at line 124 of file N5110.cpp.

void setPixel ( unsigned int const   x,
unsigned int const   y,
bool const   state = true 
)

Set a Pixel.

Parameters:
xThe x co-ordinate of the pixel (0 to 83)
yThe y co-ordinate of the pixel (0 to 47)
stateThe state of the pixel [true=black (default), false=white]

This function sets the state of a pixel in the screen buffer. The third parameter can be omitted,

Definition at line 193 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 101 of file N5110.cpp.