Differential pressure meter
Dependencies: TFT_Touch_WaveShare
SPI_TFT.cpp
00001 /* mbed library for 240*320 pixel display TFT based on HX8347D LCD Controller 00002 * Copyright (c) 2011 Peter Drescher - DC2PD 00003 * 00004 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00005 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00006 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00007 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00008 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00009 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00010 * THE SOFTWARE. 00011 */ 00012 00013 #include "SPI_TFT.h" 00014 #include "mbed.h" 00015 00016 00017 #define BPP 16 // Bits per pixel 00018 00019 00020 00021 SPI_TFT::SPI_TFT(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName reset, const char *name) 00022 : _spi(mosi, miso, sclk), _cs(cs), _reset(reset),GraphicsDisplay(name) { 00023 tft_reset(); 00024 orientation = 0; 00025 char_x = 0; 00026 } 00027 00028 int SPI_TFT::width() { 00029 if (orientation == 0 || orientation == 2) return 240; 00030 else return 320; 00031 } 00032 00033 00034 int SPI_TFT::height() { 00035 if (orientation == 0 || orientation == 2) return 320; 00036 else return 240; 00037 } 00038 00039 00040 00041 void SPI_TFT::set_orientation(unsigned int o) { 00042 orientation = o; 00043 switch (orientation) { 00044 case 0: 00045 wr_reg(0x16, 0x0008); 00046 break; 00047 case 1: 00048 wr_reg(0x16, 0x0068); 00049 break; 00050 case 2: 00051 wr_reg(0x16, 0x00C8); 00052 break; 00053 case 3: 00054 wr_reg(0x16, 0x00A8); 00055 break; 00056 } 00057 } 00058 00059 00060 00061 void SPI_TFT::wr_cmd(int cmd) { 00062 _cs = 0; 00063 _spi.write(SPI_START | SPI_WR | SPI_INDEX); /* Write : RS = 0, RW = 0 */ 00064 _spi.write(cmd); 00065 _cs = 1; 00066 } 00067 00068 00069 00070 void SPI_TFT::wr_dat(int dat) { 00071 _cs = 0; 00072 _spi.write(SPI_START | SPI_WR | SPI_DATA); // Write : RS = 1, RW = 0 00073 _spi.format(16,3); // switch to 16 bit Mode 3 00074 _spi.write(dat); // Write D0..D15 00075 _spi.format(8,3); // 8 bit Mode 3 00076 _cs = 1; 00077 } 00078 00079 00080 00081 void SPI_TFT::wr_dat_start(void) { 00082 _cs = 0; 00083 _spi.write(SPI_START | SPI_WR | SPI_DATA); /* Write : RS = 1, RW = 0 */ 00084 } 00085 00086 00087 00088 void SPI_TFT::wr_dat_stop (void) { 00089 _cs = 1; 00090 } 00091 00092 00093 00094 void SPI_TFT::wr_dat_only (unsigned short dat) { 00095 00096 _spi.format(16,3); // switch to 16 bit Mode 3 00097 _spi.write(dat); // Write D0..D15 00098 _spi.format(8,3); // 8 bit Mode 3 00099 } 00100 00101 00102 00103 unsigned short SPI_TFT::rd_dat (void) { 00104 unsigned short val = 0; 00105 00106 _cs = 0; 00107 _spi.write(SPI_START | SPI_RD | SPI_DATA); /* Read: RS = 1, RW = 1 */ 00108 _spi.write(0); /* Dummy read 1 */ 00109 val = _spi.write(0); /* Read D8..D15 */ 00110 val <<= 8; 00111 val |= _spi.write(0); /* Read D0..D7 */ 00112 _cs = 1; 00113 return (val); 00114 } 00115 00116 00117 00118 void SPI_TFT::wr_reg (unsigned char reg, unsigned short val) { 00119 00120 wr_cmd(reg); 00121 wr_dat(val); 00122 } 00123 00124 00125 00126 unsigned short SPI_TFT::rd_reg (unsigned char reg) { 00127 00128 wr_cmd(reg); 00129 return(rd_dat()); 00130 } 00131 00132 00133 00134 void SPI_TFT::tft_reset() { 00135 static unsigned short driverCode; 00136 _spi.format(8,3); // 8 bit spi mode 3 00137 _spi.frequency(48000000); // 48Mhz SPI clock 00138 _reset = 0; // reset 00139 _cs = 1; 00140 wait_us(50); 00141 _reset = 1; // end reset 00142 wait_ms(5); 00143 00144 driverCode = rd_reg(0x00); // read controller ID 00145 //printf("Disp_ID = %x",driverCode); 00146 00147 /* Start Initial Sequence ----------------------------------------------------*/ 00148 wr_reg(0xEA, 0x0000); /* Reset Power Control 1 */ 00149 wr_reg(0xEB, 0x0020); /* Power Control 2 */ 00150 wr_reg(0xEC, 0x000C); /* Power Control 3 */ 00151 wr_reg(0xED, 0x00C4); /* Power Control 4 */ 00152 wr_reg(0xE8, 0x0040); /* Source OPON_N */ 00153 wr_reg(0xE9, 0x0038); /* Source OPON_I */ 00154 wr_reg(0xF1, 0x0001); /* */ 00155 wr_reg(0xF2, 0x0010); /* */ 00156 wr_reg(0x27, 0x00A3); /* Display Control 2 */ 00157 00158 /* Power On sequence ---------------------------------------------------------*/ 00159 wr_reg(0x1B, 0x001B); /* Power Control 2 */ 00160 wr_reg(0x1A, 0x0001); /* Power Control 1 */ 00161 wr_reg(0x24, 0x002F); /* Vcom Control 2 */ 00162 wr_reg(0x25, 0x0057); /* Vcom Control 3 */ 00163 wr_reg(0x23, 0x008D); /* Vcom Control 1 */ 00164 00165 /* Gamma settings -----------------------------------------------------------*/ 00166 wr_reg(0x40,0x00); // 00167 wr_reg(0x41,0x00); // 00168 wr_reg(0x42,0x01); // 00169 wr_reg(0x43,0x13); // 00170 wr_reg(0x44,0x10); // 00171 wr_reg(0x45,0x26); // 00172 wr_reg(0x46,0x08); // 00173 wr_reg(0x47,0x51); // 00174 wr_reg(0x48,0x02); // 00175 wr_reg(0x49,0x12); // 00176 wr_reg(0x4A,0x18); // 00177 wr_reg(0x4B,0x19); // 00178 wr_reg(0x4C,0x14); // 00179 wr_reg(0x50,0x19); // 00180 wr_reg(0x51,0x2F); // 00181 wr_reg(0x52,0x2C); // 00182 wr_reg(0x53,0x3E); // 00183 wr_reg(0x54,0x3F); // 00184 wr_reg(0x55,0x3F); // 00185 wr_reg(0x56,0x2E); // 00186 wr_reg(0x57,0x77); // 00187 wr_reg(0x58,0x0B); // 00188 wr_reg(0x59,0x06); // 00189 wr_reg(0x5A,0x07); // 00190 wr_reg(0x5B,0x0D); // 00191 wr_reg(0x5C,0x1D); // 00192 wr_reg(0x5D,0xCC); // 00193 00194 /* Power + Osc ---------------------------------------------------------------*/ 00195 wr_reg(0x18, 0x0036); /* OSC Control 1 */ 00196 wr_reg(0x19, 0x0001); /* OSC Control 2 */ 00197 wr_reg(0x01, 0x0000); /* Display Mode Control */ 00198 wr_reg(0x1F, 0x0088); /* Power Control 6 */ 00199 wait_ms(5); /* Delay 5 ms */ 00200 wr_reg(0x1F, 0x0080); /* Power Control 6 */ 00201 wait_ms(5); /* Delay 5 ms */ 00202 wr_reg(0x1F, 0x0090); /* Power Control 6 */ 00203 wait_ms(5); /* Delay 5 ms */ 00204 wr_reg(0x1F, 0x00D0); /* Power Control 6 */ 00205 wait_ms(5); /* Delay 5 ms */ 00206 00207 wr_reg(0x17, 0x0005); /* Colmod 16Bit/Pixel */ 00208 00209 wr_reg(0x36, 0x0000); /* Panel Characteristic */ 00210 wr_reg(0x28, 0x0038); /* Display Control 3 */ 00211 wait_ms(40); 00212 wr_reg(0x28, 0x003C); /* Display Control 3 */ 00213 switch (orientation) { 00214 case 0: 00215 wr_reg(0x16, 0x0008); 00216 break; 00217 case 1: 00218 wr_reg(0x16, 0x0068); 00219 break; 00220 case 2: 00221 wr_reg(0x16, 0x00C8); 00222 break; 00223 case 3: 00224 wr_reg(0x16, 0x00A8); 00225 break; 00226 } 00227 00228 WindowMax (); 00229 } 00230 00231 00232 00233 00234 void SPI_TFT::pixel(int x, int y, int color) { 00235 wr_reg(0x03, (x >> 0)); 00236 wr_reg(0x02, (x >> 8)); 00237 wr_reg(0x07, (y >> 0)); 00238 wr_reg(0x06, (y >> 8)); 00239 wr_cmd(0x22); 00240 wr_dat(color); 00241 } 00242 00243 00244 00245 00246 void SPI_TFT::window (unsigned int x, unsigned int y, unsigned int w, unsigned int h) { 00247 wr_reg(0x03, (x >> 0)); 00248 wr_reg(0x02, (x >> 8)); 00249 wr_reg(0x05, (x+w-1 >> 0)); 00250 wr_reg(0x04, (x+w-1 >> 8)); 00251 wr_reg(0x07, ( y >> 0)); 00252 wr_reg(0x06, ( y >> 8)); 00253 wr_reg(0x09, ( y+h-1 >> 0)); 00254 wr_reg(0x08, ( y+h-1 >> 8)); 00255 //wr_cmd(0x22); 00256 } 00257 00258 00259 void SPI_TFT::WindowMax (void) { 00260 window (0, 0, width(), height()); 00261 } 00262 00263 00264 void SPI_TFT::cls (void) { 00265 unsigned int i; 00266 WindowMax(); 00267 wr_cmd(0x22); 00268 wr_dat_start(); 00269 _spi.format(16,3); // 16 bit Mode 3 00270 for (i = 0; i < ( width() * height()); i++) 00271 _spi.write(_background); 00272 _spi.format(8,3); // 8 bit Mode 3 00273 wr_dat_stop(); 00274 } 00275 00276 00277 void SPI_TFT::circle(int x0, int y0, int r, int color) { 00278 00279 int draw_x0, draw_y0; 00280 int draw_x1, draw_y1; 00281 int draw_x2, draw_y2; 00282 int draw_x3, draw_y3; 00283 int draw_x4, draw_y4; 00284 int draw_x5, draw_y5; 00285 int draw_x6, draw_y6; 00286 int draw_x7, draw_y7; 00287 int xx, yy; 00288 int di; 00289 WindowMax(); 00290 if (r == 0) { /* no radius */ 00291 return; 00292 } 00293 00294 draw_x0 = draw_x1 = x0; 00295 draw_y0 = draw_y1 = y0 + r; 00296 if (draw_y0 < height()) { 00297 pixel(draw_x0, draw_y0, color); /* 90 degree */ 00298 } 00299 00300 draw_x2 = draw_x3 = x0; 00301 draw_y2 = draw_y3 = y0 - r; 00302 if (draw_y2 >= 0) { 00303 pixel(draw_x2, draw_y2, color); /* 270 degree */ 00304 } 00305 00306 draw_x4 = draw_x6 = x0 + r; 00307 draw_y4 = draw_y6 = y0; 00308 if (draw_x4 < width()) { 00309 pixel(draw_x4, draw_y4, color); /* 0 degree */ 00310 } 00311 00312 draw_x5 = draw_x7 = x0 - r; 00313 draw_y5 = draw_y7 = y0; 00314 if (draw_x5>=0) { 00315 pixel(draw_x5, draw_y5, color); /* 180 degree */ 00316 } 00317 00318 if (r == 1) { 00319 return; 00320 } 00321 00322 di = 3 - 2*r; 00323 xx = 0; 00324 yy = r; 00325 while (xx < yy) { 00326 00327 if (di < 0) { 00328 di += 4*xx + 6; 00329 } else { 00330 di += 4*(xx - yy) + 10; 00331 yy--; 00332 draw_y0--; 00333 draw_y1--; 00334 draw_y2++; 00335 draw_y3++; 00336 draw_x4--; 00337 draw_x5++; 00338 draw_x6--; 00339 draw_x7++; 00340 } 00341 xx++; 00342 draw_x0++; 00343 draw_x1--; 00344 draw_x2++; 00345 draw_x3--; 00346 draw_y4++; 00347 draw_y5++; 00348 draw_y6--; 00349 draw_y7--; 00350 00351 if ( (draw_x0 <= width()) && (draw_y0>=0) ) { 00352 pixel(draw_x0, draw_y0, color); 00353 } 00354 00355 if ( (draw_x1 >= 0) && (draw_y1 >= 0) ) { 00356 pixel(draw_x1, draw_y1, color); 00357 } 00358 00359 if ( (draw_x2 <= width()) && (draw_y2 <= height()) ) { 00360 pixel(draw_x2, draw_y2, color); 00361 } 00362 00363 if ( (draw_x3 >=0 ) && (draw_y3 <= height()) ) { 00364 pixel(draw_x3, draw_y3, color); 00365 } 00366 00367 if ( (draw_x4 <= width()) && (draw_y4 >= 0) ) { 00368 pixel(draw_x4, draw_y4, color); 00369 } 00370 00371 if ( (draw_x5 >= 0) && (draw_y5 >= 0) ) { 00372 pixel(draw_x5, draw_y5, color); 00373 } 00374 if ( (draw_x6 <=width()) && (draw_y6 <= height()) ) { 00375 pixel(draw_x6, draw_y6, color); 00376 } 00377 if ( (draw_x7 >= 0) && (draw_y7 <= height()) ) { 00378 pixel(draw_x7, draw_y7, color); 00379 } 00380 } 00381 return; 00382 } 00383 00384 void SPI_TFT::fillcircle(int x, int y, int r, int color){ 00385 int i; 00386 for (i = 0;i <= r; i++) 00387 circle(x,y,i,color); 00388 } 00389 00390 00391 00392 void SPI_TFT::hline(int x0, int x1, int y, int color) { 00393 int w; 00394 w = x1 - x0 + 1; 00395 window(x0,y,w,1); 00396 wr_cmd(0x22); 00397 wr_dat_start(); 00398 _spi.format(16,3); // pixel are send in 16 bit mode to speed up 00399 for (int x=0; x<w; x++) { 00400 _spi.write(color); 00401 } 00402 _spi.format(8,3); 00403 wr_dat_stop(); 00404 return; 00405 } 00406 00407 00408 00409 void SPI_TFT::vline(int x, int y0, int y1, int color) { 00410 int h; 00411 h = y1 - y0 + 1; 00412 window(x,y0,1,h); 00413 wr_cmd(0x22); 00414 wr_dat_start(); 00415 _spi.format(16,3); // pixel are send in 16 bit mode to speed up 00416 for (int y=0; y<h; y++) { 00417 _spi.write(color); 00418 } 00419 _spi.format(8,3); 00420 wr_dat_stop(); 00421 return; 00422 } 00423 00424 00425 00426 void SPI_TFT::line(int x0, int y0, int x1, int y1, int color) { 00427 WindowMax(); 00428 int dx = 0, dy = 0; 00429 int dx_sym = 0, dy_sym = 0; 00430 int dx_x2 = 0, dy_x2 = 0; 00431 int di = 0; 00432 00433 dx = x1-x0; 00434 dy = y1-y0; 00435 00436 if (dx == 0) { /* vertical line */ 00437 if (y1 > y0) vline(x0,y0,y1,color); 00438 else vline(x0,y1,y0,color); 00439 return; 00440 } 00441 00442 if (dx > 0) { 00443 dx_sym = 1; 00444 } else { 00445 dx_sym = -1; 00446 } 00447 if (dy == 0) { /* horizontal line */ 00448 if (x1 > x0) hline(x0,x1,y0,color); 00449 else hline(x1,x0,y0,color); 00450 return; 00451 } 00452 00453 if (dy > 0) { 00454 dy_sym = 1; 00455 } else { 00456 dy_sym = -1; 00457 } 00458 00459 dx = dx_sym*dx; 00460 dy = dy_sym*dy; 00461 00462 dx_x2 = dx*2; 00463 dy_x2 = dy*2; 00464 00465 if (dx >= dy) { 00466 di = dy_x2 - dx; 00467 while (x0 != x1) { 00468 00469 pixel(x0, y0, color); 00470 x0 += dx_sym; 00471 if (di<0) { 00472 di += dy_x2; 00473 } else { 00474 di += dy_x2 - dx_x2; 00475 y0 += dy_sym; 00476 } 00477 } 00478 pixel(x0, y0, color); 00479 } else { 00480 di = dx_x2 - dy; 00481 while (y0 != y1) { 00482 pixel(x0, y0, color); 00483 y0 += dy_sym; 00484 if (di < 0) { 00485 di += dx_x2; 00486 } else { 00487 di += dx_x2 - dy_x2; 00488 x0 += dx_sym; 00489 } 00490 } 00491 pixel(x0, y0, color); 00492 } 00493 return; 00494 } 00495 00496 00497 00498 00499 void SPI_TFT::rect(int x0, int y0, int x1, int y1, int color) { 00500 00501 if (x1 > x0) hline(x0,x1,y0,color); 00502 else hline(x1,x0,y0,color); 00503 00504 if (y1 > y0) vline(x0,y0,y1,color); 00505 else vline(x0,y1,y0,color); 00506 00507 if (x1 > x0) hline(x0,x1,y1,color); 00508 else hline(x1,x0,y1,color); 00509 00510 if (y1 > y0) vline(x1,y0,y1,color); 00511 else vline(x1,y1,y0,color); 00512 00513 return; 00514 } 00515 00516 00517 00518 void SPI_TFT::fillrect(int x0, int y0, int x1, int y1, int color) { 00519 00520 int h = y1 - y0 + 1; 00521 int w = x1 - x0 + 1; 00522 int pixel = h * w; 00523 window(x0,y0,w,h); 00524 wr_cmd(0x22); 00525 wr_dat_start(); 00526 _spi.format(16,3); // pixel are send in 16 bit mode to speed up 00527 for (int p=0; p<pixel; p++) { 00528 _spi.write(color); 00529 } 00530 _spi.format(8,3); 00531 wr_dat_stop(); 00532 return; 00533 } 00534 00535 00536 00537 void SPI_TFT::locate(int column, int row) { 00538 _column = column; 00539 char_x = font[1] * column; // get the horz. size of the actual font 00540 _row = row; 00541 } 00542 00543 00544 00545 int SPI_TFT::columns() { 00546 return width() / font[1]; 00547 } 00548 00549 00550 00551 int SPI_TFT::rows() { 00552 return height() / font[2]; 00553 } 00554 00555 00556 00557 int SPI_TFT::_putc(int value) { 00558 if (value == '\n') { 00559 _column = 0; 00560 char_x = 0; 00561 _row++; 00562 if (_row >= rows()) { 00563 _row = 0; 00564 } 00565 } else { 00566 character(_column, _row, value); 00567 _column++; 00568 } 00569 return value; 00570 } 00571 00572 00573 00574 00575 void SPI_TFT::character(int col, int row, int c) { 00576 unsigned int hor,vert,offset,bpl,j,i,b; 00577 unsigned char* zeichen; 00578 unsigned char z,w; 00579 00580 if ((c < 31) || (c > 127)) return; // test char range 00581 00582 // read font parameter from start of array 00583 offset = font[0]; // bytes / char 00584 hor = font[1]; // get hor size of font 00585 vert = font[2]; // get vert size of font 00586 bpl = font[3]; // bytes per line 00587 00588 if (char_x + hor > width()) { 00589 char_x = 0; 00590 _column = 0; 00591 _row ++; 00592 row++; 00593 if (_row >= rows()) { 00594 _row = 0; 00595 row=0; 00596 } 00597 } 00598 00599 window(char_x, row * vert,hor,vert); // char box 00600 wr_cmd(0x22); 00601 wr_dat_start(); 00602 zeichen = &font[((c -32) * offset) + 4]; // start of char bitmap 00603 w = zeichen[0]; // width of actual char 00604 _spi.format(16,3); // pixel are 16 bit 00605 00606 for (j=0; j<vert; j++) { // vert line 00607 for (i=0; i<hor; i++) { // horz line 00608 z = zeichen[bpl * i + ((j & 0xF8) >> 3)+1]; 00609 b = 1 << (j & 0x07); 00610 if (( z & b ) == 0x00) { 00611 _spi.write(_background); 00612 } else { 00613 _spi.write(_foreground); 00614 } 00615 } 00616 } 00617 _spi.format(8,3); // 8 bit 00618 wr_dat_stop(); 00619 if ((w + 2) < hor) { // x offset to next char 00620 char_x += w + 2; 00621 } else char_x += hor; 00622 } 00623 00624 00625 00626 00627 00628 void SPI_TFT::set_font(unsigned char* f) { 00629 font = f; 00630 } 00631 00632 00633 00634 void SPI_TFT::Bitmap(unsigned int x, unsigned int y, unsigned int w, unsigned int h,unsigned char *bitmap) { 00635 unsigned int i,j; 00636 unsigned short *bitmap_ptr = (unsigned short *)bitmap; 00637 window(x, y, w, h); 00638 wr_cmd(0x22); 00639 wr_dat_start(); 00640 _spi.format(16,3); 00641 bitmap_ptr += ((h - 1)*w); 00642 for (j = 0; j < h; j++) { //Lines 00643 for (i = 0; i < w; i++) { // copy pixel data to TFT 00644 _spi.write(*bitmap_ptr); // one line 00645 bitmap_ptr++; 00646 } 00647 bitmap_ptr -= 2*w; 00648 } 00649 _spi.format(8,3); 00650 wr_dat_stop(); 00651 }
Generated on Tue Jul 19 2022 13:55:32 by 1.7.2