N5110 Library for "Racing Cars" Game

Dependents:   RacingCarsGame

Fork of N5110 by Craig Evans

Committer:
el13gs
Date:
Sat May 02 17:31:53 2015 +0000
Revision:
20:a6fc42d720c2
Parent:
19:cf23186a762f
Child:
21:ae05483c1767
Good Working Version 2

Who changed what in which revision?

UserRevisionLine numberNew contents of line
eencae 5:6ea180eef702 1 /**
eencae 5:6ea180eef702 2 @file N5110.cpp
eencae 5:6ea180eef702 3
eencae 5:6ea180eef702 4 @brief Member functions implementations
eencae 5:6ea180eef702 5
eencae 5:6ea180eef702 6 */
eencae 0:d563e74f0ae9 7 #include "mbed.h"
eencae 0:d563e74f0ae9 8 #include "N5110.h"
eencae 0:d563e74f0ae9 9
eencae 2:e93021cfb0a9 10
eencae 1:df68f34cd32d 11 N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin)
eencae 0:d563e74f0ae9 12 {
eencae 13:908644099648 13
eencae 0:d563e74f0ae9 14 spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise
eencae 13:908644099648 15 initSPI();
eencae 13:908644099648 16
eencae 6:adb79338d40f 17 // set up pins as required
eencae 0:d563e74f0ae9 18 led = new PwmOut(ledPin);
eencae 0:d563e74f0ae9 19 pwr = new DigitalOut(pwrPin);
eencae 0:d563e74f0ae9 20 sce = new DigitalOut(scePin);
eencae 0:d563e74f0ae9 21 rst = new DigitalOut(rstPin);
eencae 0:d563e74f0ae9 22 dc = new DigitalOut(dcPin);
eencae 0:d563e74f0ae9 23
eencae 0:d563e74f0ae9 24 }
eencae 0:d563e74f0ae9 25
eencae 6:adb79338d40f 26 // initialise function - powers up and sends the initialisation commands
eencae 0:d563e74f0ae9 27 void N5110::init()
eencae 0:d563e74f0ae9 28 {
eencae 6:adb79338d40f 29 turnOn(); // power up
eencae 6:adb79338d40f 30 wait_ms(10); // small delay seems to prevent spurious pixels during mbed reset
eencae 6:adb79338d40f 31 reset(); // reset LCD - must be done within 100 ms
eencae 0:d563e74f0ae9 32
eencae 0:d563e74f0ae9 33 // function set - extended
eencae 0:d563e74f0ae9 34 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
eencae 6:adb79338d40f 35 // Don't completely understand these parameters - they seem to work as they are
eencae 6:adb79338d40f 36 // Consult the datasheet if you need to change them
eencae 1:df68f34cd32d 37 sendCommand(CMD_VOP_7V38); // operating voltage - these values are from Chris Yan's Library
eencae 0:d563e74f0ae9 38 sendCommand(CMD_TC_TEMP_2); // temperature control
eencae 0:d563e74f0ae9 39 sendCommand(CMD_BI_MUX_48); // bias
eencae 0:d563e74f0ae9 40
eencae 0:d563e74f0ae9 41 // function set - basic
eencae 0:d563e74f0ae9 42 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
eencae 1:df68f34cd32d 43 normalMode(); // normal video mode by default
eencae 0:d563e74f0ae9 44 sendCommand(CMD_DC_NORMAL_MODE); // black on white
eencae 0:d563e74f0ae9 45
eencae 0:d563e74f0ae9 46 // RAM is undefined at power-up so clear
eencae 0:d563e74f0ae9 47 clearRAM();
eencae 0:d563e74f0ae9 48
eencae 0:d563e74f0ae9 49 }
eencae 13:908644099648 50
eencae 13:908644099648 51 // sets normal video mode (black on white)
eencae 13:908644099648 52 void N5110::normalMode()
eencae 13:908644099648 53 {
eencae 13:908644099648 54 sendCommand(CMD_DC_NORMAL_MODE);
eencae 13:908644099648 55
eencae 1:df68f34cd32d 56 }
eencae 1:df68f34cd32d 57
eencae 13:908644099648 58 // sets normal video mode (white on black)
eencae 13:908644099648 59 void N5110::inverseMode()
eencae 13:908644099648 60 {
eencae 13:908644099648 61 sendCommand(CMD_DC_INVERT_VIDEO);
eencae 1:df68f34cd32d 62 }
eencae 0:d563e74f0ae9 63
eencae 0:d563e74f0ae9 64 // function to power up the LCD and backlight
eencae 0:d563e74f0ae9 65 void N5110::turnOn()
eencae 0:d563e74f0ae9 66 {
eencae 0:d563e74f0ae9 67 // set brightness of LED - 0.0 to 1.0 - default is 50%
eencae 0:d563e74f0ae9 68 setBrightness(0.5);
eencae 0:d563e74f0ae9 69 pwr->write(1); // apply power
eencae 0:d563e74f0ae9 70 }
eencae 0:d563e74f0ae9 71
eencae 0:d563e74f0ae9 72 // function to power down LCD
eencae 0:d563e74f0ae9 73 void N5110::turnOff()
eencae 0:d563e74f0ae9 74 {
eencae 0:d563e74f0ae9 75 setBrightness(0.0); // turn backlight off
eencae 0:d563e74f0ae9 76 clearRAM(); // clear RAM to ensure specified current consumption
eencae 10:6f3abb40202b 77 // send command to ensure we are in basic mode
eencae 0:d563e74f0ae9 78 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
eencae 6:adb79338d40f 79 // clear the display
eencae 0:d563e74f0ae9 80 sendCommand(CMD_DC_CLEAR_DISPLAY);
eencae 6:adb79338d40f 81 // enter the extended mode and power down
eencae 0:d563e74f0ae9 82 sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
eencae 6:adb79338d40f 83 // small delay and then turn off the power pin
eencae 6:adb79338d40f 84 wait_ms(10);
eencae 0:d563e74f0ae9 85 pwr->write(0);
eencae 0:d563e74f0ae9 86
eencae 0:d563e74f0ae9 87 }
eencae 0:d563e74f0ae9 88
eencae 0:d563e74f0ae9 89 // function to change LED backlight brightness
eencae 0:d563e74f0ae9 90 void N5110::setBrightness(float brightness)
eencae 0:d563e74f0ae9 91 {
eencae 0:d563e74f0ae9 92 // check whether brightness is within range
eencae 0:d563e74f0ae9 93 if (brightness < 0.0)
eencae 0:d563e74f0ae9 94 brightness = 0.0;
eencae 0:d563e74f0ae9 95 if (brightness > 1.0)
eencae 0:d563e74f0ae9 96 brightness = 1.0;
eencae 0:d563e74f0ae9 97 // set PWM duty cycle
eencae 0:d563e74f0ae9 98 led->write(brightness);
eencae 0:d563e74f0ae9 99 }
eencae 0:d563e74f0ae9 100
eencae 0:d563e74f0ae9 101
eencae 0:d563e74f0ae9 102 // pulse the active low reset line
eencae 0:d563e74f0ae9 103 void N5110::reset()
eencae 0:d563e74f0ae9 104 {
eencae 0:d563e74f0ae9 105 rst->write(0); // reset the LCD
eencae 0:d563e74f0ae9 106 rst->write(1);
eencae 0:d563e74f0ae9 107 }
eencae 0:d563e74f0ae9 108
eencae 0:d563e74f0ae9 109 // function to initialise SPI peripheral
eencae 0:d563e74f0ae9 110 void N5110::initSPI()
eencae 0:d563e74f0ae9 111 {
eencae 0:d563e74f0ae9 112 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
eencae 0:d563e74f0ae9 113 spi->frequency(4000000); // maximum of screen is 4 MHz
eencae 0:d563e74f0ae9 114 }
eencae 0:d563e74f0ae9 115
eencae 6:adb79338d40f 116 // send a command to the display
eencae 0:d563e74f0ae9 117 void N5110::sendCommand(unsigned char command)
eencae 0:d563e74f0ae9 118 {
eencae 0:d563e74f0ae9 119 dc->write(0); // set DC low for command
eencae 0:d563e74f0ae9 120 sce->write(0); // set CE low to begin frame
eencae 0:d563e74f0ae9 121 spi->write(command); // send command
eencae 0:d563e74f0ae9 122 dc->write(1); // turn back to data by default
eencae 0:d563e74f0ae9 123 sce->write(1); // set CE high to end frame (expected for transmission of single byte)
eencae 0:d563e74f0ae9 124
eencae 0:d563e74f0ae9 125 }
eencae 0:d563e74f0ae9 126
eencae 6:adb79338d40f 127 // send data to the display at the current XY address
eencae 6:adb79338d40f 128 // dc is set to 1 (i.e. data) after sending a command and so should
eencae 6:adb79338d40f 129 // be the default mode.
eencae 0:d563e74f0ae9 130 void N5110::sendData(unsigned char data)
eencae 0:d563e74f0ae9 131 {
eencae 0:d563e74f0ae9 132 sce->write(0); // set CE low to begin frame
eencae 0:d563e74f0ae9 133 spi->write(data);
eencae 0:d563e74f0ae9 134 sce->write(1); // set CE high to end frame (expected for transmission of single byte)
eencae 0:d563e74f0ae9 135 }
eencae 0:d563e74f0ae9 136
eencae 0:d563e74f0ae9 137 // this function writes 0 to the 504 bytes to clear the RAM
eencae 0:d563e74f0ae9 138 void N5110::clearRAM()
eencae 0:d563e74f0ae9 139 {
eencae 0:d563e74f0ae9 140 int i;
eencae 0:d563e74f0ae9 141 sce->write(0); //set CE low to begin frame
eencae 17:780a542d5f8b 142 for(i = 0; i < WIDTH * HEIGHT; i++) { // 48 x 84 bits = 504 bytes
eencae 0:d563e74f0ae9 143 spi->write(0x00); // send 0's
eencae 0:d563e74f0ae9 144 }
eencae 0:d563e74f0ae9 145 sce->write(1); // set CE high to end frame
eencae 0:d563e74f0ae9 146
eencae 0:d563e74f0ae9 147 }
eencae 0:d563e74f0ae9 148
eencae 13:908644099648 149 // function to set the XY address in RAM for subsequenct data write
eencae 0:d563e74f0ae9 150 void N5110::setXYAddress(int x, int y)
eencae 0:d563e74f0ae9 151 {
eencae 17:780a542d5f8b 152 if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range
eencae 17:780a542d5f8b 153 sendCommand(0x80 | x); // send addresses to display with relevant mask
eencae 17:780a542d5f8b 154 sendCommand(0x40 | y);
eencae 17:780a542d5f8b 155 }
eencae 0:d563e74f0ae9 156 }
eencae 0:d563e74f0ae9 157
eencae 6:adb79338d40f 158 // These functions are used to set, clear and get the value of pixels in the display
eencae 6:adb79338d40f 159 // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x). The refresh()
eencae 6:adb79338d40f 160 // function must be called after set and clear in order to update the display
eencae 0:d563e74f0ae9 161 void N5110::setPixel(int x, int y)
eencae 0:d563e74f0ae9 162 {
el13gs 18:45c3696a1447 163
eencae 17:780a542d5f8b 164 if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range
eencae 17:780a542d5f8b 165 // calculate bank and shift 1 to required position in the data byte
eencae 17:780a542d5f8b 166 buffer[x][y/8] |= (1 << y%8);
eencae 17:780a542d5f8b 167 }
el13gs 18:45c3696a1447 168
eencae 0:d563e74f0ae9 169 }
eencae 0:d563e74f0ae9 170
eencae 0:d563e74f0ae9 171 void N5110::clearPixel(int x, int y)
eencae 0:d563e74f0ae9 172 {
eencae 17:780a542d5f8b 173 if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range
eencae 17:780a542d5f8b 174 // calculate bank and shift 1 to required position (using bit clear)
eencae 17:780a542d5f8b 175 buffer[x][y/8] &= ~(1 << y%8);
eencae 17:780a542d5f8b 176 }
eencae 0:d563e74f0ae9 177 }
eencae 0:d563e74f0ae9 178
eencae 7:3010f24e0a81 179 int N5110::getPixel(int x, int y)
eencae 0:d563e74f0ae9 180 {
eencae 17:780a542d5f8b 181 if (x>=0 && x<WIDTH && y>=0 && y<HEIGHT) { // check within range
eencae 17:780a542d5f8b 182 // return relevant bank and mask required bit
eencae 17:780a542d5f8b 183 return (int) buffer[x][y/8] & (1 << y%8);
eencae 17:780a542d5f8b 184 } else {
eencae 17:780a542d5f8b 185 return 0;
eencae 17:780a542d5f8b 186 }
eencae 0:d563e74f0ae9 187 }
eencae 0:d563e74f0ae9 188
eencae 6:adb79338d40f 189 // function to refresh the display
eencae 6:adb79338d40f 190 void N5110::refresh()
eencae 0:d563e74f0ae9 191 {
eencae 0:d563e74f0ae9 192 int i,j;
eencae 13:908644099648 193
eencae 7:3010f24e0a81 194 setXYAddress(0,0); // important to set address back to 0,0 before refreshing display
eencae 7:3010f24e0a81 195 // address auto increments after printing string, so buffer[0][0] will not coincide
eencae 7:3010f24e0a81 196 // with top-left pixel after priting string
eencae 13:908644099648 197
eencae 0:d563e74f0ae9 198 sce->write(0); //set CE low to begin frame
eencae 0:d563e74f0ae9 199
eencae 17:780a542d5f8b 200 for(j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing
eencae 17:780a542d5f8b 201 for(i = 0; i < WIDTH; i++) {
eencae 0:d563e74f0ae9 202 spi->write(buffer[i][j]); // send buffer
eencae 0:d563e74f0ae9 203 }
eencae 0:d563e74f0ae9 204 }
eencae 0:d563e74f0ae9 205 sce->write(1); // set CE high to end frame
eencae 0:d563e74f0ae9 206
eencae 0:d563e74f0ae9 207 }
eencae 0:d563e74f0ae9 208
eencae 6:adb79338d40f 209 // fills the buffer with random bytes. Can be used to test the display.
eencae 6:adb79338d40f 210 // The rand() function isn't seeded so it probably creates the same pattern everytime
eencae 0:d563e74f0ae9 211 void N5110::randomiseBuffer()
eencae 0:d563e74f0ae9 212 {
eencae 0:d563e74f0ae9 213 int i,j;
eencae 17:780a542d5f8b 214 for(j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing
eencae 17:780a542d5f8b 215 for(i = 0; i < WIDTH; i++) {
eencae 0:d563e74f0ae9 216 buffer[i][j] = rand()%256; // generate random byte
eencae 0:d563e74f0ae9 217 }
eencae 0:d563e74f0ae9 218 }
eencae 0:d563e74f0ae9 219
eencae 0:d563e74f0ae9 220 }
eencae 0:d563e74f0ae9 221
eencae 0:d563e74f0ae9 222 // function to print 5x7 font
eencae 13:908644099648 223 void N5110::printChar(char c,int x,int y)
eencae 0:d563e74f0ae9 224 {
eencae 13:908644099648 225 for (int i = 0; i < 5 ; i++ ) {
eencae 13:908644099648 226 buffer[x+i][y] = font5x7[(c - 32)*5 + i];
eencae 17:780a542d5f8b 227 // array is offset by 32 relative to ASCII, each character is 5 pixels wide
eencae 0:d563e74f0ae9 228 }
eencae 17:780a542d5f8b 229
eencae 17:780a542d5f8b 230 refresh(); // this sends the buffer to the display and sets address (cursor) back to 0,0
eencae 0:d563e74f0ae9 231 }
eencae 0:d563e74f0ae9 232
eencae 0:d563e74f0ae9 233 // function to print string at specified position
eencae 0:d563e74f0ae9 234 void N5110::printString(const char * str,int x,int y)
eencae 0:d563e74f0ae9 235 {
eencae 0:d563e74f0ae9 236 int n = 0 ; // counter for number of characters in string
eencae 0:d563e74f0ae9 237 // loop through string and print character
eencae 0:d563e74f0ae9 238 while(*str) {
eencae 0:d563e74f0ae9 239
eencae 13:908644099648 240 // writes the character bitmap data to the buffer, so that
eencae 9:7701f0126ba7 241 // text and pixels can be displayed at the same time
eencae 9:7701f0126ba7 242 for (int i = 0; i < 5 ; i++ ) {
eencae 9:7701f0126ba7 243 buffer[x+i+n*6][y] = font5x7[(*str - 32)*5 + i];
eencae 9:7701f0126ba7 244 }
eencae 13:908644099648 245
eencae 0:d563e74f0ae9 246 str++; // go to next character in string
eencae 0:d563e74f0ae9 247 n++; // increment index
eencae 0:d563e74f0ae9 248 }
eencae 13:908644099648 249
eencae 9:7701f0126ba7 250 refresh(); // this sends the buffer to the display and sets address (cursor) back to 0,0
eencae 0:d563e74f0ae9 251
eencae 0:d563e74f0ae9 252 }
eencae 0:d563e74f0ae9 253
eencae 6:adb79338d40f 254 // function to clear the screen
eencae 0:d563e74f0ae9 255 void N5110::clear()
eencae 0:d563e74f0ae9 256 {
eencae 6:adb79338d40f 257 clearBuffer(); // clear the buffer then call the refresh function
eencae 6:adb79338d40f 258 refresh();
eencae 0:d563e74f0ae9 259 }
eencae 0:d563e74f0ae9 260
eencae 6:adb79338d40f 261 // function to clear the buffer
eencae 0:d563e74f0ae9 262 void N5110::clearBuffer()
eencae 0:d563e74f0ae9 263 {
eencae 0:d563e74f0ae9 264 int i,j;
eencae 17:780a542d5f8b 265 for (i=0; i<WIDTH; i++) { // loop through the banks and set the buffer to 0
eencae 17:780a542d5f8b 266 for (j=0; j<BANKS; j++) {
eencae 0:d563e74f0ae9 267 buffer[i][j]=0;
eencae 0:d563e74f0ae9 268 }
eencae 0:d563e74f0ae9 269 }
eencae 8:40abe5736eca 270 }
eencae 8:40abe5736eca 271
eencae 8:40abe5736eca 272 // function to plot array on display
eencae 13:908644099648 273 void N5110::plotArray(float array[])
eencae 13:908644099648 274 {
eencae 13:908644099648 275
eencae 8:40abe5736eca 276 int i;
eencae 13:908644099648 277
eencae 17:780a542d5f8b 278 for (i=0; i<WIDTH; i++) { // loop through array
eencae 8:40abe5736eca 279 // elements are normalised from 0.0 to 1.0, so multiply
eencae 8:40abe5736eca 280 // by 47 to convert to pixel range, and subtract from 47
eencae 8:40abe5736eca 281 // since top-left is 0,0 in the display geometry
eencae 9:7701f0126ba7 282 setPixel(i,47 - int(array[i]*47.0));
eencae 13:908644099648 283 }
eencae 13:908644099648 284
eencae 8:40abe5736eca 285 refresh();
eencae 13:908644099648 286
eencae 17:780a542d5f8b 287 }
eencae 13:908644099648 288
eencae 17:780a542d5f8b 289 // function to draw circle
eencae 17:780a542d5f8b 290 void N5110:: drawCircle(int x0,int y0,int radius,int fill)
eencae 17:780a542d5f8b 291 {
eencae 17:780a542d5f8b 292 // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
eencae 17:780a542d5f8b 293 int x = radius;
eencae 17:780a542d5f8b 294 int y = 0;
eencae 17:780a542d5f8b 295 int radiusError = 1-x;
eencae 17:780a542d5f8b 296
eencae 17:780a542d5f8b 297 while(x >= y) {
eencae 17:780a542d5f8b 298
eencae 17:780a542d5f8b 299 // if transparent, just draw outline
eencae 17:780a542d5f8b 300 if (fill == 0) {
eencae 17:780a542d5f8b 301 setPixel( x + x0, y + y0);
eencae 17:780a542d5f8b 302 setPixel(-x + x0, y + y0);
eencae 17:780a542d5f8b 303 setPixel( y + x0, x + y0);
eencae 17:780a542d5f8b 304 setPixel(-y + x0, x + y0);
eencae 17:780a542d5f8b 305 setPixel(-y + x0, -x + y0);
eencae 17:780a542d5f8b 306 setPixel( y + x0, -x + y0);
eencae 17:780a542d5f8b 307 setPixel( x + x0, -y + y0);
eencae 17:780a542d5f8b 308 setPixel(-x + x0, -y + y0);
eencae 17:780a542d5f8b 309 } else { // drawing filled circle, so draw lines between points at same y value
eencae 17:780a542d5f8b 310
eencae 17:780a542d5f8b 311 int type = (fill==1) ? 1:0; // black or white fill
eencae 17:780a542d5f8b 312
eencae 17:780a542d5f8b 313 drawLine(x+x0,y+y0,-x+x0,y+y0,type);
eencae 17:780a542d5f8b 314 drawLine(y+x0,x+y0,-y+x0,x+y0,type);
eencae 17:780a542d5f8b 315 drawLine(y+x0,-x+y0,-y+x0,-x+y0,type);
eencae 17:780a542d5f8b 316 drawLine(x+x0,-y+y0,-x+x0,-y+y0,type);
eencae 17:780a542d5f8b 317 }
eencae 17:780a542d5f8b 318
eencae 17:780a542d5f8b 319
eencae 17:780a542d5f8b 320 y++;
eencae 17:780a542d5f8b 321 if (radiusError<0) {
eencae 17:780a542d5f8b 322 radiusError += 2 * y + 1;
eencae 17:780a542d5f8b 323 } else {
eencae 17:780a542d5f8b 324 x--;
eencae 17:780a542d5f8b 325 radiusError += 2 * (y - x) + 1;
eencae 17:780a542d5f8b 326 }
eencae 17:780a542d5f8b 327 }
eencae 17:780a542d5f8b 328
eencae 17:780a542d5f8b 329 refresh();
eencae 17:780a542d5f8b 330 }
eencae 17:780a542d5f8b 331
el13gs 19:cf23186a762f 332 void N5110:: clearCircle(int x0,int y0,int radius,int fill){
el13gs 19:cf23186a762f 333 // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
el13gs 19:cf23186a762f 334 int x = radius;
el13gs 19:cf23186a762f 335 int y = 0;
el13gs 19:cf23186a762f 336 int radiusError = 1-x;
el13gs 19:cf23186a762f 337
el13gs 19:cf23186a762f 338 while(x >= y) {
el13gs 19:cf23186a762f 339
el13gs 19:cf23186a762f 340 // if transparent, just draw outline
el13gs 19:cf23186a762f 341 if (fill == 0) {
el13gs 19:cf23186a762f 342 clearPixel( x + x0, y + y0);
el13gs 19:cf23186a762f 343 clearPixel(-x + x0, y + y0);
el13gs 19:cf23186a762f 344 clearPixel( y + x0, x + y0);
el13gs 19:cf23186a762f 345 clearPixel(-y + x0, x + y0);
el13gs 19:cf23186a762f 346 clearPixel(-y + x0, -x + y0);
el13gs 19:cf23186a762f 347 clearPixel( y + x0, -x + y0);
el13gs 19:cf23186a762f 348 clearPixel( x + x0, -y + y0);
el13gs 19:cf23186a762f 349 clearPixel(-x + x0, -y + y0);
el13gs 19:cf23186a762f 350 } else { // drawing filled circle, so draw lines between points at same y value
el13gs 19:cf23186a762f 351
el13gs 19:cf23186a762f 352 int type = (fill==1) ? 1:0; // black or white fill
el13gs 19:cf23186a762f 353
el13gs 20:a6fc42d720c2 354 clearLine(x+x0,y+y0,-x+x0,y+y0,type);
el13gs 20:a6fc42d720c2 355 clearLine(y+x0,x+y0,-y+x0,x+y0,type);
el13gs 20:a6fc42d720c2 356 clearLine(y+x0,-x+y0,-y+x0,-x+y0,type);
el13gs 20:a6fc42d720c2 357 clearLine(x+x0,-y+y0,-x+x0,-y+y0,type);
el13gs 19:cf23186a762f 358 }
el13gs 19:cf23186a762f 359
el13gs 19:cf23186a762f 360
el13gs 19:cf23186a762f 361 y++;
el13gs 19:cf23186a762f 362 if (radiusError<0) {
el13gs 19:cf23186a762f 363 radiusError += 2 * y + 1;
el13gs 19:cf23186a762f 364 } else {
el13gs 19:cf23186a762f 365 x--;
el13gs 19:cf23186a762f 366 radiusError += 2 * (y - x) + 1;
el13gs 19:cf23186a762f 367 }
el13gs 19:cf23186a762f 368 }
el13gs 19:cf23186a762f 369
el13gs 19:cf23186a762f 370 refresh();
el13gs 19:cf23186a762f 371 }
el13gs 19:cf23186a762f 372
el13gs 19:cf23186a762f 373
el13gs 19:cf23186a762f 374
el13gs 19:cf23186a762f 375
eencae 17:780a542d5f8b 376 void N5110::drawLine(int x0,int y0,int x1,int y1,int type)
eencae 17:780a542d5f8b 377 {
eencae 17:780a542d5f8b 378 int y_range = y1-y0; // calc range of y and x
eencae 17:780a542d5f8b 379 int x_range = x1-x0;
eencae 17:780a542d5f8b 380 int start,stop,step;
el13gs 18:45c3696a1447 381
eencae 17:780a542d5f8b 382 // if dotted line, set step to 2, else step is 1
eencae 17:780a542d5f8b 383 step = (type==2) ? 2:1;
eencae 17:780a542d5f8b 384
eencae 17:780a542d5f8b 385 // make sure we loop over the largest range to get the most pixels on the display
eencae 17:780a542d5f8b 386 // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
eencae 17:780a542d5f8b 387 // or else we'll only end up with 1 pixel in the x column
eencae 17:780a542d5f8b 388 if ( abs(x_range) > abs(y_range) ) {
eencae 17:780a542d5f8b 389
eencae 17:780a542d5f8b 390 // ensure we loop from smallest to largest or else for-loop won't run as expected
eencae 17:780a542d5f8b 391 start = x1>x0 ? x0:x1;
eencae 17:780a542d5f8b 392 stop = x1>x0 ? x1:x0;
eencae 17:780a542d5f8b 393
eencae 17:780a542d5f8b 394 // loop between x pixels
eencae 17:780a542d5f8b 395 for (int x = start; x<= stop ; x+=step) {
eencae 17:780a542d5f8b 396 // do linear interpolation
eencae 17:780a542d5f8b 397 int y = y0 + (y1-y0)*(x-x0)/(x1-x0);
eencae 17:780a542d5f8b 398
eencae 17:780a542d5f8b 399 if (type == 0) // if 'white' line, turn off pixel
eencae 17:780a542d5f8b 400 clearPixel(x,y);
eencae 17:780a542d5f8b 401 else
eencae 17:780a542d5f8b 402 setPixel(x,y); // else if 'black' or 'dotted' turn on pixel
el13gs 18:45c3696a1447 403
eencae 17:780a542d5f8b 404 }
eencae 17:780a542d5f8b 405 } else {
eencae 17:780a542d5f8b 406
eencae 17:780a542d5f8b 407 // ensure we loop from smallest to largest or else for-loop won't run as expected
eencae 17:780a542d5f8b 408 start = y1>y0 ? y0:y1;
eencae 17:780a542d5f8b 409 stop = y1>y0 ? y1:y0;
eencae 17:780a542d5f8b 410
eencae 17:780a542d5f8b 411 for (int y = start; y<= stop ; y+=step) {
eencae 17:780a542d5f8b 412 // do linear interpolation
eencae 17:780a542d5f8b 413 int x = x0 + (x1-x0)*(y-y0)/(y1-y0);
eencae 17:780a542d5f8b 414
eencae 17:780a542d5f8b 415 if (type == 0) // if 'white' line, turn off pixel
eencae 17:780a542d5f8b 416 clearPixel(x,y);
eencae 17:780a542d5f8b 417 else
eencae 17:780a542d5f8b 418 setPixel(x,y); // else if 'black' or 'dotted' turn on pixel
eencae 17:780a542d5f8b 419
eencae 17:780a542d5f8b 420 }
eencae 17:780a542d5f8b 421 }
eencae 17:780a542d5f8b 422
eencae 17:780a542d5f8b 423 refresh();
eencae 17:780a542d5f8b 424 }
eencae 17:780a542d5f8b 425
el13gs 18:45c3696a1447 426
el13gs 20:a6fc42d720c2 427 void N5110::clearLine(int x0,int y0,int x1,int y1,int type)
el13gs 20:a6fc42d720c2 428 {
el13gs 20:a6fc42d720c2 429 int y_range = y1-y0; // calc range of y and x
el13gs 20:a6fc42d720c2 430 int x_range = x1-x0;
el13gs 20:a6fc42d720c2 431 int start,stop,step;
el13gs 20:a6fc42d720c2 432
el13gs 20:a6fc42d720c2 433 // if dotted line, set step to 2, else step is 1
el13gs 20:a6fc42d720c2 434 step = (type==2) ? 2:1;
el13gs 20:a6fc42d720c2 435
el13gs 20:a6fc42d720c2 436 // make sure we loop over the largest range to get the most pixels on the display
el13gs 20:a6fc42d720c2 437 // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
el13gs 20:a6fc42d720c2 438 // or else we'll only end up with 1 pixel in the x column
el13gs 20:a6fc42d720c2 439 if ( abs(x_range) > abs(y_range) ) {
el13gs 20:a6fc42d720c2 440
el13gs 20:a6fc42d720c2 441 // ensure we loop from smallest to largest or else for-loop won't run as expected
el13gs 20:a6fc42d720c2 442 start = x1>x0 ? x0:x1;
el13gs 20:a6fc42d720c2 443 stop = x1>x0 ? x1:x0;
el13gs 20:a6fc42d720c2 444
el13gs 20:a6fc42d720c2 445 // loop between x pixels
el13gs 20:a6fc42d720c2 446 for (int x = start; x<= stop ; x+=step) {
el13gs 20:a6fc42d720c2 447 // do linear interpolation
el13gs 20:a6fc42d720c2 448 int y = y0 + (y1-y0)*(x-x0)/(x1-x0);
el13gs 20:a6fc42d720c2 449
el13gs 20:a6fc42d720c2 450 if (type == 0) // if 'white' line, turn off pixel
el13gs 20:a6fc42d720c2 451 clearPixel(x,y);
el13gs 20:a6fc42d720c2 452 else
el13gs 20:a6fc42d720c2 453 clearPixel(x,y); // else if 'black' or 'dotted' turn on pixel
el13gs 20:a6fc42d720c2 454
el13gs 20:a6fc42d720c2 455 }
el13gs 20:a6fc42d720c2 456 } else {
el13gs 20:a6fc42d720c2 457
el13gs 20:a6fc42d720c2 458 // ensure we loop from smallest to largest or else for-loop won't run as expected
el13gs 20:a6fc42d720c2 459 start = y1>y0 ? y0:y1;
el13gs 20:a6fc42d720c2 460 stop = y1>y0 ? y1:y0;
el13gs 20:a6fc42d720c2 461
el13gs 20:a6fc42d720c2 462 for (int y = start; y<= stop ; y+=step) {
el13gs 20:a6fc42d720c2 463 // do linear interpolation
el13gs 20:a6fc42d720c2 464 int x = x0 + (x1-x0)*(y-y0)/(y1-y0);
el13gs 20:a6fc42d720c2 465
el13gs 20:a6fc42d720c2 466 if (type == 0) // if 'white' line, turn off pixel
el13gs 20:a6fc42d720c2 467 clearPixel(x,y);
el13gs 20:a6fc42d720c2 468 else
el13gs 20:a6fc42d720c2 469 clearPixel(x,y); // else if 'black' or 'dotted' turn on pixel
el13gs 20:a6fc42d720c2 470
el13gs 20:a6fc42d720c2 471 }
el13gs 20:a6fc42d720c2 472 }
el13gs 20:a6fc42d720c2 473
el13gs 20:a6fc42d720c2 474 refresh();
el13gs 20:a6fc42d720c2 475 }
el13gs 20:a6fc42d720c2 476
el13gs 20:a6fc42d720c2 477
el13gs 18:45c3696a1447 478 void N5110::gameInitial(){
el13gs 19:cf23186a762f 479
el13gs 19:cf23186a762f 480 //road lanes
el13gs 18:45c3696a1447 481 drawLine(60,0,60,48,1);
el13gs 18:45c3696a1447 482 drawLine(60,16,84,16,1);
el13gs 18:45c3696a1447 483 drawLine(60,32,84,32,1);
el13gs 18:45c3696a1447 484 drawLine(20,0,20,48,2);
el13gs 18:45c3696a1447 485 drawLine(40,0,40,48,2);
el13gs 19:cf23186a762f 486
el13gs 19:cf23186a762f 487 //heart shape
el13gs 19:cf23186a762f 488 setPixel(62,9);
el13gs 19:cf23186a762f 489 setPixel(62,10);
el13gs 19:cf23186a762f 490 setPixel(62,11);
el13gs 19:cf23186a762f 491
el13gs 19:cf23186a762f 492 setPixel(63,8);
el13gs 19:cf23186a762f 493 setPixel(63,9);
el13gs 19:cf23186a762f 494 setPixel(63,10);
el13gs 19:cf23186a762f 495 setPixel(63,11);
el13gs 19:cf23186a762f 496 setPixel(63,12);
el13gs 19:cf23186a762f 497
el13gs 19:cf23186a762f 498 setPixel(64,7);
el13gs 19:cf23186a762f 499 setPixel(64,8);
el13gs 19:cf23186a762f 500 setPixel(64,9);
el13gs 19:cf23186a762f 501 setPixel(64,10);
el13gs 19:cf23186a762f 502 setPixel(64,11);
el13gs 19:cf23186a762f 503 setPixel(64,12);
el13gs 19:cf23186a762f 504 setPixel(64,13);
el13gs 19:cf23186a762f 505
el13gs 19:cf23186a762f 506 setPixel(65,8);
el13gs 19:cf23186a762f 507 setPixel(65,9);
el13gs 19:cf23186a762f 508 setPixel(65,10);
el13gs 19:cf23186a762f 509 setPixel(65,11);
el13gs 19:cf23186a762f 510 setPixel(65,12);
el13gs 19:cf23186a762f 511 setPixel(65,13);
el13gs 19:cf23186a762f 512 setPixel(65,14);
el13gs 19:cf23186a762f 513
el13gs 19:cf23186a762f 514 setPixel(66,9);
el13gs 19:cf23186a762f 515 setPixel(66,10);
el13gs 19:cf23186a762f 516 setPixel(66,11);
el13gs 19:cf23186a762f 517 setPixel(66,12);
el13gs 19:cf23186a762f 518 setPixel(66,13);
el13gs 19:cf23186a762f 519 setPixel(66,14);
el13gs 19:cf23186a762f 520 setPixel(66,15);
el13gs 19:cf23186a762f 521
el13gs 19:cf23186a762f 522 setPixel(67,8);
el13gs 19:cf23186a762f 523 setPixel(67,9);
el13gs 19:cf23186a762f 524 setPixel(67,10);
el13gs 19:cf23186a762f 525 setPixel(67,11);
el13gs 19:cf23186a762f 526 setPixel(67,12);
el13gs 19:cf23186a762f 527 setPixel(67,13);
el13gs 19:cf23186a762f 528 setPixel(67,14);
el13gs 19:cf23186a762f 529
el13gs 19:cf23186a762f 530 setPixel(68,7);
el13gs 19:cf23186a762f 531 setPixel(68,8);
el13gs 19:cf23186a762f 532 setPixel(68,9);
el13gs 19:cf23186a762f 533 setPixel(68,10);
el13gs 19:cf23186a762f 534 setPixel(68,11);
el13gs 19:cf23186a762f 535 setPixel(68,12);
el13gs 19:cf23186a762f 536 setPixel(68,13);
el13gs 19:cf23186a762f 537
el13gs 19:cf23186a762f 538 setPixel(69,8);
el13gs 19:cf23186a762f 539 setPixel(69,9);
el13gs 19:cf23186a762f 540 setPixel(69,10);
el13gs 19:cf23186a762f 541 setPixel(69,11);
el13gs 19:cf23186a762f 542 setPixel(69,12);
el13gs 19:cf23186a762f 543
el13gs 19:cf23186a762f 544 setPixel(70,9);
el13gs 19:cf23186a762f 545 setPixel(70,10);
el13gs 19:cf23186a762f 546 setPixel(70,11);
el13gs 19:cf23186a762f 547
el13gs 19:cf23186a762f 548
el13gs 19:cf23186a762f 549 //print Round Indicator
el13gs 19:cf23186a762f 550 printChar('R',62,3);
el13gs 19:cf23186a762f 551 printChar(':',68,3);
el13gs 19:cf23186a762f 552
el13gs 19:cf23186a762f 553
el13gs 19:cf23186a762f 554 //set the Coin Shape
el13gs 19:cf23186a762f 555 drawCircle(66,43,4,0);
el13gs 19:cf23186a762f 556
el13gs 19:cf23186a762f 557 setPixel(66,41);
el13gs 19:cf23186a762f 558 setPixel(66,44);
el13gs 19:cf23186a762f 559
el13gs 19:cf23186a762f 560 setPixel(67,41);
el13gs 19:cf23186a762f 561 setPixel(67,44);
el13gs 19:cf23186a762f 562
el13gs 19:cf23186a762f 563 setPixel(65,42);
el13gs 19:cf23186a762f 564 setPixel(65,43);
el13gs 19:cf23186a762f 565
el13gs 19:cf23186a762f 566
el13gs 19:cf23186a762f 567
el13gs 19:cf23186a762f 568
el13gs 19:cf23186a762f 569
el13gs 19:cf23186a762f 570
el13gs 19:cf23186a762f 571
el13gs 19:cf23186a762f 572
el13gs 19:cf23186a762f 573
el13gs 18:45c3696a1447 574 }
el13gs 18:45c3696a1447 575
el13gs 18:45c3696a1447 576
el13gs 18:45c3696a1447 577
eencae 17:780a542d5f8b 578 void N5110::drawRect(int x0,int y0,int width,int height,int fill)
eencae 17:780a542d5f8b 579 {
el13gs 18:45c3696a1447 580 gameInitial();
eencae 17:780a542d5f8b 581 if (fill == 0) { // transparent, just outline
eencae 17:780a542d5f8b 582 drawLine(x0,y0,x0+width,y0,1); // top
eencae 17:780a542d5f8b 583 drawLine(x0,y0+height,x0+width,y0+height,1); // bottom
eencae 17:780a542d5f8b 584 drawLine(x0,y0,x0,y0+height,1); // left
eencae 17:780a542d5f8b 585 drawLine(x0+width,y0,x0+width,y0+height,1); // right
eencae 17:780a542d5f8b 586 } else { // filled rectangle
eencae 17:780a542d5f8b 587 int type = (fill==1) ? 1:0; // black or white fill
eencae 17:780a542d5f8b 588 for (int y = y0; y<= y0+height; y++) { // loop through rows of rectangle
eencae 17:780a542d5f8b 589 drawLine(x0,y,x0+width,y,type); // draw line across screen
eencae 17:780a542d5f8b 590 }
eencae 17:780a542d5f8b 591 }
el13gs 18:45c3696a1447 592 }
eencae 17:780a542d5f8b 593