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