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