publish my project

Dependencies:   mbed

Committer:
dongyuhu
Date:
Wed Apr 29 10:51:02 2020 +0000
Revision:
7:4b92c1ee6231
Parent:
0:9e95a5ef2659
publish

Who changed what in which revision?

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