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