ZIYI CHEN ml17z4c 201214999

Dependencies:   mbed

Committer:
ziyi11
Date:
Thu May 09 14:36:47 2019 +0000
Revision:
13:08bdb4cffacd
try;

Who changed what in which revision?

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