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