6666666666

Dependencies:   Joystick

Committer:
Jenny121
Date:
Mon May 06 09:40:20 2019 +0000
Revision:
1:dfa872e88821
6666666666666666

Who changed what in which revision?

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