(DA) Internet of Things and Smart Electronics- ELE3006M2122 / Mbed 2 deprecated Final_Project_V15_DLeaming_25574043

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers N5110.cpp Source File

N5110.cpp

00001 /* * Print String
00002 *
00003 * Prints a string of characters to the screen buffer, string is cut off after the 83rd  pixel.
00004 * @param x - the column number (0 to 83)
00005 * @param y - the row number (0-5) - the display is split into 6 banks - each bank can be considered a row
00006 * @author - David Leaming - 25574043
00007 * @ Date - December 2021
00008 *
00009 * Acknowledgements 
00010 * Craig A. Evans, University of Leeds, TMP102 Library ,Feb 2016
00011 * Dr Edmond Nurellari, University of Lincoln, Joystick & N5110 Libraries
00012 *
00013 */ 
00014 
00015 #include "mbed.h"
00016 #include "N5110.h"
00017 
00018 // overloaded constructor includes power pin - LCD Vcc connected to GPIO pin
00019 // this constructor works fine with LPC1768 - enough current sourced from GPIO
00020 // to power LCD. Doesn't work well with K64F.
00021 N5110::N5110(PinName const pwrPin,
00022              PinName const scePin,
00023              PinName const rstPin,
00024              PinName const dcPin,
00025              PinName const mosiPin,
00026              PinName const sclkPin,
00027              PinName const ledPin)
00028     :
00029     _spi(new SPI(mosiPin,NC,sclkPin)), // create new SPI instance and initialise
00030     _led(new PwmOut(ledPin)),
00031     _pwr(new DigitalOut(pwrPin)),
00032     _sce(new DigitalOut(scePin)),
00033     _rst(new DigitalOut(rstPin)),
00034     _dc(new DigitalOut(dcPin))
00035 {}
00036 
00037 // overloaded constructor does not include power pin - LCD Vcc must be tied to +3V3
00038 // Best to use this with K64F as the GPIO hasn't sufficient output current to reliably
00039 // drive the LCD.
00040 N5110::N5110(PinName const scePin,
00041              PinName const rstPin,
00042              PinName const dcPin,
00043              PinName const mosiPin,
00044              PinName const sclkPin,
00045              PinName const ledPin)
00046     :
00047     _spi(new SPI(mosiPin,NC,sclkPin)), // create new SPI instance and initialise
00048     _led(new PwmOut(ledPin)),
00049     _pwr(NULL), // pwr not needed so null it to be safe
00050     _sce(new DigitalOut(scePin)),
00051     _rst(new DigitalOut(rstPin)),
00052     _dc(new DigitalOut(dcPin))
00053 {}
00054 
00055 N5110::~N5110()
00056 {
00057     delete _spi;
00058 
00059     if(_pwr) {
00060         delete _pwr;
00061     }
00062 
00063     delete _led;
00064     delete _sce;
00065     delete _rst;
00066     delete _dc;
00067 }
00068 
00069 // initialise function - powers up and sends the initialisation commands
00070 void N5110::init()
00071 {
00072     turnOn();     // power up
00073       wait_ms(10);  // small delay seems to prevent spurious pixels during mbed reset
00074     reset();      // reset LCD - must be done within 100 ms
00075    // initSPI();    
00076     
00077     setContrast(0.55);  // this may need tuning (say 0.4 to 0.6)
00078     setBias(3);   // datasheet - 48:1 mux - don't mess with if you don't know what you're doing! (0 to 7)
00079     setTempCoefficient(0); // datasheet - may need increasing (range 0 to 3) at very low temperatures
00080     normalMode();  // normal video mode by default
00081     
00082     clearRAM();      // RAM is undefined at power-up so clear to be sure
00083     clear();   // clear buffer
00084     setBrightness(0.5);
00085 }
00086 
00087 // sets normal video mode (black on white)
00088 void N5110::normalMode()
00089 {
00090     sendCommand(0b00100000);   // basic instruction
00091     sendCommand(0b00001100);  // normal video mode- datasheet
00092 }
00093 
00094 // sets normal video mode (white on black)
00095 void N5110::inverseMode()
00096 {
00097     sendCommand(0b00100000);   // basic instruction
00098     sendCommand(0b00001101);   // inverse video mode - datasheet
00099 }
00100 
00101 // function to power up the LCD and backlight - only works when using GPIO to power
00102 void N5110::turnOn()
00103 {
00104     if (_pwr != NULL) {
00105         _pwr->write(1);  // apply power
00106     }
00107 }
00108 
00109 // function to power down LCD
00110 void N5110::turnOff()
00111 {
00112     clear(); // clear buffer
00113     refresh();
00114     setBrightness(0.0);  // turn backlight off
00115     clearRAM();   // clear RAM to ensure specified current consumption
00116     // send command to ensure we are in basic mode
00117     
00118     sendCommand(0b00100000); // basic mode
00119     sendCommand(0b00001000); // clear display
00120     sendCommand(0b00100001); // extended mode
00121     sendCommand(0b00100100); // power down
00122     
00123     // if we are powering the LCD using the GPIO then make it low to turn off
00124     if (_pwr != NULL) {
00125         wait_ms(10);  // small delay and then turn off the power pin
00126         _pwr->write(0);  // turn off power
00127     }
00128 
00129 }
00130 
00131 // function to change LED backlight brightness
00132 void N5110::setBrightness(float brightness)
00133 {
00134     // check whether brightness is within range
00135     if (brightness < 0.0f)
00136         brightness = 0.0f;
00137     if (brightness > 1.0f)
00138         brightness = 1.0f;
00139     // set PWM duty cycle
00140     _led->write(brightness);
00141 }
00142 
00143 void N5110::setContrast(float contrast) {
00144     
00145     // enforce limits
00146     if (contrast > 1.0f)
00147         contrast = 1.0f;
00148     else if (contrast < 0.0f)
00149         contrast = 0.0;
00150     
00151     // convert to char in range 0 to 127 (i.e. 6 bits)
00152     char ic = char(contrast*127.0f);
00153     
00154     sendCommand(0b00100001);  // extended instruction set
00155     sendCommand(0b10000000 | ic);   // set Vop (which controls contrast)
00156     sendCommand(0b00100000);  // back to basic instruction set
00157 }
00158 
00159 void N5110::setTempCoefficient(char tc) {
00160     
00161     // enforce limits
00162     if (tc>3) {
00163         tc=3;
00164     }
00165     
00166     // temperature coefficient may need increasing at low temperatures
00167 
00168     sendCommand(0b00100001);  // extended instruction set
00169     sendCommand(0b00000100 | tc);
00170     sendCommand(0b00100000);  // back to basic instruction set
00171 }
00172     
00173 void N5110::setBias(char bias) {
00174     
00175     // from data sheet
00176     // bias      mux rate
00177     // 0        1:100
00178     // 1        1:80
00179     // 2        1:65
00180     // 3        1:48   (default)
00181     // 4        1:40/1:34
00182     // 5        1:24
00183     // 6        1:18/1:16
00184     // 7        1:10/1:9/1:8
00185     
00186     // enforce limits
00187     if (bias>7) {
00188         bias=7;
00189     }
00190         
00191     sendCommand(0b00100001);  // extended mode instruction
00192     sendCommand(0b00010000 | bias);  
00193     sendCommand(0b00100000); // end of extended mode instruction
00194 }
00195 
00196 // pulse the active low reset line
00197 void N5110::reset()
00198 {
00199     _rst->write(0);  // reset the LCD
00200     _rst->write(1);
00201 }
00202 
00203 // function to initialise SPI peripheral
00204 void N5110::initSPI()
00205 {
00206     _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
00207     _spi->frequency(4000000);  // maximum of screen is 4 MHz
00208 }
00209 
00210 // send a command to the display
00211 void N5110::sendCommand(unsigned char command)
00212 {
00213     _dc->write(0);  // set DC low for command
00214     _sce->write(0); // set CE low to begin frame
00215     _spi->write(command);  // send command
00216     _dc->write(1);  // turn back to data by default
00217     _sce->write(1); // set CE high to end frame (expected for transmission of single byte)
00218 }
00219 
00220 // send data to the display at the current XY address
00221 // dc is set to 1 (i.e. data) after sending a command and so should
00222 // be the default mode.
00223 void N5110::sendData(unsigned char data)
00224 {
00225     _sce->write(0);   // set CE low to begin frame
00226     _spi->write(data);
00227     _sce->write(1);  // set CE high to end frame (expected for transmission of single byte)
00228 }
00229 
00230 // this function writes 0 to the 504 bytes to clear the RAM
00231 void N5110::clearRAM()
00232 {
00233     _sce->write(0);  //set CE low to begin frame
00234     for(int i = 0; i < WIDTH * HEIGHT; i++) { // 48 x 84 bits = 504 bytes
00235         _spi->write(0x00);  // send 0's
00236     }
00237     _sce->write(1); // set CE high to end frame
00238 }
00239 
00240 // function to set the XY address in RAM for subsequenct data write
00241 void N5110::setXYAddress(unsigned int const x,
00242                          unsigned int const y)
00243 {
00244     if (x<WIDTH && y<HEIGHT) {  // check within range
00245         sendCommand(0b00100000);  // basic instruction
00246         sendCommand(0b10000000 | x);  // send addresses to display with relevant mask
00247         sendCommand(0b01000000 | y);
00248     }
00249 }
00250 
00251 // These functions are used to set, clear and get the value of pixels in the display
00252 // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x).  The refresh()
00253 // function must be called after set and clear in order to update the display
00254 void N5110::setPixel(unsigned int const x,
00255                      unsigned int const y,
00256                      bool const         state)
00257 {
00258     if (x<WIDTH && y<HEIGHT) {  // check within range
00259         // calculate bank and shift 1 to required position in the data byte
00260         if(state) buffer[x][y/8] |= (1 << y%8);
00261         else      buffer[x][y/8] &= ~(1 << y%8);
00262     }
00263 }
00264 
00265 void N5110::clearPixel(unsigned int const x,
00266                        unsigned int const y)
00267 {
00268     if (x<WIDTH && y<HEIGHT) {  // check within range
00269         // calculate bank and shift 1 to required position (using bit clear)
00270         buffer[x][y/8] &= ~(1 << y%8);
00271     }
00272 }
00273 
00274 int N5110::getPixel(unsigned int const x,
00275                     unsigned int const y) const
00276 {
00277     if (x<WIDTH && y<HEIGHT) {  // check within range
00278         // return relevant bank and mask required bit
00279 
00280         int pixel = (int) buffer[x][y/8] & (1 << y%8);
00281 
00282         if (pixel)
00283             return 1;
00284         else
00285             return 0;
00286     }
00287 
00288     return 0;
00289 
00290 }
00291 
00292 // function to refresh the display
00293 void N5110::refresh()
00294 {
00295     setXYAddress(0,0);  // important to set address back to 0,0 before refreshing display
00296     // address auto increments after printing string, so buffer[0][0] will not coincide
00297     // with top-left pixel after priting string
00298 
00299     _sce->write(0);  //set CE low to begin frame
00300 
00301     for(int j = 0; j < BANKS; j++) {  // be careful to use correct order (j,i) for horizontal addressing
00302         for(int i = 0; i < WIDTH; i++) {
00303             _spi->write(buffer[i][j]);  // send buffer
00304         }
00305     }
00306     _sce->write(1); // set CE high to end frame
00307 
00308 }
00309 
00310 // fills the buffer with random bytes.  Can be used to test the display.
00311 // The rand() function isn't seeded so it probably creates the same pattern everytime
00312 void N5110::randomiseBuffer()
00313 {
00314     int i,j;
00315     for(j = 0; j < BANKS; j++) {  // be careful to use correct order (j,i) for horizontal addressing
00316         for(i = 0; i < WIDTH; i++) {
00317             buffer[i][j] = rand()%256;  // generate random byte
00318         }
00319     }
00320 
00321 }
00322 
00323 // function to print 5x7 font
00324 void N5110::printChar(char const          c,
00325                       unsigned int const  x,
00326                       unsigned int const  y)
00327 {
00328     if (y<BANKS) {  // check if printing in range of y banks
00329 
00330         for (int i = 0; i < 5 ; i++ ) {
00331             int pixel_x = x+i;
00332             if (pixel_x > WIDTH-1)  // ensure pixel isn't outside the buffer size (0 - 83)
00333                 break;
00334             buffer[pixel_x][y] = font5x7[(c - 32)*5 + i];
00335             // array is offset by 32 relative to ASCII, each character is 5 pixels wide
00336         }
00337 
00338     }
00339 }
00340 
00341 // function to print string at specified position
00342 void N5110::printString(const char         *str,
00343                         unsigned int const  x,
00344                         unsigned int const  y)
00345 {
00346     if (y<BANKS) {  // check if printing in range of y banks
00347 
00348         int n = 0 ; // counter for number of characters in string
00349         // loop through string and print character
00350         while(*str) {
00351 
00352             // writes the character bitmap data to the buffer, so that
00353             // text and pixels can be displayed at the same time
00354             for (int i = 0; i < 5 ; i++ ) {
00355                 int pixel_x = x+i+n*6;
00356                 if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83)
00357                     break;
00358                 buffer[pixel_x][y] = font5x7[(*str - 32)*5 + i];
00359             }
00360             str++;  // go to next character in string
00361             n++;    // increment index
00362         }
00363     }
00364 }
00365 
00366 // function to clear the screen buffer
00367 void N5110::clear()
00368 {
00369     memset(buffer,0,sizeof(buffer));
00370 }
00371 
00372 // function to plot array on display
00373 void N5110::plotArray(float const array[])
00374 {
00375     for (int i=0; i<WIDTH; i++) {  // loop through array
00376         // elements are normalised from 0.0 to 1.0, so multiply
00377         // by 47 to convert to pixel range, and subtract from 47
00378         // since top-left is 0,0 in the display geometry
00379         setPixel(i,47 - int(array[i]*47.0f),true);
00380     }
00381 
00382 }
00383 
00384 // function to draw circle
00385 void N5110:: drawCircle(unsigned int const x0,
00386                         unsigned int const y0,
00387                         unsigned int const radius,
00388                         FillType const     fill)
00389 {
00390     // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
00391     int x = radius;
00392     int y = 0;
00393     int radiusError = 1-x;
00394 
00395     while(x >= y) {
00396 
00397         // if transparent, just draw outline
00398         if (fill == FILL_TRANSPARENT) {
00399             setPixel( x + x0,  y + y0,true);
00400             setPixel(-x + x0,  y + y0,true);
00401             setPixel( y + x0,  x + y0,true);
00402             setPixel(-y + x0,  x + y0,true);
00403             setPixel(-y + x0, -x + y0,true);
00404             setPixel( y + x0, -x + y0,true);
00405             setPixel( x + x0, -y + y0,true);
00406             setPixel(-x + x0, -y + y0,true);
00407         } else {  // drawing filled circle, so draw lines between points at same y value
00408 
00409             int type = (fill==FILL_BLACK) ? 1:0;  // black or white fill
00410 
00411             drawLine(x+x0,y+y0,-x+x0,y+y0,type);
00412             drawLine(y+x0,x+y0,-y+x0,x+y0,type);
00413             drawLine(y+x0,-x+y0,-y+x0,-x+y0,type);
00414             drawLine(x+x0,-y+y0,-x+x0,-y+y0,type);
00415         }
00416 
00417         y++;
00418         if (radiusError<0) {
00419             radiusError += 2 * y + 1;
00420         } else {
00421             x--;
00422             radiusError += 2 * (y - x) + 1;
00423         }
00424     }
00425 
00426 }
00427 
00428 void N5110::drawLine(unsigned int const x0,
00429                      unsigned int const y0,
00430                      unsigned int const x1,
00431                      unsigned int const y1,
00432                      unsigned int const type)
00433 {
00434     // Note that the ranges can be negative so we have to turn the input values
00435     // into signed integers first
00436     int const y_range = static_cast<int>(y1) - static_cast<int>(y0);
00437     int const x_range = static_cast<int>(x1) - static_cast<int>(x0);
00438 
00439     // if dotted line, set step to 2, else step is 1
00440     unsigned int const step = (type==2) ? 2:1;
00441 
00442     // make sure we loop over the largest range to get the most pixels on the display
00443     // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
00444     // or else we'll only end up with 1 pixel in the x column
00445     if ( abs(x_range) > abs(y_range) ) {
00446 
00447         // ensure we loop from smallest to largest or else for-loop won't run as expected
00448         unsigned int const start = x_range > 0 ? x0:x1;
00449         unsigned int const stop =  x_range > 0 ? x1:x0;
00450 
00451         // loop between x pixels
00452         for (unsigned int x = start; x<= stop ; x+=step) {
00453             // do linear interpolation
00454             int const dx = static_cast<int>(x)-static_cast<int>(x0);
00455             unsigned int const y = y0 + y_range * dx / x_range;
00456 
00457             // If the line type is '0', this will clear the pixel
00458             // If it is '1' or '2', the pixel will be set
00459             setPixel(x,y, type);
00460         }
00461     } else {
00462 
00463         // ensure we loop from smallest to largest or else for-loop won't run as expected
00464         unsigned int const start = y_range > 0 ? y0:y1;
00465         unsigned int const stop =  y_range > 0 ? y1:y0;
00466 
00467         for (unsigned int y = start; y<= stop ; y+=step) {
00468             // do linear interpolation
00469             int const dy = static_cast<int>(y)-static_cast<int>(y0);
00470             unsigned int const x = x0 + x_range * dy / y_range;
00471 
00472             // If the line type is '0', this will clear the pixel
00473             // If it is '1' or '2', the pixel will be set
00474             setPixel(x,y, type);
00475         }
00476     }
00477 
00478 }
00479 
00480 void N5110::drawRect(unsigned int const x0,
00481                      unsigned int const y0,
00482                      unsigned int const width,
00483                      unsigned int const height,
00484                      FillType const     fill)
00485 {
00486     if (fill == FILL_TRANSPARENT) { // transparent, just outline
00487         drawLine(x0,y0,x0+(width-1),y0,1);  // top
00488         drawLine(x0,y0+(height-1),x0+(width-1),y0+(height-1),1);  // bottom
00489         drawLine(x0,y0,x0,y0+(height-1),1);  // left
00490         drawLine(x0+(width-1),y0,x0+(width-1),y0+(height-1),1);  // right
00491     } else { // filled rectangle
00492         int type = (fill==FILL_BLACK) ? 1:0;  // black or white fill
00493         for (int y = y0; y<y0+height; y++) {  // loop through rows of rectangle
00494             drawLine(x0,y,x0+(width-1),y,type);  // draw line across screen
00495         }
00496     }
00497 }
00498 
00499 void N5110::drawSprite(int x0,
00500                        int y0,
00501                        int nrows,
00502                        int ncols,
00503                        int *sprite)
00504 {
00505     for (int i = 0; i < nrows; i++) {
00506         for (int j = 0 ; j < ncols ; j++) {
00507 
00508             int pixel = *((sprite+i*ncols)+j);
00509             setPixel(x0+j,y0+i, pixel);
00510         }
00511     }
00512 }