Yang Zhenye 201199680

Dependencies:   mbed

Committer:
yangzhenye
Date:
Fri Apr 24 06:58:33 2020 +0000
Revision:
0:ac2868313b41
Child:
5:fcad75e9b9e1
Roony's model

Who changed what in which revision?

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