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