a modified version of N5110 library to be used with freescale boards
Fork of N5110 by
N5110.cpp@8:2993b19ba7dd, 2015-05-14 (annotated)
- Committer:
- pai2015eminahuskic
- Date:
- Thu May 14 17:10:24 2015 +0000
- Revision:
- 8:2993b19ba7dd
- Parent:
- 7:77bd2c73fe41
LV8 Grupa7 Tim008 Zadatak3 Huskic Emina i Dizdarevic Dino
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eencae | 5:6ea180eef702 | 1 | /** |
eencae | 5:6ea180eef702 | 2 | @file N5110.cpp |
eencae | 5:6ea180eef702 | 3 | |
eencae | 5:6ea180eef702 | 4 | @brief Member functions implementations |
eencae | 5:6ea180eef702 | 5 | |
eencae | 5:6ea180eef702 | 6 | */ |
eencae | 0:d563e74f0ae9 | 7 | #include "mbed.h" |
eencae | 0:d563e74f0ae9 | 8 | #include "N5110.h" |
eencae | 0:d563e74f0ae9 | 9 | |
pai2015eminahuskic | 8:2993b19ba7dd | 10 | #define M_PI 3.14 |
eencae | 1:df68f34cd32d | 11 | N5110::N5110(PinName pwrPin, PinName scePin, PinName rstPin, PinName dcPin, PinName mosiPin, PinName sclkPin, PinName ledPin) |
eencae | 0:d563e74f0ae9 | 12 | { |
eencae | 0:d563e74f0ae9 | 13 | |
eencae | 0:d563e74f0ae9 | 14 | spi = new SPI(mosiPin,NC,sclkPin); // create new SPI instance and initialise |
Francesco Fantoni |
7:77bd2c73fe41 | 15 | spi->format(LCD_SPI_BITS, LCD_SPI_MODE); |
Francesco Fantoni |
7:77bd2c73fe41 | 16 | spi->frequency(LCD_FREQ); |
Francesco Fantoni |
7:77bd2c73fe41 | 17 | |
eencae | 6:adb79338d40f | 18 | // set up pins as required |
eencae | 0:d563e74f0ae9 | 19 | led = new PwmOut(ledPin); |
Francesco Fantoni |
7:77bd2c73fe41 | 20 | //pwr = new DigitalOut(pwrPin); |
eencae | 0:d563e74f0ae9 | 21 | sce = new DigitalOut(scePin); |
eencae | 0:d563e74f0ae9 | 22 | rst = new DigitalOut(rstPin); |
eencae | 0:d563e74f0ae9 | 23 | dc = new DigitalOut(dcPin); |
Francesco Fantoni |
7:77bd2c73fe41 | 24 | W = 83; |
Francesco Fantoni |
7:77bd2c73fe41 | 25 | H = 47; |
eencae | 0:d563e74f0ae9 | 26 | |
eencae | 0:d563e74f0ae9 | 27 | } |
eencae | 0:d563e74f0ae9 | 28 | |
eencae | 6:adb79338d40f | 29 | // initialise function - powers up and sends the initialisation commands |
eencae | 0:d563e74f0ae9 | 30 | void N5110::init() |
eencae | 0:d563e74f0ae9 | 31 | { |
eencae | 6:adb79338d40f | 32 | turnOn(); // power up |
eencae | 6:adb79338d40f | 33 | wait_ms(10); // small delay seems to prevent spurious pixels during mbed reset |
eencae | 6:adb79338d40f | 34 | reset(); // reset LCD - must be done within 100 ms |
eencae | 0:d563e74f0ae9 | 35 | |
eencae | 0:d563e74f0ae9 | 36 | // function set - extended |
eencae | 0:d563e74f0ae9 | 37 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); |
eencae | 6:adb79338d40f | 38 | // Don't completely understand these parameters - they seem to work as they are |
eencae | 6:adb79338d40f | 39 | // Consult the datasheet if you need to change them |
eencae | 1:df68f34cd32d | 40 | sendCommand(CMD_VOP_7V38); // operating voltage - these values are from Chris Yan's Library |
eencae | 0:d563e74f0ae9 | 41 | sendCommand(CMD_TC_TEMP_2); // temperature control |
eencae | 0:d563e74f0ae9 | 42 | sendCommand(CMD_BI_MUX_48); // bias |
eencae | 0:d563e74f0ae9 | 43 | |
eencae | 0:d563e74f0ae9 | 44 | // function set - basic |
eencae | 0:d563e74f0ae9 | 45 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); |
eencae | 1:df68f34cd32d | 46 | normalMode(); // normal video mode by default |
eencae | 0:d563e74f0ae9 | 47 | sendCommand(CMD_DC_NORMAL_MODE); // black on white |
eencae | 0:d563e74f0ae9 | 48 | |
eencae | 0:d563e74f0ae9 | 49 | // RAM is undefined at power-up so clear |
eencae | 0:d563e74f0ae9 | 50 | clearRAM(); |
eencae | 0:d563e74f0ae9 | 51 | |
eencae | 0:d563e74f0ae9 | 52 | } |
eencae | 1:df68f34cd32d | 53 | |
eencae | 1:df68f34cd32d | 54 | // sets normal video mode (black on white) |
eencae | 1:df68f34cd32d | 55 | void N5110::normalMode() { |
eencae | 1:df68f34cd32d | 56 | sendCommand(CMD_DC_NORMAL_MODE); |
eencae | 1:df68f34cd32d | 57 | |
eencae | 1:df68f34cd32d | 58 | } |
eencae | 1:df68f34cd32d | 59 | |
eencae | 1:df68f34cd32d | 60 | // sets normal video mode (white on black) |
eencae | 1:df68f34cd32d | 61 | void N5110::inverseMode() { |
eencae | 1:df68f34cd32d | 62 | sendCommand(CMD_DC_INVERT_VIDEO); |
eencae | 1:df68f34cd32d | 63 | } |
eencae | 0:d563e74f0ae9 | 64 | |
eencae | 0:d563e74f0ae9 | 65 | // function to power up the LCD and backlight |
eencae | 0:d563e74f0ae9 | 66 | void N5110::turnOn() |
eencae | 0:d563e74f0ae9 | 67 | { |
eencae | 0:d563e74f0ae9 | 68 | // set brightness of LED - 0.0 to 1.0 - default is 50% |
eencae | 0:d563e74f0ae9 | 69 | setBrightness(0.5); |
Francesco Fantoni |
7:77bd2c73fe41 | 70 | //led->write(0); // apply power |
eencae | 0:d563e74f0ae9 | 71 | } |
eencae | 0:d563e74f0ae9 | 72 | |
eencae | 0:d563e74f0ae9 | 73 | // function to power down LCD |
eencae | 0:d563e74f0ae9 | 74 | void N5110::turnOff() |
eencae | 0:d563e74f0ae9 | 75 | { |
Francesco Fantoni |
7:77bd2c73fe41 | 76 | setBrightness(1.0); // turn backlight off |
eencae | 0:d563e74f0ae9 | 77 | clearRAM(); // clear RAM to ensure specified current consumption |
eencae | 6:adb79338d40f | 78 | // send command to ensure we are in basic model |
eencae | 0:d563e74f0ae9 | 79 | sendCommand(0x20 | CMD_FS_ACTIVE_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_BASIC_MODE); |
eencae | 6:adb79338d40f | 80 | // clear the display |
eencae | 0:d563e74f0ae9 | 81 | sendCommand(CMD_DC_CLEAR_DISPLAY); |
eencae | 6:adb79338d40f | 82 | // enter the extended mode and power down |
eencae | 0:d563e74f0ae9 | 83 | sendCommand(0x20 | CMD_FS_POWER_DOWN_MODE | CMD_FS_HORIZONTAL_MODE | CMD_FS_EXTENDED_MODE); |
eencae | 6:adb79338d40f | 84 | // small delay and then turn off the power pin |
eencae | 6:adb79338d40f | 85 | wait_ms(10); |
Francesco Fantoni |
7:77bd2c73fe41 | 86 | // pwr->write(1); |
eencae | 0:d563e74f0ae9 | 87 | |
eencae | 0:d563e74f0ae9 | 88 | } |
eencae | 0:d563e74f0ae9 | 89 | |
eencae | 0:d563e74f0ae9 | 90 | // function to change LED backlight brightness |
eencae | 0:d563e74f0ae9 | 91 | void N5110::setBrightness(float brightness) |
eencae | 0:d563e74f0ae9 | 92 | { |
eencae | 0:d563e74f0ae9 | 93 | // check whether brightness is within range |
eencae | 0:d563e74f0ae9 | 94 | if (brightness < 0.0) |
eencae | 0:d563e74f0ae9 | 95 | brightness = 0.0; |
eencae | 0:d563e74f0ae9 | 96 | if (brightness > 1.0) |
eencae | 0:d563e74f0ae9 | 97 | brightness = 1.0; |
eencae | 0:d563e74f0ae9 | 98 | // set PWM duty cycle |
eencae | 0:d563e74f0ae9 | 99 | led->write(brightness); |
eencae | 0:d563e74f0ae9 | 100 | } |
eencae | 0:d563e74f0ae9 | 101 | |
eencae | 0:d563e74f0ae9 | 102 | |
eencae | 0:d563e74f0ae9 | 103 | // pulse the active low reset line |
eencae | 0:d563e74f0ae9 | 104 | void N5110::reset() |
eencae | 0:d563e74f0ae9 | 105 | { |
eencae | 0:d563e74f0ae9 | 106 | rst->write(0); // reset the LCD |
eencae | 0:d563e74f0ae9 | 107 | rst->write(1); |
eencae | 0:d563e74f0ae9 | 108 | } |
eencae | 0:d563e74f0ae9 | 109 | |
Francesco Fantoni |
7:77bd2c73fe41 | 110 | |
eencae | 0:d563e74f0ae9 | 111 | |
eencae | 6:adb79338d40f | 112 | // send a command to the display |
eencae | 0:d563e74f0ae9 | 113 | void N5110::sendCommand(unsigned char command) |
eencae | 0:d563e74f0ae9 | 114 | { |
eencae | 0:d563e74f0ae9 | 115 | dc->write(0); // set DC low for command |
Francesco Fantoni |
7:77bd2c73fe41 | 116 | //sce->write(0); // set CE low to begin frame |
eencae | 0:d563e74f0ae9 | 117 | spi->write(command); // send command |
eencae | 0:d563e74f0ae9 | 118 | dc->write(1); // turn back to data by default |
Francesco Fantoni |
7:77bd2c73fe41 | 119 | //sce->write(1); // set CE high to end frame (expected for transmission of single byte) |
eencae | 0:d563e74f0ae9 | 120 | |
eencae | 0:d563e74f0ae9 | 121 | } |
eencae | 0:d563e74f0ae9 | 122 | |
eencae | 6:adb79338d40f | 123 | // send data to the display at the current XY address |
eencae | 6:adb79338d40f | 124 | // dc is set to 1 (i.e. data) after sending a command and so should |
eencae | 6:adb79338d40f | 125 | // be the default mode. |
eencae | 0:d563e74f0ae9 | 126 | void N5110::sendData(unsigned char data) |
eencae | 0:d563e74f0ae9 | 127 | { |
Francesco Fantoni |
7:77bd2c73fe41 | 128 | //sce->write(0); // set CE low to begin frame |
eencae | 0:d563e74f0ae9 | 129 | spi->write(data); |
Francesco Fantoni |
7:77bd2c73fe41 | 130 | //sce->write(1); // set CE high to end frame (expected for transmission of single byte) |
eencae | 0:d563e74f0ae9 | 131 | } |
eencae | 0:d563e74f0ae9 | 132 | |
eencae | 0:d563e74f0ae9 | 133 | // this function writes 0 to the 504 bytes to clear the RAM |
eencae | 0:d563e74f0ae9 | 134 | void N5110::clearRAM() |
eencae | 0:d563e74f0ae9 | 135 | { |
eencae | 0:d563e74f0ae9 | 136 | int i; |
Francesco Fantoni |
7:77bd2c73fe41 | 137 | //sce->write(0); //set CE low to begin frame |
eencae | 0:d563e74f0ae9 | 138 | for(i = 0; i < 504; i++) { // 48 x 84 bits = 504 bytes |
eencae | 0:d563e74f0ae9 | 139 | spi->write(0x00); // send 0's |
eencae | 0:d563e74f0ae9 | 140 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 141 | //sce->write(1); // set CE high to end frame |
eencae | 0:d563e74f0ae9 | 142 | |
eencae | 0:d563e74f0ae9 | 143 | } |
eencae | 0:d563e74f0ae9 | 144 | |
eencae | 6:adb79338d40f | 145 | // function to set the XY address in RAM for subsequenct data write |
eencae | 0:d563e74f0ae9 | 146 | void N5110::setXYAddress(int x, int y) |
eencae | 0:d563e74f0ae9 | 147 | { |
eencae | 0:d563e74f0ae9 | 148 | // check whether address is in range |
eencae | 0:d563e74f0ae9 | 149 | if (x > 83) |
eencae | 0:d563e74f0ae9 | 150 | x=83; |
eencae | 0:d563e74f0ae9 | 151 | if (y > 5) |
eencae | 0:d563e74f0ae9 | 152 | y=5; |
eencae | 3:f90dd1042d17 | 153 | if (x < 0) |
eencae | 3:f90dd1042d17 | 154 | x=0; |
eencae | 3:f90dd1042d17 | 155 | if (y < 0) |
eencae | 3:f90dd1042d17 | 156 | y=0; |
eencae | 0:d563e74f0ae9 | 157 | |
eencae | 3:f90dd1042d17 | 158 | sendCommand(0x80 | x); // send addresses to display with relevant mask |
eencae | 0:d563e74f0ae9 | 159 | sendCommand(0x40 | y); |
eencae | 0:d563e74f0ae9 | 160 | } |
eencae | 0:d563e74f0ae9 | 161 | |
eencae | 6:adb79338d40f | 162 | // These functions are used to set, clear and get the value of pixels in the display |
eencae | 6:adb79338d40f | 163 | // Pixels are addressed in the range of 0 to 47 (y) and 0 to 83 (x). The refresh() |
eencae | 6:adb79338d40f | 164 | // function must be called after set and clear in order to update the display |
eencae | 0:d563e74f0ae9 | 165 | void N5110::setPixel(int x, int y) |
eencae | 0:d563e74f0ae9 | 166 | { |
eencae | 6:adb79338d40f | 167 | // calculate bank and shift 1 to required position in the data byte |
eencae | 0:d563e74f0ae9 | 168 | buffer[x][y/8] |= (1 << y%8); |
eencae | 0:d563e74f0ae9 | 169 | } |
eencae | 0:d563e74f0ae9 | 170 | |
eencae | 0:d563e74f0ae9 | 171 | void N5110::clearPixel(int x, int y) |
eencae | 0:d563e74f0ae9 | 172 | { |
eencae | 0:d563e74f0ae9 | 173 | // calculate bank and shift 1 to required position (using bit clear) |
eencae | 0:d563e74f0ae9 | 174 | buffer[x][y/8] &= ~(1 << y%8); |
eencae | 0:d563e74f0ae9 | 175 | } |
eencae | 0:d563e74f0ae9 | 176 | |
Francesco Fantoni |
7:77bd2c73fe41 | 177 | void N5110::drawHline(int x, int y, int l) |
Francesco Fantoni |
7:77bd2c73fe41 | 178 | { |
Francesco Fantoni |
7:77bd2c73fe41 | 179 | for(int i=0; i<l; i++) |
Francesco Fantoni |
7:77bd2c73fe41 | 180 | { |
Francesco Fantoni |
7:77bd2c73fe41 | 181 | int ps = x+i; |
Francesco Fantoni |
7:77bd2c73fe41 | 182 | if ((ps < 84 && ps >= 0) && (y<48 && y>=0)) { |
Francesco Fantoni |
7:77bd2c73fe41 | 183 | buffer[ps][y/8] |= (1 << y%8); |
Francesco Fantoni |
7:77bd2c73fe41 | 184 | //refresh(); |
Francesco Fantoni |
7:77bd2c73fe41 | 185 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 186 | |
Francesco Fantoni |
7:77bd2c73fe41 | 187 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 188 | refresh(); |
Francesco Fantoni |
7:77bd2c73fe41 | 189 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 190 | |
Francesco Fantoni |
7:77bd2c73fe41 | 191 | void N5110::drawVline(int x, int y, int l) |
Francesco Fantoni |
7:77bd2c73fe41 | 192 | { |
Francesco Fantoni |
7:77bd2c73fe41 | 193 | for(int i=0; i<l; i++) |
Francesco Fantoni |
7:77bd2c73fe41 | 194 | { |
Francesco Fantoni |
7:77bd2c73fe41 | 195 | int ps = y+i; |
Francesco Fantoni |
7:77bd2c73fe41 | 196 | if ((ps < 48 && ps >= 0) && (x<84 && x>=0)){ |
Francesco Fantoni |
7:77bd2c73fe41 | 197 | buffer[x][ps/8] |= (1 << ps%8); |
Francesco Fantoni |
7:77bd2c73fe41 | 198 | //refresh(); |
Francesco Fantoni |
7:77bd2c73fe41 | 199 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 200 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 201 | refresh(); |
Francesco Fantoni |
7:77bd2c73fe41 | 202 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 203 | |
Francesco Fantoni |
7:77bd2c73fe41 | 204 | void N5110::drawRectangle(int x, int y, int w, int h) |
Francesco Fantoni |
7:77bd2c73fe41 | 205 | { |
Francesco Fantoni |
7:77bd2c73fe41 | 206 | drawHline(x,y,w); |
Francesco Fantoni |
7:77bd2c73fe41 | 207 | drawVline(x+w,y,h); |
Francesco Fantoni |
7:77bd2c73fe41 | 208 | drawHline(x,y+h,w); |
Francesco Fantoni |
7:77bd2c73fe41 | 209 | drawVline(x,y,h); |
Francesco Fantoni |
7:77bd2c73fe41 | 210 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 211 | |
Francesco Fantoni |
7:77bd2c73fe41 | 212 | void N5110::drawGrid(int stepx, int stepy) |
Francesco Fantoni |
7:77bd2c73fe41 | 213 | { |
Francesco Fantoni |
7:77bd2c73fe41 | 214 | for (int y=0; y<47; y += stepy){ |
Francesco Fantoni |
7:77bd2c73fe41 | 215 | for(int x=0; x<84; x++){ |
Francesco Fantoni |
7:77bd2c73fe41 | 216 | setPixel(x,y); |
Francesco Fantoni |
7:77bd2c73fe41 | 217 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 218 | refresh(); |
Francesco Fantoni |
7:77bd2c73fe41 | 219 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 220 | for (int x=0; x<84; x += stepx){ |
Francesco Fantoni |
7:77bd2c73fe41 | 221 | for(int y=0; y<47; y++){ |
Francesco Fantoni |
7:77bd2c73fe41 | 222 | setPixel(x,y); |
Francesco Fantoni |
7:77bd2c73fe41 | 223 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 224 | refresh(); |
Francesco Fantoni |
7:77bd2c73fe41 | 225 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 226 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 227 | |
Francesco Fantoni |
7:77bd2c73fe41 | 228 | // draw line between two points using C implementation of Bresenham's line algorithm |
Francesco Fantoni |
7:77bd2c73fe41 | 229 | void N5110::drawLine(int x0, int y0, int x1, int y1) { |
Francesco Fantoni |
7:77bd2c73fe41 | 230 | |
Francesco Fantoni |
7:77bd2c73fe41 | 231 | int dx = abs(x1-x0), sx = x0<x1 ? 1 : -1; |
Francesco Fantoni |
7:77bd2c73fe41 | 232 | int dy = abs(y1-y0), sy = y0<y1 ? 1 : -1; |
Francesco Fantoni |
7:77bd2c73fe41 | 233 | int err = (dx>dy ? dx : -dy)/2, e2; |
Francesco Fantoni |
7:77bd2c73fe41 | 234 | |
Francesco Fantoni |
7:77bd2c73fe41 | 235 | for(;;){ |
Francesco Fantoni |
7:77bd2c73fe41 | 236 | if ((x0 < 84 && x0 >= 0) && (y0<47 && y0>=0)) { |
Francesco Fantoni |
7:77bd2c73fe41 | 237 | buffer[x0][y0/8] |= (1 << y0%8); |
Francesco Fantoni |
7:77bd2c73fe41 | 238 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 239 | if (x0==x1 && y0==y1) break; |
Francesco Fantoni |
7:77bd2c73fe41 | 240 | e2 = err; |
Francesco Fantoni |
7:77bd2c73fe41 | 241 | if (e2 >-dx) { err -= dy; x0 += sx; } |
Francesco Fantoni |
7:77bd2c73fe41 | 242 | if (e2 < dy) { err += dx; y0 += sy; } |
Francesco Fantoni |
7:77bd2c73fe41 | 243 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 244 | refresh(); |
Francesco Fantoni |
7:77bd2c73fe41 | 245 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 246 | |
Francesco Fantoni |
7:77bd2c73fe41 | 247 | |
Francesco Fantoni |
7:77bd2c73fe41 | 248 | void N5110::drawLineAngle(int x0, int y0, int l, float angle) { |
Francesco Fantoni |
7:77bd2c73fe41 | 249 | float radian = 0-angle * (M_PI/180); |
Francesco Fantoni |
7:77bd2c73fe41 | 250 | int x1 = x0 + (l*cos(radian)); |
Francesco Fantoni |
7:77bd2c73fe41 | 251 | int y1 = y0 + (l*sin(radian)); |
Francesco Fantoni |
7:77bd2c73fe41 | 252 | drawLine(x0,y0,x1,y1); |
Francesco Fantoni |
7:77bd2c73fe41 | 253 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 254 | |
Francesco Fantoni |
7:77bd2c73fe41 | 255 | |
Francesco Fantoni |
7:77bd2c73fe41 | 256 | void N5110::drawCircle(int x, int y, int radius, int divisions) { |
Francesco Fantoni |
7:77bd2c73fe41 | 257 | |
Francesco Fantoni |
7:77bd2c73fe41 | 258 | float angleIncrement = M_PI * 2 / divisions; |
Francesco Fantoni |
7:77bd2c73fe41 | 259 | double tempAngle = 0.0; |
Francesco Fantoni |
7:77bd2c73fe41 | 260 | |
Francesco Fantoni |
7:77bd2c73fe41 | 261 | for (int i = 0; i < divisions; ++i) { |
Francesco Fantoni |
7:77bd2c73fe41 | 262 | double px = cos(tempAngle) * radius + x; |
Francesco Fantoni |
7:77bd2c73fe41 | 263 | double py = sin(tempAngle) * radius + y; |
Francesco Fantoni |
7:77bd2c73fe41 | 264 | if ((px < 84 && px >= 0) && (py<47 && py>=0)) { |
Francesco Fantoni |
7:77bd2c73fe41 | 265 | buffer[(int)px][(int)py/8] |= (1 << (int)py%8); |
Francesco Fantoni |
7:77bd2c73fe41 | 266 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 267 | tempAngle += angleIncrement; |
Francesco Fantoni |
7:77bd2c73fe41 | 268 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 269 | refresh(); |
Francesco Fantoni |
7:77bd2c73fe41 | 270 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 271 | |
eencae | 0:d563e74f0ae9 | 272 | unsigned char N5110::getPixel(int x, int y) |
eencae | 0:d563e74f0ae9 | 273 | { |
eencae | 0:d563e74f0ae9 | 274 | // return relevant bank and mask required bit |
eencae | 0:d563e74f0ae9 | 275 | return buffer[x][y/8] & (1 << y%8); |
eencae | 0:d563e74f0ae9 | 276 | |
eencae | 0:d563e74f0ae9 | 277 | } |
eencae | 0:d563e74f0ae9 | 278 | |
eencae | 6:adb79338d40f | 279 | // function to refresh the display |
eencae | 6:adb79338d40f | 280 | void N5110::refresh() |
eencae | 0:d563e74f0ae9 | 281 | { |
eencae | 0:d563e74f0ae9 | 282 | int i,j; |
Francesco Fantoni |
7:77bd2c73fe41 | 283 | // sce->write(0); //set CE low to begin frame |
eencae | 0:d563e74f0ae9 | 284 | |
eencae | 0:d563e74f0ae9 | 285 | for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing |
eencae | 0:d563e74f0ae9 | 286 | for(i = 0; i < 84; i++) { |
eencae | 0:d563e74f0ae9 | 287 | spi->write(buffer[i][j]); // send buffer |
eencae | 0:d563e74f0ae9 | 288 | } |
eencae | 0:d563e74f0ae9 | 289 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 290 | // sce->write(1); // set CE high to end frame |
eencae | 0:d563e74f0ae9 | 291 | |
eencae | 0:d563e74f0ae9 | 292 | } |
eencae | 0:d563e74f0ae9 | 293 | |
eencae | 6:adb79338d40f | 294 | // fills the buffer with random bytes. Can be used to test the display. |
eencae | 6:adb79338d40f | 295 | // The rand() function isn't seeded so it probably creates the same pattern everytime |
eencae | 0:d563e74f0ae9 | 296 | void N5110::randomiseBuffer() |
eencae | 0:d563e74f0ae9 | 297 | { |
eencae | 0:d563e74f0ae9 | 298 | int i,j; |
eencae | 0:d563e74f0ae9 | 299 | for(j = 0; j < 6; j++) { // be careful to use correct order (j,i) for horizontal addressing |
eencae | 0:d563e74f0ae9 | 300 | for(i = 0; i < 84; i++) { |
eencae | 0:d563e74f0ae9 | 301 | buffer[i][j] = rand()%256; // generate random byte |
eencae | 0:d563e74f0ae9 | 302 | } |
eencae | 0:d563e74f0ae9 | 303 | } |
eencae | 0:d563e74f0ae9 | 304 | |
eencae | 0:d563e74f0ae9 | 305 | } |
eencae | 0:d563e74f0ae9 | 306 | |
eencae | 0:d563e74f0ae9 | 307 | // function to print 5x7 font |
eencae | 0:d563e74f0ae9 | 308 | void N5110::printChar(char c) |
eencae | 0:d563e74f0ae9 | 309 | { |
eencae | 6:adb79338d40f | 310 | int i; |
eencae | 0:d563e74f0ae9 | 311 | // loop through 5 columns |
eencae | 6:adb79338d40f | 312 | for (i = 0; i < 5 ; i++ ) { |
Francesco Fantoni |
7:77bd2c73fe41 | 313 | sendData((font5x7[(c - 32)*5 + i])); |
Francesco Fantoni |
7:77bd2c73fe41 | 314 | // array is offset by 32 relative to ASCII, each character is 5 pixels wide |
Francesco Fantoni |
7:77bd2c73fe41 | 315 | // the X address is automatically incremented after each data write |
Francesco Fantoni |
7:77bd2c73fe41 | 316 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 317 | sendData(0); // send an empty byte to introduce space between characters |
Francesco Fantoni |
7:77bd2c73fe41 | 318 | |
Francesco Fantoni |
7:77bd2c73fe41 | 319 | } |
pai2015eminahuskic | 8:2993b19ba7dd | 320 | void N5110::printChar(char c, int x, int y) |
pai2015eminahuskic | 8:2993b19ba7dd | 321 | { |
pai2015eminahuskic | 8:2993b19ba7dd | 322 | setXYAddress(x,y); |
pai2015eminahuskic | 8:2993b19ba7dd | 323 | printChar(c); |
pai2015eminahuskic | 8:2993b19ba7dd | 324 | |
pai2015eminahuskic | 8:2993b19ba7dd | 325 | } |
pai2015eminahuskic | 8:2993b19ba7dd | 326 | |
Francesco Fantoni |
7:77bd2c73fe41 | 327 | |
Francesco Fantoni |
7:77bd2c73fe41 | 328 | // function to print 6x7 negative font |
Francesco Fantoni |
7:77bd2c73fe41 | 329 | void N5110::printNegChar(char c) |
Francesco Fantoni |
7:77bd2c73fe41 | 330 | { |
Francesco Fantoni |
7:77bd2c73fe41 | 331 | int i; |
Francesco Fantoni |
7:77bd2c73fe41 | 332 | // loop through 5 columns |
Francesco Fantoni |
7:77bd2c73fe41 | 333 | for (i = 0; i < 6 ; i++ ) { |
Francesco Fantoni |
7:77bd2c73fe41 | 334 | sendData(~(font6x7[(c - 32)*6 + i])); |
eencae | 0:d563e74f0ae9 | 335 | // array is offset by 32 relative to ASCII, each character is 5 pixels wide |
eencae | 6:adb79338d40f | 336 | // the X address is automatically incremented after each data write |
eencae | 0:d563e74f0ae9 | 337 | } |
eencae | 1:df68f34cd32d | 338 | sendData(0); // send an empty byte to introduce space between characters |
eencae | 0:d563e74f0ae9 | 339 | |
eencae | 0:d563e74f0ae9 | 340 | } |
eencae | 0:d563e74f0ae9 | 341 | |
eencae | 0:d563e74f0ae9 | 342 | // function to print string at specified position |
eencae | 0:d563e74f0ae9 | 343 | void N5110::printString(const char * str,int x,int y) |
eencae | 0:d563e74f0ae9 | 344 | { |
eencae | 0:d563e74f0ae9 | 345 | int n = 0 ; // counter for number of characters in string |
eencae | 0:d563e74f0ae9 | 346 | // loop through string and print character |
eencae | 0:d563e74f0ae9 | 347 | while(*str) { |
eencae | 0:d563e74f0ae9 | 348 | |
eencae | 0:d563e74f0ae9 | 349 | setXYAddress(x+6*n,y); // leave 1 pixel (6 = 5 + 1) between each character |
eencae | 6:adb79338d40f | 350 | printChar(*str); // print the char - can probably so *str++ and remove next line |
eencae | 0:d563e74f0ae9 | 351 | str++; // go to next character in string |
eencae | 0:d563e74f0ae9 | 352 | n++; // increment index |
eencae | 0:d563e74f0ae9 | 353 | } |
eencae | 0:d563e74f0ae9 | 354 | |
eencae | 0:d563e74f0ae9 | 355 | } |
eencae | 0:d563e74f0ae9 | 356 | |
Francesco Fantoni |
7:77bd2c73fe41 | 357 | // function to print string at specified position |
Francesco Fantoni |
7:77bd2c73fe41 | 358 | void N5110::printNegString(const char * str,int x,int y) |
Francesco Fantoni |
7:77bd2c73fe41 | 359 | { |
Francesco Fantoni |
7:77bd2c73fe41 | 360 | int n = 0 ; // counter for number of characters in string |
Francesco Fantoni |
7:77bd2c73fe41 | 361 | // loop through string and print character |
Francesco Fantoni |
7:77bd2c73fe41 | 362 | while(*str) { |
Francesco Fantoni |
7:77bd2c73fe41 | 363 | |
Francesco Fantoni |
7:77bd2c73fe41 | 364 | setXYAddress(x+6*n,y); // leave 1 pixel (6 = 5 + 1) between each character |
Francesco Fantoni |
7:77bd2c73fe41 | 365 | printNegChar(*str); // print the char - can probably so *str++ and remove next line |
Francesco Fantoni |
7:77bd2c73fe41 | 366 | str++; // go to next character in string |
Francesco Fantoni |
7:77bd2c73fe41 | 367 | n++; // increment index |
Francesco Fantoni |
7:77bd2c73fe41 | 368 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 369 | |
Francesco Fantoni |
7:77bd2c73fe41 | 370 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 371 | |
eencae | 6:adb79338d40f | 372 | // function to clear the screen |
eencae | 0:d563e74f0ae9 | 373 | void N5110::clear() |
eencae | 0:d563e74f0ae9 | 374 | { |
eencae | 6:adb79338d40f | 375 | clearBuffer(); // clear the buffer then call the refresh function |
eencae | 6:adb79338d40f | 376 | refresh(); |
eencae | 0:d563e74f0ae9 | 377 | } |
eencae | 0:d563e74f0ae9 | 378 | |
eencae | 6:adb79338d40f | 379 | // function to clear the buffer |
eencae | 0:d563e74f0ae9 | 380 | void N5110::clearBuffer() |
eencae | 0:d563e74f0ae9 | 381 | { |
eencae | 0:d563e74f0ae9 | 382 | int i,j; |
eencae | 6:adb79338d40f | 383 | for (i=0; i<84; i++) { // loop through the banks and set the buffer to 0 |
eencae | 0:d563e74f0ae9 | 384 | for (j=0; j<6; j++) { |
eencae | 0:d563e74f0ae9 | 385 | buffer[i][j]=0; |
eencae | 0:d563e74f0ae9 | 386 | } |
eencae | 0:d563e74f0ae9 | 387 | } |
Francesco Fantoni |
7:77bd2c73fe41 | 388 | } |