Emir Sokic / N5110SPI

Dependents:   Nokia_FRDM_v2

Fork of N5110 by Craig Evans

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers N5110.cpp Source File

N5110.cpp

Go to the documentation of this file.
00001 /**
00002 @file N5110.cpp
00003 
00004 @brief Member functions implementations
00005 
00006 */
00007 #include "mbed.h"
00008 #include "N5110.h"
00009 #include "SWSPI.h"
00010 
00011 
00012 N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin)
00013 {
00014 
00015     spi = new SWSPI(mosiPin,PTC0,sclkPin); // create new SPI instance and initialise
00016     initSPI();
00017 
00018     // set up pins as required
00019     led = new PwmOut(ledPin);
00020     pwr = new DigitalOut(pwrPin);
00021     sce = new DigitalOut(scePin);
00022     rst = new DigitalOut(rstPin);
00023     dc = new DigitalOut(dcPin);
00024 
00025 }
00026 
00027 // initialise function - powers up and sends the initialisation commands
00028 void N5110::init()
00029 {
00030     turnOn();     // power up
00031     wait_ms(10);  // small delay seems to prevent spurious pixels during mbed reset
00032     reset();      // reset LCD - must be done within 100 ms
00033 
00034     // function set - extended
00035     sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
00036     // Don't completely understand these parameters - they seem to work as they are
00037     // Consult the datasheet if you need to change them
00038     sendCommand(CMD_VOP_7V38);    // operating voltage - these values are from Chris Yan's Library
00039     sendCommand(CMD_TC_TEMP_2);   // temperature control
00040     sendCommand(CMD_BI_MUX_48);   // bias
00041 
00042     // function set - basic
00043     sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
00044     normalMode();  // normal video mode by default
00045     sendCommand(CMD_DC_NORMAL_MODE);  // black on white
00046 
00047     // RAM is undefined at power-up so clear
00048     clearRAM();
00049 
00050 }
00051 
00052 // sets normal video mode (black on white)
00053 void N5110::normalMode()
00054 {
00055     sendCommand(CMD_DC_NORMAL_MODE);
00056 
00057 }
00058 
00059 // sets normal video mode (white on black)
00060 void N5110::inverseMode()
00061 {
00062     sendCommand(CMD_DC_INVERT_VIDEO);
00063 }
00064 
00065 // function to power up the LCD and backlight
00066 void N5110::turnOn()
00067 {
00068     // set brightness of LED - 0.0 to 1.0 - default is 50%
00069     setBrightness(0.5);
00070     pwr->write(1);  // apply power
00071 }
00072 
00073 // function to power down LCD
00074 void N5110::turnOff()
00075 {
00076     setBrightness(0.0);  // turn backlight off
00077     clearRAM();   // clear RAM to ensure specified current consumption
00078     // send command to ensure we are in basic mode
00079     sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
00080     // clear the display
00081     sendCommand(CMD_DC_CLEAR_DISPLAY);
00082     // enter the extended mode and power down
00083     sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
00084     // small delay and then turn off the power pin
00085     wait_ms(10);
00086     pwr->write(0);
00087 
00088 }
00089 
00090 // function to change LED backlight brightness
00091 void N5110::setBrightness(float brightness)
00092 {
00093     // check whether brightness is within range
00094     if (brightness < 0.0)
00095         brightness = 0.0;
00096     if (brightness > 1.0)
00097         brightness = 1.0;
00098     // set PWM duty cycle
00099     led->write(brightness);
00100 }
00101 
00102 
00103 // pulse the active low reset line
00104 void N5110::reset()
00105 {
00106     rst->write(0);  // reset the LCD
00107     rst->write(1);
00108 }
00109 
00110 // function to initialise SPI peripheral
00111 void N5110::initSPI()
00112 {
00113     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
00114     spi->frequency(4000000);  // maximum of screen is 4 MHz
00115 }
00116 
00117 // send a command to the display
00118 void N5110::sendCommand(unsigned char command)
00119 {
00120     dc->write(0);  // set DC low for command
00121     sce->write(0); // set CE low to begin frame
00122     spi->write(command);  // send command
00123     dc->write(1);  // turn back to data by default
00124     sce->write(1); // set CE high to end frame (expected for transmission of single byte)
00125 
00126 }
00127 
00128 // send data to the display at the current XY address
00129 // dc is set to 1 (i.e. data) after sending a command and so should
00130 // be the default mode.
00131 void N5110::sendData(unsigned char data)
00132 {
00133     sce->write(0);   // set CE low to begin frame
00134     spi->write(data);
00135     sce->write(1);  // set CE high to end frame (expected for transmission of single byte)
00136 }
00137 
00138 // this function writes 0 to the 504 bytes to clear the RAM
00139 void N5110::clearRAM()
00140 {
00141     int i;
00142     sce->write(0);  //set CE low to begin frame
00143     for(i = 0; i < WIDTH * HEIGHT; i++) { // 48 x 84 bits = 504 bytes
00144         spi->write(0x00);  // send 0's
00145     }
00146     sce->write(1); // set CE high to end frame
00147 
00148 }
00149 
00150 // function to set the XY address in RAM for subsequenct data write
00151 void N5110::setXYAddress(int x, int y)
00152 {
00153     if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
00154         sendCommand(0x80 | x);  // send addresses to display with relevant mask
00155         sendCommand(0x40 | y);
00156     }
00157 }
00158 
00159 // These functions are used to set, clear and get the value of pixels in the display
00160 // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x).  The refresh()
00161 // function must be called after set and clear in order to update the display
00162 void N5110::setPixel(int x, int y)
00163 {
00164     if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
00165         // calculate bank and shift 1 to required position in the data byte
00166         buffer[x][y/8] |= (1 << y%8);
00167     }
00168 }
00169 
00170 void N5110::clearPixel(int x, int y)
00171 {
00172     if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
00173         // calculate bank and shift 1 to required position (using bit clear)
00174         buffer[x][y/8] &= ~(1 << y%8);
00175     }
00176 }
00177 
00178 int N5110::getPixel(int x, int y)
00179 {
00180     if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) {  // check within range
00181         // return relevant bank and mask required bit
00182         return (int) buffer[x][y/8] & (1 << y%8);
00183         // note this does not necessarily return 1 - a non-zero number represents a pixel
00184     } else {
00185         return 0;
00186     }
00187 }
00188 
00189 // function to refresh the display
00190 void N5110::refresh()
00191 {
00192     int i,j;
00193 
00194     setXYAddress(0,0);  // important to set address back to 0,0 before refreshing display
00195     // address auto increments after printing string, so buffer[0][0] will not coincide
00196     // with top-left pixel after priting string
00197 
00198     sce->write(0);  //set CE low to begin frame
00199 
00200     for(j = 0; j < BANKS; j++) {  // be careful to use correct order (j,i) for horizontal addressing
00201         for(i = 0; i < WIDTH; i++) {
00202             spi->write(buffer[i][j]);  // send buffer
00203         }
00204     }
00205     sce->write(1); // set CE high to end frame
00206 
00207 }
00208 
00209 // fills the buffer with random bytes.  Can be used to test the display.
00210 // The rand() function isn't seeded so it probably creates the same pattern everytime
00211 void N5110::randomiseBuffer()
00212 {
00213     int i,j;
00214     for(j = 0; j < BANKS; j++) {  // be careful to use correct order (j,i) for horizontal addressing
00215         for(i = 0; i < WIDTH; i++) {
00216             buffer[i][j] = rand()%256;  // generate random byte
00217         }
00218     }
00219 
00220 }
00221 
00222 // function to print 5x7 font
00223 void N5110::printChar(char c,int x,int y)
00224 {
00225     if (y>=0 && y<BANKS) {  // check if printing in range of y banks
00226 
00227         for (int i = 0; i < 5 ; i++ ) {
00228             int pixel_x = x+i;
00229             if (pixel_x > WIDTH-1)  // ensure pixel isn't outside the buffer size (0 - 83)
00230                 break;
00231             buffer[pixel_x][y] = font5x7[(c - 32)*5 + i];
00232             // array is offset by 32 relative to ASCII, each character is 5 pixels wide
00233         }
00234 
00235         refresh();  // this sends the buffer to the display and sets address (cursor) back to 0,0
00236     }
00237 }
00238 
00239 // function to print string at specified position
00240 void N5110::printString(const char * str,int x,int y)
00241 {
00242     if (y>=0 && y<BANKS) {  // check if printing in range of y banks
00243 
00244         int n = 0 ; // counter for number of characters in string
00245         // loop through string and print character
00246         while(*str) {
00247 
00248             // writes the character bitmap data to the buffer, so that
00249             // text and pixels can be displayed at the same time
00250             for (int i = 0; i < 5 ; i++ ) {
00251                 int pixel_x = x+i+n*6;
00252                 if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83)
00253                     break;
00254                 buffer[pixel_x][y] = font5x7[(*str - 32)*5 + i];
00255             }
00256 
00257             str++;  // go to next character in string
00258 
00259             n++;    // increment index
00260 
00261         }
00262 
00263         refresh();  // this sends the buffer to the display and sets address (cursor) back to 0,0
00264     }
00265 }
00266 
00267 // function to clear the screen
00268 void N5110::clear()
00269 {
00270     clearBuffer();  // clear the buffer then call the refresh function
00271     refresh();
00272 }
00273 
00274 // function to clear the buffer
00275 void N5110::clearBuffer()
00276 {
00277     int i,j;
00278     for (i=0; i<WIDTH; i++) {  // loop through the banks and set the buffer to 0
00279         for (j=0; j<BANKS; j++) {
00280             buffer[i][j]=0;
00281         }
00282     }
00283 }
00284 
00285 // function to plot array on display
00286 void N5110::plotArray(float array[])
00287 {
00288 
00289     int i;
00290 
00291     for (i=0; i<WIDTH; i++) {  // loop through array
00292         // elements are normalised from 0.0 to 1.0, so multiply
00293         // by 47 to convert to pixel range, and subtract from 47
00294         // since top-left is 0,0 in the display geometry
00295         setPixel(i,47 - int(array[i]*47.0));
00296     }
00297 
00298     refresh();
00299 
00300 }
00301 
00302 // function to draw circle
00303 void N5110:: drawCircle(int x0,int y0,int radius,int fill)
00304 {
00305     // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
00306     int x = radius;
00307     int y = 0;
00308     int radiusError = 1-x;
00309 
00310     while(x >= y) {
00311 
00312         // if transparent, just draw outline
00313         if (fill == 0) {
00314             setPixel( x + x0,  y + y0);
00315             setPixel(-x + x0,  y + y0);
00316             setPixel( y + x0,  x + y0);
00317             setPixel(-y + x0,  x + y0);
00318             setPixel(-y + x0, -x + y0);
00319             setPixel( y + x0, -x + y0);
00320             setPixel( x + x0, -y + y0);
00321             setPixel(-x + x0, -y + y0);
00322         } else {  // drawing filled circle, so draw lines between points at same y value
00323 
00324             int type = (fill==1) ? 1:0;  // black or white fill
00325 
00326             drawLine(x+x0,y+y0,-x+x0,y+y0,type);
00327             drawLine(y+x0,x+y0,-y+x0,x+y0,type);
00328             drawLine(y+x0,-x+y0,-y+x0,-x+y0,type);
00329             drawLine(x+x0,-y+y0,-x+x0,-y+y0,type);
00330         }
00331 
00332 
00333         y++;
00334         if (radiusError<0) {
00335             radiusError += 2 * y + 1;
00336         } else {
00337             x--;
00338             radiusError += 2 * (y - x) + 1;
00339         }
00340     }
00341 
00342 }
00343 
00344 void N5110::drawLine(int x0,int y0,int x1,int y1,int type)
00345 {
00346     int y_range = y1-y0;  // calc range of y and x
00347     int x_range = x1-x0;
00348     int start,stop,step;
00349 
00350     // if dotted line, set step to 2, else step is 1
00351     step = (type==2) ? 2:1;
00352 
00353     // make sure we loop over the largest range to get the most pixels on the display
00354     // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
00355     // or else we'll only end up with 1 pixel in the x column
00356     if ( abs(x_range) > abs(y_range) ) {
00357 
00358         // ensure we loop from smallest to largest or else for-loop won't run as expected
00359         start = x1>x0 ? x0:x1;
00360         stop =  x1>x0 ? x1:x0;
00361 
00362         // loop between x pixels
00363         for (int x = start; x<= stop ; x+=step) {
00364             // do linear interpolation
00365             int y = y0 + (y1-y0)*(x-x0)/(x1-x0);
00366 
00367             if (type == 0)   // if 'white' line, turn off pixel
00368                 clearPixel(x,y);
00369             else
00370                 setPixel(x,y);  // else if 'black' or 'dotted' turn on pixel
00371         }
00372     } else {
00373 
00374         // ensure we loop from smallest to largest or else for-loop won't run as expected
00375         start = y1>y0 ? y0:y1;
00376         stop =  y1>y0 ? y1:y0;
00377 
00378         for (int y = start; y<= stop ; y+=step) {
00379             // do linear interpolation
00380             int x = x0 + (x1-x0)*(y-y0)/(y1-y0);
00381 
00382             if (type == 0)   // if 'white' line, turn off pixel
00383                 clearPixel(x,y);
00384             else
00385                 setPixel(x,y);  // else if 'black' or 'dotted' turn on pixel
00386 
00387         }
00388     }
00389 
00390 }
00391 
00392 void N5110::drawRect(int x0,int y0,int width,int height,int fill)
00393 {
00394 
00395     if (fill == 0) { // transparent, just outline
00396         drawLine(x0,y0,x0+width,y0,1);  // top
00397         drawLine(x0,y0+height,x0+width,y0+height,1);  // bottom
00398         drawLine(x0,y0,x0,y0+height,1);  // left
00399         drawLine(x0+width,y0,x0+width,y0+height,1);  // right
00400     } else { // filled rectangle
00401         int type = (fill==1) ? 1:0;  // black or white fill
00402         for (int y = y0; y<= y0+height; y++) {  // loop through rows of rectangle
00403             drawLine(x0,y,x0+width,y,type);  // draw line across screen
00404         }
00405     }
00406 
00407 }