Snake game on nokia N5110 LCD
Dependencies: mbed
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 initSPI(); 00016 00017 // set up pins as required 00018 led = new PwmOut(ledPin); 00019 pwr = new DigitalOut(pwrPin); 00020 sce = new DigitalOut(scePin); 00021 rst = new DigitalOut(rstPin); 00022 dc = new DigitalOut(dcPin); 00023 00024 } 00025 00026 // initialise function - powers up and sends the initialisation commands 00027 void N5110::init() 00028 { 00029 turnOn(); // power up 00030 wait_ms(10); // small delay seems to prevent spurious pixels during mbed reset 00031 reset(); // reset LCD - must be done within 100 ms 00032 00033 // function set - extended 00034 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); 00035 // Don't completely understand these parameters - they seem to work as they are 00036 // Consult the datasheet if you need to change them 00037 sendCommand(CMD_VOP_7V38); // operating voltage - these values are from Chris Yan's Library 00038 sendCommand(CMD_TC_TEMP_2); // temperature control 00039 sendCommand(CMD_BI_MUX_48); // bias 00040 00041 // function set - basic 00042 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); 00043 normalMode(); // normal video mode by default 00044 sendCommand(CMD_DC_NORMAL_MODE); // black on white 00045 00046 // RAM is undefined at power-up so clear 00047 clearRAM(); 00048 00049 } 00050 00051 // sets normal video mode (black on white) 00052 void N5110::normalMode() { 00053 sendCommand(CMD_DC_NORMAL_MODE); 00054 00055 } 00056 00057 // sets normal video mode (white on black) 00058 void N5110::inverseMode() { 00059 sendCommand(CMD_DC_INVERT_VIDEO); 00060 } 00061 00062 // function to power up the LCD and backlight 00063 void N5110::turnOn() 00064 { 00065 // set brightness of LED - 0.0 to 1.0 - default is 50% 00066 setBrightness(0.5); 00067 pwr->write(1); // apply power 00068 } 00069 00070 // function to power down LCD 00071 void N5110::turnOff() 00072 { 00073 setBrightness(0.0); // turn backlight off 00074 clearRAM(); // clear RAM to ensure specified current consumption 00075 // send command to ensure we are in basic model 00076 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); 00077 // clear the display 00078 sendCommand(CMD_DC_CLEAR_DISPLAY); 00079 // enter the extended mode and power down 00080 sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); 00081 // small delay and then turn off the power pin 00082 wait_ms(10); 00083 pwr->write(0); 00084 00085 } 00086 00087 // function to change LED backlight brightness 00088 void N5110::setBrightness(float brightness) 00089 { 00090 // check whether brightness is within range 00091 if (brightness < 0.0) 00092 brightness = 0.0; 00093 if (brightness > 1.0) 00094 brightness = 1.0; 00095 // set PWM duty cycle 00096 led->write(brightness); 00097 } 00098 00099 00100 // pulse the active low reset line 00101 void N5110::reset() 00102 { 00103 rst->write(0); // reset the LCD 00104 rst->write(1); 00105 } 00106 00107 // function to initialise SPI peripheral 00108 void N5110::initSPI() 00109 { 00110 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 00111 spi->frequency(4000000); // maximum of screen is 4 MHz 00112 } 00113 00114 // send a command to the display 00115 void N5110::sendCommand(unsigned char command) 00116 { 00117 dc->write(0); // set DC low for command 00118 sce->write(0); // set CE low to begin frame 00119 spi->write(command); // send command 00120 dc->write(1); // turn back to data by default 00121 sce->write(1); // set CE high to end frame (expected for transmission of single byte) 00122 00123 } 00124 00125 // send data to the display at the current XY address 00126 // dc is set to 1 (i.e. data) after sending a command and so should 00127 // be the default mode. 00128 void N5110::sendData(unsigned char data) 00129 { 00130 sce->write(0); // set CE low to begin frame 00131 spi->write(data); 00132 sce->write(1); // set CE high to end frame (expected for transmission of single byte) 00133 } 00134 00135 // this function writes 0 to the 504 bytes to clear the RAM 00136 void N5110::clearRAM() 00137 { 00138 int i; 00139 sce->write(0); //set CE low to begin frame 00140 for(i = 0; i < 504; i++) { // 48 x 84 bits = 504 bytes 00141 spi->write(0x00); // send 0's 00142 } 00143 sce->write(1); // set CE high to end frame 00144 00145 } 00146 00147 // function to set the XY address in RAM for subsequenct data write 00148 void N5110::setXYAddress(int x, int y) 00149 { 00150 // check whether address is in range 00151 if (x > 83) 00152 x=83; 00153 if (y > 5) 00154 y=5; 00155 if (x < 0) 00156 x=0; 00157 if (y < 0) 00158 y=0; 00159 00160 sendCommand(0x80 | x); // send addresses to display with relevant mask 00161 sendCommand(0x40 | y); 00162 } 00163 00164 // These functions are used to set, clear and get the value of pixels in the display 00165 // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x). The refresh() 00166 // function must be called after set and clear in order to update the display 00167 void N5110::setPixel(int x, int y) 00168 { 00169 // calculate bank and shift 1 to required position in the data byte 00170 buffer[x][y/8] |= (1 << y%8); 00171 } 00172 00173 void N5110::clearPixel(int x, int y) 00174 { 00175 // calculate bank and shift 1 to required position (using bit clear) 00176 buffer[x][y/8] &= ~(1 << y%8); 00177 } 00178 00179 unsigned char N5110::getPixel(int x, int y) 00180 { 00181 // return relevant bank and mask required bit 00182 return buffer[x][y/8] & (1 << y%8); 00183 00184 } 00185 00186 // function to refresh the display 00187 void N5110::refresh() 00188 { 00189 int i,j; 00190 sce->write(0); //set CE low to begin frame 00191 00192 for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing 00193 for(i = 0; i < 84; i++) { 00194 spi->write(buffer[i][j]); // send buffer 00195 } 00196 } 00197 sce->write(1); // set CE high to end frame 00198 00199 } 00200 00201 // fills the buffer with random bytes. Can be used to test the display. 00202 // The rand() function isn't seeded so it probably creates the same pattern everytime 00203 void N5110::randomiseBuffer() 00204 { 00205 int i,j; 00206 for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing 00207 for(i = 0; i < 84; i++) { 00208 buffer[i][j] = rand()%256; // generate random byte 00209 } 00210 } 00211 00212 } 00213 00214 // function to print 5x7 font 00215 void N5110::printChar(char c) 00216 { 00217 int i; 00218 // loop through 5 columns 00219 for (i = 0; i < 5 ; i++ ) { 00220 sendData(font5x7[(c - 32)*5 + i]); 00221 // array is offset by 32 relative to ASCII, each character is 5 pixels wide 00222 // the X address is automatically incremented after each data write 00223 } 00224 sendData(0); // send an empty byte to introduce space between characters 00225 00226 } 00227 00228 // function to print string at specified position 00229 void N5110::printString(const char * str,int x,int y) 00230 { 00231 int n = 0 ; // counter for number of characters in string 00232 // loop through string and print character 00233 while(*str) { 00234 00235 setXYAddress(x+6*n,y); // leave 1 pixel (6 = 5 + 1) between each character 00236 printChar(*str); // print the char - can probably so *str++ and remove next line 00237 str++; // go to next character in string 00238 n++; // increment index 00239 } 00240 00241 } 00242 00243 // function to clear the screen 00244 void N5110::clear() 00245 { 00246 clearBuffer(); // clear the buffer then call the refresh function 00247 refresh(); 00248 } 00249 00250 // function to clear the buffer 00251 void N5110::clearBuffer() 00252 { 00253 int i,j; 00254 for (i=0; i<84; i++) { // loop through the banks and set the buffer to 0 00255 for (j=0; j<6; j++) { 00256 buffer[i][j]=0; 00257 } 00258 } 00259 }
Generated on Tue Jul 12 2022 21:11:46 by 1.7.2