Coursework

Committer:
sesa514652
Date:
Fri Feb 04 16:44:14 2022 +0000
Revision:
45:f1db729741f7
Parent:
0:1f799c7cce2b
Submission

Who changed what in which revision?

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