ELEC2645 (2018/19) / Mbed 2 deprecated el17ebs

Dependencies:   mbed FATFileSystem

Committer:
ellisbhastroud
Date:
Tue Mar 12 14:38:03 2019 +0000
Revision:
0:c6ceddb241df
initial commit

Who changed what in which revision?

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