Ahmed Hedait / Mbed 2 deprecated el16ah

Dependencies:   mbed

Committer:
ahmedhedait
Date:
Tue Apr 10 21:59:06 2018 +0000
Revision:
0:1b04b9bacf5a
initial commit

Who changed what in which revision?

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