acid-drop- Hacking the planet from a LilyGo T-Deck using custom firmware |
git clone git://git.acid.vegas/acid-drop.git |
Log | Files | Refs | Archive | README | LICENSE |
Arduino_TFT_18bit.cpp (21019B)
1 /* 2 * start rewrite from: 3 * https://github.com/adafruit/Adafruit-GFX-Library.git 4 */ 5 #include "Arduino_DataBus.h" 6 #include "Arduino_GFX.h" 7 #include "Arduino_TFT_18bit.h" 8 9 Arduino_TFT_18bit::Arduino_TFT_18bit( 10 Arduino_DataBus *bus, int8_t rst, uint8_t r, 11 bool ips, int16_t w, int16_t h, 12 uint8_t col_offset1, uint8_t row_offset1, uint8_t col_offset2, uint8_t row_offset2) 13 : Arduino_TFT(bus, rst, r, ips, w, h, col_offset1, row_offset1, col_offset2, row_offset2) 14 { 15 } 16 17 void Arduino_TFT_18bit::writeColor(uint16_t color) 18 { 19 _bus->write((color & 0xF800) >> 8); 20 _bus->write((color & 0x07E0) >> 3); 21 _bus->write(color << 3); 22 } 23 24 void Arduino_TFT_18bit::writePixelPreclipped(int16_t x, int16_t y, uint16_t color) 25 { 26 writeAddrWindow(x, y, 1, 1); 27 _bus->write((color & 0xF800) >> 8); 28 _bus->write((color & 0x07E0) >> 3); 29 _bus->write(color << 3); 30 } 31 32 void Arduino_TFT_18bit::writeRepeat(uint16_t color, uint32_t len) 33 { 34 #if defined(ESP8266) || defined(ESP32) 35 uint8_t c[3] = {(uint8_t)((color & 0xF800) >> 8), (uint8_t)((color & 0x07E0) >> 3), (uint8_t)((color & 0x001F) << 3)}; 36 _bus->writePattern(c, 3, len); 37 #else 38 uint8_t c1 = (uint8_t)((color & 0xF800) >> 8); 39 uint8_t c2 = (uint8_t)((color & 0x07E0) >> 3); 40 uint8_t c3 = (uint8_t)((color & 0x001F) << 3); 41 while (len--) 42 { 43 _bus->write(c1); 44 _bus->write(c2); 45 _bus->write(c3); 46 } 47 #endif 48 } 49 50 // TFT optimization code, too big for ATMEL family 51 #if !defined(LITTLE_FOOT_PRINT) 52 53 void Arduino_TFT_18bit::writePixels(uint16_t *data, uint32_t len) 54 { 55 uint16_t d; 56 while (len--) 57 { 58 d = *data++; 59 _bus->write((d & 0xF800) >> 8); 60 _bus->write((d & 0x07E0) >> 3); 61 _bus->write(d << 3); 62 } 63 } 64 65 // TFT tuned BITMAP / XBITMAP / GRAYSCALE / RGB BITMAP FUNCTIONS --------------------- 66 67 /**************************************************************************/ 68 /*! 69 @brief Draw a Indexed 16-bit image (RGB 5/6/5) at the specified (x,y) position. 70 @param bitmap byte array of Indexed color bitmap 71 @param color_index byte array of 16-bit color index 72 @param w Width of bitmap in pixels 73 @param h Height of bitmap in pixels 74 */ 75 /**************************************************************************/ 76 void Arduino_TFT_18bit::writeIndexedPixels(uint8_t *bitmap, uint16_t *color_index, uint32_t len) 77 { 78 uint16_t d; 79 while (len--) 80 { 81 d = color_index[*(bitmap++)]; 82 _bus->write((d & 0xF800) >> 8); 83 _bus->write((d & 0x07E0) >> 3); 84 _bus->write(d << 3); 85 } 86 } 87 88 /**************************************************************************/ 89 /*! 90 @brief Draw a Indexed 16-bit image (RGB 5/6/5) at the specified (x,y) position. 91 @param bitmap byte array of Indexed color bitmap 92 @param color_index byte array of 16-bit color index 93 @param w Width of bitmap in pixels 94 @param h Height of bitmap in pixels 95 */ 96 /**************************************************************************/ 97 void Arduino_TFT_18bit::writeIndexedPixelsDouble(uint8_t *bitmap, uint16_t *color_index, uint32_t len) 98 { 99 uint8_t r, g, b; 100 uint16_t d; 101 while (len--) 102 { 103 d = color_index[*(bitmap++)]; 104 r = (d & 0xF800) >> 8; 105 g = (d & 0x07E0) >> 3; 106 b = d << 3; 107 _bus->write(r); 108 _bus->write(g); 109 _bus->write(b); 110 _bus->write(r); 111 _bus->write(g); 112 _bus->write(b); 113 } 114 } 115 116 /**************************************************************************/ 117 /*! 118 @brief Draw a PROGMEM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors. 119 @param x Top left corner x coordinate 120 @param y Top left corner y coordinate 121 @param bitmap byte array with monochrome bitmap 122 @param w Width of bitmap in pixels 123 @param h Height of bitmap in pixels 124 @param color 16-bit 5-6-5 Color to draw pixels with 125 @param bg 16-bit 5-6-5 Color to draw background with 126 */ 127 /**************************************************************************/ 128 void Arduino_TFT_18bit::drawBitmap(int16_t x, int16_t y, 129 const uint8_t bitmap[], int16_t w, int16_t h, 130 uint16_t color, uint16_t bg) 131 { 132 if ( 133 ((x + w - 1) < 0) || // Outside left 134 ((y + h - 1) < 0) || // Outside top 135 (x > _max_x) || // Outside right 136 (y > _max_y) // Outside bottom 137 ) 138 { 139 return; 140 } 141 else if ( 142 (x < 0) || // Clip left 143 (y < 0) || // Clip top 144 ((x + w - 1) > _max_x) || // Clip right 145 ((y + h - 1) > _max_y) // Clip bottom 146 ) 147 { 148 Arduino_GFX::drawBitmap(x, y, bitmap, w, h, color, bg); 149 } 150 else 151 { 152 uint16_t c; 153 int32_t pixels = w * h; 154 uint8_t byte = 0; 155 uint16_t idx = 0; 156 startWrite(); 157 writeAddrWindow(x, y, w, h); 158 for (int32_t i = 0; i < pixels; i++) 159 { 160 if (i & 7) 161 { 162 byte <<= 1; 163 } 164 else 165 { 166 byte = pgm_read_byte(&bitmap[idx++]); 167 } 168 c = (byte & 0x80) ? color : bg; 169 _bus->write((c & 0xF800) >> 8); 170 _bus->write((c & 0x07E0) >> 3); 171 _bus->write((c & 0x001F) << 3); 172 } 173 endWrite(); 174 } 175 } 176 177 /**************************************************************************/ 178 /*! 179 @brief Draw a RAM-resident 1-bit image at the specified (x,y) position, using the specified foreground (for set bits) and background (unset bits) colors. 180 @param x Top left corner x coordinate 181 @param y Top left corner y coordinate 182 @param bitmap byte array with monochrome bitmap 183 @param w Width of bitmap in pixels 184 @param h Height of bitmap in pixels 185 @param color 16-bit 5-6-5 Color to draw pixels with 186 @param bg 16-bit 5-6-5 Color to draw background with 187 */ 188 /**************************************************************************/ 189 void Arduino_TFT_18bit::drawBitmap(int16_t x, int16_t y, 190 uint8_t *bitmap, int16_t w, int16_t h, uint16_t color, uint16_t bg) 191 { 192 if ( 193 ((x + w - 1) < 0) || // Outside left 194 ((y + h - 1) < 0) || // Outside top 195 (x > _max_x) || // Outside right 196 (y > _max_y) // Outside bottom 197 ) 198 { 199 return; 200 } 201 else if ( 202 (x < 0) || // Clip left 203 (y < 0) || // Clip top 204 ((x + w - 1) > _max_x) || // Clip right 205 ((y + h - 1) > _max_y) // Clip bottom 206 ) 207 { 208 Arduino_GFX::drawBitmap(x, y, bitmap, w, h, color, bg); 209 } 210 else 211 { 212 uint16_t c; 213 int32_t pixels = w * h; 214 uint8_t byte = 0; 215 startWrite(); 216 writeAddrWindow(x, y, w, h); 217 for (int32_t i = 0; i < pixels; i++) 218 { 219 if (i & 7) 220 { 221 byte <<= 1; 222 } 223 else 224 { 225 byte = *(bitmap++); 226 } 227 c = (byte & 0x80) ? color : bg; 228 _bus->write((c & 0xF800) >> 8); 229 _bus->write((c & 0x07E0) >> 3); 230 _bus->write((c & 0x001F) << 3); 231 } 232 endWrite(); 233 } 234 } 235 236 /**************************************************************************/ 237 /*! 238 @brief Draw a PROGMEM-resident 8-bit image (grayscale) at the specified (x,y) pos. 239 @param x Top left corner x coordinate 240 @param y Top left corner y coordinate 241 @param bitmap byte array with grayscale bitmap 242 @param w Width of bitmap in pixels 243 @param h Height of bitmap in pixels 244 */ 245 /**************************************************************************/ 246 void Arduino_TFT_18bit::drawGrayscaleBitmap(int16_t x, int16_t y, 247 const uint8_t bitmap[], int16_t w, int16_t h) 248 { 249 if ( 250 ((x + w - 1) < 0) || // Outside left 251 ((y + h - 1) < 0) || // Outside top 252 (x > _max_x) || // Outside right 253 (y > _max_y) // Outside bottom 254 ) 255 { 256 return; 257 } 258 else if ( 259 (x < 0) || // Clip left 260 (y < 0) || // Clip top 261 ((x + w - 1) > _max_x) || // Clip right 262 ((y + h - 1) > _max_y) // Clip bottom 263 ) 264 { 265 Arduino_GFX::drawGrayscaleBitmap(x, y, bitmap, w, h); 266 } 267 else 268 { 269 uint8_t v; 270 startWrite(); 271 writeAddrWindow(x, y, w, h); 272 for (int16_t j = 0; j < h; j++, y++) 273 { 274 for (int16_t i = 0; i < w; i++) 275 { 276 v = (uint8_t)pgm_read_byte(&bitmap[j * w + i]); 277 _bus->write(v); 278 _bus->write(v); 279 _bus->write(v); 280 } 281 } 282 endWrite(); 283 } 284 } 285 286 /**************************************************************************/ 287 /*! 288 @brief Draw a RAM-resident 8-bit image (grayscale) at the specified (x,y) pos. 289 @param x Top left corner x coordinate 290 @param y Top left corner y coordinate 291 @param bitmap byte array with grayscale bitmap 292 @param w Width of bitmap in pixels 293 @param h Height of bitmap in pixels 294 */ 295 /**************************************************************************/ 296 void Arduino_TFT_18bit::drawGrayscaleBitmap(int16_t x, int16_t y, 297 uint8_t *bitmap, int16_t w, int16_t h) 298 { 299 if ( 300 ((x + w - 1) < 0) || // Outside left 301 ((y + h - 1) < 0) || // Outside top 302 (x > _max_x) || // Outside right 303 (y > _max_y) // Outside bottom 304 ) 305 { 306 return; 307 } 308 else if ( 309 (x < 0) || // Clip left 310 (y < 0) || // Clip top 311 ((x + w - 1) > _max_x) || // Clip right 312 ((y + h - 1) > _max_y) // Clip bottom 313 ) 314 { 315 Arduino_GFX::drawGrayscaleBitmap(x, y, bitmap, w, h); 316 } 317 else 318 { 319 uint8_t v; 320 startWrite(); 321 writeAddrWindow(x, y, w, h); 322 for (int16_t j = 0; j < h; j++, y++) 323 { 324 for (int16_t i = 0; i < w; i++) 325 { 326 v = bitmap[j * w + i]; 327 _bus->write(v); 328 _bus->write(v); 329 _bus->write(v); 330 } 331 } 332 endWrite(); 333 } 334 } 335 336 /**************************************************************************/ 337 /*! 338 @brief Draw a Indexed 16-bit image (RGB 5/6/5) at the specified (x,y) position. 339 @param x Top left corner x coordinate 340 @param y Top left corner y coordinate 341 @param bitmap byte array with 16-bit color bitmap 342 @param w Width of bitmap in pixels 343 @param h Height of bitmap in pixels 344 */ 345 /**************************************************************************/ 346 void Arduino_TFT_18bit::drawIndexedBitmap(int16_t x, int16_t y, 347 uint8_t *bitmap, uint16_t *color_index, int16_t w, int16_t h) 348 { 349 if ( 350 ((x + w - 1) < 0) || // Outside left 351 ((y + h - 1) < 0) || // Outside top 352 (x > _max_x) || // Outside right 353 (y > _max_y) // Outside bottom 354 ) 355 { 356 return; 357 } 358 else if ( 359 (x < 0) || // Clip left 360 (y < 0) || // Clip top 361 ((x + w - 1) > _max_x) || // Clip right 362 ((y + h - 1) > _max_y) // Clip bottom 363 ) 364 { 365 Arduino_GFX::drawIndexedBitmap(x, y, bitmap, color_index, w, h); 366 } 367 else 368 { 369 startWrite(); 370 writeAddrWindow(x, y, w, h); 371 writeIndexedPixels(bitmap, color_index, w * h); 372 endWrite(); 373 } 374 } 375 376 /**************************************************************************/ 377 /*! 378 @brief Draw a PROGMEM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position. 379 For 16-bit display devices; no color reduction performed. 380 @param x Top left corner x coordinate 381 @param y Top left corner y coordinate 382 @param bitmap byte array with 16-bit color bitmap 383 @param w Width of bitmap in pixels 384 @param h Height of bitmap in pixels 385 */ 386 /**************************************************************************/ 387 void Arduino_TFT_18bit::draw16bitRGBBitmap(int16_t x, int16_t y, 388 const uint16_t bitmap[], int16_t w, int16_t h) 389 { 390 if ( 391 ((x + w - 1) < 0) || // Outside left 392 ((y + h - 1) < 0) || // Outside top 393 (x > _max_x) || // Outside right 394 (y > _max_y) // Outside bottom 395 ) 396 { 397 return; 398 } 399 else if ( 400 (x < 0) || // Clip left 401 (y < 0) || // Clip top 402 ((x + w - 1) > _max_x) || // Clip right 403 ((y + h - 1) > _max_y) // Clip bottom 404 ) 405 { 406 Arduino_GFX::draw16bitRGBBitmap(x, y, bitmap, w, h); 407 } 408 else 409 { 410 uint16_t d; 411 startWrite(); 412 writeAddrWindow(x, y, w, h); 413 for (int16_t j = 0; j < h; j++, y++) 414 { 415 for (int16_t i = 0; i < w; i++) 416 { 417 d = pgm_read_word(&bitmap[j * w + i]); 418 _bus->write((d & 0xF800) >> 8); 419 _bus->write((d & 0x07E0) >> 3); 420 _bus->write(d << 3); 421 } 422 } 423 endWrite(); 424 } 425 } 426 427 /**************************************************************************/ 428 /*! 429 @brief Draw a RAM-resident 16-bit image (RGB 5/6/5) with a 1-bit mask 430 (set bits = opaque, unset bits = clear) at the specified (x,y) position. 431 BOTH buffers (color and mask) must be RAM-resident. 432 @param x Top left corner x coordinate 433 @param y Top left corner y coordinate 434 @param bitmap byte array with 16-bit color bitmap 435 @param mask byte array with monochrome mask bitmap 436 @param w Width of bitmap in pixels 437 @param h Height of bitmap in pixels 438 */ 439 /**************************************************************************/ 440 void Arduino_TFT_18bit::draw16bitRGBBitmap(int16_t x, int16_t y, 441 uint16_t *bitmap, uint8_t *mask, int16_t w, int16_t h) 442 { 443 if ( 444 ((x + w - 1) < 0) || // Outside left 445 ((y + h - 1) < 0) || // Outside top 446 (x > _max_x) || // Outside right 447 (y > _max_y) // Outside bottom 448 ) 449 { 450 return; 451 } 452 else if ( 453 (x < 0) || // Clip left 454 (y < 0) || // Clip top 455 ((x + w - 1) > _max_x) || // Clip right 456 ((y + h - 1) > _max_y) // Clip bottom 457 ) 458 { 459 Arduino_GFX::draw16bitRGBBitmap(x, y, bitmap, mask, w, h); 460 } 461 else 462 { 463 uint16_t d; 464 int32_t offset = 0, maskIdx = 0, len = 0; 465 uint8_t byte = 0; 466 startWrite(); 467 for (int16_t j = 0; j < h; j++, y++) 468 { 469 for (int16_t i = 0; i < w; i++) 470 { 471 if (i & 7) 472 { 473 byte <<= 1; 474 } 475 else 476 { 477 byte = mask[maskIdx++]; 478 } 479 if (byte & 0x80) 480 { 481 len++; 482 } 483 else 484 { 485 if (len) 486 { 487 writeAddrWindow(x + i - len, y, len, 1); 488 for (int16_t i = len; i > 0; i--) 489 { 490 d = bitmap[offset - i]; 491 _bus->write((d & 0xF800) >> 8); 492 _bus->write((d & 0x07E0) >> 3); 493 _bus->write(d << 3); 494 } 495 len = 0; 496 } 497 } 498 offset++; 499 } 500 if (len) 501 { 502 writeAddrWindow(x + w - 1 - len, y, len, 1); 503 for (int16_t i = len; i > 0; i--) 504 { 505 d = bitmap[offset - i]; 506 _bus->write((d & 0xF800) >> 8); 507 _bus->write((d & 0x07E0) >> 3); 508 _bus->write(d << 3); 509 } 510 len = 0; 511 } 512 } 513 endWrite(); 514 } 515 } 516 517 /**************************************************************************/ 518 /*! 519 @brief Draw a RAM-resident 16-bit image (RGB 5/6/5) at the specified (x,y) position. 520 For 16-bit display devices; no color reduction performed. 521 @param x Top left corner x coordinate 522 @param y Top left corner y coordinate 523 @param bitmap byte array with 16-bit color bitmap 524 @param w Width of bitmap in pixels 525 @param h Height of bitmap in pixels 526 */ 527 /**************************************************************************/ 528 void Arduino_TFT_18bit::draw16bitRGBBitmap(int16_t x, int16_t y, 529 uint16_t *bitmap, int16_t w, int16_t h) 530 { 531 if ( 532 ((x + w - 1) < 0) || // Outside left 533 ((y + h - 1) < 0) || // Outside top 534 (x > _max_x) || // Outside right 535 (y > _max_y) // Outside bottom 536 ) 537 { 538 return; 539 } 540 else if ( 541 (x < 0) || // Clip left 542 (y < 0) || // Clip top 543 ((x + w - 1) > _max_x) || // Clip right 544 ((y + h - 1) > _max_y) // Clip bottom 545 ) 546 { 547 Arduino_GFX::draw16bitRGBBitmap(x, y, bitmap, w, h); 548 } 549 else 550 { 551 uint16_t d; 552 startWrite(); 553 writeAddrWindow(x, y, w, h); 554 for (int16_t j = 0; j < h; j++, y++) 555 { 556 for (int16_t i = 0; i < w; i++) 557 { 558 d = bitmap[j * w + i]; 559 _bus->write((d & 0xF800) >> 8); 560 _bus->write((d & 0x07E0) >> 3); 561 _bus->write(d << 3); 562 } 563 } 564 endWrite(); 565 } 566 } 567 568 /**************************************************************************/ 569 /*! 570 @brief Draw a RAM-resident 16-bit Big Endian image (RGB 5/6/5) at the specified (x,y) position. 571 For 16-bit display devices; no color reduction performed. 572 @param x Top left corner x coordinate 573 @param y Top left corner y coordinate 574 @param bitmap byte array with 16-bit color bitmap 575 @param w Width of bitmap in pixels 576 @param h Height of bitmap in pixels 577 */ 578 /**************************************************************************/ 579 void Arduino_TFT_18bit::draw16bitBeRGBBitmap(int16_t x, int16_t y, 580 uint16_t *bitmap, int16_t w, int16_t h) 581 { 582 if ( 583 ((x + w - 1) < 0) || // Outside left 584 ((y + h - 1) < 0) || // Outside top 585 (x > _max_x) || // Outside right 586 (y > _max_y) // Outside bottom 587 ) 588 { 589 return; 590 } 591 else if ( 592 (x < 0) || // Clip left 593 (y < 0) || // Clip top 594 ((x + w - 1) > _max_x) || // Clip right 595 ((y + h - 1) > _max_y) // Clip bottom 596 ) 597 { 598 Arduino_GFX::draw16bitBeRGBBitmap(x, y, bitmap, w, h); 599 } 600 else 601 { 602 uint16_t d; 603 startWrite(); 604 writeAddrWindow(x, y, w, h); 605 for (int16_t j = 0; j < h; j++, y++) 606 { 607 for (int16_t i = 0; i < w; i++) 608 { 609 d = bitmap[j * w + i]; 610 _bus->write((d & 0x00F8)); 611 _bus->write(((d & 0xE000) >> 11) | (d & 0x0007) << 5); 612 _bus->write((d & 0x1F00) >> 5); 613 } 614 } 615 endWrite(); 616 } 617 } 618 619 /**************************************************************************/ 620 /*! 621 @brief Draw a PROGMEM-resident 24-bit image (RGB 5/6/5) at the specified (x,y) position. 622 @param x Top left corner x coordinate 623 @param y Top left corner y coordinate 624 @param bitmap byte array with 16-bit color bitmap 625 @param w Width of bitmap in pixels 626 @param h Height of bitmap in pixels 627 */ 628 /**************************************************************************/ 629 void Arduino_TFT_18bit::draw24bitRGBBitmap(int16_t x, int16_t y, 630 const uint8_t bitmap[], int16_t w, int16_t h) 631 { 632 if ( 633 ((x + w - 1) < 0) || // Outside left 634 ((y + h - 1) < 0) || // Outside top 635 (x > _max_x) || // Outside right 636 (y > _max_y) // Outside bottom 637 ) 638 { 639 return; 640 } 641 else if ( 642 (x < 0) || // Clip left 643 (y < 0) || // Clip top 644 ((x + w - 1) > _max_x) || // Clip right 645 ((y + h - 1) > _max_y) // Clip bottom 646 ) 647 { 648 Arduino_GFX::draw24bitRGBBitmap(x, y, bitmap, w, h); 649 } 650 else 651 { 652 int16_t offset = 0; 653 startWrite(); 654 writeAddrWindow(x, y, w, h); 655 for (int16_t j = 0; j < h; j++, y++) 656 { 657 for (int16_t i = 0; i < w; i++) 658 { 659 _bus->write(pgm_read_byte(&bitmap[offset++])); 660 _bus->write(pgm_read_byte(&bitmap[offset++])); 661 _bus->write(pgm_read_byte(&bitmap[offset++])); 662 } 663 } 664 endWrite(); 665 } 666 } 667 668 /**************************************************************************/ 669 /*! 670 @brief Draw a RAM-resident 24-bit image (RGB 5/6/5) at the specified (x,y) position. 671 @param x Top left corner x coordinate 672 @param y Top left corner y coordinate 673 @param bitmap byte array with 16-bit color bitmap 674 @param w Width of bitmap in pixels 675 @param h Height of bitmap in pixels 676 */ 677 /**************************************************************************/ 678 void Arduino_TFT_18bit::draw24bitRGBBitmap(int16_t x, int16_t y, 679 uint8_t *bitmap, int16_t w, int16_t h) 680 { 681 if ( 682 ((x + w - 1) < 0) || // Outside left 683 ((y + h - 1) < 0) || // Outside top 684 (x > _max_x) || // Outside right 685 (y > _max_y) // Outside bottom 686 ) 687 { 688 return; 689 } 690 else if ( 691 (x < 0) || // Clip left 692 (y < 0) || // Clip top 693 ((x + w - 1) > _max_x) || // Clip right 694 ((y + h - 1) > _max_y) // Clip bottom 695 ) 696 { 697 Arduino_GFX::draw24bitRGBBitmap(x, y, bitmap, w, h); 698 } 699 else 700 { 701 startWrite(); 702 writeAddrWindow(x, y, w, h); 703 _bus->writeBytes(bitmap, w * h * 3); 704 endWrite(); 705 } 706 } 707 708 #endif // !defined(LITTLE_FOOT_PRINT)