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