Library for interfacing to Nokia 5110 LCD display (as found on the SparkFun website).

Dependents:   LV7_LCDtest LV7_Grupa5_Tim003_Zadatak1 lv7_Grupa5_Tim008_zad1 LV7_PAI_Grupa5_tim10_Zadatak1 ... more

This library is designed to make it easy to interface an mbed with a Nokia 5110 LCD display.

These can be found at Sparkfun (https://www.sparkfun.com/products/10168) and Adafruit (http://www.adafruit.com/product/338).

The library uses the SPI peripheral on the mbed which means it is much faster sending data to the display than other libraries available on other platforms that use software SPI.

The library can print strings as well as controlling individual pixels, meaning that both text and primitive graphics can be displayed.

Committer:
eencae
Date:
Wed Mar 01 14:41:31 2017 +0000
Revision:
34:626b873e6362
Parent:
30:11986573659e
Parent:
29:5bc91bd44c77
Re-merge

Who changed what in which revision?

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