Add shiftRectLeft() and shiftRectRight()

Added 2 functions to shift a sprite content to left or right.
Added example for usage of shift functions.

Signed-off-by: beegee-tokyo <beegee@giesecke.tk>
This commit is contained in:
beegee-tokyo 2017-12-06 17:20:26 +08:00
parent 7f909a1527
commit cde86db8ad
3 changed files with 130 additions and 0 deletions

View File

@ -4516,6 +4516,92 @@ void TFT_eSprite::pushRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint
}
}
/***************************************************************************************
** Function name: readRect
** Description: reads 565 colour bitmap from a defined area into a buffer
***************************************************************************************/
void TFT_eSprite::readRect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t *data)
{
if ((x > _iwidth) || (y > _iheight) || (w == 0) || (h == 0) || !_created) return;
if (_bpp16)
{
for (uint32_t yp = y; yp < y + h; yp++)
{
for (uint32_t xp = x; xp < x + w; xp++)
{
*data++ = _img[xp + yp * _iwidth];
}
}
}
else
{
for (uint32_t yp = y; yp < y + h; yp++)
{
for (uint32_t xp = x; xp < x + w; xp++)
{
uint16_t color = _img8[xp + yp * _iwidth];
*data++ = (uint8_t)((color & 0xE000)>>8 | (color & 0x0700)>>6 | (color & 0x0018)>>3);
}
}
}
}
/***************************************************************************************
** Function name: shiftRectLeft
** Description: shifts sprite left and fill right side with given color
***************************************************************************************/
void TFT_eSprite::shiftRectLeft(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t shift, uint32_t color)
{
if ((x > _iwidth) || (y > _iheight) || (w == 0) || (h == 0) || !_created) return;
for (uint32_t yp = y; yp < y + h; yp++)
{
for (uint32_t xp = x + shift; xp < x + w; xp++)
{
_img[xp-shift + yp * _iwidth] = _img[xp + yp * _iwidth];
}
for (uint32_t xp = x + w; xp < x + w + shift; xp++)
{
if (_bpp16)
{
_img[xp-shift + yp * _iwidth] = (uint16_t) (color >> 8) | (color << 8);
}
else
{
_img[xp-shift + yp * _iwidth] = (uint8_t)((color & 0xE000)>>8 | (color & 0x0700)>>6 | (color & 0x0018)>>3);
}
}
}
}
/***************************************************************************************
** Function name: shiftRectRight
** Description: shifts sprite right and fill left side with given color
***************************************************************************************/
void TFT_eSprite::shiftRectRight(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t shift, uint32_t color)
{
if ((x > _iwidth) || (y > _iheight) || (w == 0) || (h == 0) || !_created) return;
for (uint32_t yp = y; yp < y + h; yp++)
{
for (uint32_t xp = x + w - 1; xp != x; xp--)
{
_img[xp + yp * _iwidth] = _img[xp-shift + yp * _iwidth];
}
for (uint32_t xp = x; xp < x + shift; xp++)
{
if (_bpp16)
{
_img[xp + yp * _iwidth] = (uint16_t) (color >> 8) | (color << 8);
}
else
{
_img[xp + yp * _iwidth] = (uint8_t)((color & 0xE000)>>8 | (color & 0x0700)>>6 | (color & 0x0018)>>3);
}
}
}
}
/***************************************************************************************
** Function name: pushBitmap (same as pushRect)

View File

@ -592,6 +592,12 @@ class TFT_eSprite : public TFT_eSPI {
// Read the colour of a pixel at x,y and return value in 565 format
uint16_t readPixel(int32_t x0, int32_t y0);
// Read a block of pixels to a data buffer, buffer is 16 bit and the array size must be at least w * h
void readRect(uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, uint16_t *data);
// Shift the sprite shift pixels to the left and fill right side with color
void shiftRectLeft(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t shift, uint32_t color);
// Shift the sprite shift pixels to the right and fill left side with color
void shiftRectRight(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint32_t shift, uint32_t color);
// Write a block of pixels to the sprite
void pushRect(uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, uint16_t *data);
void pushBitmap(uint32_t x0, uint32_t y0, uint32_t w, uint32_t h, uint16_t *data);

View File

@ -0,0 +1,38 @@
#include <Arduino.h>
#include <TFT_eSPI.h>
/** TFT_eSPI class for display */
TFT_eSPI tft = TFT_eSPI();
/** Sprite needed for graph scrolling */
TFT_eSprite graph1 = TFT_eSprite(&tft);
/** Sprite needed for graph scrolling */
TFT_eSprite graph2 = TFT_eSprite(&tft);
/** Value for graph1 */
int graph1Val = 1;
/** Value for graph2 */
int graph2Val = 1;
void setup() {
tft.init();
tft.fillScreen(TFT_BLACK);
graph1.createSprite(128, 64);
graph1.fillSprite(TFT_BLACK);
graph2.createSprite(128, 64);
graph2.fillSprite(TFT_BLACK);
}
void loop() {
graph1.shiftRectLeft(0, 0, 128, 64, 1, TFT_BLACK);
graph1.drawFastVLine(127,64-graph1Val,graph1Val,TFT_YELLOW);
graph2.shiftRectRight(0, 0, 128, 64, 1, TFT_BLACK);
graph2.drawFastVLine(0,64-graph2Val,graph2Val,TFT_RED);
graph1.pushSprite(0, 32);
graph2.pushSprite(0, 96);
graph1Val++;
graph2Val++;
if (graph1Val == 65) graph1Val = 1;
if (graph2Val == 65) graph2Val = 1;
delay(50);
}