@@ -34,6 +34,8 @@ using namespace tcgfx;
3434#endif // AVR reduced size buffer
3535#endif // COOKIE_CUT_MEMBUFFER_SIZE
3636
37+ uint16_t memBuffer[COOKIE_CUT_MEMBUFFER_SIZE];
38+
3739void AdafruitDrawable::transaction (bool isStarting, bool redrawNeeded) {
3840 if (!isStarting) refreshDisplayIfNeeded (graphics, redrawNeeded);
3941}
@@ -52,15 +54,58 @@ void AdafruitDrawable::drawBitmap(const Coord &where, const DrawableIcon *icon,
5254 if (icon->getIconType () == DrawableIcon::ICON_XBITMAP) {
5355 graphics->fillRect (where.x , where.y , icon->getDimensions ().x , icon->getDimensions ().y , backgroundColor);
5456 graphics->drawXBitmap (where.x , where.y , icon->getIcon (selected), icon->getDimensions ().x , icon->getDimensions ().y , drawColor);
55- }
56- else if (icon->getIconType () == DrawableIcon::ICON_NATIVE) {
57+ } else if (icon->getIconType () == DrawableIcon::ICON_NATIVE) {
5758 graphics->drawRGBBitmap (where.x , where.y , (const uint16_t *)icon->getIcon (selected), icon->getDimensions ().x , icon->getDimensions ().y );
58- }
59- else if (icon->getIconType () == DrawableIcon::ICON_MONO) {
59+ } else if (icon->getIconType () == DrawableIcon::ICON_MONO) {
6060 graphics->drawBitmap (where.x , where.y , icon->getIcon (selected), icon->getDimensions ().x , icon->getDimensions ().y , drawColor, backgroundColor);
61+ } else if (icon->getPalette () != nullptr ){
62+ auto bpp = icon->getIconType () == tcgfx::DrawableIcon::ICON_PALLETE_2BPP ? 2 : 4 ;
63+ drawBitmapNbpp (where, icon->getIcon (selected), icon->getDimensions (), bpp, icon->getPalette ());
6164 }
6265}
6366
67+ void AdafruitDrawable::drawBitmapNbpp (const Coord& where, const uint8_t * data, const Coord& size, int bpp, const color_t * palette) {
68+ auto * asTft = reinterpret_cast <Adafruit_SPITFT *>(graphics);
69+ auto yTot = int16_t (where.y + size.y );
70+ auto xTot = int16_t (where.x + size.x );
71+ int bitsInByte = bpp == 2 ? 4 : 2 ;
72+ uint8_t downShift = bpp == 2 ? 6 : 4 ;
73+
74+ uint16_t next = 0 ;
75+ uint8_t byteIteration = bitsInByte;
76+ uint8_t current;
77+
78+ asTft->startWrite ();
79+
80+ for (int16_t y = where.y ; y<yTot; y++) {
81+ asTft->setAddrWindow (where.x , y, size.x , 1 );
82+ for (int16_t x = where.x ; x<xTot; x++) {
83+ if (byteIteration == bitsInByte) {
84+ current = pgm_read_byte (data);
85+ data += 1 ;
86+ byteIteration = 0 ;
87+ }
88+ uint8_t idx = current >> downShift;
89+ current = current << bpp;
90+ byteIteration++;
91+
92+ memBuffer[next] = palette[idx];
93+ next = next + 1 ;
94+ if (next == COOKIE_CUT_MEMBUFFER_SIZE) {
95+ asTft->writePixels (memBuffer, next);
96+ next = 0 ;
97+ }
98+ }
99+ if (next != 0 ) {
100+ asTft->writePixels (memBuffer, next);
101+ next = 0 ;
102+ }
103+ byteIteration = bitsInByte; // always need a new byte in this case
104+ }
105+
106+ asTft->endWrite ();
107+ }
108+
64109void AdafruitDrawable::drawXBitmap (const Coord &where, const Coord &size, const uint8_t *data) {
65110 graphics->fillRect (where.x , where.y , size.x , size.y , backgroundColor);
66111 graphics->drawXBitmap (where.x , where.y , data, size.x , size.y , drawColor);
@@ -69,8 +114,7 @@ void AdafruitDrawable::drawXBitmap(const Coord &where, const Coord &size, const
69114void AdafruitDrawable::drawBox (const Coord &where, const Coord &size, bool filled) {
70115 if (filled) {
71116 graphics->fillRect (where.x , where.y , size.x , size.y , drawColor);
72- }
73- else {
117+ } else {
74118 graphics->drawRect (where.x , where.y , size.x , size.y , drawColor);
75119 }
76120}
@@ -157,8 +201,6 @@ UnicodeFontHandler *AdafruitDrawable::createFontHandler() {
157201// helper functions
158202//
159203
160- uint16_t memBuffer[COOKIE_CUT_MEMBUFFER_SIZE];
161-
162204void drawCookieCutBitmap (Adafruit_SPITFT* gfx, int16_t x, int16_t y, const uint8_t *bitmap, int16_t w,
163205 int16_t h, int16_t totalWidth, int16_t xStart, int16_t yStart,
164206 uint16_t fgColor, uint16_t bgColor) {
@@ -519,7 +561,7 @@ DeviceDrawable *AdafruitDrawable::getSubDeviceFor(const Coord &where, const Coor
519561}
520562
521563AdafruitCanvasDrawable2bpp::AdafruitCanvasDrawable2bpp (AdafruitDrawable *root, int width, int height) : root(root),
522- sizeMax({width, height}), sizeCurrent(), palette{} {
564+ sizeMax({width, height}), sizeCurrent(), palette{} {
523565 canvas = new TcGFXcanvas2 (width, height);
524566 setGraphics (canvas);
525567}
@@ -561,3 +603,26 @@ DeviceDrawable *AdafruitCanvasDrawable2bpp::getSubDeviceFor(const Coord&, const
561603 return nullptr ; // don't allow further nesting.
562604}
563605
606+ void AdafruitCanvasDrawable2bpp::drawBitmapNbpp (const Coord& where, const uint8_t * data, const Coord& size, int bpp, const uint16_t * palette) {
607+ auto yTot = int16_t (where.y + size.y );
608+ auto xTot = int16_t (where.x + size.x );
609+ int bitsInByte = bpp == 2 ? 4 : 2 ;
610+ uint8_t downShift = bpp == 2 ? 6 : 4 ;
611+
612+ uint8_t byteIteration = bitsInByte;
613+ uint8_t current;
614+ for (int16_t y = where.y ; y<yTot; y++) {
615+ for (int16_t x = where.x ; x<xTot; x++) {
616+ if (byteIteration == bitsInByte) {
617+ current = pgm_read_byte (data);
618+ data += 1 ;
619+ byteIteration = 0 ;
620+ }
621+ uint8_t idx = current >> downShift;
622+ current = current << bitsInByte;
623+ byteIteration++;
624+ canvas->drawPixel (x, y, idx);
625+ }
626+ byteIteration = bitsInByte; // always need a new byte in this case
627+ }
628+ }
0 commit comments