Matis Requis 201241242

Dependencies:   mbed

Tempest Game

Game Screen

https://os.mbed.com/media/uploads/MatisRequis/tempest_board_wiki.png The board is made of 12 columns. The Hero stays at the top of the column

Game Controls

https://os.mbed.com/media/uploads/MatisRequis/gamepad_buttons.png

To control the hero spaceship point the joystick to the column you want the hero to go to.

Press the A button to shoot a bullet in the column you are currently in.

Committer:
MatisRequis
Date:
Fri May 15 11:14:58 2020 +0000
Revision:
1:03d1c29c2f8a
Basic Files

Who changed what in which revision?

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