Final Commit

Dependencies:   mbed

Committer:
JRM1986
Date:
Tue May 08 12:32:46 2018 +0000
Revision:
27:bd0f69a75d8b
Parent:
0:53ee0f689634
Final Commit

Who changed what in which revision?

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