ELEC2645 (2018/19) / Mbed 2 deprecated el18jz_

Dependencies:   mbed

Committer:
jiaxinZHOU
Date:
Wed May 08 21:44:02 2019 +0000
Revision:
0:07c4fef6c0af
1st edition

Who changed what in which revision?

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