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