Simple library for interfacing to Nokia 5110 LCD display (as found on the SparkFun website). Edited to include additional feaatures, such as 2d array loading, multiple screen buffers, and better backlight control

Dependencies:   N5110

Dependents:   Main_code_ver18

Fork of N5110 by Craig Evans

Revision:
18:d2140be00144
Parent:
17:780a542d5f8b
--- a/N5110.cpp	Tue Mar 17 12:56:03 2015 +0000
+++ b/N5110.cpp	Mon May 09 14:18:12 2016 +0000
@@ -20,6 +20,8 @@
     sce = new DigitalOut(scePin);
     rst = new DigitalOut(rstPin);
     dc = new DigitalOut(dcPin);
+    bufferFrameEdit = 1;
+    bufferFrameDisplay = 1;
 
 }
 
@@ -98,6 +100,12 @@
     led->write(brightness);
 }
 
+// function to change LED backlight PWM frequency
+void N5110::setPwmFreq(float freq)
+{
+    led->period_us(freq);
+}
+
 
 // pulse the active low reset line
 void N5110::reset()
@@ -155,14 +163,43 @@
     }
 }
 
+void N5110::selectBuffer(int type,int buffer)
+{
+    switch(type) {
+        case 1: //display
+            bufferFrameDisplay = buffer;
+            break;
+        case 2: //editable
+            bufferFrameEdit = buffer;
+            break;
+        case 3: //both
+            bufferFrameEdit = buffer;
+            bufferFrameDisplay = buffer;
+            break;
+    }
+}
+
 // These functions are used to set, clear and get the value of pixels in the display
 // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x).  The refresh()
 // function must be called after set and clear in order to update the display
+void N5110::writePixel(int x, int y, int v)
+{
+    if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
+        // calculate bank and shift 1 to required position in the data byte
+        if(v==1) {
+            buffer[x][y/8][bufferFrameEdit] |= (1 << y%8);
+        }
+        if(v==0) {
+            buffer[x][y/8][bufferFrameEdit] &= ~(1 << y%8);
+        }
+    }
+}
+
 void N5110::setPixel(int x, int y)
 {
     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);
+        buffer[x][y/8][bufferFrameEdit] |= (1 << y%8);
     }
 }
 
@@ -170,7 +207,7 @@
 {
     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);
+        buffer[x][y/8][bufferFrameEdit] &= ~(1 << y%8);
     }
 }
 
@@ -178,7 +215,7 @@
 {
     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);
+        return (int) buffer[x][y/8][bufferFrameEdit] & (1 << y%8);
     } else {
         return 0;
     }
@@ -197,7 +234,7 @@
 
     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
+            spi->write(buffer[i][j][bufferFrameDisplay]);  // send buffer
         }
     }
     sce->write(1); // set CE high to end frame
@@ -211,7 +248,7 @@
     int i,j;
     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
+            buffer[i][j][bufferFrameEdit] = rand()%256;  // generate random byte
         }
     }
 
@@ -221,7 +258,7 @@
 void N5110::printChar(char c,int x,int y)
 {
     for (int i = 0; i < 5 ; i++ ) {
-        buffer[x+i][y] = font5x7[(c - 32)*5 + i];
+        buffer[x+i][y][bufferFrameEdit] = font5x7[(c - 32)*5 + i];
         // array is offset by 32 relative to ASCII, each character is 5 pixels wide
     }
 
@@ -238,7 +275,7 @@
         // writes the character bitmap data to the buffer, so that
         // text and pixels can be displayed at the same time
         for (int i = 0; i < 5 ; i++ ) {
-            buffer[x+i+n*6][y] = font5x7[(*str - 32)*5 + i];
+            buffer[x+i+n*6][y][bufferFrameEdit] = font5x7[(*str - 32)*5 + i];
         }
 
         str++;  // go to next character in string
@@ -262,9 +299,33 @@
     int i,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;
+            buffer[i][j][bufferFrameEdit]=0;
+        }
+    }
+}
+
+// function to write array to buffer
+void N5110::plotArray2d(bool array2d[][HEIGHT])
+{
+    int i,j;
+    for (i=0; i<=WIDTH; i++) {  // loop through the banks and copy data across
+        for (j=0; j<=HEIGHT; j++) {
+            writePixel(i,j,array2d[i][j]);
         }
     }
+    refresh();
+}
+
+// function to write offset 3d array to buffer
+void N5110::plotArray3d(bool array3d[][50][2],int z,int off_x, int off_y)
+{
+    int i,j;
+    for (i=0; i<=WIDTH; i++) {  // loop through the banks and copy data across
+        for (j=0; j<=HEIGHT; j++) {
+            writePixel(i,j,array3d[i-off_x][j-off_y][z]);
+        }
+    }
+    refresh();
 }
 
 // function to plot array on display