a modified version of N5110 library to be used with freescale boards

Files at this revision

API Documentation at this revision

Comitter:
Francesco Fantoni
Date:
Thu May 22 21:50:41 2014 +0200
Parent:
6:adb79338d40f
Commit message:
initial commit

Changed in this revision

N5110.cpp Show annotated file Show diff for this revision Revisions of this file
N5110.h Show annotated file Show diff for this revision Revisions of this file
N5110.o Show annotated file Show diff for this revision Revisions of this file
--- a/N5110.cpp	Mon Jan 27 18:41:45 2014 +0000
+++ b/N5110.cpp	Thu May 22 21:50:41 2014 +0200
@@ -12,14 +12,17 @@
 {
     
     spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise
-    initSPI();    
-    
+    spi->format(LCD_SPI_BITS, LCD_SPI_MODE);
+    spi->frequency(LCD_FREQ);
+
     // set up pins as required
     led = new PwmOut(ledPin);
-    pwr = new DigitalOut(pwrPin);
+    //pwr = new DigitalOut(pwrPin);
     sce = new DigitalOut(scePin);
     rst = new DigitalOut(rstPin);
     dc = new DigitalOut(dcPin);
+    W = 83;
+    H = 47;
 
 }
 
@@ -64,13 +67,13 @@
 {
     // set brightness of LED - 0.0 to 1.0 - default is 50%
     setBrightness(0.5);
-    pwr->write(1);  // apply power
+    //led->write(0);  // apply power
 }
 
 // function to power down LCD
 void N5110::turnOff()
 {
-    setBrightness(0.0);  // turn backlight off
+    setBrightness(1.0);  // turn backlight off
     clearRAM();   // clear RAM to ensure specified current consumption
     // send command to ensure we are in basic model
     sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
@@ -80,7 +83,7 @@
     sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
     // small delay and then turn off the power pin
     wait_ms(10);
-    pwr->write(0);
+   // pwr->write(1);
 
 }
 
@@ -104,21 +107,16 @@
     rst->write(1);
 }
 
-// function to initialise SPI peripheral
-void N5110::initSPI()
-{
-    spi->format(8,1);    // 8 bits, Mode 1 - polarity 0, phase 1 - base value of clock is 0, data captured on falling edge/propagated on rising edge
-    spi->frequency(4000000);  // maximum of screen is 4 MHz
-}
+
 
 // send a command to the display
 void N5110::sendCommand(unsigned char command)
 {
     dc->write(0);  // set DC low for command
-    sce->write(0); // set CE low to begin frame
+    //sce->write(0); // set CE low to begin frame
     spi->write(command);  // send command
     dc->write(1);  // turn back to data by default
-    sce->write(1); // set CE high to end frame (expected for transmission of single byte)
+    //sce->write(1); // set CE high to end frame (expected for transmission of single byte)
 
 }
 
@@ -127,20 +125,20 @@
 // be the default mode.
 void N5110::sendData(unsigned char data)
 {
-    sce->write(0);   // set CE low to begin frame
+    //sce->write(0);   // set CE low to begin frame
     spi->write(data);
-    sce->write(1);  // set CE high to end frame (expected for transmission of single byte)
+    //sce->write(1);  // set CE high to end frame (expected for transmission of single byte)
 }
 
 // this function writes 0 to the 504 bytes to clear the RAM
 void N5110::clearRAM()
 {
     int i;
-    sce->write(0);  //set CE low to begin frame
+    //sce->write(0);  //set CE low to begin frame
     for(i = 0; i < 504; i++) { // 48 x 84 bits = 504 bytes
         spi->write(0x00);  // send 0's
     }
-    sce->write(1); // set CE high to end frame
+    //sce->write(1); // set CE high to end frame
 
 }
 
@@ -176,6 +174,101 @@
     buffer[x][y/8] &= ~(1 << y%8);
 }
 
