bird

Dependencies:   mbed

Committer:
1013373474
Date:
Fri May 03 17:51:38 2019 +0000
Revision:
0:3887ebee1b37
fly bird

Who changed what in which revision?

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