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:
15:ee645611ff94
Child:
18:1af393359298
--- a/N5110.cpp	Tue Mar 10 19:33:55 2015 +0000
+++ b/N5110.cpp	Tue Mar 17 12:56:03 2015 +0000
@@ -139,7 +139,7 @@
 {
     int i;
     sce->write(0);  //set CE low to begin frame
-    for(i = 0; i < 504; i++) { // 48 x 84 bits = 504 bytes
+    for(i = 0; i < WIDTH * HEIGHT; i++) { // 48 x 84 bits = 504 bytes
         spi->write(0x00);  // send 0's
     }
     sce->write(1); // set CE high to end frame
@@ -149,18 +149,10 @@
 // function to set the XY address in RAM for subsequenct data write
 void N5110::setXYAddress(int x, int y)
 {
-    // check whether address is in range
-    if (x > 83)
-        x=83;
-    if (y > 5)
-        y=5;
-    if (x < 0)
-        x=0;
-    if (y < 0)
-        y=0;
-
-    sendCommand(0x80 | x);  // send addresses to display with relevant mask
-    sendCommand(0x40 | y);
+    if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
+        sendCommand(0x80 | x);  // send addresses to display with relevant mask
+        sendCommand(0x40 | y);
+    }
 }
 
 // These functions are used to set, clear and get the value of pixels in the display
@@ -168,21 +160,28 @@
 // function must be called after set and clear in order to update the display
 void N5110::setPixel(int x, int y)
 {
-    // calculate bank and shift 1 to required position in the data byte
-    buffer[x][y/8] |= (1 << y%8);
+    if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
+        // calculate bank and shift 1 to required position in the data byte
+        buffer[x][y/8] |= (1 << y%8);
+    }
 }
 
 void N5110::clearPixel(int x, int y)
 {
-    // calculate bank and shift 1 to required position (using bit clear)
-    buffer[x][y/8] &= ~(1 << y%8);
+    if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
+        // calculate bank and shift 1 to required position (using bit clear)
+        buffer[x][y/8] &= ~(1 << y%8);
+    }
 }
 
 int N5110::getPixel(int x, int y)
 {
-    // return relevant bank and mask required bit
-    return (int) buffer[x][y/8] & (1 << y%8);
-
+    if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
+        // return relevant bank and mask required bit
+        return (int) buffer[x][y/8] & (1 << y%8);
+    } else {
+        return 0;
+    }
 }
 
 // function to refresh the display
@@ -196,8 +195,8 @@
 
     sce->write(0);  //set CE low to begin frame
 
