Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
N5110.cpp
00001 /** 00002 @file N5110.cpp 00003 00004 @brief Member functions implementations 00005 00006 */ 00007 #include "mbed.h" 00008 #include "N5110.h" 00009 00010 00011 N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin) 00012 { 00013 00014 spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise 00015 spi->format(LCD_SPI_BITS, LCD_SPI_MODE); 00016 spi->frequency(LCD_FREQ); 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 W = 83; 00025 H = 47; 00026 00027 } 00028 00029 // initialise function - powers up and sends the initialisation commands 00030 void N5110::init() 00031 { 00032 turnOn(); // power up 00033 wait_ms(10); // small delay seems to prevent spurious pixels during mbed reset 00034 reset(); // reset LCD - must be done within 100 ms 00035 00036 // function set - extended 00037 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); 00038 // Don't completely understand these parameters - they seem to work as they are 00039 // Consult the datasheet if you need to change them 00040 sendCommand(CMD_VOP_7V38); // operating voltage - these values are from Chris Yan's Library 00041 sendCommand(CMD_TC_TEMP_2); // temperature control 00042 sendCommand(CMD_BI_MUX_48); // bias 00043 00044 // function set - basic 00045 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); 00046 normalMode(); // normal video mode by default 00047 sendCommand(CMD_DC_NORMAL_MODE); // black on white 00048 00049 // RAM is undefined at power-up so clear 00050 clearRAM(); 00051 00052 } 00053 00054 // sets normal video mode (black on white) 00055 void N5110::normalMode() { 00056 sendCommand(CMD_DC_NORMAL_MODE); 00057 00058 } 00059 00060 // sets normal video mode (white on black) 00061 void N5110::inverseMode() { 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 //led->write(0); // apply power 00071 } 00072 00073 // function to power down LCD 00074 void N5110::turnOff() 00075 { 00076 setBrightness(1.0); // turn backlight off 00077 clearRAM(); // clear RAM to ensure specified current consumption 00078 // send command to ensure we are in basic model 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(1); 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 00111 00112 // send a command to the display 00113 void N5110::sendCommand(unsigned char command) 00114 { 00115 dc->write(0); // set DC low for command 00116 //sce->write(0); // set CE low to begin frame 00117 spi->write(command); // send command 00118 dc->write(1); // turn back to data by default 00119 //sce->write(1); // set CE high to end frame (expected for transmission of single byte) 00120 00121 } 00122 00123 // send data to the display at the current XY address 00124 // dc is set to 1 (i.e. data) after sending a command and so should 00125 // be the default mode. 00126 void N5110::sendData(unsigned char data) 00127 { 00128 //sce->write(0); // set CE low to begin frame 00129 spi->write(data); 00130 //sce->write(1); // set CE high to end frame (expected for transmission of single byte) 00131 } 00132 00133 // this function writes 0 to the 504 bytes to clear the RAM 00134 void N5110::clearRAM() 00135 { 00136 int i; 00137 //sce->write(0); //set CE low to begin frame 00138 for(i = 0; i < 504; i++) { // 48 x 84 bits = 504 bytes 00139 spi->write(0x00); // send 0's 00140 } 00141 //sce->write(1); // set CE high to end frame 00142 00143 } 00144 00145 // function to set the XY address in RAM for subsequenct data write 00146 void N5110::setXYAddress(int x, int y) 00147 { 00148 // check whether address is in range 00149 if (x > 83) 00150 x=83; 00151 if (y > 5) 00152 y=5; 00153 if (x < 0) 00154 x=0; 00155 if (y < 0) 00156 y=0; 00157 00158 sendCommand(0x80 | x); // send addresses to display with relevant mask 00159 sendCommand(0x40 | y); 00160 } 00161 00162 // These functions are used to set, clear and get the value of pixels in the display 00163 // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x). The refresh() 00164 // function must be called after set and clear in order to update the display 00165 void N5110::setPixel(int x, int y) 00166 { 00167 // calculate bank and shift 1 to required position in the data byte 00168 buffer[x][y/8] |= (1 << y%8); 00169 } 00170 00171 void N5110::clearPixel(int x, int y) 00172 { 00173 // calculate bank and shift 1 to required position (using bit clear) 00174 buffer[x][y/8] &= ~(1 << y%8); 00175 } 00176 00177 void N5110::drawHline(int x, int y, int l) 00178 { 00179 for(int i=0; i<l; i++) 00180 { 00181 int ps = x+i; 00182 if ((ps < 84 && ps >= 0) && (y<48 && y>=0)) { 00183 buffer[ps][y/8] |= (1 << y%8); 00184 //refresh(); 00185 } 00186 00187 } 00188 refresh(); 00189 } 00190 00191 void N5110::drawVline(int x, int y, int l) 00192 { 00193 for(int i=0; i<l; i++) 00194 { 00195 int ps = y+i; 00196 if ((ps < 48 && ps >= 0) && (x<84 && x>=0)){ 00197 buffer[x][ps/8] |= (1 << ps%8); 00198 //refresh(); 00199 } 00200 } 00201 refresh(); 00202 } 00203 00204 void N5110::drawRectangle(int x, int y, int w, int h) 00205 { 00206 drawHline(x,y,w); 00207 drawVline(x+w,y,h); 00208 drawHline(x,y+h,w); 00209 drawVline(x,y,h); 00210 } 00211 00212 void N5110::drawGrid(int stepx, int stepy) 00213 { 00214 for (int y=0; y<47; y += stepy){ 00215 for(int x=0; x<84; x++){ 00216 setPixel(x,y); 00217 } 00218 refresh(); 00219 } 00220 for (int x=0; x<84; x += stepx){ 00221 for(int y=0; y<47; y++){ 00222 setPixel(x,y); 00223 } 00224 refresh(); 00225 } 00226 } 00227 00228 // draw line between two points using C implementation of Bresenham's line algorithm 00229 void N5110::drawLine(int x0, int y0, int x1, int y1) { 00230 00231 int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1; 00232 int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1; 00233 int err = (dx>dy ? dx : -dy)/2, e2; 00234 00235 for(;;){ 00236 if ((x0 < 84 && x0 >= 0) && (y0<47 && y0>=0)) { 00237 buffer[x0][y0/8] |= (1 << y0%8); 00238 } 00239 if (x0==x1 && y0==y1) break; 00240 e2 = err; 00241 if (e2 >-dx) { err -= dy; x0 += sx; } 00242 if (e2 < dy) { err += dx; y0 += sy; } 00243 } 00244 refresh(); 00245 } 00246 00247 00248 void N5110::drawLineAngle(int x0, int y0, int l, float angle) { 00249 float radian = 0-angle * (M_PI/180); 00250 int x1 = x0 + (l*cos(radian)); 00251 int y1 = y0 + (l*sin(radian)); 00252 drawLine(x0,y0,x1,y1); 00253 } 00254 00255 00256 void N5110::drawCircle(int x, int y, int radius, int divisions) { 00257 00258 float angleIncrement = M_PI * 2 / divisions; 00259 double tempAngle = 0.0; 00260 00261 for (int i = 0; i < divisions; ++i) { 00262 double px = cos(tempAngle) * radius + x; 00263 double py = sin(tempAngle) * radius + y; 00264 if ((px < 84 && px >= 0) && (py<47 && py>=0)) { 00265 buffer[(int)px][(int)py/8] |= (1 << (int)py%8); 00266 } 00267 tempAngle += angleIncrement; 00268 } 00269 refresh(); 00270 } 00271 00272 unsigned char N5110::getPixel(int x, int y) 00273 { 00274 // return relevant bank and mask required bit 00275 return buffer[x][y/8] & (1 << y%8); 00276 00277 } 00278 00279 // function to refresh the display 00280 void N5110::refresh() 00281 { 00282 int i,j; 00283 // sce->write(0); //set CE low to begin frame 00284 00285 for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing 00286 for(i = 0; i < 84; i++) { 00287 spi->write(buffer[i][j]); // send buffer 00288 } 00289 } 00290 // sce->write(1); // set CE high to end frame 00291 00292 } 00293 00294 // fills the buffer with random bytes. Can be used to test the display. 00295 // The rand() function isn't seeded so it probably creates the same pattern everytime 00296 void N5110::randomiseBuffer() 00297 { 00298 int i,j; 00299 for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing 00300 for(i = 0; i < 84; i++) { 00301 buffer[i][j] = rand()%256; // generate random byte 00302 } 00303 } 00304 00305 } 00306 00307 // function to print 5x7 font 00308 void N5110::printChar(char c) 00309 { 00310 int i; 00311 // loop through 5 columns 00312 for (i = 0; i < 5 ; i++ ) { 00313 sendData((font5x7[(c - 32)*5 + i])); 00314 // array is offset by 32 relative to ASCII, each character is 5 pixels wide 00315 // the X address is automatically incremented after each data write 00316 } 00317 sendData(0); // send an empty byte to introduce space between characters 00318 00319 } 00320 00321 // function to print 6x7 negative font 00322 void N5110::printNegChar(char c) 00323 { 00324 int i; 00325 // loop through 5 columns 00326 for (i = 0; i < 6 ; i++ ) { 00327 sendData(~(font6x7[(c - 32)*6 + i])); 00328 // array is offset by 32 relative to ASCII, each character is 5 pixels wide 00329 // the X address is automatically incremented after each data write 00330 } 00331 sendData(0); // send an empty byte to introduce space between characters 00332 00333 } 00334 00335 // function to print string at specified position 00336 void N5110::printString(const char * str,int x,int y) 00337 { 00338 int n = 0 ; // counter for number of characters in string 00339 // loop through string and print character 00340 while(*str) { 00341 00342 setXYAddress(x+6*n,y); // leave 1 pixel (6 = 5 + 1) between each character 00343 printChar(*str); // print the char - can probably so *str++ and remove next line 00344 str++; // go to next character in string 00345 n++; // increment index 00346 } 00347 00348 } 00349 00350 // function to print string at specified position 00351 void N5110::printNegString(const char * str,int x,int y) 00352 { 00353 int n = 0 ; // counter for number of characters in string 00354 // loop through string and print character 00355 while(*str) { 00356 00357 setXYAddress(x+6*n,y); // leave 1 pixel (6 = 5 + 1) between each character 00358 printNegChar(*str); // print the char - can probably so *str++ and remove next line 00359 str++; // go to next character in string 00360 n++; // increment index 00361 } 00362 00363 } 00364 00365 // function to clear the screen 00366 void N5110::clear() 00367 { 00368 clearBuffer(); // clear the buffer then call the refresh function 00369 refresh(); 00370 } 00371 00372 // function to clear the buffer 00373 void N5110::clearBuffer() 00374 { 00375 int i,j; 00376 for (i=0; i<84; i++) { // loop through the banks and set the buffer to 0 00377 for (j=0; j<6; j++) { 00378 buffer[i][j]=0; 00379 } 00380 } 00381 }
Generated on Sat Jul 16 2022 17:25:27 by
1.7.2