Du Xianjie 201089059

Dependencies:   mbed FXOS8700Q

Committer:
VivianDu
Date:
Mon May 06 04:25:09 2019 +0000
Revision:
13:9a6aff771774
Parent:
0:bc1d36f5f772
Final Submission. I have read and agreed with Statement of Academic Integrity.

Who changed what in which revision?

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