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

Fork of N5110 by Craig Evans

Revision:
17:780a542d5f8b
Parent:
16:b37a3a260598
Child:
18:1af393359298
--- a/N5110.h	Tue Mar 10 19:33:55 2015 +0000
+++ b/N5110.h	Tue Mar 17 12:56:03 2015 +0000
@@ -32,35 +32,36 @@
 #define CMD_VOP_6V06           0xB2
 #define CMD_VOP_7V38           0xC8
 
-// number of pixels on display  
+// number of pixels on display
 #define WIDTH 84
 #define HEIGHT 48
+#define BANKS 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 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.1
+@brief Revision 1.2
 
 @author Craig A. Evans
-@date   March 2015
+@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 
+// Can give better performance due to current limitation from GPIO pin
 
 int main()
 {
@@ -69,63 +70,88 @@
 
     while(1) {
 
-        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
-        
-        lcd.clear();            // clear display
         // these are default settings so not strictly needed
         lcd.normalMode();      // normal colour mode
-        lcd.setBrightness(0.5); // put LED backlight on full
-        
+        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
+
+        // 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)
+
+        // draw a line across the display at y = 40 pixels (origin top-left)
         for (int i = 0; i < WIDTH; i++) {
-            lcd.setPixel(i,40);    
+            lcd.setPixel(i,40);
         }
         // need to refresh display after setting pixels
-        lcd.refresh(); 
-        
+        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);    
+            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); 
+        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);
+        }
+
+        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
+
+        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
+
+         wait(5.0);
+        lcd.clear();
 
     }
 }
 
-  
+
  * @endcode
  */
 class N5110
@@ -140,56 +166,56 @@
     * @param dc  Pin connected to data/command select (pin 5)
     * @param mosi Pin connected to data input (MOSI) (pin 6)
     * @param sclk Pin connected to serial clock (SCLK) (pin 7)
-    * @param led Pin connected to LED backlight (must be PWM) (pin 8) 
-    * 
+    * @param led Pin connected to LED backlight (must be PWM) (pin 8)
+    *
     */
     N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin);
-    
+
     /** 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.
     */
     void init();
-    
+
     /** Turn off
     *
     *   Powers down the display and turns of the backlight.
     *   Needs to be reinitialised before being re-used.
     */
     void turnOff();
-    
+
     /** Clears
     *
     *   Clears the screen.
     */
     void clear();
-    
+
     /** Turn on normal video mode (default)
     *  Black on white
     */
     void normalMode();
-    
+
     /** Turn on inverse video mode (default)
     *  White on black
     */
     void inverseMode();
-    
+
     /** Set Brightness
     *
     *   Sets brightness of LED backlight.
     *   @param brightness - float in range 0.0 to 1.0
     */
     void setBrightness(float brightness);
-    
+
     /** Print String
     *
-    *   Prints a string of characters to the display.  
+    *   Prints a string of characters to the display.
     *   @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
     */
     void printString(const char * str,int x,int y);
-    
+
     /** Print Character
     *
     *   Sends a character to the display.  Printed at the specified location
@@ -198,60 +224,93 @@
     *   @param y - the row number (0 to 5) - the display is split into 6 banks - each bank can be considered a row
     */
     void printChar(char c,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.
     *   @param  x - the x co-ordinate of the pixel (0 to 83)
-    *   @param  y - the y co-ordinate of the pixel (0 to 47) 
+    *   @param  y - the y co-ordinate of the pixel (0 to 47)
     */
     void setPixel(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.
     *   @param  x - the x co-ordinate of the pixel (0 to 83)
-    *   @param  y - the y co-ordinate of the pixel (0 to 47) 
+    *   @param  y - the y co-ordinate of the pixel (0 to 47)
     */
     void clearPixel(int x, int y);
-    
+
     /** Get a Pixel
     *
     *   This function gets the status of a pixel in the display.
     *   @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 
+    *   @param  y - the y co-ordinate of the pixel (0 to 47)
+    *   @returns
     *       0           - pixel is clear
     *       non-zero    - pixel is set
     */
     int getPixel(int x, int y);
-    
+
     /** Refresh display
     *
     *   This functions refreshes the display to reflect the current data in the buffer.
-    */    
+    */
     void refresh();
-    
+
     /** Randomise buffer
     *
-    *   This function fills the buffer with random data.  Can be used to test the display.  
+    *   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.
     */
     void randomiseBuffer();
-    
+
     /** Plot Array
     *
-    *   This function plots a one-dimensional array on the display. The values in the array
-    *   correspond to the y values of the plot and these are plotted along the x-axis (one
-    *   value per pixel along the x-axis of the display. This means only the first 84 values are 
-    *   plotted. y values in the array should be normalised in the range 0.0 to 1.0. 
+    *   This function plots a one-dimensional array on the display.
+    *   @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.
+    *   Uses the midpoint circle algorithm.
+    *   @see http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
+    *   @param  x0 - x-coordinate of centre
+    *   @param  y0 - y-coordinate of centre
+    *   @param  radius - radius of circle in pixels
+    *   @param  fill - 0 transparent (w/outline), 1 filled black, 2 filled white (wo/outline)
+    */
+    void drawCircle(int x0,int y0,int radius,int fill);
+
+    /** Draw Line
+    *
+    *   This function draws a line between the specified points using linear interpolation.
+    *   @param  x0 - x-coordinate of first point
+    *   @param  y0 - y-coordinate of first point
+    *   @param  x1 - x-coordinate of last point
+    *   @param  y1 - y-coordinate of last point
+    *   @param  type - 0 white,1 black,2 dotted
+    */
+    void drawLine(int x0,int y0,int x1,int y1,int type);
+
+    /** Draw Rectangle
+    *
+    *   This function draws a rectangle.
+    *   @param  x0 - x-coordinate of origin (top-left)
+    *   @param  y0 - y-coordinate of origin (top-left)
+    *   @param  width - width of rectangle
+    *   @param  height - height of rectangle
+    *   @param  fill - 0 transparent (w/outline), 1 filled black, 2 filled white (wo/outline)
+    */
+    void drawRect(int x0,int y0,int width,int height,int fill);
+
+
 private:
 
     void setXYAddress(int x, int y);