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