+void N5110::drawHline(int x, int y, int l)
+{
+	for(int i=0; i<l; i++)
+	{
+		int ps = x+i;
+		if ((ps < 84 && ps >= 0) && (y<48 && y>=0)) {
+			buffer[ps][y/8] |= (1 << y%8);
+			//refresh();
+		}
+
+	}
+	refresh();
+}
+
+void N5110::drawVline(int x, int y, int l)
+{
+	for(int i=0; i<l; i++)
+	{
+		int ps = y+i;
+		if ((ps < 48 && ps >= 0) && (x<84 && x>=0)){
+			buffer[x][ps/8] |= (1 << ps%8);
+			//refresh();
+		}
+	}
+	refresh();
+}
+
+void N5110::drawRectangle(int x, int y, int w, int h)
+{
+	drawHline(x,y,w);
+	drawVline(x+w,y,h);
+	drawHline(x,y+h,w);
+	drawVline(x,y,h);
+}
+
+void N5110::drawGrid(int stepx, int stepy)
+{
+	for (int y=0; y<47; y += stepy){
+		for(int x=0; x<84; x++){
+			setPixel(x,y);
+		 	}
+		  refresh();
+	}
+	for (int x=0; x<84; x += stepx){
+		for(int y=0; y<47; y++){
+			setPixel(x,y);
+		 }
+		 refresh();
+	}
+}
+
+// draw line between two points using C implementation of Bresenham's line algorithm
+void N5110::drawLine(int x0, int y0, int x1, int y1) {
+
+  int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1;
+  int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1;
+  int err = (dx>dy ? dx : -dy)/2, e2;
+
+  for(;;){
+	if ((x0 < 84 && x0 >= 0) && (y0<47 && y0>=0)) {
+			buffer[x0][y0/8] |= (1 << y0%8);
+		}
+    if (x0==x1 && y0==y1) break;
+    e2 = err;
+    if (e2 >-dx) { err -= dy; x0 += sx; }
+    if (e2 < dy) { err += dx; y0 += sy; }
+  }
+  refresh();
+}
+
+
+void N5110::drawLineAngle(int x0, int y0, int l, float angle) {
+	float radian = 0-angle * (M_PI/180);
+	int x1 = x0 + (l*cos(radian));
+	int y1 = y0 + (l*sin(radian));
+	drawLine(x0,y0,x1,y1);
+}
+
+
+void N5110::drawCircle(int x, int y, int radius, int divisions) {
+
+	float angleIncrement = M_PI * 2 / divisions;
+	double tempAngle = 0.0;
+
+	for (int i = 0; i < divisions; ++i) {
+		double px = cos(tempAngle) * radius + x;
+		double py = sin(tempAngle) * radius + y;
+		if ((px < 84 && px >= 0) && (py<47 && py>=0)) {
+				buffer[(int)px][(int)py/8] |= (1 << (int)py%8);
+			}
+		tempAngle += angleIncrement;
+	    }
+	refresh();
+}
+
 unsigned char N5110::getPixel(int x, int y)
 {
     // return relevant bank and mask required bit
@@ -187,14 +280,14 @@
 void N5110::refresh()
 {
     int i,j;
-    sce->write(0);  //set CE low to begin frame
+  //  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++) {
             spi->write(buffer[i][j]);  // send buffer
         }
     }
-    sce->write(1); // set CE high to end frame
+  //  sce->write(1); // set CE high to end frame
 
 }
 
@@ -217,7 +310,21 @@
     int i;
     // loop through 5 columns
     for (i = 0; i < 5 ; i++ ) {
-        sendData(font5x7[(c - 32)*5 + i]);
+        sendData((font5x7[(c - 32)*5 + i]));
+        // array is offset by 32 relative to ASCII, each character is 5 pixels wide
+        // the X address is automatically incremented after each data write
+    }
+    sendData(0); // send an empty byte to introduce space between characters
+
+}
+
+// function to print 6x7 negative font
+void N5110::printNegChar(char c)
+{
+    int i;
+    // loop through 5 columns
+    for (i = 0; i < 6 ; i++ ) {
+        sendData(~(font6x7[(c - 32)*6 + i]));
         // array is offset by 32 relative to ASCII, each character is 5 pixels wide
         // the X address is automatically incremented after each data write
     }
@@ -240,6 +347,21 @@
 
 }
 
+// function to print string at specified position
+void N5110::printNegString(const char * str,int x,int y)
+{
+    int n = 0 ; // counter for number of characters in string
+    // loop through string and print character
+    while(*str) {
+
+        setXYAddress(x+6*n,y);  // leave 1 pixel (6 = 5 + 1) between each character
+        printNegChar(*str);   // print the char - can probably so *str++ and remove next line
+        str++;  // go to next character in string
+        n++;    // increment index
+    }
+
+}
+
 // function to clear the screen
 void N5110::clear()
 {
@@ -256,4 +378,4 @@
             buffer[i][j]=0;
         }
     }
