Aaron Reavley / Mbed 2 deprecated FingerPrintSenor

Dependencies:   mbed

Committer:
Reavley
Date:
Mon May 06 16:31:59 2019 +0000
Revision:
0:1c1e369b9269
University of Lincoln, ELE3006M, Smart Electronics; Fingerprint smart assistive device.

Who changed what in which revision?

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