Li Ruofan 201199450

Dependencies:   mbed Gamepad Joystick

Committer:
DannyLee
Date:
Sat May 16 17:26:10 2020 +0000
Revision:
8:b4a2954dd74f
Parent:
0:44c1a60f8732
Debugging

Who changed what in which revision?

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