Li Ruofan 201199450

Dependencies:   mbed

Committer:
DannyLee
Date:
Sun May 24 08:16:35 2020 +0000
Revision:
1:bd7c99a5bd10
Parent:
0:80a59a49d504
Li Ruofan 201199450

Who changed what in which revision?

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