Zikang Qian / Mbed 2 deprecated el17z2q

Dependencies:   mbed

Fork of el17z2q by ELEC2645 (2017/18)

Committer:
yzjdxl
Date:
Mon May 07 22:11:43 2018 +0000
Branch:
GameEngine
Revision:
2:6dc7bc55c1cb
Parent:
0:f707ce1010fa
publish

Who changed what in which revision?

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