Example Pong game for mbed.

Dependencies:   mbed

Committer:
eencae
Date:
Fri Feb 16 13:37:49 2018 +0000
Revision:
5:3c9407e2fe55
Converted to folders.

Who changed what in which revision?

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