FINAL VERSION

Dependencies:   mbed

Committer:
jamesheavey
Date:
Thu May 09 14:36:51 2019 +0000
Revision:
140:d8634e76ecce
Parent:
0:7d4d2023ed9c
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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