200943373MAZE

Dependencies:   mbed

Fork of 200943373MAZE by hongyun AHN

Committer:
hongyunAHN
Date:
Thu May 04 14:08:31 2017 +0000
Revision:
1:bd92ef8d00ac
200943373  hongyun AHN

Who changed what in which revision?

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