ELEC2645 (2018/19) / Mbed 2 deprecated el17cd

Dependencies:   mbed

Committer:
el17cd
Date:
Tue Feb 26 09:35:03 2019 +0000
Revision:
8:a667bc5050c1
Parent:
0:efb5eec6b8ea
added prototype asteroid idea;

Who changed what in which revision?

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