This is test version of Pokemongo game. ELEC 2645 final project.

Dependencies:   Tone

Committer:
shalwego
Date:
Thu Apr 15 15:35:12 2021 +0000
Revision:
0:819c2d6a69ac
Issue about music playing

Who changed what in which revision?

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