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