demo

Dependencies:   mbed

Committer:
sjuzyz
Date:
Mon Apr 27 13:53:33 2020 +0000
Revision:
4:7743a7670ae7
Parent:
0:6fe791dc9958
demo

Who changed what in which revision?

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