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