Dependencies:   mbed ELEC2645_Project_el17my

Committer:
yumaowei
Date:
Tue May 26 07:24:21 2020 +0000
Revision:
2:5e54476c518f
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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