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
Fork of N5110 by
Revision 18:d2140be00144, committed 2016-05-09
- Comitter:
- el14pjgn
- Date:
- Mon May 09 14:18:12 2016 +0000
- Parent:
- 17:780a542d5f8b
- Commit message:
- Expanded to include 2d array printing, backlight setting adjustment, and other similar features.
Changed in this revision
--- 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
--- a/N5110.h Tue Mar 17 12:56:03 2015 +0000 +++ b/N5110.h Mon May 09 14:18:12 2016 +0000 @@ -207,6 +207,12 @@ * @param brightness - float in range 0.0 to 1.0 */ void setBrightness(float brightness); + + /** Set PWM frequency + * Sets PWM frequency of LED backlight + * @param freq - float in the range 0.1+ + */ + void setPwmFreq(float freq); /** Print String * @@ -224,6 +230,16 @@ * @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); + + /** Select Buffer + * + * Selects active buffer(s). + * @param type - switches between editable and displayed buffer edit; + * type = 1; selects visible buffer. + * type = 2; selects buffer to be written to. + * type = 3; switches both. + */ + void selectBuffer(int type,int buffer); /** Set a Pixel * @@ -233,6 +249,16 @@ * @param y - the y co-ordinate of the pixel (0 to 47) */ void setPixel(int x, int y); + + /** write to a pixel + * + * This function writes the value v to a pixel on 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 v - the value to be written to the pixel (0 to 1) + */ + void writePixel(int x, int y, int v); /** Clear a Pixel * @@ -268,6 +294,21 @@ * TODO: Randomise the seed - maybe using the noise on the AnalogIn pins. */ void randomiseBuffer(); + + /** Plot Array 2d + * + * This function plots a two-dimensional array on the display. + * @param array2d[][] - y values of the plot. Values should be normalised in the range 0.0 to 1.0. First 84 plotted. + */ + void plotArray2d(bool array2d[][48]); + + /** Plot Array 3d + * + * This function plots a two-dimensional array on the display. + * @param array2d[][] - y values of the plot. Values should be normalised in the range 0.0 to 1.0. First 84 plotted. + * Slightly over-specific to project + */ + void plotArray3d(bool array3d[][50][2],int z,int off_x=0, int off_y=0); /** Plot Array * @@ -323,7 +364,9 @@ void sendData(unsigned char data); public: - unsigned char buffer[84][6]; // screen buffer - the 6 is for the banks - each one is 8 bits; + unsigned char buffer[84][6][3]; // screen buffer - the 6 is for the banks - each one is 8 bits; + int bufferFrameEdit; + int bufferFrameDisplay; private: // private variables SPI* spi;
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/N5110.lib Mon May 09 14:18:12 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/eencae/code/N5110/#adb79338d40f