ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18lg

Dependencies:   mbed

Committer:
el18lg
Date:
Sat May 23 17:21:24 2020 +0000
Revision:
1:bdafa20e71a0
Started my map, making my way onto my snake programme;

Who changed what in which revision?

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