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