James Heavey / Mbed 2 deprecated 2665-Breakout-Game

Dependencies:   mbed

Committer:
jamesheavey
Date:
Tue Jan 05 01:14:11 2021 +0000
Revision:
0:92b180c8d407
test

Who changed what in which revision?

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