RUOFAN LI
/
el17rl
Li Ruofan 201199450
N5110/N5110.cpp@7:e3844250b77d, 2020-05-15 (annotated)
- Committer:
- DannyLee
- Date:
- Fri May 15 22:23:22 2020 +0000
- Revision:
- 7:e3844250b77d
- Parent:
- 0:44c1a60f8732
Still has problems, actually I can't compile it
Who changed what in which revision?
User | Revision | Line number | New 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 | 0:44c1a60f8732 | 201 | |
DannyLee | 0:44c1a60f8732 | 202 | void N5110::clearPixel(unsigned int const x, |
DannyLee | 0:44c1a60f8732 | 203 | unsigned int const y) |
DannyLee | 0:44c1a60f8732 | 204 | { |
DannyLee | 0:44c1a60f8732 | 205 | if (x<WIDTH && y<HEIGHT) { // check within range |
DannyLee | 0:44c1a60f8732 | 206 | // calculate bank and shift 1 to required position (using bit clear) |
DannyLee | 0:44c1a60f8732 | 207 | buffer[x][y/8] &= ~(1 << y%8); |
DannyLee | 0:44c1a60f8732 | 208 | } |
DannyLee | 0:44c1a60f8732 | 209 | } |
DannyLee | 0:44c1a60f8732 | 210 | |
DannyLee | 0:44c1a60f8732 | 211 | int N5110::getPixel(unsigned int const x, |
DannyLee | 0:44c1a60f8732 | 212 | unsigned int const y) const |
DannyLee | 0:44c1a60f8732 | 213 | { |
DannyLee | 0:44c1a60f8732 | 214 | if (x<WIDTH && y<HEIGHT) { // check within range |
DannyLee | 0:44c1a60f8732 | 215 | // return relevant bank and mask required bit |
DannyLee | 0:44c1a60f8732 | 216 | |
DannyLee | 0:44c1a60f8732 | 217 | int pixel = (int) buffer[x][y/8] & (1 << y%8); |
DannyLee | 0:44c1a60f8732 | 218 | |
DannyLee | 0:44c1a60f8732 | 219 | if (pixel) |
DannyLee | 0:44c1a60f8732 | 220 | return 1; |
DannyLee | 0:44c1a60f8732 | 221 | else |
DannyLee | 0:44c1a60f8732 | 222 | return 0; |
DannyLee | 0:44c1a60f8732 | 223 | } |
DannyLee | 0:44c1a60f8732 | 224 | |
DannyLee | 0:44c1a60f8732 | 225 | return 0; |
DannyLee | 0:44c1a60f8732 | 226 | |
DannyLee | 0:44c1a60f8732 | 227 | } |
DannyLee | 0:44c1a60f8732 | 228 | |
DannyLee | 0:44c1a60f8732 | 229 | // function to refresh the display |
DannyLee | 0:44c1a60f8732 | 230 | void N5110::refresh() |
DannyLee | 0:44c1a60f8732 | 231 | { |
DannyLee | 0:44c1a60f8732 | 232 | setXYAddress(0,0); // important to set address back to 0,0 before refreshing display |
DannyLee | 0:44c1a60f8732 | 233 | // address auto increments after printing string, so buffer[0][0] will not coincide |
DannyLee | 0:44c1a60f8732 | 234 | // with top-left pixel after priting string |
DannyLee | 0:44c1a60f8732 | 235 | |
DannyLee | 0:44c1a60f8732 | 236 | _sce->write(0); //set CE low to begin frame |
DannyLee | 0:44c1a60f8732 | 237 | |
DannyLee | 0:44c1a60f8732 | 238 | for(int j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing |
DannyLee | 0:44c1a60f8732 | 239 | for(int i = 0; i < WIDTH; i++) { |
DannyLee | 0:44c1a60f8732 | 240 | _spi->write(buffer[i][j]); // send buffer |
DannyLee | 0:44c1a60f8732 | 241 | } |
DannyLee | 0:44c1a60f8732 | 242 | } |
DannyLee | 0:44c1a60f8732 | 243 | _sce->write(1); // set CE high to end frame |
DannyLee | 0:44c1a60f8732 | 244 | |
DannyLee | 0:44c1a60f8732 | 245 | } |
DannyLee | 0:44c1a60f8732 | 246 | |
DannyLee | 0:44c1a60f8732 | 247 | // fills the buffer with random bytes. Can be used to test the display. |
DannyLee | 0:44c1a60f8732 | 248 | // The rand() function isn't seeded so it probably creates the same pattern everytime |
DannyLee | 0:44c1a60f8732 | 249 | void N5110::randomiseBuffer() |
DannyLee | 0:44c1a60f8732 | 250 | { |
DannyLee | 0:44c1a60f8732 | 251 | int i,j; |
DannyLee | 0:44c1a60f8732 | 252 | for(j = 0; j < BANKS; j++) { // be careful to use correct order (j,i) for horizontal addressing |
DannyLee | 0:44c1a60f8732 | 253 | for(i = 0; i < WIDTH; i++) { |
DannyLee | 0:44c1a60f8732 | 254 | buffer[i][j] = rand()%256; // generate random byte |
DannyLee | 0:44c1a60f8732 | 255 | } |
DannyLee | 0:44c1a60f8732 | 256 | } |
DannyLee | 0:44c1a60f8732 | 257 | |
DannyLee | 0:44c1a60f8732 | 258 | } |
DannyLee | 0:44c1a60f8732 | 259 | |
DannyLee | 0:44c1a60f8732 | 260 | // function to print 5x7 font |
DannyLee | 0:44c1a60f8732 | 261 | void N5110::printChar(char const c, |
DannyLee | 0:44c1a60f8732 | 262 | unsigned int const x, |
DannyLee | 0:44c1a60f8732 | 263 | unsigned int const y) |
DannyLee | 0:44c1a60f8732 | 264 | { |
DannyLee | 0:44c1a60f8732 | 265 | if (y<BANKS) { // check if printing in range of y banks |
DannyLee | 0:44c1a60f8732 | 266 | |
DannyLee | 0:44c1a60f8732 | 267 | for (int i = 0; i < 5 ; i++ ) { |
DannyLee | 0:44c1a60f8732 | 268 | int pixel_x = x+i; |
DannyLee | 0:44c1a60f8732 | 269 | if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83) |
DannyLee | 0:44c1a60f8732 | 270 | break; |
DannyLee | 0:44c1a60f8732 | 271 | buffer[pixel_x][y] = font5x7[(c - 32)*5 + i]; |
DannyLee | 0:44c1a60f8732 | 272 | // array is offset by 32 relative to ASCII, each character is 5 pixels wide |
DannyLee | 0:44c1a60f8732 | 273 | } |
DannyLee | 0:44c1a60f8732 | 274 | |
DannyLee | 0:44c1a60f8732 | 275 | } |
DannyLee | 0:44c1a60f8732 | 276 | } |
DannyLee | 0:44c1a60f8732 | 277 | |
DannyLee | 0:44c1a60f8732 | 278 | // function to print string at specified position |
DannyLee | 0:44c1a60f8732 | 279 | void N5110::printString(const char *str, |
DannyLee | 0:44c1a60f8732 | 280 | unsigned int const x, |
DannyLee | 0:44c1a60f8732 | 281 | unsigned int const y) |
DannyLee | 0:44c1a60f8732 | 282 | { |
DannyLee | 0:44c1a60f8732 | 283 | if (y<BANKS) { // check if printing in range of y banks |
DannyLee | 0:44c1a60f8732 | 284 | |
DannyLee | 0:44c1a60f8732 | 285 | int n = 0 ; // counter for number of characters in string |
DannyLee | 0:44c1a60f8732 | 286 | // loop through string and print character |
DannyLee | 0:44c1a60f8732 | 287 | while(*str) { |
DannyLee | 0:44c1a60f8732 | 288 | |
DannyLee | 0:44c1a60f8732 | 289 | // writes the character bitmap data to the buffer, so that |
DannyLee | 0:44c1a60f8732 | 290 | // text and pixels can be displayed at the same time |
DannyLee | 0:44c1a60f8732 | 291 | for (int i = 0; i < 5 ; i++ ) { |
DannyLee | 0:44c1a60f8732 | 292 | int pixel_x = x+i+n*6; |
DannyLee | 0:44c1a60f8732 | 293 | if (pixel_x > WIDTH-1) // ensure pixel isn't outside the buffer size (0 - 83) |
DannyLee | 0:44c1a60f8732 | 294 | break; |
DannyLee | 0:44c1a60f8732 | 295 | buffer[pixel_x][y] = font5x7[(*str - 32)*5 + i]; |
DannyLee | 0:44c1a60f8732 | 296 | } |
DannyLee | 0:44c1a60f8732 | 297 | str++; // go to next character in string |
DannyLee | 0:44c1a60f8732 | 298 | n++; // increment index |
DannyLee | 0:44c1a60f8732 | 299 | } |
DannyLee | 0:44c1a60f8732 | 300 | } |
DannyLee | 0:44c1a60f8732 | 301 | } |
DannyLee | 0:44c1a60f8732 | 302 | |
DannyLee | 0:44c1a60f8732 | 303 | // function to clear the screen buffer |
DannyLee | 0:44c1a60f8732 | 304 | void N5110::clear() |
DannyLee | 0:44c1a60f8732 | 305 | { |
DannyLee | 0:44c1a60f8732 | 306 | memset(buffer,0,sizeof(buffer)); |
DannyLee | 0:44c1a60f8732 | 307 | } |
DannyLee | 0:44c1a60f8732 | 308 | |
DannyLee | 0:44c1a60f8732 | 309 | // function to plot array on display |
DannyLee | 0:44c1a60f8732 | 310 | void N5110::plotArray(float const array[]) |
DannyLee | 0:44c1a60f8732 | 311 | { |
DannyLee | 0:44c1a60f8732 | 312 | for (int i=0; i<WIDTH; i++) { // loop through array |
DannyLee | 0:44c1a60f8732 | 313 | // elements are normalised from 0.0 to 1.0, so multiply |
DannyLee | 0:44c1a60f8732 | 314 | // by 47 to convert to pixel range, and subtract from 47 |
DannyLee | 0:44c1a60f8732 | 315 | // since top-left is 0,0 in the display geometry |
DannyLee | 0:44c1a60f8732 | 316 | setPixel(i,47 - int(array[i]*47.0f)); |
DannyLee | 0:44c1a60f8732 | 317 | } |
DannyLee | 0:44c1a60f8732 | 318 | |
DannyLee | 0:44c1a60f8732 | 319 | } |
DannyLee | 0:44c1a60f8732 | 320 | |
DannyLee | 0:44c1a60f8732 | 321 | // function to draw circle |
DannyLee | 0:44c1a60f8732 | 322 | void N5110:: drawCircle(unsigned int const x0, |
DannyLee | 0:44c1a60f8732 | 323 | unsigned int const y0, |
DannyLee | 0:44c1a60f8732 | 324 | unsigned int const radius, |
DannyLee | 0:44c1a60f8732 | 325 | FillType const fill) |
DannyLee | 0:44c1a60f8732 | 326 | { |
DannyLee | 0:44c1a60f8732 | 327 | // from http://en.wikipedia.org/wiki/Midpoint_circle_algorithm |
DannyLee | 0:44c1a60f8732 | 328 | int x = radius; |
DannyLee | 0:44c1a60f8732 | 329 | int y = 0; |
DannyLee | 0:44c1a60f8732 | 330 | int radiusError = 1-x; |
DannyLee | 0:44c1a60f8732 | 331 | |
DannyLee | 0:44c1a60f8732 | 332 | while(x >= y) { |
DannyLee | 0:44c1a60f8732 | 333 | |
DannyLee | 0:44c1a60f8732 | 334 | // if transparent, just draw outline |
DannyLee | 0:44c1a60f8732 | 335 | if (fill == FILL_TRANSPARENT) { |
DannyLee | 0:44c1a60f8732 | 336 | setPixel( x + x0, y + y0); |
DannyLee | 0:44c1a60f8732 | 337 | setPixel(-x + x0, y + y0); |
DannyLee | 0:44c1a60f8732 | 338 | setPixel( y + x0, x + y0); |
DannyLee | 0:44c1a60f8732 | 339 | setPixel(-y + x0, x + y0); |
DannyLee | 0:44c1a60f8732 | 340 | setPixel(-y + x0, -x + y0); |
DannyLee | 0:44c1a60f8732 | 341 | setPixel( y + x0, -x + y0); |
DannyLee | 0:44c1a60f8732 | 342 | setPixel( x + x0, -y + y0); |
DannyLee | 0:44c1a60f8732 | 343 | setPixel(-x + x0, -y + y0); |
DannyLee | 0:44c1a60f8732 | 344 | } else { // drawing filled circle, so draw lines between points at same y value |
DannyLee | 0:44c1a60f8732 | 345 | |
DannyLee | 0:44c1a60f8732 | 346 | int type = (fill==FILL_BLACK) ? 1:0; // black or white fill |
DannyLee | 0:44c1a60f8732 | 347 | |
DannyLee | 0:44c1a60f8732 | 348 | drawLine(x+x0,y+y0,-x+x0,y+y0,type); |
DannyLee | 0:44c1a60f8732 | 349 | drawLine(y+x0,x+y0,-y+x0,x+y0,type); |
DannyLee | 0:44c1a60f8732 | 350 | drawLine(y+x0,-x+y0,-y+x0,-x+y0,type); |
DannyLee | 0:44c1a60f8732 | 351 | drawLine(x+x0,-y+y0,-x+x0,-y+y0,type); |
DannyLee | 0:44c1a60f8732 | 352 | } |
DannyLee | 0:44c1a60f8732 | 353 | |
DannyLee | 0:44c1a60f8732 | 354 | y++; |
DannyLee | 0:44c1a60f8732 | 355 | if (radiusError<0) { |
DannyLee | 0:44c1a60f8732 | 356 | radiusError += 2 * y + 1; |
DannyLee | 0:44c1a60f8732 | 357 | } else { |
DannyLee | 0:44c1a60f8732 | 358 | x--; |
DannyLee | 0:44c1a60f8732 | 359 | radiusError += 2 * (y - x) + 1; |
DannyLee | 0:44c1a60f8732 | 360 | } |
DannyLee | 0:44c1a60f8732 | 361 | } |
DannyLee | 0:44c1a60f8732 | 362 | |
DannyLee | 0:44c1a60f8732 | 363 | } |
DannyLee | 0:44c1a60f8732 | 364 | |
DannyLee | 0:44c1a60f8732 | 365 | void N5110::drawLine(unsigned int const x0, |
DannyLee | 0:44c1a60f8732 | 366 | unsigned int const y0, |
DannyLee | 0:44c1a60f8732 | 367 | unsigned int const x1, |
DannyLee | 0:44c1a60f8732 | 368 | unsigned int const y1, |
DannyLee | 0:44c1a60f8732 | 369 | unsigned int const type) |
DannyLee | 0:44c1a60f8732 | 370 | { |
DannyLee | 0:44c1a60f8732 | 371 | int y_range = y1-y0; // calc range of y and x |
DannyLee | 0:44c1a60f8732 | 372 | int x_range = x1-x0; |
DannyLee | 0:44c1a60f8732 | 373 | int start,stop,step; |
DannyLee | 0:44c1a60f8732 | 374 | |
DannyLee | 0:44c1a60f8732 | 375 | // if dotted line, set step to 2, else step is 1 |
DannyLee | 0:44c1a60f8732 | 376 | step = (type==2) ? 2:1; |
DannyLee | 0:44c1a60f8732 | 377 | |
DannyLee | 0:44c1a60f8732 | 378 | // make sure we loop over the largest range to get the most pixels on the display |
DannyLee | 0:44c1a60f8732 | 379 | // for instance, if drawing a vertical line (x_range = 0), we need to loop down the y pixels |
DannyLee | 0:44c1a60f8732 | 380 | // or else we'll only end up with 1 pixel in the x column |
DannyLee | 0:44c1a60f8732 | 381 | if ( abs(x_range) > abs(y_range) ) { |
DannyLee | 0:44c1a60f8732 | 382 | |
DannyLee | 0:44c1a60f8732 | 383 | // ensure we loop from smallest to largest or else for-loop won't run as expected |
DannyLee | 0:44c1a60f8732 | 384 | start = x1>x0 ? x0:x1; |
DannyLee | 0:44c1a60f8732 | 385 | stop = x1>x0 ? x1:x0; |
DannyLee | 0:44c1a60f8732 | 386 | |
DannyLee | 0:44c1a60f8732 | 387 | // loop between x pixels |
DannyLee | 0:44c1a60f8732 | 388 | for (int x = start; x<= stop ; x+=step) { |
DannyLee | 0:44c1a60f8732 | 389 | // do linear interpolation |
DannyLee | 0:44c1a60f8732 | 390 | int y = y0 + (y1-y0)*(x-x0)/(x1-x0); |
DannyLee | 0:44c1a60f8732 | 391 | |
DannyLee | 0:44c1a60f8732 | 392 | if (type == 0) // if 'white' line, turn off pixel |
DannyLee | 0:44c1a60f8732 | 393 | clearPixel(x,y); |
DannyLee | 0:44c1a60f8732 | 394 | else |
DannyLee | 0:44c1a60f8732 | 395 | setPixel(x,y); // else if 'black' or 'dotted' turn on pixel |
DannyLee | 0:44c1a60f8732 | 396 | } |
DannyLee | 0:44c1a60f8732 | 397 | } else { |
DannyLee | 0:44c1a60f8732 | 398 | |
DannyLee | 0:44c1a60f8732 | 399 | // ensure we loop from smallest to largest or else for-loop won't run as expected |
DannyLee | 0:44c1a60f8732 | 400 | start = y1>y0 ? y0:y1; |
DannyLee | 0:44c1a60f8732 | 401 | stop = y1>y0 ? y1:y0; |
DannyLee | 0:44c1a60f8732 | 402 | |
DannyLee | 0:44c1a60f8732 | 403 | for (int y = start; y<= stop ; y+=step) { |
DannyLee | 0:44c1a60f8732 | 404 | // do linear interpolation |
DannyLee | 0:44c1a60f8732 | 405 | int x = x0 + (x1-x0)*(y-y0)/(y1-y0); |
DannyLee | 0:44c1a60f8732 | 406 | |
DannyLee | 0:44c1a60f8732 | 407 | if (type == 0) // if 'white' line, turn off pixel |
DannyLee | 0:44c1a60f8732 | 408 | clearPixel(x,y); |
DannyLee | 0:44c1a60f8732 | 409 | else |
DannyLee | 0:44c1a60f8732 | 410 | setPixel(x,y); // else if 'black' or 'dotted' turn on pixel |
DannyLee | 0:44c1a60f8732 | 411 | |
DannyLee | 0:44c1a60f8732 | 412 | } |
DannyLee | 0:44c1a60f8732 | 413 | } |
DannyLee | 0:44c1a60f8732 | 414 | |
DannyLee | 0:44c1a60f8732 | 415 | } |
DannyLee | 0:44c1a60f8732 | 416 | |
DannyLee | 0:44c1a60f8732 | 417 | void N5110::drawRect(unsigned int const x0, |
DannyLee | 0:44c1a60f8732 | 418 | unsigned int const y0, |
DannyLee | 0:44c1a60f8732 | 419 | unsigned int const width, |
DannyLee | 0:44c1a60f8732 | 420 | unsigned int const height, |
DannyLee | 0:44c1a60f8732 | 421 | FillType const fill) |
DannyLee | 0:44c1a60f8732 | 422 | { |
DannyLee | 0:44c1a60f8732 | 423 | if (fill == FILL_TRANSPARENT) { // transparent, just outline |
DannyLee | 0:44c1a60f8732 | 424 | drawLine(x0,y0,x0+(width-1),y0,1); // top |
DannyLee | 0:44c1a60f8732 | 425 | drawLine(x0,y0+(height-1),x0+(width-1),y0+(height-1),1); // bottom |
DannyLee | 0:44c1a60f8732 | 426 | drawLine(x0,y0,x0,y0+(height-1),1); // left |
DannyLee | 0:44c1a60f8732 | 427 | drawLine(x0+(width-1),y0,x0+(width-1),y0+(height-1),1); // right |
DannyLee | 0:44c1a60f8732 | 428 | } else { // filled rectangle |
DannyLee | 0:44c1a60f8732 | 429 | int type = (fill==FILL_BLACK) ? 1:0; // black or white fill |
DannyLee | 0:44c1a60f8732 | 430 | for (int y = y0; y<y0+height; y++) { // loop through rows of rectangle |
DannyLee | 0:44c1a60f8732 | 431 | drawLine(x0,y,x0+(width-1),y,type); // draw line across screen |
DannyLee | 0:44c1a60f8732 | 432 | } |
DannyLee | 0:44c1a60f8732 | 433 | } |
DannyLee | 0:44c1a60f8732 | 434 | } |