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