-}
\ No newline at end of file
+}
--- a/N5110.h	Mon Jan 27 18:41:45 2014 +0000
+++ b/N5110.h	Thu May 22 21:50:41 2014 +0200
@@ -32,7 +32,18 @@
 #define CMD_VOP_6V06           0xB2
 #define CMD_VOP_7V38           0xC8
 
+// LCD Characteristics
+#define LCD_FREQ 2000000
+#define LCD_SPI_MODE 0x01
+#define LCD_SPI_BITS 0x08
+#define LCD_X_MAX 84
+#define LCD_Y_MAX 48
+
+// MATH CONSTANTS
+//# define PI           3.14159265358979323846  /* pi */
+
 #include "mbed.h"
+#include <math.h>
 
 /** 
 @brief Simple library for interfacing with Nokia 5110 LCD display (https://www.sparkfun.com/products/10168) using the hardware SPI on the mbed.
@@ -142,6 +153,8 @@
     */
     void printString(const char * str,int x,int y);
     
+    void printNegString(const char * str,int x,int y);
+
     /** Print Character
     *
     *   Sends a character to the display.  Will be printed at the current address.
@@ -150,6 +163,8 @@
     */
     void printChar(char c);
     
+    void printNegChar(char c);
+
     /** Set a Pixel
     *
     *   This function sets a pixel in the display. A call to refresh() must be made
@@ -166,6 +181,21 @@
     *   @param  x - the x co-ordinate of the pixel (0 to 83)
     *   @param  y - the y co-ordinate of the pixel (0 to 47) 
     */
