a modified version of N5110 library to be used with freescale boards
Diff: N5110.cpp
- Revision:
- 7:77bd2c73fe41
- Parent:
- 6:adb79338d40f
--- 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 +}