Library for interfacing to Nokia 5110 LCD display with Image Loader (as found on the SparkFun website).
Diff: N5110.h
- Revision:
- 24:342bdb6679a1
- Parent:
- 23:eb7e6632fc9e
- Child:
- 25:444e55e2e419
--- a/N5110.h Wed Feb 01 16:26:22 2017 +0000 +++ b/N5110.h Tue Feb 07 10:21:43 2017 +0000 @@ -2,6 +2,143 @@ @file N5110.h @brief Header file containing member functions and variables +@brief Library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed. +@brief 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). +@brief Can print characters and strings to the display using the included 5x7 font. +@brief The library also implements a screen buffer so that individual pixels on the display (84 x 48) can be set, cleared and read. +@brief The library can print primitive shapes (lines, circles, rectangles) +@brief Acknowledgements to Chris Yan's Nokia_5110 Library. + +@brief Revision 1.3 + +@author Craig A. Evans +@date 7th February 2017 + +@code + +#include "mbed.h" +#include "N5110.h" + +// VCC,SCE,RST,D/C,MOSI,SCLK,LED +//N5110 lcd(p7,p8,p9,p10,p11,p13,p21); // LPC1768 - pwr from GPIO +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(); // 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); + + // 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); + + printf("2,2 Pixel value = %i\n",pixel_to_test); + + 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); + + printf("3,3 Pixel value = %i\n",pixel_to_test); + + 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,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(); // 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,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(); // refresh after drawing shapes + wait(5.0); + + } +} + +@endcode */ @@ -39,125 +176,6 @@ #include "mbed.h" -/** -@brief Library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed. -@brief 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). -@brief Can print characters and strings to the display using the included 5x7 font. -@brief The library also implements a screen buffer so that individual pixels on the display (84 x 48) can be set, cleared and read. -@brief The library can print primitive shapes (lines, circles, rectangles) -@brief Acknowledgements to Chris Yan's Nokia_5110 Library. - -@brief Revision 1.2 - -@author Craig A. Evans -@date 17th March 2015 -* -* Example: -* @code - -#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(); - -} -} - - -* @endcode -*/ class N5110 { @@ -201,9 +219,9 @@ */ void turnOff(); -/** Clears +/** Clear * -* Clears the screen. +* Clears the screen buffer. */ void clear(); @@ -226,7 +244,7 @@ /** Print String * -* Prints a string of characters to the display. String is cut-off after the 83rd pixel. +* Prints a string of characters to the screen buffer. String is cut-off after the 83rd pixel. * @param x - the column number (0 to 83) * @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row */ @@ -234,7 +252,7 @@ /** Print Character * -* Sends a character to the display. Printed at the specified location. Character is cut-off after the 83rd pixel. +* Sends a character to the screen buffer. Printed at the specified location. Character is cut-off after the 83rd pixel. * @param c - the character to print. Can print ASCII as so printChar('C'). * @param x - the column number (0 to 83) * @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row @@ -243,8 +261,7 @@ /** 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. +* This function sets a pixel in the screen buffer. * @param x - the x co-ordinate of the pixel (0 to 83) * @param y - the y co-ordinate of the pixel (0 to 47) */ @@ -252,8 +269,7 @@ /** 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. +* This function clears pixel in the screen buffer * @param x - the x co-ordinate of the pixel (0 to 83) * @param y - the y co-ordinate of the pixel (0 to 47) */ @@ -261,18 +277,18 @@ /** Get a Pixel * -* This function gets the status of a pixel in the display. +* This function gets the status of a pixel in the screen buffer. * @param x - the x co-ordinate of the pixel (0 to 83) * @param y - the y co-ordinate of the pixel (0 to 47) * @returns * 0 - pixel is clear -* non-zero - pixel is set +* 1 - pixel is set */ int getPixel(int x, int y); /** Refresh display * -* This functions refreshes the display to reflect the current data in the buffer. +* This functions sends the screen buffer to the display. */ void refresh(); @@ -287,14 +303,14 @@ /** Plot Array * -* This function plots a one-dimensional array on the display. +* This function plots a one-dimensional array in the buffer. * @param array[] - y values of the plot. Values should be normalised in the range 0.0 to 1.0. First 84 plotted. */ void plotArray(float array[]); /** Draw Circle * -* This function draws a circle at the specified origin with specified radius to the display. +* This function draws a circle at the specified origin with specified radius in the screen buffer * Uses the midpoint circle algorithm. * @see http://en.wikipedia.org/wiki/Midpoint_circle_algorithm * @param x0 - x-coordinate of centre @@ -326,22 +342,9 @@ */ void drawRect(int x0,int y0,int width,int height,int fill); - private: -void setXYAddress(int x, int y); -void initSPI(); -void turnOn(); -void reset(); -void clearRAM(); -void clearBuffer(); -void sendCommand(unsigned char command); -void sendData(unsigned char data); - -public: - -private: // private variables -unsigned char buffer[84][6]; // screen buffer - the 6 is for the banks - each one is 8 bits; +// objects SPI* spi; PwmOut* led; DigitalOut* pwr; @@ -349,6 +352,18 @@ DigitalOut* rst; DigitalOut* dc; +// methods +void setXYAddress(int x, int y); +void initSPI(); +void turnOn(); +void reset(); +void clearRAM(); +void sendCommand(unsigned char command); +void sendData(unsigned char data); + +// variables +unsigned char buffer[84][6]; // screen buffer - the 6 is for the banks - each one is 8 bits; + }; const unsigned char font5x7[480] = {