First Draft, serial print change based on distance

Committer:
liam94
Date:
Sat Dec 04 17:15:09 2021 +0000
Revision:
0:506531d0531c
1st Draft

Who changed what in which revision?

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