Snake game on nokia N5110 LCD
Dependencies: mbed
Snake game on nokia N5110 LCD and Keyes Syos Joystick. You control snake using joystick. Start/pause game ba using button on joystick or by pressing p on your keyboard (serial communication). More info on my blog: http://sdizdarevic.com/post/94147065625/frdm-k64f-project
N5110/N5110.cpp@0:5bdb67970267, 2014-08-08 (annotated)
- Committer:
- sdizdarevic
- Date:
- Fri Aug 08 06:44:50 2014 +0000
- Revision:
- 0:5bdb67970267
Check connections
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sdizdarevic | 0:5bdb67970267 | 1 | /** |
sdizdarevic | 0:5bdb67970267 | 2 | @file N5110.cpp |
sdizdarevic | 0:5bdb67970267 | 3 | |
sdizdarevic | 0:5bdb67970267 | 4 | @brief Member functions implementations |
sdizdarevic | 0:5bdb67970267 | 5 | |
sdizdarevic | 0:5bdb67970267 | 6 | */ |
sdizdarevic | 0:5bdb67970267 | 7 | #include "mbed.h" |
sdizdarevic | 0:5bdb67970267 | 8 | #include "N5110.h" |
sdizdarevic | 0:5bdb67970267 | 9 | |
sdizdarevic | 0:5bdb67970267 | 10 | |
sdizdarevic | 0:5bdb67970267 | 11 | N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin) |
sdizdarevic | 0:5bdb67970267 | 12 | { |
sdizdarevic | 0:5bdb67970267 | 13 | |
sdizdarevic | 0:5bdb67970267 | 14 | spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise |
sdizdarevic | 0:5bdb67970267 | 15 | initSPI(); |
sdizdarevic | 0:5bdb67970267 | 16 | |
sdizdarevic | 0:5bdb67970267 | 17 | // set up pins as required |
sdizdarevic | 0:5bdb67970267 | 18 | led = new PwmOut(ledPin); |
sdizdarevic | 0:5bdb67970267 | 19 | pwr = new DigitalOut(pwrPin); |
sdizdarevic | 0:5bdb67970267 | 20 | sce = new DigitalOut(scePin); |
sdizdarevic | 0:5bdb67970267 | 21 | rst = new DigitalOut(rstPin); |
sdizdarevic | 0:5bdb67970267 | 22 | dc = new DigitalOut(dcPin); |
sdizdarevic | 0:5bdb67970267 | 23 | |
sdizdarevic | 0:5bdb67970267 | 24 | } |
sdizdarevic | 0:5bdb67970267 | 25 | |
sdizdarevic | 0:5bdb67970267 | 26 | // initialise function - powers up and sends the initialisation commands |
sdizdarevic | 0:5bdb67970267 | 27 | void N5110::init() |
sdizdarevic | 0:5bdb67970267 | 28 | { |
sdizdarevic | 0:5bdb67970267 | 29 | turnOn(); // power up |
sdizdarevic | 0:5bdb67970267 | 30 | wait_ms(10); // small delay seems to prevent spurious pixels during mbed reset |
sdizdarevic | 0:5bdb67970267 | 31 | reset(); // reset LCD - must be done within 100 ms |
sdizdarevic | 0:5bdb67970267 | 32 | |
sdizdarevic | 0:5bdb67970267 | 33 | // function set - extended |
sdizdarevic | 0:5bdb67970267 | 34 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); |
sdizdarevic | 0:5bdb67970267 | 35 | // Don't completely understand these parameters - they seem to work as they are |
sdizdarevic | 0:5bdb67970267 | 36 | // Consult the datasheet if you need to change them |
sdizdarevic | 0:5bdb67970267 | 37 | sendCommand(CMD_VOP_7V38); // operating voltage - these values are from Chris Yan's Library |
sdizdarevic | 0:5bdb67970267 | 38 | sendCommand(CMD_TC_TEMP_2); // temperature control |
sdizdarevic | 0:5bdb67970267 | 39 | sendCommand(CMD_BI_MUX_48); // bias |
sdizdarevic | 0:5bdb67970267 | 40 | |
sdizdarevic | 0:5bdb67970267 | 41 | // function set - basic |
sdizdarevic | 0:5bdb67970267 | 42 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); |
sdizdarevic | 0:5bdb67970267 | 43 | normalMode(); // normal video mode by default |
sdizdarevic | 0:5bdb67970267 | 44 | sendCommand(CMD_DC_NORMAL_MODE); // black on white |
sdizdarevic | 0:5bdb67970267 | 45 | |
sdizdarevic | 0:5bdb67970267 | 46 | // RAM is undefined at power-up so clear |
sdizdarevic | 0:5bdb67970267 | 47 | clearRAM(); |
sdizdarevic | 0:5bdb67970267 | 48 | |
sdizdarevic | 0:5bdb67970267 | 49 | } |
sdizdarevic | 0:5bdb67970267 | 50 | |
sdizdarevic | 0:5bdb67970267 | 51 | // sets normal video mode (black on white) |
sdizdarevic | 0:5bdb67970267 | 52 | void N5110::normalMode() { |
sdizdarevic | 0:5bdb67970267 | 53 | sendCommand(CMD_DC_NORMAL_MODE); |
sdizdarevic | 0:5bdb67970267 | 54 | |
sdizdarevic | 0:5bdb67970267 | 55 | } |
sdizdarevic | 0:5bdb67970267 | 56 | |
sdizdarevic | 0:5bdb67970267 | 57 | // sets normal video mode (white on black) |
sdizdarevic | 0:5bdb67970267 | 58 | void N5110::inverseMode() { |
sdizdarevic | 0:5bdb67970267 | 59 | sendCommand(CMD_DC_INVERT_VIDEO); |
sdizdarevic | 0:5bdb67970267 | 60 | } |
sdizdarevic | 0:5bdb67970267 | 61 | |
sdizdarevic | 0:5bdb67970267 | 62 | // function to power up the LCD and backlight |
sdizdarevic | 0:5bdb67970267 | 63 | void N5110::turnOn() |
sdizdarevic | 0:5bdb67970267 | 64 | { |
sdizdarevic | 0:5bdb67970267 | 65 | // set brightness of LED - 0.0 to 1.0 - default is 50% |
sdizdarevic | 0:5bdb67970267 | 66 | setBrightness(0.5); |
sdizdarevic | 0:5bdb67970267 | 67 | pwr->write(1); // apply power |
sdizdarevic | 0:5bdb67970267 | 68 | } |
sdizdarevic | 0:5bdb67970267 | 69 | |
sdizdarevic | 0:5bdb67970267 | 70 | // function to power down LCD |
sdizdarevic | 0:5bdb67970267 | 71 | void N5110::turnOff() |
sdizdarevic | 0:5bdb67970267 | 72 | { |
sdizdarevic | 0:5bdb67970267 | 73 | setBrightness(0.0); // turn backlight off |
sdizdarevic | 0:5bdb67970267 | 74 | clearRAM(); // clear RAM to ensure specified current consumption |
sdizdarevic | 0:5bdb67970267 | 75 | // send command to ensure we are in basic model |
sdizdarevic | 0:5bdb67970267 | 76 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); |
sdizdarevic | 0:5bdb67970267 | 77 | // clear the display |
sdizdarevic | 0:5bdb67970267 | 78 | sendCommand(CMD_DC_CLEAR_DISPLAY); |
sdizdarevic | 0:5bdb67970267 | 79 | // enter the extended mode and power down |
sdizdarevic | 0:5bdb67970267 | 80 | sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); |
sdizdarevic | 0:5bdb67970267 | 81 | // small delay and then turn off the power pin |
sdizdarevic | 0:5bdb67970267 | 82 | wait_ms(10); |
sdizdarevic | 0:5bdb67970267 | 83 | pwr->write(0); |
sdizdarevic | 0:5bdb67970267 | 84 | |
sdizdarevic | 0:5bdb67970267 | 85 | } |
sdizdarevic | 0:5bdb67970267 | 86 | |
sdizdarevic | 0:5bdb67970267 | 87 | // function to change LED backlight brightness |
sdizdarevic | 0:5bdb67970267 | 88 | void N5110::setBrightness(float brightness) |
sdizdarevic | 0:5bdb67970267 | 89 | { |
sdizdarevic | 0:5bdb67970267 | 90 | // check whether brightness is within range |
sdizdarevic | 0:5bdb67970267 | 91 | if (brightness < 0.0) |
sdizdarevic | 0:5bdb67970267 | 92 | brightness = 0.0; |
sdizdarevic | 0:5bdb67970267 | 93 | if (brightness > 1.0) |
sdizdarevic | 0:5bdb67970267 | 94 | brightness = 1.0; |
sdizdarevic | 0:5bdb67970267 | 95 | // set PWM duty cycle |
sdizdarevic | 0:5bdb67970267 | 96 | led->write(brightness); |
sdizdarevic | 0:5bdb67970267 | 97 | } |
sdizdarevic | 0:5bdb67970267 | 98 | |
sdizdarevic | 0:5bdb67970267 | 99 | |
sdizdarevic | 0:5bdb67970267 | 100 | // pulse the active low reset line |
sdizdarevic | 0:5bdb67970267 | 101 | void N5110::reset() |
sdizdarevic | 0:5bdb67970267 | 102 | { |
sdizdarevic | 0:5bdb67970267 | 103 | rst->write(0); // reset the LCD |
sdizdarevic | 0:5bdb67970267 | 104 | rst->write(1); |
sdizdarevic | 0:5bdb67970267 | 105 | } |
sdizdarevic | 0:5bdb67970267 | 106 | |
sdizdarevic | 0:5bdb67970267 | 107 | // function to initialise SPI peripheral |
sdizdarevic | 0:5bdb67970267 | 108 | void N5110::initSPI() |
sdizdarevic | 0:5bdb67970267 | 109 | { |
sdizdarevic | 0:5bdb67970267 | 110 | 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 |
sdizdarevic | 0:5bdb67970267 | 111 | spi->frequency(4000000); // maximum of screen is 4 MHz |
sdizdarevic | 0:5bdb67970267 | 112 | } |
sdizdarevic | 0:5bdb67970267 | 113 | |
sdizdarevic | 0:5bdb67970267 | 114 | // send a command to the display |
sdizdarevic | 0:5bdb67970267 | 115 | void N5110::sendCommand(unsigned char command) |
sdizdarevic | 0:5bdb67970267 | 116 | { |
sdizdarevic | 0:5bdb67970267 | 117 | dc->write(0); // set DC low for command |
sdizdarevic | 0:5bdb67970267 | 118 | sce->write(0); // set CE low to begin frame |
sdizdarevic | 0:5bdb67970267 | 119 | spi->write(command); // send command |
sdizdarevic | 0:5bdb67970267 | 120 | dc->write(1); // turn back to data by default |
sdizdarevic | 0:5bdb67970267 | 121 | sce->write(1); // set CE high to end frame (expected for transmission of single byte) |
sdizdarevic | 0:5bdb67970267 | 122 | |
sdizdarevic | 0:5bdb67970267 | 123 | } |
sdizdarevic | 0:5bdb67970267 | 124 | |
sdizdarevic | 0:5bdb67970267 | 125 | // send data to the display at the current XY address |
sdizdarevic | 0:5bdb67970267 | 126 | // dc is set to 1 (i.e. data) after sending a command and so should |
sdizdarevic | 0:5bdb67970267 | 127 | // be the default mode. |
sdizdarevic | 0:5bdb67970267 | 128 | void N5110::sendData(unsigned char data) |
sdizdarevic | 0:5bdb67970267 | 129 | { |
sdizdarevic | 0:5bdb67970267 | 130 | sce->write(0); // set CE low to begin frame |
sdizdarevic | 0:5bdb67970267 | 131 | spi->write(data); |
sdizdarevic | 0:5bdb67970267 | 132 | sce->write(1); // set CE high to end frame (expected for transmission of single byte) |
sdizdarevic | 0:5bdb67970267 | 133 | } |
sdizdarevic | 0:5bdb67970267 | 134 | |
sdizdarevic | 0:5bdb67970267 | 135 | // this function writes 0 to the 504 bytes to clear the RAM |
sdizdarevic | 0:5bdb67970267 | 136 | void N5110::clearRAM() |
sdizdarevic | 0:5bdb67970267 | 137 | { |
sdizdarevic | 0:5bdb67970267 | 138 | int i; |
sdizdarevic | 0:5bdb67970267 | 139 | sce->write(0); //set CE low to begin frame |
sdizdarevic | 0:5bdb67970267 | 140 | for(i = 0; i < 504; i++) { // 48 x 84 bits = 504 bytes |
sdizdarevic | 0:5bdb67970267 | 141 | spi->write(0x00); // send 0's |
sdizdarevic | 0:5bdb67970267 | 142 | } |
sdizdarevic | 0:5bdb67970267 | 143 | sce->write(1); // set CE high to end frame |
sdizdarevic | 0:5bdb67970267 | 144 | |
sdizdarevic | 0:5bdb67970267 | 145 | } |
sdizdarevic | 0:5bdb67970267 | 146 | |
sdizdarevic | 0:5bdb67970267 | 147 | // function to set the XY address in RAM for subsequenct data write |
sdizdarevic | 0:5bdb67970267 | 148 | void N5110::setXYAddress(int x, int y) |
sdizdarevic | 0:5bdb67970267 | 149 | { |
sdizdarevic | 0:5bdb67970267 | 150 | // check whether address is in range |
sdizdarevic | 0:5bdb67970267 | 151 | if (x > 83) |
sdizdarevic | 0:5bdb67970267 | 152 | x=83; |
sdizdarevic | 0:5bdb67970267 | 153 | if (y > 5) |
sdizdarevic | 0:5bdb67970267 | 154 | y=5; |
sdizdarevic | 0:5bdb67970267 | 155 | if (x < 0) |
sdizdarevic | 0:5bdb67970267 | 156 | x=0; |
sdizdarevic | 0:5bdb67970267 | 157 | if (y < 0) |
sdizdarevic | 0:5bdb67970267 | 158 | y=0; |
sdizdarevic | 0:5bdb67970267 | 159 | |
sdizdarevic | 0:5bdb67970267 | 160 | sendCommand(0x80 | x); // send addresses to display with relevant mask |
sdizdarevic | 0:5bdb67970267 | 161 | sendCommand(0x40 | y); |
sdizdarevic | 0:5bdb67970267 | 162 | } |
sdizdarevic | 0:5bdb67970267 | 163 | |
sdizdarevic | 0:5bdb67970267 | 164 | // These functions are used to set, clear and get the value of pixels in the display |
sdizdarevic | 0:5bdb67970267 | 165 | // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x). The refresh() |
sdizdarevic | 0:5bdb67970267 | 166 | // function must be called after set and clear in order to update the display |
sdizdarevic | 0:5bdb67970267 | 167 | void N5110::setPixel(int x, int y) |
sdizdarevic | 0:5bdb67970267 | 168 | { |
sdizdarevic | 0:5bdb67970267 | 169 | // calculate bank and shift 1 to required position in the data byte |
sdizdarevic | 0:5bdb67970267 | 170 | buffer[x][y/8] |= (1 << y%8); |
sdizdarevic | 0:5bdb67970267 | 171 | } |
sdizdarevic | 0:5bdb67970267 | 172 | |
sdizdarevic | 0:5bdb67970267 | 173 | void N5110::clearPixel(int x, int y) |
sdizdarevic | 0:5bdb67970267 | 174 | { |
sdizdarevic | 0:5bdb67970267 | 175 | // calculate bank and shift 1 to required position (using bit clear) |
sdizdarevic | 0:5bdb67970267 | 176 | buffer[x][y/8] &= ~(1 << y%8); |
sdizdarevic | 0:5bdb67970267 | 177 | } |
sdizdarevic | 0:5bdb67970267 | 178 | |
sdizdarevic | 0:5bdb67970267 | 179 | unsigned char N5110::getPixel(int x, int y) |
sdizdarevic | 0:5bdb67970267 | 180 | { |
sdizdarevic | 0:5bdb67970267 | 181 | // return relevant bank and mask required bit |
sdizdarevic | 0:5bdb67970267 | 182 | return buffer[x][y/8] & (1 << y%8); |
sdizdarevic | 0:5bdb67970267 | 183 | |
sdizdarevic | 0:5bdb67970267 | 184 | } |
sdizdarevic | 0:5bdb67970267 | 185 | |
sdizdarevic | 0:5bdb67970267 | 186 | // function to refresh the display |
sdizdarevic | 0:5bdb67970267 | 187 | void N5110::refresh() |
sdizdarevic | 0:5bdb67970267 | 188 | { |
sdizdarevic | 0:5bdb67970267 | 189 | int i,j; |
sdizdarevic | 0:5bdb67970267 | 190 | sce->write(0); //set CE low to begin frame |
sdizdarevic | 0:5bdb67970267 | 191 | |
sdizdarevic | 0:5bdb67970267 | 192 | for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing |
sdizdarevic | 0:5bdb67970267 | 193 | for(i = 0; i < 84; i++) { |
sdizdarevic | 0:5bdb67970267 | 194 | spi->write(buffer[i][j]); // send buffer |
sdizdarevic | 0:5bdb67970267 | 195 | } |
sdizdarevic | 0:5bdb67970267 | 196 | } |
sdizdarevic | 0:5bdb67970267 | 197 | sce->write(1); // set CE high to end frame |
sdizdarevic | 0:5bdb67970267 | 198 | |
sdizdarevic | 0:5bdb67970267 | 199 | } |
sdizdarevic | 0:5bdb67970267 | 200 | |
sdizdarevic | 0:5bdb67970267 | 201 | // fills the buffer with random bytes. Can be used to test the display. |
sdizdarevic | 0:5bdb67970267 | 202 | // The rand() function isn't seeded so it probably creates the same pattern everytime |
sdizdarevic | 0:5bdb67970267 | 203 | void N5110::randomiseBuffer() |
sdizdarevic | 0:5bdb67970267 | 204 | { |
sdizdarevic | 0:5bdb67970267 | 205 | int i,j; |
sdizdarevic | 0:5bdb67970267 | 206 | for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing |
sdizdarevic | 0:5bdb67970267 | 207 | for(i = 0; i < 84; i++) { |
sdizdarevic | 0:5bdb67970267 | 208 | buffer[i][j] = rand()%256; // generate random byte |
sdizdarevic | 0:5bdb67970267 | 209 | } |
sdizdarevic | 0:5bdb67970267 | 210 | } |
sdizdarevic | 0:5bdb67970267 | 211 | |
sdizdarevic | 0:5bdb67970267 | 212 | } |
sdizdarevic | 0:5bdb67970267 | 213 | |
sdizdarevic | 0:5bdb67970267 | 214 | // function to print 5x7 font |
sdizdarevic | 0:5bdb67970267 | 215 | void N5110::printChar(char c) |
sdizdarevic | 0:5bdb67970267 | 216 | { |
sdizdarevic | 0:5bdb67970267 | 217 | int i; |
sdizdarevic | 0:5bdb67970267 | 218 | // loop through 5 columns |
sdizdarevic | 0:5bdb67970267 | 219 | for (i = 0; i < 5 ; i++ ) { |
sdizdarevic | 0:5bdb67970267 | 220 | sendData(font5x7[(c - 32)*5 + i]); |
sdizdarevic | 0:5bdb67970267 | 221 | // array is offset by 32 relative to ASCII, each character is 5 pixels wide |
sdizdarevic | 0:5bdb67970267 | 222 | // the X address is automatically incremented after each data write |
sdizdarevic | 0:5bdb67970267 | 223 | } |
sdizdarevic | 0:5bdb67970267 | 224 | sendData(0); // send an empty byte to introduce space between characters |
sdizdarevic | 0:5bdb67970267 | 225 | |
sdizdarevic | 0:5bdb67970267 | 226 | } |
sdizdarevic | 0:5bdb67970267 | 227 | |
sdizdarevic | 0:5bdb67970267 | 228 | // function to print string at specified position |
sdizdarevic | 0:5bdb67970267 | 229 | void N5110::printString(const char * str,int x,int y) |
sdizdarevic | 0:5bdb67970267 | 230 | { |
sdizdarevic | 0:5bdb67970267 | 231 | int n = 0 ; // counter for number of characters in string |
sdizdarevic | 0:5bdb67970267 | 232 | // loop through string and print character |
sdizdarevic | 0:5bdb67970267 | 233 | while(*str) { |
sdizdarevic | 0:5bdb67970267 | 234 | |
sdizdarevic | 0:5bdb67970267 | 235 | setXYAddress(x+6*n,y); // leave 1 pixel (6 = 5 + 1) between each character |
sdizdarevic | 0:5bdb67970267 | 236 | printChar(*str); // print the char - can probably so *str++ and remove next line |
sdizdarevic | 0:5bdb67970267 | 237 | str++; // go to next character in string |
sdizdarevic | 0:5bdb67970267 | 238 | n++; // increment index |
sdizdarevic | 0:5bdb67970267 | 239 | } |
sdizdarevic | 0:5bdb67970267 | 240 | |
sdizdarevic | 0:5bdb67970267 | 241 | } |
sdizdarevic | 0:5bdb67970267 | 242 | |
sdizdarevic | 0:5bdb67970267 | 243 | // function to clear the screen |
sdizdarevic | 0:5bdb67970267 | 244 | void N5110::clear() |
sdizdarevic | 0:5bdb67970267 | 245 | { |
sdizdarevic | 0:5bdb67970267 | 246 | clearBuffer(); // clear the buffer then call the refresh function |
sdizdarevic | 0:5bdb67970267 | 247 | refresh(); |
sdizdarevic | 0:5bdb67970267 | 248 | } |
sdizdarevic | 0:5bdb67970267 | 249 | |
sdizdarevic | 0:5bdb67970267 | 250 | // function to clear the buffer |
sdizdarevic | 0:5bdb67970267 | 251 | void N5110::clearBuffer() |
sdizdarevic | 0:5bdb67970267 | 252 | { |
sdizdarevic | 0:5bdb67970267 | 253 | int i,j; |
sdizdarevic | 0:5bdb67970267 | 254 | for (i=0; i<84; i++) { // loop through the banks and set the buffer to 0 |
sdizdarevic | 0:5bdb67970267 | 255 | for (j=0; j<6; j++) { |
sdizdarevic | 0:5bdb67970267 | 256 | buffer[i][j]=0; |
sdizdarevic | 0:5bdb67970267 | 257 | } |
sdizdarevic | 0:5bdb67970267 | 258 | } |
sdizdarevic | 0:5bdb67970267 | 259 | } |