From cde86db8adb53ea91b801c62eb050a3e094e82d3 Mon Sep 17 00:00:00 2001 From: beegee-tokyo Date: Wed, 6 Dec 2017 17:20:26 +0800 Subject: [PATCH] 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 --- TFT_eSPI.cpp | 86 +++++++++++++++++++ TFT_eSPI.h | 6 ++ .../TFT_ShiftSprite/TFT_ShiftSprite.ino | 38 ++++++++ 3 files changed, 130 insertions(+) create mode 100644 examples/160 x 128/TFT_ShiftSprite/TFT_ShiftSprite.ino diff --git a/TFT_eSPI.cpp b/TFT_eSPI.cpp index 70939c2..2c7c478 100644 --- a/TFT_eSPI.cpp +++ b/TFT_eSPI.cpp @@ -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) diff --git a/TFT_eSPI.h b/TFT_eSPI.h index ef4ecf8..d942708 100644 --- a/TFT_eSPI.h +++ b/TFT_eSPI.h @@ -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); diff --git a/examples/160 x 128/TFT_ShiftSprite/TFT_ShiftSprite.ino b/examples/160 x 128/TFT_ShiftSprite/TFT_ShiftSprite.ino new file mode 100644 index 0000000..c0d5b98 --- /dev/null +++ b/examples/160 x 128/TFT_ShiftSprite/TFT_ShiftSprite.ino @@ -0,0 +1,38 @@ +#include +#include + +/** 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); +}