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