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