Snake vs Block Game to be run upon K64F.

Dependencies:   mbed

Committer:
AhmedPlaymaker
Date:
Thu Mar 07 19:05:10 2019 +0000
Revision:
0:4b15c2d4aa58
initial commit

Who changed what in which revision?

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