Mochu Yao explorer game

Dependencies:   mbed

Committer:
el17my
Date:
Wed Apr 08 08:33:48 2020 +0000
Revision:
0:39a85181b766
Child:
1:ed745421d8c4
wrtiting N5110

Who changed what in which revision?

UserRevisionLine numberNew contents of line
el17my 0:39a85181b766 1 #include "mbed.h"
el17my 0:39a85181b766 2 #include "N5110.h"
el17my 0:39a85181b766 3
el17my 0:39a85181b766 4 // overloaded constructor includes power pin - LCD Vcc connected to GPIO pin
el17my 0:39a85181b766 5 // this constructor works fine with LPC1768 - enough current sourced from GPIO
el17my 0:39a85181b766 6 // to power LCD. Doesn't work well with K64F.
el17my 0:39a85181b766 7 N5110::N5110(PinName const pwrPin,
el17my 0:39a85181b766 8 PinName const scePin,
el17my 0:39a85181b766 9 PinName const rstPin,
el17my 0:39a85181b766 10 PinName const dcPin,
el17my 0:39a85181b766 11 PinName const mosiPin,
el17my 0:39a85181b766 12 PinName const sclkPin,
el17my 0:39a85181b766 13 PinName const ledPin)
el17my 0:39a85181b766 14 :
el17my 0:39a85181b766 15 _spi(new SPI(mosiPin,NC,sclkPin)), // create new SPI instance and initialise
el17my 0:39a85181b766 16 _led(new PwmOut(ledPin)),
el17my 0:39a85181b766 17 _pwr(new DigitalOut(pwrPin)),
el17my 0:39a85181b766 18 _sce(new DigitalOut(scePin)),
el17my 0:39a85181b766 19 _rst(new DigitalOut(rstPin)),
el17my 0:39a85181b766 20 _dc(new DigitalOut(dcPin))
el17my 0:39a85181b766 21 {}
el17my 0:39a85181b766 22
el17my 0:39a85181b766 23 // overloaded constructor does not include power pin - LCD Vcc must be tied to +3V3
el17my 0:39a85181b766 24 // Best to use this with K64F as the GPIO hasn't sufficient output current to reliably
el17my 0:39a85181b766 25 // drive the LCD.
el17my 0:39a85181b766 26 N5110::N5110(PinName const scePin,
el17my 0:39a85181b766 27 PinName const rstPin,
el17my 0:39a85181b766 28 PinName const dcPin,
el17my 0:39a85181b766 29 PinName const mosiPin,
el17my 0:39a85181b766 30 PinName const sclkPin,
el17my 0:39a85181b766 31 PinName const ledPin)
el17my 0:39a85181b766 32 :
el17my 0:39a85181b766 33 _spi(new SPI(mosiPin,NC,sclkPin)), // create new SPI instance and initialise
el17my 0:39a85181b766 34 _led(new PwmOut(ledPin)),
el17my 0:39a85181b766 35 _pwr(NULL), // pwr not needed so null it to be safe
el17my 0:39a85181b766 36 _sce(new DigitalOut(scePin)),
el17my 0:39a85181b766 37 _rst(new DigitalOut(rstPin)),
el17my 0:39a85181b766 38 _dc(new DigitalOut(dcPin))
el17my 0:39a85181b766 39 {}
el17my 0:39a85181b766 40
el17my 0:39a85181b766 41 N5110::~N5110()
el17my 0:39a85181b766 42 {
el17my 0:39a85181b766 43 delete _spi;
el17my 0:39a85181b766 44
el17my 0:39a85181b766 45 if(_pwr) {
el17my 0:39a85181b766 46 delete _pwr;
el17my 0:39a85181b766 47 }
el17my 0:39a85181b766 48
el17my 0:39a85181b766 49 delete _led;
el17my 0:39a85181b766 50 delete _sce;
el17my 0:39a85181b766 51 delete _rst;
el17my 0:39a85181b766 52 delete _dc;
el17my 0:39a85181b766 53 }
el17my 0:39a85181b766 54
el17my 0:39a85181b766 55 // initialise function - powers up and sends the initialisation commands
el17my 0:39a85181b766 56 void N5110::init()
el17my 0:39a85181b766 57 {
el17my 0:39a85181b766 58 turnOn(); // power up
el17my 0:39a85181b766 59 reset(); // reset LCD - must be done within 100 ms
el17my 0:39a85181b766 60
el17my 0:39a85181b766 61 initSPI();
el17my 0:39a85181b766 62 // function set - extended
el17my 0:39a85181b766 63 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
el17my 0:39a85181b766 64 // Don't completely understand these parameters - they seem to work as they are
el17my 0:39a85181b766 65 // Consult the datasheet if you need to change them
el17my 0:39a85181b766 66 sendCommand(CMD_VOP_7V38); // operating voltage - these values are from Chris Yan's Library
el17my 0:39a85181b766 67 sendCommand(CMD_TC_TEMP_2); // temperature control
el17my 0:39a85181b766 68 sendCommand(CMD_BI_MUX_48); // changing this can sometimes improve the contrast on some displays
el17my 0:39a85181b766 69
el17my 0:39a85181b766 70 // function set - basic
el17my 0:39a85181b766 71 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
el17my 0:39a85181b766 72 normalMode(); // normal video mode by default
el17my 0:39a85181b766 73 sendCommand(CMD_DC_NORMAL_MODE); // black on white
el17my 0:39a85181b766 74
el17my 0:39a85181b766 75 clearRAM(); // RAM is undefined at power-up so clear
el17my 0:39a85181b766 76 clear(); // clear buffer
el17my 0:39a85181b766 77 setBrightness(0.5);
el17my 0:39a85181b766 78 }
el17my 0:39a85181b766 79
el17my 0:39a85181b766 80 // sets normal video mode (black on white)
el17my 0:39a85181b766 81 void N5110::normalMode()
el17my 0:39a85181b766 82 {
el17my 0:39a85181b766 83 sendCommand(CMD_DC_NORMAL_MODE);
el17my 0:39a85181b766 84 }
el17my 0:39a85181b766 85
el17my 0:39a85181b766 86 // sets normal video mode (white on black)
el17my 0:39a85181b766 87 void N5110::inverseMode()
el17my 0:39a85181b766 88 {
el17my 0:39a85181b766 89 sendCommand(CMD_DC_INVERT_VIDEO);
el17my 0:39a85181b766 90 }
el17my 0:39a85181b766 91
el17my 0:39a85181b766 92 // function to power up the LCD and backlight - only works when using GPIO to power
el17my 0:39a85181b766 93 void N5110::turnOn()
el17my 0:39a85181b766 94 {
el17my 0:39a85181b766 95 if (_pwr != NULL) {
el17my 0:39a85181b766 96 _pwr->write(1); // apply power
el17my 0:39a85181b766 97 }
el17my 0:39a85181b766 98 }
el17my 0:39a85181b766 99
el17my 0:39a85181b766 100 // function to power down LCD
el17my 0:39a85181b766 101 void N5110::turnOff()
el17my 0:39a85181b766 102 {
el17my 0:39a85181b766 103 clear(); // clear buffer
el17my 0:39a85181b766 104 refresh();
el17my 0:39a85181b766 105 setBrightness(0.0); // turn backlight off
el17my 0:39a85181b766 106 clearRAM(); // clear RAM to ensure specified current consumption
el17my 0:39a85181b766 107 // send command to ensure we are in basic mode
el17my 0:39a85181b766 108 sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE);
el17my 0:39a85181b766 109 // clear the display
el17my 0:39a85181b766 110 sendCommand(CMD_DC_CLEAR_DISPLAY);
el17my 0:39a85181b766 111 // enter the extended mode and power down
el17my 0:39a85181b766 112 sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE);
el17my 0:39a85181b766 113 // small delay and then turn off the power pin
el17my 0:39a85181b766 114 wait_ms(10);
el17my 0:39a85181b766 115
el17my 0:39a85181b766 116 // if we are powering the LCD using the GPIO then make it low to turn off
el17my 0:39a85181b766 117 if (_pwr != NULL) {
el17my 0:39a85181b766 118 _pwr->write(0); // turn off power
el17my 0:39a85181b766 119 }
el17my 0:39a85181b766 120
el17my 0:39a85181b766 121 }
el17my 0:39a85181b766 122
el17my 0:39a85181b766 123 // function to change LED backlight brightness
el17my 0:39a85181b766 124 void N5110::setBrightness(float brightness)
el17my 0:39a85181b766 125 {
el17my 0:39a85181b766 126 // check whether brightness is within range
el17my 0:39a85181b766 127 if (brightness < 0.0f)
el17my 0:39a85181b766 128 brightness = 0.0f;
el17my 0:39a85181b766 129 if (brightness > 1.0f)
el17my 0:39a85181b766 130 brightness = 1.0f;
el17my 0:39a85181b766 131 // set PWM duty cycle
el17my 0:39a85181b766 132 _led->write(brightness);
el17my 0:39a85181b766 133 }
el17my 0:39a85181b766 134
el17my 0:39a85181b766 135
el17my 0:39a85181b766 136 // pulse the active low reset line
el17my 0:39a85181b766 137 void N5110::reset()
el17my 0:39a85181b766 138 {
el17my 0:39a85181b766 139 _rst->write(0); // reset the LCD
el17my 0:39a85181b766 140 _rst->write(1);
el17my 0:39a85181b766 141 }
el17my 0:39a85181b766 142
el17my 0:39a85181b766 143 // function to initialise SPI peripheral
el17my 0:39a85181b766 144 void N5110::initSPI()
el17my 0:39a85181b766 145 {
el17my 0:39a85181b766 146 _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
el17my 0:39a85181b766 147 _spi->frequency(4000000); // maximum of screen is 4 MHz
el17my 0:39a85181b766 148 }
el17my 0:39a85181b766 149
el17my 0:39a85181b766 150 // send a command to the display
el17my 0:39a85181b766 151 void N5110::sendCommand(unsigned char command)
el17my 0:39a85181b766 152 {
el17my 0:39a85181b766 153 _dc->write(0); // set DC low for command
el17my 0:39a85181b766 154 _sce->write(0); // set CE low to begin frame
el17my 0:39a85181b766 155 _spi->write(command); // send command
el17my 0:39a85181b766 156 _dc->write(1); // turn back to data by default
el17my 0:39a85181b766 157 _sce->write(1); // set CE high to end frame (expected for transmission of single byte)
el17my 0:39a85181b766 158 }
el17my 0:39a85181b766 159
el17my 0:39a85181b766 160 // send data to the display at the current XY address
el17my 0:39a85181b766 161 // dc is set to 1 (i.e. data) after sending a command and so should
el17my 0:39a85181b766 162 // be the default mode.
el17my 0:39a85181b766 163 void N5110::sendData(unsigned char data)
el17my 0:39a85181b766 164 {
el17my 0:39a85181b766 165 _sce->write(0); // set CE low to begin frame
el17my 0:39a85181b766 166 _spi->write(data);
el17my 0:39a85181b766 167 _sce->write(1); // set CE high to end frame (expected for transmission of single byte)
el17my 0:39a85181b766 168 }
el17my 0:39a85181b766 169
el17my 0:39a85181b766 170 // this function writes 0 to the 504 bytes to clear the RAM
el17my 0:39a85181b766 171 void N5110::clearRAM()
el17my 0:39a85181b766 172 {
el17my 0:39a85181b766 173 _sce->write(0); //set CE low to begin frame
el17my 0:39a85181b766 174 for(int i = 0; i < WIDTH * HEIGHT; i++) { // 48 x 84 bits = 504 bytes
el17my 0:39a85181b766 175 _spi->write(0x00); // send 0's
el17my 0:39a85181b766 176 }
el17my 0:39a85181b766 177 _sce->write(1); // set CE high to end frame
el17my 0:39a85181b766 178 }
el17my 0:39a85181b766 179
el17my 0:39a85181b766 180 // function to set the XY address in RAM for subsequenct data write
el17my 0:39a85181b766 181 void N5110::setXYAddress(unsigned int const x,
el17my 0:39a85181b766 182 unsigned int const y)
el17my 0:39a85181b766 183 {
el17my 0:39a85181b766 184 if (x<WIDTH && y<HEIGHT) { // check within range
el17my 0:39a85181b766 185 sendCommand(0x80 | x); // send addresses to display with relevant mask
el17my 0:39a85181b766 186 sendCommand(0x40 | y);
el17my 0:39a85181b766 187 }
el17my 0:39a85181b766 188 }
el17my 0:39a85181b766 189
el17my 0:39a85181b766 190 // These functions are used to set, clear and get the value of pixels in the display
el17my 0:39a85181b766 191 // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x). The refresh()
el17my 0:39a85181b766 192 // function must be called after set and clear in order to update the display
el17my 0:39a85181b766 193 void N5110::setPixel(unsigned int const x,
el17my 0:39a85181b766 194 unsigned int const y)
el17my 0:39a85181b766 195 {
el17my 0:39a85181b766 196 if (x<WIDTH && y<HEIGHT) { // check within range
el17my 0:39a85181b766 197 // calculate bank and shift 1 to required position in the data byte
el17my 0:39a85181b766 198 buffer[x][y/8] |= (1 << y%8);
el17my 0:39a85181b766 199 }
el17my 0:39a85181b766 200 }
el17my 0:39a85181b766 201
el17my 0:39a85181b766 202 void N5110::clearPixel(unsigned int const x,
el17my 0:39a85181b766 203 unsigned int const y)
el17my 0:39a85181b766 204 {
el17my 0:39a85181b766 205 if (x<WIDTH && y<HEIGHT) { // check within range
el17my 0:39a85181b766 206 // calculate bank and shift 1 to required position (using bit clear)
el17my 0:39a85181b766 207 buffer[x][y/8] &= ~(1 << y%8);
el17my 0:39a85181b766 208 }
el17my 0:39a85181b766 209 }
el17my 0:39a85181b766 210
el17my 0:39a85181b766 211 int N5110::getPixel(unsigned int const x,
el17my 0:39a85181b766 212 unsigned int const y) const
el17my 0:39a85181b766 213 {
el17my 0:39a85181b766 214 if (x<WIDTH && y<HEIGHT) { // check within range
el17my 0:39a85181b766 215 // return relevant bank and mask required bit
el17my 0:39a85181b766 216
el17my 0:39a85181b766 217 int pixel = (int) buffer[x][y/8] & (1 << y%8);
el17my 0:39a85181b766 218
el17my 0:39a85181b766 219 if (pixel)
el17my 0:39a85181b766 220 return 1;
el17my 0:39a85181b766 221 else
el17my 0:39a85181b766 222 return 0;
el17my 0:39a85181b766 223 }
el17my 0:39a85181b766 224
el17my 0:39a85181b766 225 return 0;
el17my 0:39a85181b766 226
el17my 0:39a85181b766 227 }
el17my 0:39a85181b766 228
el17my 0:39a85181b766 229 // function to refresh the display
el17my 0:39a85181b766 230 void N5110::refresh()
el17my 0:39a85181b766 231 {
el17my 0:39a85181b766 232 setXYAddress(0,0); // important to set address back to 0,0 before refreshing display
el17my 0:39a85181b766 233 // address auto increments after printing string, so buffer[0][0] will not coincide
el17my 0:39a85181b766 234 // with top-left pixel after priting string
el17my 0:39a85181b766 235
el17my 0:39a85181b766 236 _sce->write(0); //set CE low to begin frame
el17my 0:39a85181b766 237
el17my 0:39a85181b766 238 for(int j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing
el17my 0:39a85181b766 239 for(int i = 0; i < WIDTH; i++) {
el17my 0:39a85181b766 240 _spi->write(buffer[i][j]); // send buffer
el17my 0:39a85181b766 241 }
el17my 0:39a85181b766 242 }
el17my 0:39a85181b766 243 _sce->write(1); // set CE high to end frame
el17my 0:39a85181b766 244
el17my 0:39a85181b766 245 }
el17my 0:39a85181b766 246
el17my 0:39a85181b766 247 // fills the buffer with random bytes. Can be used to test the display.
el17my 0:39a85181b766 248 // The rand() function isn't seeded so it probably creates the same pattern everytime
el17my 0:39a85181b766 249 void N5110::randomiseBuffer()
el17my 0:39a85181b766 250 {
el17my 0:39a85181b766 251 int i,j;
el17my 0:39a85181b766 252 for(j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing
el17my 0:39a85181b766 253 for(i = 0; i < WIDTH; i++) {
el17my 0:39a85181b766 254 buffer[i][j] = rand()%256; // generate random byte
el17my 0:39a85181b766 255 }
el17my 0:39a85181b766 256 }
el17my 0:39a85181b766 257
el17my 0:39a85181b766 258 }
el17my 0:39a85181b766 259
el17my 0:39a85181b766 260 // function to print 5x7 font
el17my 0:39a85181b766 261 void N5110::printChar(char const c,
el17my 0:39a85181b766 262 unsigned int const x,
el17my 0:39a85181b766 263 unsigned int const y)
el17my 0:39a85181b766 264 {
el17my 0:39a85181b766 265 if (y<BANKS) { // check if printing in range of y banks
el17my 0:39a85181b766 266
el17my 0:39a85181b766 267 for (int i = 0; i < 5 ; i++ ) {
el17my 0:39a85181b766 268 int pixel_x = x+i;
el17my 0:39a85181b766 269 if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83)
el17my 0:39a85181b766 270 break;
el17my 0:39a85181b766 271 buffer[pixel_x][y] = font5x7[(c - 32)*5 + i];
el17my 0:39a85181b766 272 // array is offset by 32 relative to ASCII, each character is 5 pixels wide
el17my 0:39a85181b766 273 }
el17my 0:39a85181b766 274
el17my 0:39a85181b766 275 }
el17my 0:39a85181b766 276 }
el17my 0:39a85181b766 277
el17my 0:39a85181b766 278 // function to print string at specified position
el17my 0:39a85181b766 279 void N5110::printString(const char *str,
el17my 0:39a85181b766 280 unsigned int const x,
el17my 0:39a85181b766 281 unsigned int const y)
el17my 0:39a85181b766 282 {
el17my 0:39a85181b766 283 if (y<BANKS) { // check if printing in range of y banks
el17my 0:39a85181b766 284
el17my 0:39a85181b766 285 int n = 0 ; // counter for number of characters in string
el17my 0:39a85181b766 286 // loop through string and print character
el17my 0:39a85181b766 287 while(*str) {
el17my 0:39a85181b766 288
el17my 0:39a85181b766 289 // writes the character bitmap data to the buffer, so that
el17my 0:39a85181b766 290 // text and pixels can be displayed at the same time
el17my 0:39a85181b766 291 for (int i = 0; i < 5 ; i++ ) {
el17my 0:39a85181b766 292 int pixel_x = x+i+n*6;
el17my 0:39a85181b766 293 if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83)
el17my 0:39a85181b766 294 break;
el17my 0:39a85181b766 295 buffer[pixel_x][y] = font5x7[(*str - 32)*5 + i];
el17my 0:39a85181b766 296 }
el17my 0:39a85181b766 297 str++; // go to next character in string
el17my 0:39a85181b766 298 n++; // increment index
el17my 0:39a85181b766 299 }
el17my 0:39a85181b766 300 }
el17my 0:39a85181b766 301 }
el17my 0:39a85181b766 302
el17my 0:39a85181b766 303 // function to clear the screen buffer
el17my 0:39a85181b766 304 void N5110::clear()
el17my 0:39a85181b766 305 {
el17my 0:39a85181b766 306 memset(buffer,0,sizeof(buffer));
el17my 0:39a85181b766 307 }
el17my 0:39a85181b766 308
el17my 0:39a85181b766 309 // function to plot array on display
el17my 0:39a85181b766 310 void N5110::plotArray(float const array[])
el17my 0:39a85181b766 311 {
el17my 0:39a85181b766 312 for (int i=0; i<WIDTH; i++) { // loop through array
el17my 0:39a85181b766 313 // elements are normalised from 0.0 to 1.0, so multiply
el17my 0:39a85181b766 314 // by 47 to convert to pixel range, and subtract from 47
el17my 0:39a85181b766 315 // since top-left is 0,0 in the display geometry
el17my 0:39a85181b766 316 setPixel(i,47 - int(array[i]*47.0f));
el17my 0:39a85181b766 317 }
el17my 0:39a85181b766 318
el17my 0:39a85181b766 319 }
el17my 0:39a85181b766 320
el17my 0:39a85181b766 321 // function to draw circle
el17my 0:39a85181b766 322 void N5110:: drawCircle(unsigned int const x0,
el17my 0:39a85181b766 323 unsigned int const y0,
el17my 0:39a85181b766 324 unsigned int const radius,
el17my 0:39a85181b766 325 FillType const fill)
el17my 0:39a85181b766 326 {
el17my 0:39a85181b766 327 // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm
el17my 0:39a85181b766 328 int x = radius;
el17my 0:39a85181b766 329 int y = 0;
el17my 0:39a85181b766 330 int radiusError = 1-x;
el17my 0:39a85181b766 331
el17my 0:39a85181b766 332 while(x >= y) {
el17my 0:39a85181b766 333
el17my 0:39a85181b766 334 // if transparent, just draw outline
el17my 0:39a85181b766 335 if (fill == FILL_TRANSPARENT) {
el17my 0:39a85181b766 336 setPixel( x + x0, y + y0);
el17my 0:39a85181b766 337 setPixel(-x + x0, y + y0);
el17my 0:39a85181b766 338 setPixel( y + x0, x + y0);
el17my 0:39a85181b766 339 setPixel(-y + x0, x + y0);
el17my 0:39a85181b766 340 setPixel(-y + x0, -x + y0);
el17my 0:39a85181b766 341 setPixel( y + x0, -x + y0);
el17my 0:39a85181b766 342 setPixel( x + x0, -y + y0);
el17my 0:39a85181b766 343 setPixel(-x + x0, -y + y0);
el17my 0:39a85181b766 344 } else { // drawing filled circle, so draw lines between points at same y value
el17my 0:39a85181b766 345
el17my 0:39a85181b766 346 int type = (fill==FILL_BLACK) ? 1:0; // black or white fill
el17my 0:39a85181b766 347
el17my 0:39a85181b766 348 drawLine(x+x0,y+y0,-x+x0,y+y0,type);
el17my 0:39a85181b766 349 drawLine(y+x0,x+y0,-y+x0,x+y0,type);
el17my 0:39a85181b766 350 drawLine(y+x0,-x+y0,-y+x0,-x+y0,type);
el17my 0:39a85181b766 351 drawLine(x+x0,-y+y0,-x+x0,-y+y0,type);
el17my 0:39a85181b766 352 }
el17my 0:39a85181b766 353
el17my 0:39a85181b766 354 y++;
el17my 0:39a85181b766 355 if (radiusError<0) {
el17my 0:39a85181b766 356 radiusError += 2 * y + 1;
el17my 0:39a85181b766 357 } else {
el17my 0:39a85181b766 358 x--;
el17my 0:39a85181b766 359 radiusError += 2 * (y - x) + 1;
el17my 0:39a85181b766 360 }
el17my 0:39a85181b766 361 }
el17my 0:39a85181b766 362
el17my 0:39a85181b766 363 }
el17my 0:39a85181b766 364
el17my 0:39a85181b766 365 void N5110::drawLine(unsigned int const x0,
el17my 0:39a85181b766 366 unsigned int const y0,
el17my 0:39a85181b766 367 unsigned int const x1,
el17my 0:39a85181b766 368 unsigned int const y1,
el17my 0:39a85181b766 369 unsigned int const type)
el17my 0:39a85181b766 370 {
el17my 0:39a85181b766 371 int y_range = y1-y0; // calc range of y and x
el17my 0:39a85181b766 372 int x_range = x1-x0;
el17my 0:39a85181b766 373 int start,stop,step;
el17my 0:39a85181b766 374
el17my 0:39a85181b766 375 // if dotted line, set step to 2, else step is 1
el17my 0:39a85181b766 376 step = (type==2) ? 2:1;
el17my 0:39a85181b766 377
el17my 0:39a85181b766 378 // make sure we loop over the largest range to get the most pixels on the display
el17my 0:39a85181b766 379 // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels
el17my 0:39a85181b766 380 // or else we'll only end up with 1 pixel in the x column
el17my 0:39a85181b766 381 if ( abs(x_range) > abs(y_range) ) {
el17my 0:39a85181b766 382
el17my 0:39a85181b766 383 // ensure we loop from smallest to largest or else for-loop won't run as expected
el17my 0:39a85181b766 384 start = x1>x0 ? x0:x1;
el17my 0:39a85181b766 385 stop = x1>x0 ? x1:x0;
el17my 0:39a85181b766 386
el17my 0:39a85181b766 387 // loop between x pixels
el17my 0:39a85181b766 388 for (int x = start; x<= stop ; x+=step) {
el17my 0:39a85181b766 389 // do linear interpolation
el17my 0:39a85181b766 390 int y = y0 + (y1-y0)*(x-x0)/(x1-x0);
el17my 0:39a85181b766 391
el17my 0:39a85181b766 392 if (type == 0) // if 'white' line, turn off pixel
el17my 0:39a85181b766 393 clearPixel(x,y);
el17my 0:39a85181b766 394 else
el17my 0:39a85181b766 395 setPixel(x,y); // else if 'black' or 'dotted' turn on pixel
el17my 0:39a85181b766 396 }
el17my 0:39a85181b766 397 } else {
el17my 0:39a85181b766 398
el17my 0:39a85181b766 399 // ensure we loop from smallest to largest or else for-loop won't run as expected
el17my 0:39a85181b766 400 start = y1>y0 ? y0:y1;
el17my 0:39a85181b766 401 stop = y1>y0 ? y1:y0;
el17my 0:39a85181b766 402
el17my 0:39a85181b766 403 for (int y = start; y<= stop ; y+=step) {
el17my 0:39a85181b766 404 // do linear interpolation
el17my 0:39a85181b766 405 int x = x0 + (x1-x0)*(y-y0)/(y1-y0);
el17my 0:39a85181b766 406
el17my 0:39a85181b766 407 if (type == 0) // if 'white' line, turn off pixel
el17my 0:39a85181b766 408 clearPixel(x,y);
el17my 0:39a85181b766 409 else
el17my 0:39a85181b766 410 setPixel(x,y); // else if 'black' or 'dotted' turn on pixel
el17my 0:39a85181b766 411
el17my 0:39a85181b766 412 }
el17my 0:39a85181b766 413 }
el17my 0:39a85181b766 414
el17my 0:39a85181b766 415 }
el17my 0:39a85181b766 416
el17my 0:39a85181b766 417 void N5110::drawRect(unsigned int const x0,
el17my 0:39a85181b766 418 unsigned int const y0,
el17my 0:39a85181b766 419 unsigned int const width,
el17my 0:39a85181b766 420 unsigned int const height,
el17my 0:39a85181b766 421 FillType const fill)
el17my 0:39a85181b766 422 {
el17my 0:39a85181b766 423 if (fill == FILL_TRANSPARENT) { // transparent, just outline
el17my 0:39a85181b766 424 drawLine(x0,y0,x0+(width-1),y0,1); // top
el17my 0:39a85181b766 425 drawLine(x0,y0+(height-1),x0+(width-1),y0+(height-1),1); // bottom
el17my 0:39a85181b766 426 drawLine(x0,y0,x0,y0+(height-1),1); // left
el17my 0:39a85181b766 427 drawLine(x0+(width-1),y0,x0+(width-1),y0+(height-1),1); // right
el17my 0:39a85181b766 428 } else { // filled rectangle
el17my 0:39a85181b766 429 int type = (fill==FILL_BLACK) ? 1:0; // black or white fill
el17my 0:39a85181b766 430 for (int y = y0; y<y0+height; y++) { // loop through rows of rectangle
el17my 0:39a85181b766 431 drawLine(x0,y,x0+(width-1),y,type); // draw line across screen
el17my 0:39a85181b766 432 }
el17my 0:39a85181b766 433 }
el17my 0:39a85181b766 434 }