+
+    void drawHline(int x, int y, int l);
+
+    void drawVline(int x, int y, int l);
+
+    void drawRectangle(int x, int y, int w, int h);
+
+    void drawGrid(int stepx, int stepy);
+
+    void drawLine(int x0, int y0, int x1, int y1);
+
+    void drawLineAngle(int x0, int y0, int l, float angle);
+
+    void drawCircle(int x, int y, int radius, int divisions);
+
     void clearPixel(int x, int y);
     
     /** Get a Pixel
@@ -195,7 +225,7 @@
     void randomiseBuffer();
 
 private:
-    void initSPI();
+
     void turnOn();
     void reset();
     void clearRAM();
@@ -205,6 +235,8 @@
 
 public:
     unsigned char buffer[84][6];  // screen buffer - the 6 is for the banks - each one is 8 bits;
+    int W;
+    int H;
 
 private:  // private variables
     SPI*    spi;
@@ -315,4 +347,103 @@
     0x08, 0x1C, 0x2A, 0x08, 0x08 // <-
 };
 
-#endif
\ No newline at end of file
+const unsigned char font6x7[576] = {
+		0x00, 0x00, 0x00, 0x00, 0x00, 0x00,      // Code for char space
+		0x00, 0x2E, 0x00, 0x00, 0x00, 0x00,      // Code for char !
+		0x06, 0x00, 0x06, 0x00, 0x00, 0x00,      // Code for char "
+		0x14, 0x3E, 0x14, 0x3E, 0x14, 0x00,      // Code for char #
+		0x2E, 0x2A, 0x3E, 0x2A, 0x3A, 0x00,      // Code for char $
+		0x26, 0x16, 0x08, 0x34, 0x32, 0x00,      // Code for char %
+		0x36, 0x2A, 0x2A, 0x36, 0x10, 0x28,      // Code for char &
+		0x06, 0x00, 0x00, 0x00, 0x00, 0x00,      // Code for char '
+		0x1C, 0x22, 0x00, 0x00, 0x00, 0x00,      // Code for char (
+		0x22, 0x1C, 0x00, 0x00, 0x00, 0x00,      // Code for char )
+		0x0A, 0x04, 0x0A, 0x00, 0x00, 0x00,      // Code for char *
+		0x08, 0x08, 0x3E, 0x08, 0x08, 0x00,      // Code for char +
+		0x30, 0x00, 0x00, 0x00, 0x00, 0x00,      // Code for char ,
+		0x08, 0x08, 0x08, 0x08, 0x08, 0x00,      // Code for char -
+		0x20, 0x00, 0x00, 0x00, 0x00, 0x00,      // Code for char .
+		0x20, 0x10, 0x08, 0x04, 0x02, 0x00,      // Code for char /
+		0x1C, 0x22, 0x22, 0x22, 0x1C, 0x00,      // Code for char 0
+		0x22, 0x3E, 0x20, 0x00, 0x00, 0x00,      // Code for char 1
+		0x3A, 0x2A, 0x2A, 0x2A, 0x2E, 0x00,      // Code for char 2
+		0x2A, 0x2A, 0x2A, 0x2A, 0x3E, 0x00,      // Code for char 3
+		0x18, 0x14, 0x12, 0x3E, 0x10, 0x00,      // Code for char 4
+		0x2E, 0x2A, 0x2A, 0x2A, 0x3A, 0x00,      // Code for char 5
+		0x3E, 0x2A, 0x2A, 0x2A, 0x3A, 0x00,      // Code for char 6
+		0x02, 0x22, 0x12, 0x0A, 0x06, 0x00,      // Code for char 7
+		0x3E, 0x2A, 0x2A, 0x2A, 0x3E, 0x00,      // Code for char 8
+		0x2E, 0x2A, 0x2A, 0x2A, 0x3E, 0x00,      // Code for char 9
+		0x14, 0x00, 0x00, 0x00, 0x00, 0x00,      // Code for char :
+		0x34, 0x00, 0x00, 0x00, 0x00, 0x00,      // Code for char ;
+		0x08, 0x14, 0x22, 0x00, 0x00, 0x00,      // Code for char <
+		0x14, 0x14, 0x14, 0x14, 0x14, 0x00,      // Code for char =
+		0x22, 0x14, 0x08, 0x00, 0x00, 0x00,      // Code for char >
+		0x02, 0x02, 0x2A, 0x0A, 0x0E, 0x00,      // Code for char ?
+		0x3E, 0x22, 0x3A, 0x2A, 0x3E, 0x00,      // Code for char @
+		0x3E, 0x0A, 0x0A, 0x0A, 0x3E, 0x00,      // Code for char A
+		0x3E, 0x2A, 0x2A, 0x2A, 0x1C, 0x00,      // Code for char B
+		0x3E, 0x22, 0x22, 0x22, 0x22, 0x00,      // Code for char C
+		0x3E, 0x22, 0x22, 0x22, 0x1C, 0x00,      // Code for char D
+		0x3E, 0x2A, 0x2A, 0x2A, 0x2A, 0x00,      // Code for char E
+		0x3E, 0x0A, 0x0A, 0x0A, 0x0A, 0x00,      // Code for char F
+		0x3E, 0x22, 0x2A, 0x2A, 0x3A, 0x00,      // Code for char G
+		0x3E, 0x08, 0x08, 0x08, 0x3E, 0x00,      // Code for char H
+		0x22, 0x3E, 0x22, 0x00, 0x00, 0x00,      // Code for char I
+		0x10, 0x20, 0x22, 0x22, 0x1E, 0x00,      // Code for char J
+		0x3E, 0x08, 0x08, 0x14, 0x22, 0x00,      // Code for char K
+		0x3E, 0x20, 0x20, 0x20, 0x20, 0x00,      // Code for char L
+		0x3E, 0x04, 0x08, 0x04, 0x3E, 0x00,      // Code for char M
+		0x3E, 0x04, 0x08, 0x10, 0x3E, 0x00,      // Code for char N
+		0x3E, 0x22, 0x22, 0x22, 0x3E, 0x00,      // Code for char O
+		0x3E, 0x0A, 0x0A, 0x0A, 0x0E, 0x00,      // Code for char P
+		0x3E, 0x22, 0x22, 0x32, 0x3E, 0x00,      // Code for char Q
+		0x3E, 0x0A, 0x0A, 0x3A, 0x2E, 0x00,      // Code for char R
+		0x2E, 0x2A, 0x2A, 0x2A, 0x3A, 0x00,      // Code for char S
+		0x02, 0x02, 0x3E, 0x02, 0x02, 0x00,      // Code for char T
+		0x3E, 0x20, 0x20, 0x20, 0x3E, 0x00,      // Code for char U
+		0x0E, 0x10, 0x20, 0x10, 0x0E, 0x00,      // Code for char V
+		0x1E, 0x20, 0x1E, 0x20, 0x1E, 0x00,      // Code for char W
+		0x22, 0x14, 0x08, 0x14, 0x22, 0x00,      // Code for char X
+		0x06, 0x08, 0x30, 0x08, 0x06, 0x00,      // Code for char Y
+		0x22, 0x32, 0x2A, 0x26, 0x22, 0x00,      // Code for char Z
+		0x3E, 0x22, 0x00, 0x00, 0x00, 0x00,      // Code for char [
+		0x02, 0x04, 0x08, 0x10, 0x20, 0x00,      // Code for char BackSlash
+		0x22, 0x3E, 0x00, 0x00, 0x00, 0x00,      // Code for char ]
+		0x04, 0x02, 0x04, 0x00, 0x00, 0x00,      // Code for char ^
+		0x20, 0x20, 0x20, 0x20, 0x20, 0x00,      // Code for char _
+		0x02, 0x04, 0x00, 0x00, 0x00, 0x00,      // Code for char `
+		0x3E, 0x0A, 0x0A, 0x0A, 0x3E, 0x00,      // Code for char a
+		0x3E, 0x2A, 0x2A, 0x2A, 0x1C, 0x00,      // Code for char b
+		0x3E, 0x22, 0x22, 0x22, 0x22, 0x00,      // Code for char c
+		0x3E, 0x22, 0x22, 0x22, 0x1C, 0x00,      // Code for char d
+		0x3E, 0x2A, 0x2A, 0x2A, 0x2A, 0x00,      // Code for char e
+		0x3E, 0x0A, 0x0A, 0x0A, 0x0A, 0x00,      // Code for char f
+		0x3E, 0x22, 0x2A, 0x2A, 0x3A, 0x00,      // Code for char g
+		0x3E, 0x08, 0x08, 0x08, 0x3E, 0x00,      // Code for char h
+		0x22, 0x3E, 0x22, 0x00, 0x00, 0x00,      // Code for char i
+		0x10, 0x20, 0x22, 0x22, 0x1E, 0x00,      // Code for char j
+		0x3E, 0x08, 0x08, 0x14, 0x22, 0x00,      // Code for char k
+		0x3E, 0x20, 0x20, 0x20, 0x20, 0x00,      // Code for char l
+		0x3E, 0x04, 0x08, 0x04, 0x3E, 0x00,      // Code for char m
+		0x3E, 0x04, 0x08, 0x10, 0x3E, 0x00,      // Code for char n
+		0x3E, 0x22, 0x22, 0x22, 0x3E, 0x00,      // Code for char o
+		0x3E, 0x0A, 0x0A, 0x0A, 0x0E, 0x00,      // Code for char p
+		0x3E, 0x22, 0x22, 0x32, 0x3E, 0x00,      // Code for char q
+		0x3E, 0x0A, 0x0A, 0x3A, 0x2E, 0x00,      // Code for char r
+		0x2E, 0x2A, 0x2A, 0x2A, 0x3A, 0x00,      // Code for char s
+		0x02, 0x02, 0x3E, 0x02, 0x02, 0x00,      // Code for char t
+		0x3E, 0x20, 0x20, 0x20, 0x3E, 0x00,      // Code for char u
+		0x0E, 0x10, 0x20, 0x10, 0x0E, 0x00,      // Code for char v
+		0x1E, 0x20, 0x1E, 0x20, 0x1E, 0x00,      // Code for char w
+		0x22, 0x14, 0x08, 0x14, 0x22, 0x00,      // Code for char x
+		0x06, 0x08, 0x30, 0x08, 0x06, 0x00,      // Code for char y
+		0x22, 0x32, 0x2A, 0x26, 0x22, 0x00,      // Code for char z
+		0x08, 0x36, 0x22, 0x00, 0x00, 0x00,      // Code for char {
+		0x3E, 0x00, 0x00, 0x00, 0x00, 0x00,      // Code for char |
+		0x22, 0x36, 0x08, 0x00, 0x00, 0x00,      // Code for char }
+		0x04, 0x02, 0x04, 0x02, 0x00, 0x00,      // Code for char ~
+		0x3F, 0x21, 0x3F, 0x00, 0x00, 0x00       // Code for char
+};
+
+#endif
Binary file N5110.o has changed