Class used to interface with the Nokia N5110 LCD.
Fork of N5110 by
N5110.h
00001 #ifndef N5110_H 00002 #define N5110_H 00003 00004 #include "mbed.h" 00005 00006 // Command Bytes - taken from Chris Yan's library 00007 // More information can be found in the display datasheet 00008 // H = 0 - Basic instructions 00009 #define CMD_DC_CLEAR_DISPLAY 0x08 00010 #define CMD_DC_NORMAL_MODE 0x0C 00011 #define CMD_DC_FILL_DISPLAY 0x09 00012 #define CMD_DC_INVERT_VIDEO 0x0D 00013 #define CMD_FS_HORIZONTAL_MODE 0x00 00014 #define CMD_FS_VERTICAL_MODE 0x02 00015 #define CMD_FS_BASIC_MODE 0x00 00016 #define CMD_FS_EXTENDED_MODE 0x01 00017 #define CMD_FS_ACTIVE_MODE 0x00 00018 #define CMD_FS_POWER_DOWN_MODE 0x04 00019 // H = 1 - Extended instructions 00020 #define CMD_TC_TEMP_0 0x04 00021 #define CMD_TC_TEMP_1 0x05 00022 #define CMD_TC_TEMP_2 0x06 00023 #define CMD_TC_TEMP_3 0x07 00024 #define CMD_BI_MUX_24 0x15 00025 #define CMD_BI_MUX_48 0x13 00026 #define CMD_BI_MUX_100 0x10 00027 #define CMD_VOP_6V06 0xB2 00028 #define CMD_VOP_7V38 0xC8 00029 00030 // number of pixels on display 00031 #define WIDTH 84 00032 #define HEIGHT 48 00033 #define BANKS 6 00034 00035 /// Fill types for 2D shapes 00036 enum FillType { 00037 FILL_TRANSPARENT, ///< Transparent with outline 00038 FILL_BLACK, ///< Filled black 00039 FILL_WHITE, ///< Filled white (no outline) 00040 }; 00041 00042 /** N5110 Class 00043 @brief Library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed. 00044 @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). 00045 @brief Can print characters and strings to the display using the included 5x7 font. 00046 @brief The library also implements a screen buffer so that individual pixels on the display (84 x 48) can be set, cleared and read. 00047 @brief The library can print primitive shapes (lines, circles, rectangles) 00048 @brief Acknowledgements to Chris Yan's Nokia_5110 Library. 00049 00050 @brief Revision 1.3 00051 00052 @author Craig A. Evans 00053 @date 7th February 2017 00054 00055 @code 00056 00057 #include "mbed.h" 00058 #include "N5110.h" 00059 00060 // rows,cols 00061 int sprite[8][5] = { 00062 { 0,0,1,0,0 }, 00063 { 0,1,1,1,0 }, 00064 { 0,0,1,0,0 }, 00065 { 0,1,1,1,0 }, 00066 { 1,1,1,1,1 }, 00067 { 1,1,1,1,1 }, 00068 { 1,1,0,1,1 }, 00069 { 1,1,0,1,1 }, 00070 }; 00071 00072 // VCC,SCE,RST,D/C,MOSI,SCLK,LED 00073 //N5110 lcd(p7,p8,p9,p10,p11,p13,p21); // LPC1768 - pwr from GPIO 00074 N5110 lcd(p8,p9,p10,p11,p13,p21); // LPC1768 - powered from +3V3 - JP1 in 2/3 position 00075 //N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); // K64F - pwr from 3V3 00076 00077 int main() 00078 { 00079 // first need to initialise display 00080 lcd.init(); 00081 00082 while(1) { 00083 00084 // these are default settings so not strictly needed 00085 lcd.normalMode(); // normal colour mode 00086 lcd.setBrightness(0.5); // put LED backlight on 50% 00087 00088 lcd.clear(); 00089 // x origin, y origin, rows, cols, sprite 00090 lcd.drawSprite(20,6,8,5,(int *)sprite); 00091 lcd.refresh(); 00092 wait(5.0); 00093 00094 lcd.clear(); // clear buffer at start of every loop 00095 // can directly print strings at specified co-ordinates (must be less than 84 pixels to fit on display) 00096 lcd.printString("Hello, World!",0,0); 00097 00098 char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) 00099 // so can display a string of a maximum 14 characters in length 00100 // or create formatted strings - ensure they aren't more than 14 characters long 00101 int temperature = 27; 00102 int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer 00103 // it is important the format specifier ensures the length will fit in the buffer 00104 if (length <= 14) // if string will fit on display (assuming printing at x=0) 00105 lcd.printString(buffer,0,1); // display on screen 00106 00107 float pressure = 1012.3; // same idea with floats 00108 length = sprintf(buffer,"P = %.2f mb",pressure); 00109 if (length <= 14) 00110 lcd.printString(buffer,0,2); 00111 00112 // can also print individual characters at specified place 00113 lcd.printChar('X',5,3); 00114 00115 // draw a line across the display at y = 40 pixels (origin top-left) 00116 for (int i = 0; i < WIDTH; i++) { 00117 lcd.setPixel(i,40); 00118 } 00119 // need to refresh display after setting pixels or writing strings 00120 lcd.refresh(); 00121 wait(5.0); 00122 00123 for(int i = 0; i < 3; i++) { 00124 for(int j = 0; j < 5; j++) { 00125 printf("%d,%d = %d\n",i,j,glyph[i][j]); 00126 } 00127 } 00128 00129 00130 00131 // can check status of pixel using getPixel(x,y); 00132 lcd.clear(); // clear buffer 00133 lcd.setPixel(2,2); // set random pixel in buffer 00134 lcd.refresh(); 00135 wait(1.0); 00136 00137 int pixel_to_test = lcd.getPixel(2,2); 00138 00139 if ( pixel_to_test ) { 00140 lcd.printString("2,2 is set",0,4); 00141 } 00142 00143 // this one shouldn't be set 00144 pixel_to_test = lcd.getPixel(3,3); 00145 00146 if ( pixel_to_test == 0 ) { 00147 lcd.printString("3,3 is clear",0,5); 00148 } 00149 00150 lcd.refresh(); 00151 wait(4.0); 00152 00153 lcd.clear(); // clear buffer 00154 lcd.inverseMode(); // invert colours 00155 lcd.setBrightness(1.0); // put LED backlight on full 00156 00157 float array[84]; 00158 00159 for (int i = 0; i < 84; i++) { 00160 array[i] = 0.5 + 0.5*sin(i*2*3.14/84); 00161 } 00162 00163 // can also plot graphs - 84 elements only 00164 // values must be in range 0.0 - 1.0 00165 lcd.plotArray(array); 00166 lcd.refresh(); 00167 wait(5.0); 00168 00169 lcd.clear(); 00170 lcd.normalMode(); // normal colour mode back 00171 lcd.setBrightness(0.5); // put LED backlight on 50% 00172 00173 // example of drawing lines 00174 for (int x = 0; x < WIDTH ; x+=10) { 00175 // x0,y0,x1,y1,type 0-white,1-black,2-dotted 00176 lcd.drawLine(0,0,x,HEIGHT,2); 00177 } 00178 lcd.refresh(); // refresh after drawing shapes 00179 wait(5.0); 00180 00181 00182 lcd.clear(); 00183 // example of how to draw circles 00184 lcd.drawCircle(WIDTH/2,HEIGHT/2,20,FILL_BLACK); // x,y,radius,black fill 00185 lcd.drawCircle(WIDTH/2,HEIGHT/2,10,FILL_WHITE); // x,y,radius,white fill 00186 lcd.drawCircle(WIDTH/2,HEIGHT/2,30,FILL_TRANSPARENT); // x,y,radius,transparent with outline 00187 lcd.refresh(); // refresh after drawing shapes 00188 wait(5.0); 00189 00190 lcd.clear(); 00191 // example of how to draw rectangles 00192 // origin x,y,width,height,type 00193 lcd.drawRect(10,10,50,30,FILL_BLACK); // filled black rectangle 00194 lcd.drawRect(15,15,20,10,FILL_WHITE); // filled white rectange (no outline) 00195 lcd.drawRect(2,2,70,40,FILL_TRANSPARENT); // transparent, just outline 00196 lcd.refresh(); // refresh after drawing shapes 00197 wait(5.0); 00198 00199 } 00200 } 00201 00202 00203 @endcode 00204 */ 00205 class N5110 00206 { 00207 private: 00208 // objects 00209 SPI *_spi; 00210 PwmOut *_led; 00211 DigitalOut *_pwr; 00212 DigitalOut *_sce; 00213 DigitalOut *_rst; 00214 DigitalOut *_dc; 00215 00216 // variables 00217 unsigned char buffer[84][6]; // screen buffer - the 6 is for the banks - each one is 8 bits; 00218 00219 public: 00220 /** Create a N5110 object connected to the specified pins 00221 * 00222 * @param pwr Pin connected to Vcc on the LCD display (pin 1) 00223 * @param sce Pin connected to chip enable (pin 3) 00224 * @param rst Pin connected to reset (pin 4) 00225 * @param dc Pin connected to data/command select (pin 5) 00226 * @param mosi Pin connected to data input (MOSI) (pin 6) 00227 * @param sclk Pin connected to serial clock (SCLK) (pin 7) 00228 * @param led Pin connected to LED backlight (must be PWM) (pin 8) 00229 * 00230 */ 00231 N5110(PinName const pwrPin, 00232 PinName const scePin, 00233 PinName const rstPin, 00234 PinName const dcPin, 00235 PinName const mosiPin, 00236 PinName const sclkPin, 00237 PinName const ledPin); 00238 00239 /** Create a N5110 object connected to the specified pins (Vcc to +3V3) 00240 * 00241 * @param sce Pin connected to chip enable (pin 3) 00242 * @param rst Pin connected to reset (pin 4) 00243 * @param dc Pin connected to data/command select (pin 5) 00244 * @param mosi Pin connected to data input (MOSI) (pin 6) 00245 * @param sclk Pin connected to serial clock (SCLK) (pin 7) 00246 * @param led Pin connected to LED backlight (must be PWM) (pin 8) 00247 * 00248 */ 00249 N5110(PinName const scePin, 00250 PinName const rstPin, 00251 PinName const dcPin, 00252 PinName const mosiPin, 00253 PinName const sclkPin, 00254 PinName const ledPin); 00255 00256 /** 00257 * Free allocated memory when object goes out of scope 00258 */ 00259 ~N5110(); 00260 00261 /** Initialise display 00262 * 00263 * Powers up the display and turns on backlight (50% brightness default). 00264 * Sets the display up in horizontal addressing mode and with normal video mode. 00265 */ 00266 void init(); 00267 00268 /** Turn off 00269 * 00270 * Powers down the display and turns of the backlight. 00271 * Needs to be reinitialised before being re-used. 00272 */ 00273 void turnOff(); 00274 00275 /** Clear 00276 * 00277 * Clears the screen buffer. 00278 */ 00279 void clear(); 00280 00281 /** Turn on normal video mode (default) 00282 * Black on white 00283 */ 00284 void normalMode(); 00285 00286 /** Turn on inverse video mode (default) 00287 * White on black 00288 */ 00289 void inverseMode(); 00290 00291 /** Set Brightness 00292 * 00293 * Sets brightness of LED backlight. 00294 * @param brightness - float in range 0.0 to 1.0 00295 */ 00296 void setBrightness(float const brightness); 00297 00298 /** Print String 00299 * 00300 * Prints a string of characters to the screen buffer. String is cut-off after the 83rd pixel. 00301 * @param x - the column number (0 to 83) 00302 * @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row 00303 */ 00304 void printString(char const *str, 00305 unsigned int const x, 00306 unsigned int const y); 00307 00308 /** Print Character 00309 * 00310 * Sends a character to the screen buffer. Printed at the specified location. Character is cut-off after the 83rd pixel. 00311 * @param c - the character to print. Can print ASCII as so printChar('C'). 00312 * @param x - the column number (0 to 83) 00313 * @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row 00314 */ 00315 void printChar(char const c, 00316 unsigned int const x, 00317 unsigned int const y); 00318 00319 /** 00320 * @brief Set a Pixel 00321 * 00322 * @param x The x co-ordinate of the pixel (0 to 83) 00323 * @param y The y co-ordinate of the pixel (0 to 47) 00324 * @param state The state of the pixel [true=black (default), false=white] 00325 * 00326 * @details This function sets the state of a pixel in the screen buffer. 00327 * The third parameter can be omitted, 00328 */ 00329 void setPixel(unsigned int const x, 00330 unsigned int const y, 00331 bool const state = true); 00332 00333 /** 00334 * @brief Clear a Pixel 00335 * 00336 * @param x - the x co-ordinate of the pixel (0 to 83) 00337 * @param y - the y co-ordinate of the pixel (0 to 47) 00338 * 00339 * @details This function clears pixel in the screen buffer 00340 * 00341 * @deprecated Use setPixel(x, y, false) instead 00342 */ 00343 void clearPixel(unsigned int const x, 00344 unsigned int const y); 00345 00346 /** Get a Pixel 00347 * 00348 * This function gets the status of a pixel in the screen buffer. 00349 * @param x - the x co-ordinate of the pixel (0 to 83) 00350 * @param y - the y co-ordinate of the pixel (0 to 47) 00351 * @returns 00352 * 0 - pixel is clear 00353 * 1 - pixel is set 00354 */ 00355 int getPixel(unsigned int const x, 00356 unsigned int const y) const; 00357 00358 /** Refresh display 00359 * 00360 * This functions sends the screen buffer to the display. 00361 */ 00362 void refresh(); 00363 00364 /** Randomise buffer 00365 * 00366 * This function fills the buffer with random data. Can be used to test the display. 00367 * A call to refresh() must be made to update the display to reflect the change in pixels. 00368 * The seed is not set and so the generated pattern will probably be the same each time. 00369 * TODO: Randomise the seed - maybe using the noise on the AnalogIn pins. 00370 */ 00371 void randomiseBuffer(); 00372 00373 /** Plot Array 00374 * 00375 * This function plots a one-dimensional array in the buffer. 00376 * @param array[] - y values of the plot. Values should be normalised in the range 0.0 to 1.0. First 84 plotted. 00377 */ 00378 void plotArray(float const array[]); 00379 00380 /** Draw Circle 00381 * 00382 * This function draws a circle at the specified origin with specified radius in the screen buffer 00383 * Uses the midpoint circle algorithm. 00384 * @see http://en.wikipedia.org/wiki/Midpoint_circle_algorithm 00385 * @param x0 - x-coordinate of centre 00386 * @param y0 - y-coordinate of centre 00387 * @param radius - radius of circle in pixels 00388 * @param fill - fill-type for the shape 00389 */ 00390 void drawCircle(unsigned int const x0, 00391 unsigned int const y0, 00392 unsigned int const radius, 00393 FillType const fill); 00394 00395 /** Draw Line 00396 * 00397 * This function draws a line between the specified points using linear interpolation. 00398 * @param x0 - x-coordinate of first point 00399 * @param y0 - y-coordinate of first point 00400 * @param x1 - x-coordinate of last point 00401 * @param y1 - y-coordinate of last point 00402 * @param type - 0 white,1 black,2 dotted 00403 */ 00404 void drawLine(unsigned int const x0, 00405 unsigned int const y0, 00406 unsigned int const x1, 00407 unsigned int const y1, 00408 unsigned int const type); 00409 00410 /** Draw Rectangle 00411 * 00412 * This function draws a rectangle. 00413 * @param x0 - x-coordinate of origin (top-left) 00414 * @param y0 - y-coordinate of origin (top-left) 00415 * @param width - width of rectangle 00416 * @param height - height of rectangle 00417 * @param fill - fill-type for the shape 00418 */ 00419 void drawRect(unsigned int const x0, 00420 unsigned int const y0, 00421 unsigned int const width, 00422 unsigned int const height, 00423 FillType const fill); 00424 00425 /** Draw Sprite 00426 * 00427 * This function draws a sprite as defined in a 2D array 00428 * @param x0 - x-coordinate of origin (top-left) 00429 * @param y0 - y-coordinate of origin (top-left) 00430 * @param nrows - number of rows in sprite 00431 * @param ncols - number of columns in sprite 00432 * @param sprite - 2D array representing the sprite 00433 */ 00434 void drawSprite(int x0, 00435 int y0, 00436 int nrows, 00437 int ncols, 00438 int *sprite) 00439 __attribute__((deprecated("Use the Bitmap::render() function instead"))); 00440 00441 00442 private: 00443 // methods 00444 void setXYAddress(unsigned int const x, 00445 unsigned int const y); 00446 void initSPI(); 00447 void turnOn(); 00448 void reset(); 00449 void clearRAM(); 00450 void sendCommand(unsigned char command); 00451 void sendData(unsigned char data); 00452 }; 00453 00454 const unsigned char font5x7[480] = { 00455 0x00, 0x00, 0x00, 0x00, 0x00,// (space) 00456 0x00, 0x00, 0x5F, 0x00, 0x00,// ! 00457 0x00, 0x07, 0x00, 0x07, 0x00,// " 00458 0x14, 0x7F, 0x14, 0x7F, 0x14,// # 00459 0x24, 0x2A, 0x7F, 0x2A, 0x12,// $ 00460 0x23, 0x13, 0x08, 0x64, 0x62,// % 00461 0x36, 0x49, 0x55, 0x22, 0x50,// & 00462 0x00, 0x05, 0x03, 0x00, 0x00,// ' 00463 0x00, 0x1C, 0x22, 0x41, 0x00,// ( 00464 0x00, 0x41, 0x22, 0x1C, 0x00,// ) 00465 0x08, 0x2A, 0x1C, 0x2A, 0x08,// * 00466 0x08, 0x08, 0x3E, 0x08, 0x08,// + 00467 0x00, 0x50, 0x30, 0x00, 0x00,// , 00468 0x08, 0x08, 0x08, 0x08, 0x08,// - 00469 0x00, 0x60, 0x60, 0x00, 0x00,// . 00470 0x20, 0x10, 0x08, 0x04, 0x02,// / 00471 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0 00472 0x00, 0x42, 0x7F, 0x40, 0x00,// 1 00473 0x42, 0x61, 0x51, 0x49, 0x46,// 2 00474 0x21, 0x41, 0x45, 0x4B, 0x31,// 3 00475 0x18, 0x14, 0x12, 0x7F, 0x10,// 4 00476 0x27, 0x45, 0x45, 0x45, 0x39,// 5 00477 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6 00478 0x01, 0x71, 0x09, 0x05, 0x03,// 7 00479 0x36, 0x49, 0x49, 0x49, 0x36,// 8 00480 0x06, 0x49, 0x49, 0x29, 0x1E,// 9 00481 0x00, 0x36, 0x36, 0x00, 0x00,// : 00482 0x00, 0x56, 0x36, 0x00, 0x00,// ; 00483 0x00, 0x08, 0x14, 0x22, 0x41,// < 00484 0x14, 0x14, 0x14, 0x14, 0x14,// = 00485 0x41, 0x22, 0x14, 0x08, 0x00,// > 00486 0x02, 0x01, 0x51, 0x09, 0x06,// ? 00487 0x32, 0x49, 0x79, 0x41, 0x3E,// @ 00488 0x7E, 0x11, 0x11, 0x11, 0x7E,// A 00489 0x7F, 0x49, 0x49, 0x49, 0x36,// B 00490 0x3E, 0x41, 0x41, 0x41, 0x22,// C 00491 0x7F, 0x41, 0x41, 0x22, 0x1C,// D 00492 0x7F, 0x49, 0x49, 0x49, 0x41,// E 00493 0x7F, 0x09, 0x09, 0x01, 0x01,// F 00494 0x3E, 0x41, 0x41, 0x51, 0x32,// G 00495 0x7F, 0x08, 0x08, 0x08, 0x7F,// H 00496 0x00, 0x41, 0x7F, 0x41, 0x00,// I 00497 0x20, 0x40, 0x41, 0x3F, 0x01,// J 00498 0x7F, 0x08, 0x14, 0x22, 0x41,// K 00499 0x7F, 0x40, 0x40, 0x40, 0x40,// L 00500 0x7F, 0x02, 0x04, 0x02, 0x7F,// M 00501 0x7F, 0x04, 0x08, 0x10, 0x7F,// N 00502 0x3E, 0x41, 0x41, 0x41, 0x3E,// O 00503 0x7F, 0x09, 0x09, 0x09, 0x06,// P 00504 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q 00505 0x7F, 0x09, 0x19, 0x29, 0x46,// R 00506 0x46, 0x49, 0x49, 0x49, 0x31,// S 00507 0x01, 0x01, 0x7F, 0x01, 0x01,// T 00508 0x3F, 0x40, 0x40, 0x40, 0x3F,// U 00509 0x1F, 0x20, 0x40, 0x20, 0x1F,// V 00510 0x7F, 0x20, 0x18, 0x20, 0x7F,// W 00511 0x63, 0x14, 0x08, 0x14, 0x63,// X 00512 0x03, 0x04, 0x78, 0x04, 0x03,// Y 00513 0x61, 0x51, 0x49, 0x45, 0x43,// Z 00514 0x00, 0x00, 0x7F, 0x41, 0x41,// [ 00515 0x02, 0x04, 0x08, 0x10, 0x20,// "\" 00516 0x41, 0x41, 0x7F, 0x00, 0x00,// ] 00517 0x04, 0x02, 0x01, 0x02, 0x04,// ^ 00518 0x40, 0x40, 0x40, 0x40, 0x40,// _ 00519 0x00, 0x01, 0x02, 0x04, 0x00,// ` 00520 0x20, 0x54, 0x54, 0x54, 0x78,// a 00521 0x7F, 0x48, 0x44, 0x44, 0x38,// b 00522 0x38, 0x44, 0x44, 0x44, 0x20,// c 00523 0x38, 0x44, 0x44, 0x48, 0x7F,// d 00524 0x38, 0x54, 0x54, 0x54, 0x18,// e 00525 0x08, 0x7E, 0x09, 0x01, 0x02,// f 00526 0x08, 0x14, 0x54, 0x54, 0x3C,// g 00527 0x7F, 0x08, 0x04, 0x04, 0x78,// h 00528 0x00, 0x44, 0x7D, 0x40, 0x00,// i 00529 0x20, 0x40, 0x44, 0x3D, 0x00,// j 00530 0x00, 0x7F, 0x10, 0x28, 0x44,// k 00531 0x00, 0x41, 0x7F, 0x40, 0x00,// l 00532 0x7C, 0x04, 0x18, 0x04, 0x78,// m 00533 0x7C, 0x08, 0x04, 0x04, 0x78,// n 00534 0x38, 0x44, 0x44, 0x44, 0x38,// o 00535 0x7C, 0x14, 0x14, 0x14, 0x08,// p 00536 0x08, 0x14, 0x14, 0x18, 0x7C,// q 00537 0x7C, 0x08, 0x04, 0x04, 0x08,// r 00538 0x48, 0x54, 0x54, 0x54, 0x20,// s 00539 0x04, 0x3F, 0x44, 0x40, 0x20,// t 00540 0x3C, 0x40, 0x40, 0x20, 0x7C,// u 00541 0x1C, 0x20, 0x40, 0x20, 0x1C,// v 00542 0x3C, 0x40, 0x30, 0x40, 0x3C,// w 00543 0x44, 0x28, 0x10, 0x28, 0x44,// x 00544 0x0C, 0x50, 0x50, 0x50, 0x3C,// y 00545 0x44, 0x64, 0x54, 0x4C, 0x44,// z 00546 0x00, 0x08, 0x36, 0x41, 0x00,// { 00547 0x00, 0x00, 0x7F, 0x00, 0x00,// | 00548 0x00, 0x41, 0x36, 0x08, 0x00,// } 00549 0x08, 0x08, 0x2A, 0x1C, 0x08,// -> 00550 0x08, 0x1C, 0x2A, 0x08, 0x08 // <- 00551 }; 00552 00553 #endif
Generated on Thu Jul 14 2022 02:23:06 by 1.7.2