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