Peter Nye / N5110_mod

Dependencies:   N5110

Fork of N5110 by Craig Evans

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers N5110.h Source File

N5110.h

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