mirror of
https://github.com/Bodmer/TFT_eSPI.git
synced 2025-08-11 00:24:44 +02:00
Suggested changes from code review
Set font for Transparent_Sprite_Demo_Colormap to correct value (4) renamed setColorMap to createPalette renamed cmpPixel to readPixelValue added setPaletteColor / getPaletteColor added error check to createPalette
This commit is contained in:
@@ -171,17 +171,22 @@ void* TFT_eSprite::callocSprite(int16_t w, int16_t h, uint8_t frames)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/***************************************************************************************
|
/***************************************************************************************
|
||||||
** Function name: setColorMap
|
** Function name: createPalette
|
||||||
** Description: Set a colour map for a 4-bit per pixel sprite
|
** Description: Set a palette for a 4-bit per pixel sprite
|
||||||
*************************************************************************************x*/
|
*************************************************************************************x*/
|
||||||
|
|
||||||
void TFT_eSprite::setColorMap(uint16_t colorMap[], int colors)
|
void TFT_eSprite::createPalette(uint16_t colorMap[], int colors)
|
||||||
{
|
{
|
||||||
if (_colorMap != nullptr)
|
if (_colorMap != nullptr)
|
||||||
{
|
{
|
||||||
free(_colorMap);
|
free(_colorMap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (colorMap == nullptr)
|
||||||
|
{
|
||||||
|
return; // do nothing other than clear the existing map
|
||||||
|
}
|
||||||
|
|
||||||
// allocate color map
|
// allocate color map
|
||||||
_colorMap = (uint16_t *)calloc(16, sizeof(uint16_t));
|
_colorMap = (uint16_t *)calloc(16, sizeof(uint16_t));
|
||||||
if (colors > 16)
|
if (colors > 16)
|
||||||
@@ -263,6 +268,28 @@ void TFT_eSprite::setBitmapColor(uint16_t c, uint16_t b)
|
|||||||
_tft->bitmap_bg = b;
|
_tft->bitmap_bg = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/***************************************************************************************
|
||||||
|
** Function name: setPaletteColor
|
||||||
|
** Description: Set the palette color at the given index
|
||||||
|
***************************************************************************************/
|
||||||
|
void TFT_eSprite::setPaletteColor(uint8_t index, uint16_t color)
|
||||||
|
{
|
||||||
|
if (_colorMap == nullptr || index > 15)
|
||||||
|
return; // out of bounds
|
||||||
|
_colorMap[index] = color;
|
||||||
|
}
|
||||||
|
|
||||||
|
/***************************************************************************************
|
||||||
|
** Function name: getPaletteColor
|
||||||
|
** Description: Return the palette color at index, or 0 (black) on error.
|
||||||
|
***************************************************************************************/
|
||||||
|
uint16_t TFT_eSprite::getPaletteColor(uint8_t index)
|
||||||
|
{
|
||||||
|
if (_colorMap == nullptr || index > 15)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
return _colorMap[index];
|
||||||
|
}
|
||||||
|
|
||||||
/***************************************************************************************
|
/***************************************************************************************
|
||||||
** Function name: deleteSprite
|
** Function name: deleteSprite
|
||||||
@@ -754,10 +781,10 @@ void TFT_eSprite::pushSprite(int32_t x, int32_t y, uint16_t transp)
|
|||||||
|
|
||||||
|
|
||||||
/***************************************************************************************
|
/***************************************************************************************
|
||||||
** Function name: cmapPixel
|
** Function name: readPixelValue
|
||||||
** Description: Read the color map index of a pixel at defined coordinates
|
** Description: Read the color map index of a pixel at defined coordinates
|
||||||
*************************************************************************************x*/
|
*************************************************************************************x*/
|
||||||
uint8_t TFT_eSprite::cmapPixel(int32_t x, int32_t y)
|
uint8_t TFT_eSprite::readPixelValue(int32_t x, int32_t y)
|
||||||
{
|
{
|
||||||
if ((x < 0) || (x >= _iwidth) || (y < 0) || (y >= _iheight) || !_created) return 0xFFFF;
|
if ((x < 0) || (x >= _iwidth) || (y < 0) || (y >= _iheight) || !_created) return 0xFFFF;
|
||||||
|
|
||||||
@@ -892,7 +919,9 @@ void TFT_eSprite::pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint16_
|
|||||||
{
|
{
|
||||||
// not supported. The image is unlikely to have the correct colors for the color map.
|
// not supported. The image is unlikely to have the correct colors for the color map.
|
||||||
// we could implement a way to push a 4-bit image using the color map?
|
// we could implement a way to push a 4-bit image using the color map?
|
||||||
|
#ifdef TFT_eSPI_DEBUG
|
||||||
Serial.println("pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t *data) not implemented");
|
Serial.println("pushImage(int32_t x, int32_t y, int32_t w, int32_t h, uint16_t *data) not implemented");
|
||||||
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -995,7 +1024,9 @@ void TFT_eSprite::pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const u
|
|||||||
|
|
||||||
else if (_bpp == 4)
|
else if (_bpp == 4)
|
||||||
{
|
{
|
||||||
|
#ifdef TFT_eSPI_DEBUG
|
||||||
Serial.println("TFT_eSprite::pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data) not implemented");
|
Serial.println("TFT_eSprite::pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data) not implemented");
|
||||||
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1286,8 +1317,8 @@ void TFT_eSprite::scroll(int16_t dx, int16_t dy)
|
|||||||
{ // move pixels one by one
|
{ // move pixels one by one
|
||||||
for (uint16_t xp = 0; xp < w; xp++)
|
for (uint16_t xp = 0; xp < w; xp++)
|
||||||
{
|
{
|
||||||
if (dx <= 0) drawPixel(tx + xp, ty, cmapPixel(fx + xp, fy));
|
if (dx <= 0) drawPixel(tx + xp, ty, readPixelValue(fx + xp, fy));
|
||||||
if (dx > 0) drawPixel(tx - xp, ty, cmapPixel(fx - xp, fy));
|
if (dx > 0) drawPixel(tx - xp, ty, readPixelValue(fx - xp, fy));
|
||||||
}
|
}
|
||||||
if (dy <= 0) { ty++; fy++; }
|
if (dy <= 0) { ty++; fy++; }
|
||||||
else { ty--; fy--; }
|
else { ty--; fy--; }
|
||||||
|
@@ -37,8 +37,14 @@ class TFT_eSprite : public TFT_eSPI {
|
|||||||
void* setColorDepth(int8_t b);
|
void* setColorDepth(int8_t b);
|
||||||
int8_t getColorDepth(void);
|
int8_t getColorDepth(void);
|
||||||
|
|
||||||
// Set the colour map for a 4 bit depth sprite. Only the first 16 colours in the map are used.
|
// Set the palette for a 4 bit depth sprite. Only the first 16 colours in the map are used.
|
||||||
void setColorMap(uint16_t *colorMap, int colors);
|
void createPalette(uint16_t *palette, int colors = 16);
|
||||||
|
|
||||||
|
// Set a single palette index to the given color
|
||||||
|
void setPaletteColor(uint8_t index, uint16_t color);
|
||||||
|
|
||||||
|
// Get the color at the given palette index
|
||||||
|
uint16_t getPaletteColor(uint8_t index);
|
||||||
|
|
||||||
// Set foreground and background colours for 1 bit per pixel Sprite
|
// Set foreground and background colours for 1 bit per pixel Sprite
|
||||||
void setBitmapColor(uint16_t fg, uint16_t bg);
|
void setBitmapColor(uint16_t fg, uint16_t bg);
|
||||||
@@ -104,7 +110,7 @@ class TFT_eSprite : public TFT_eSPI {
|
|||||||
uint16_t readPixel(int32_t x0, int32_t y0);
|
uint16_t readPixel(int32_t x0, int32_t y0);
|
||||||
|
|
||||||
// return the color map index of the pixel at x,y (used when scrolling)
|
// return the color map index of the pixel at x,y (used when scrolling)
|
||||||
uint8_t cmapPixel(int32_t x, int32_t y);
|
uint8_t readPixelValue(int32_t x, int32_t y);
|
||||||
|
|
||||||
// Write an image (colour bitmap) to the sprite. Not implemented for _bpp == 4.
|
// Write an image (colour bitmap) to the sprite. Not implemented for _bpp == 4.
|
||||||
void pushImage(int32_t x0, int32_t y0, int32_t w, int32_t h, uint16_t *data);
|
void pushImage(int32_t x0, int32_t y0, int32_t w, int32_t h, uint16_t *data);
|
||||||
|
@@ -84,7 +84,7 @@ void loop(void)
|
|||||||
cmap[14] = TFT_YELLOW;
|
cmap[14] = TFT_YELLOW;
|
||||||
cmap[15] = TFT_WHITE;
|
cmap[15] = TFT_WHITE;
|
||||||
|
|
||||||
spr.setColorMap(cmap, 16);
|
spr.createPalette(cmap, 16);
|
||||||
|
|
||||||
spr.pushSprite(-40, -40);
|
spr.pushSprite(-40, -40);
|
||||||
spr.pushSprite(tft.width() / 2 - WIDTH / 2, tft.height() / 2 - HEIGHT / 2, 10);
|
spr.pushSprite(tft.width() / 2 - WIDTH / 2, tft.height() / 2 - HEIGHT / 2, 10);
|
||||||
@@ -147,7 +147,7 @@ void loop(void)
|
|||||||
cmap[i] = random(0x10000);
|
cmap[i] = random(0x10000);
|
||||||
}
|
}
|
||||||
|
|
||||||
spr.setColorMap(cmap, 16);
|
spr.createPalette(cmap, 16);
|
||||||
// Now push the sprite to the TFT at position 0,0 on screen
|
// Now push the sprite to the TFT at position 0,0 on screen
|
||||||
|
|
||||||
spr.pushSprite(-40, -40);
|
spr.pushSprite(-40, -40);
|
||||||
@@ -162,7 +162,7 @@ void loop(void)
|
|||||||
// Draw a blue rectangle in sprite so when we move it 1 pixel it does not leave a trail
|
// Draw a blue rectangle in sprite so when we move it 1 pixel it does not leave a trail
|
||||||
// on the blue screen background
|
// on the blue screen background
|
||||||
cmap[14] = TFT_BLUE;
|
cmap[14] = TFT_BLUE;
|
||||||
spr.setColorMap(cmap, 16);
|
spr.createPalette(cmap, 16);
|
||||||
|
|
||||||
spr.drawRect(0, 0, WIDTH, HEIGHT, 14);
|
spr.drawRect(0, 0, WIDTH, HEIGHT, 14);
|
||||||
|
|
||||||
|
@@ -63,7 +63,7 @@ void setup() {
|
|||||||
// Create a sprite for the graph
|
// Create a sprite for the graph
|
||||||
graph1.setColorDepth(4);
|
graph1.setColorDepth(4);
|
||||||
graph1.createSprite(128, 61);
|
graph1.createSprite(128, 61);
|
||||||
graph1.setColorMap(cmap, 16);
|
graph1.createPalette(cmap, 16);
|
||||||
graph1.fillSprite(9); // Note: Sprite is filled with black when created
|
graph1.fillSprite(9); // Note: Sprite is filled with black when created
|
||||||
|
|
||||||
// The scroll area is set to the full sprite size upon creation of the sprite
|
// The scroll area is set to the full sprite size upon creation of the sprite
|
||||||
@@ -74,7 +74,7 @@ void setup() {
|
|||||||
// Create a sprite for the scrolling numbers
|
// Create a sprite for the scrolling numbers
|
||||||
stext1.setColorDepth(4);
|
stext1.setColorDepth(4);
|
||||||
stext1.createSprite(32, 64);
|
stext1.createSprite(32, 64);
|
||||||
stext1.setColorMap(cmap, 16);
|
stext1.createPalette(cmap, 16);
|
||||||
stext1.fillSprite(9); // Fill sprite with blue
|
stext1.fillSprite(9); // Fill sprite with blue
|
||||||
stext1.setScrollRect(0, 0, 32, 64, 9); // here we set scroll gap fill color to blue
|
stext1.setScrollRect(0, 0, 32, 64, 9); // here we set scroll gap fill color to blue
|
||||||
stext1.setTextColor(15); // White text, no background
|
stext1.setTextColor(15); // White text, no background
|
||||||
@@ -83,7 +83,7 @@ void setup() {
|
|||||||
// Create a sprite for Hello World
|
// Create a sprite for Hello World
|
||||||
stext2.setColorDepth(4);
|
stext2.setColorDepth(4);
|
||||||
stext2.createSprite(80, 16);
|
stext2.createSprite(80, 16);
|
||||||
stext2.setColorMap(cmap, 16);
|
stext2.createPalette(cmap, 16);
|
||||||
stext2.fillSprite(7);
|
stext2.fillSprite(7);
|
||||||
stext2.setScrollRect(0, 0, 40, 16, 7); // Scroll the "Hello" in the first 40 pixels
|
stext2.setScrollRect(0, 0, 40, 16, 7); // Scroll the "Hello" in the first 40 pixels
|
||||||
stext2.setTextColor(15); // White text, no background
|
stext2.setTextColor(15); // White text, no background
|
||||||
|
@@ -102,7 +102,7 @@ void drawStar(int x, int y, int star_color)
|
|||||||
cmap[14] = TFT_YELLOW;
|
cmap[14] = TFT_YELLOW;
|
||||||
cmap[15] = TFT_WHITE; // this one will be transparent.
|
cmap[15] = TFT_WHITE; // this one will be transparent.
|
||||||
|
|
||||||
img.setColorMap(cmap, 16);
|
img.createPalette(cmap, 16);
|
||||||
|
|
||||||
// Fill Sprite with a "transparent" colour
|
// Fill Sprite with a "transparent" colour
|
||||||
// TFT_TRANSPARENT is already defined for convenience
|
// TFT_TRANSPARENT is already defined for convenience
|
||||||
@@ -156,7 +156,7 @@ void numberBox(int x, int y, float num )
|
|||||||
img.setTextDatum(MR_DATUM);
|
img.setTextDatum(MR_DATUM);
|
||||||
|
|
||||||
// Draw the number to 3 decimal places at 70,20 in font 4
|
// Draw the number to 3 decimal places at 70,20 in font 4
|
||||||
img.drawFloat(num, 3, 70, 20, 1);
|
img.drawFloat(num, 3, 70, 20, 4);
|
||||||
|
|
||||||
// Push sprite to TFT screen CGRAM at coordinate x,y (top left corner)
|
// Push sprite to TFT screen CGRAM at coordinate x,y (top left corner)
|
||||||
// All black pixels will not be drawn hence will show as "transparent"
|
// All black pixels will not be drawn hence will show as "transparent"
|
||||||
|
Reference in New Issue
Block a user