Project Submission (late)

Dependencies:   mbed

Committer:
el17tc
Date:
Fri May 10 14:52:28 2019 +0000
Revision:
3:83e79d31930c
Parent:
0:72f372170a73
final commit, API is added.; I'm not sure if there is a specific statement of academic integrity wanted but- I declare that this work is 100% my own, I have not plagiarised any work or attempted to fabricate any part of it for extra marks.

Who changed what in which revision?

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