Nokia 5110 Library

Committer:
tomazitas
Date:
Fri Jan 14 21:36:29 2022 +0000
Revision:
51:f3b30020324f
Parent:
50:c299b1099189
...

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