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