ELEC2645 (2019/20) / Mbed 2 deprecated ELEC2645_Project_el18nw

Dependencies:   mbed

Committer:
Nicholas75179
Date:
Fri May 22 09:42:51 2020 +0000
Revision:
0:fde420d18f42
final commit;

Who changed what in which revision?

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