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