Yunting Zou 201199716

Dependencies:   mbed MotionSensor

Committer:
zhouyun123
Date:
Thu May 14 17:18:58 2020 +0000
Revision:
3:4bd22344278d
Parent:
0:047e14f53977
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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