-    for(j = 0; j < 6; j++) {  // be careful to use correct order (j,i) for horizontal addressing
-        for(i = 0; i < 84; i++) {
+    for(j = 0; j < BANKS; j++) {  // be careful to use correct order (j,i) for horizontal addressing
+        for(i = 0; i < WIDTH; i++) {
             spi->write(buffer[i][j]);  // send buffer
         }
     }
@@ -210,8 +209,8 @@
 void N5110::randomiseBuffer()
 {
     int i,j;
-    for(j = 0; j < 6; j++) {  // be careful to use correct order (j,i) for horizontal addressing
-        for(i = 0; i < 84; i++) {
+    for(j = 0; j < BANKS; j++) {  // be careful to use correct order (j,i) for horizontal addressing
+        for(i = 0; i < WIDTH; i++) {
             buffer[i][j] = rand()%256;  // generate random byte
         }
     }
@@ -223,10 +222,10 @@
 {
     for (int i = 0; i < 5 ; i++ ) {
         buffer[x+i][y] = font5x7[(c - 32)*5 + i];
-         // array is offset by 32 relative to ASCII, each character is 5 pixels wide
+        // array is offset by 32 relative to ASCII, each character is 5 pixels wide
     }
-    
-     refresh();  // this sends the buffer to the display and sets address (cursor) back to 0,0
+
+    refresh();  // this sends the buffer to the display and sets address (cursor) back to 0,0
 }
 
 // function to print string at specified position
@@ -261,8 +260,8 @@
 void N5110::clearBuffer()
 {
     int i,j;
-    for (i=0; i<84; i++) {  // loop through the banks and set the buffer to 0
-        for (j=0; j<6; j++) {
+    for (i=0; i<WIDTH; i++) {  // loop through the banks and set the buffer to 0
+        for (j=0; j<BANKS; j++) {
             buffer[i][j]=0;
         }
     }
@@ -274,7 +273,7 @@
 
     int i;
 
-    for (i=0; i<84; i++) {  // loop through array
+    for (i=0; i<WIDTH; i++) {  // loop through array
         // elements are normalised from 0.0 to 1.0, so multiply
         // by 47 to convert to pixel range, and subtract from 47
         // since top-left is 0,0 in the display geometry
@@ -283,5 +282,113 @@
 
     refresh();
 
+}
 
-}
\ No newline at end of file
+// function to draw circle
+void N5110:: drawCircle(int x0,int y0,int radius,int fill)
+{
+    // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
+    int x = radius;
+    int y = 0;
+    int radiusError = 1-x;
+
+    while(x >= y) {
+
+        // if transparent, just draw outline
+        if (fill == 0) {
+            setPixel( x + x0,  y + y0);
+            setPixel(-x + x0,  y + y0);
+            setPixel( y + x0,  x + y0);
+            setPixel(-y + x0,  x + y0);
+            setPixel(-y + x0, -x + y0);
+            setPixel( y + x0, -x + y0);
+            setPixel( x + x0, -y + y0);
+            setPixel(-x + x0, -y + y0);
+        } else {  // drawing filled circle, so draw lines between points at same y value
+
+            int type = (fill==1) ? 1:0;  // black or white fill
+
+            drawLine(x+x0,y+y0,-x+x0,y+y0,type);
+            drawLine(y+x0,x+y0,-y+x0,x+y0,type);
+            drawLine(y+x0,-x+y0,-y+x0,-x+y0,type);
+            drawLine(x+x0,-y+y0,-x+x0,-y+y0,type);
+        }
+
+
+        y++;
+        if (radiusError<0) {
+            radiusError += 2 * y + 1;
+        } else {
+            x--;
+            radiusError += 2 * (y - x) + 1;
+        }
+    }
+
+    refresh();
+}
+
+void N5110::drawLine(int x0,int y0,int x1,int y1,int type)
+{
+    int y_range = y1-y0;  // calc range of y and x
+    int x_range = x1-x0;
+    int start,stop,step;
+
+    // if dotted line, set step to 2, else step is 1
+    step = (type==2) ? 2:1;
+
+    // make sure we loop over the largest range to get the most pixels on the display
+    // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
+    // or else we'll only end up with 1 pixel in the x column
+    if ( abs(x_range) > abs(y_range) ) {
+
+        // ensure we loop from smallest to largest or else for-loop won't run as expected
+        start = x1>x0 ? x0:x1;
+        stop =  x1>x0 ? x1:x0;
+
+        // loop between x pixels
+        for (int x = start; x<= stop ; x+=step) {
+            // do linear interpolation
+            int y = y0 + (y1-y0)*(x-x0)/(x1-x0);
+
+            if (type == 0)   // if 'white' line, turn off pixel
+                clearPixel(x,y);
+            else
+                setPixel(x,y);  // else if 'black' or 'dotted' turn on pixel
+        }
+    } else {
+
+        // ensure we loop from smallest to largest or else for-loop won't run as expected
+        start = y1>y0 ? y0:y1;
+        stop =  y1>y0 ? y1:y0;
+
+        for (int y = start; y<= stop ; y+=step) {
+            // do linear interpolation
+            int x = x0 + (x1-x0)*(y-y0)/(y1-y0);
+
+            if (type == 0)   // if 'white' line, turn off pixel
+                clearPixel(x,y);
+            else
+                setPixel(x,y);  // else if 'black' or 'dotted' turn on pixel
+
+        }
+    }
+
+    refresh();
+}
+
+void N5110::drawRect(int x0,int y0,int width,int height,int fill)
+{
+
+    if (fill == 0) { // transparent, just outline
+        drawLine(x0,y0,x0+width,y0,1);  // top
+        drawLine(x0,y0+height,x0+width,y0+height,1);  // bottom
+        drawLine(x0,y0,x0,y0+height,1);  // left
+        drawLine(x0+width,y0,x0+width,y0+height,1);  // right
+    } else { // filled rectangle
+        int type = (fill==1) ? 1:0;  // black or white fill
+        for (int y = y0; y<= y0+height; y++) {  // loop through rows of rectangle
+            drawLine(x0,y,x0+width,y,type);  // draw line across screen
+        }
+    }
+
+}