ELEC2645 (2018/19) / Mbed 2 deprecated 2645_Project_SiutingWong201186503

Dependencies:   mbed

Committer:
davidwst421
Date:
Wed May 08 18:50:21 2019 +0000
Revision:
0:fd8eda608206
Child:
6:a0f3dbbc8d33
12

Who changed what in which revision?

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