Skip to content

Commit

Permalink
integrate TFT_eSPT Sprite
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1abin committed May 18, 2018
1 parent 1f4ef7b commit e9c60a5
Show file tree
Hide file tree
Showing 15 changed files with 3,109 additions and 11 deletions.
@@ -0,0 +1,102 @@
/*
Sketch to show creation of a 1bpp sprite with a transparent
background, then plot it on the M5.Lcd.
Example for library:
https://github.com/Bodmer/TFT_eSPI
A Sprite is notionally an invisible graphics screen that is
kept in the processors RAM. Graphics can be drawn into the
Sprite just as it can be drawn directly to the screen. Once
the Sprite is completed it can be plotted onto the screen in
any position. If there is sufficient RAM then the Sprite can
be the same size as the screen and used as a frame buffer.
A 1 bit Sprite occupies (width * height)/8 bytes in RAM. So,
for example, a 320 x 240 pixel Sprite occupies 9600 bytes.
*/
// A new setBitmapColor(fg_color, bg_color) function allows
// any 2 colours to be used for the 1 bit sprite.
// One colour can also be defined as transparent when
// rendering to the screen.


#include <M5Stack.h>

TFT_eSprite img = TFT_eSprite(&M5.Lcd);

#define BITS_PER_PIXEL 1 // How many bits per pixel in Sprite

// =========================================================================
void setup(void) {

M5.begin();

M5.Lcd.setRotation(0);
}
// =========================================================================
void loop() {

M5.Lcd.fillScreen(TFT_NAVY);

// Draw 10 sprites containing a "transparent" colour
for (int i = 0; i < 10; i++)
{
int x = random(240 - 70);
int y = random(320 - 80);
int c = random(0x10000); // Random colour 0 - 0xFFFF
drawStar(x, y, c);
}

delay(2000);

// Now go bananas and draw 500 more
for (int i = 0; i < 500; i++)
{
int x = random(240 - 70);
int y = random(320 - 80);
int c = random(0x10000); // Random colour
drawStar(x, y, c);
yield(); // Stop watchdog reset
}

delay(2000);
}
// =========================================================================
// Create sprite, plot graphics in it, plot to screen, then delete sprite
// =========================================================================
void drawStar(int x, int y, int star_color)
{
// 1 bpp colour values can only be 1 or 0 (one or zero)
uint16_t transparent = 0; // The transparent colour, can only be 1 or 0

// Create an 1 bit (2 colour) sprite 70x80 pixels (uses 70*80/8 = 700 bytes of RAM)
// Colour depths of 8 bits per pixel and 16 bits are also supported.
img.setColorDepth(BITS_PER_PIXEL); // Set colour depth first
img.createSprite(70, 80); // then create the sprite

// Fill Sprite with the colour that will be defined later as "transparent"
// We could also fill with any colour as transparent, and later specify that
// same colour when we push the Sprite onto the display screen.
img.fillSprite(transparent);

// Draw 2 triangles to create a filled in star
img.fillTriangle(35, 0, 0, 59, 69, 59, star_color);
img.fillTriangle(35, 79, 0, 20, 69, 20, star_color);

// Punch a star shaped hole in the middle with a smaller "transparent" star
img.fillTriangle(35, 7, 6, 56, 63, 56, transparent);
img.fillTriangle(35, 73, 6, 24, 63, 24, transparent);

// Set the 2 pixel colours that 1 and 0 represent on the display screen
img.setBitmapColor(star_color, transparent);

// Push sprite to TFT screen CGRAM at coordinate x,y (top left corner)
// Specify what colour is to be treated as transparent (black in this example)
img.pushSprite(x, y, transparent);

// Delete Sprite to free memory, creating and deleting takes very little time.
img.deleteSprite();
}
// =========================================================================

95 changes: 95 additions & 0 deletions examples/Advanced/Display/Sprite/1_bit_Yin_Yang/1_bit_Yin_Yang.ino
@@ -0,0 +1,95 @@
// This sketch draws a rotating Yin and Yang symbol. It illustrates
// the drawimg and rendering of simple animated graphics using
// a 1 bit per pixel (1 bpp) Sprite.

// Note: TFT_BLACK sets the pixel value to 0
// Any other colour sets the pixel value to 1

// A square sprite of side = 2 x RADIUS will be created
// (80 * 80)/8 = 800 bytes needed for 1 bpp sprite
// 6400 bytes for 8 bpp
// 12800 bytes for 16 bpp
#define RADIUS 40 // Radius of completed symbol = 40

#define WAIT 0 // Loop delay

// 1bpp Sprites are economical on memory but slower to render
#define COLOR_DEPTH 1 // Colour depth (1, 8 or 16 bits per pixel)

// Rotation angle increment and start angle
#define ANGLE_INC 3
int angle = 0;

#include <M5Stack.h>

TFT_eSprite img = TFT_eSprite(&M5.Lcd);


// -------------------------------------------------------------------------
void setup(void)
{
M5.begin();
M5.Lcd.setRotation(0);
M5.Lcd.fillScreen(TFT_BLUE);

img.setColorDepth(COLOR_DEPTH);
img.createSprite(RADIUS*2+1, RADIUS*2+1);
img.fillSprite(TFT_BLACK);
}
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
void loop() {
// Draw Yin and Yang symbol circles into Sprite
yinyang(RADIUS, RADIUS, angle, RADIUS);

// Set the 2 pixel palette colours that 1 and 0 represent on the display screen
img.setBitmapColor(TFT_WHITE, TFT_BLACK);

// Push Sprite image to the TFT screen at x, y
img.pushSprite(M5.Lcd.width()/2 - RADIUS, 0); // Plot sprite

angle+=3; //Increment angle to rotate circle positions
if (angle > 359) angle = 0; // Limit angle range

// Slow things down
delay(WAIT);
}
// -------------------------------------------------------------------------


// =========================================================================
// Draw circles for Yin and Yang - rotate positions to create symbol
// =========================================================================
// x,y == coordinate center within Sprite
// start_angle = 0 - 359
// r = radius

int yinyang(int x, int y, int start_angle, int r)
{
int x1 = 0; // getCoord() will update these
int y1 = 0;

getCoord(x, y, &x1, &y1, r/2, start_angle); // Get x1 ,y1
img.fillCircle( x1, y1, r/2, TFT_WHITE);
img.fillCircle( x1, y1, r/8, TFT_BLACK);

getCoord(x, y, &x1, &y1, r/2, start_angle + 180);
img.fillCircle( x1, y1, r/2, TFT_BLACK);
img.fillCircle( x1, y1, r/8, TFT_WHITE);

img.drawCircle(x, y, r, TFT_WHITE);
}

// =========================================================================
// Get coordinates of end of a vector, pivot at x,y, length r, angle a
// =========================================================================
// Coordinates are returned to caller via the xp and yp pointers
#define RAD2DEG 0.0174532925
void getCoord(int x, int y, int *xp, int *yp, int r, int a)
{
float sx1 = cos( (a-90) * RAD2DEG );
float sy1 = sin( (a-90) * RAD2DEG );
*xp = sx1 * r + x;
*yp = sy1 * r + y;
}

@@ -0,0 +1,192 @@
/*
Display all the fast rendering fonts in a sprite
Make sure all the display driver and pin comnections are correct by
editting the User_Setup.h file in the TFT_eSPI library folder.
#########################################################################
###### DON'T FORGET TO UPDATE THE User_Setup.h FILE IN THE LIBRARY ######
#########################################################################
*/

// Specify sprite 160 x 128 pixels (needs 40Kbytes of RAM for 16 bit colour)
#define IWIDTH 320
#define IHEIGHT 128

// Pause in milliseconds between screens, change to 0 to time font rendering
#define WAIT 500

#include <M5Stack.h>

TFT_eSprite img = TFT_eSprite(&M5.Lcd);

unsigned long targetTime = 0; // Used for testing draw times

void setup(void) {
M5.begin();
M5.Lcd.fillScreen(TFT_BLUE);

//img.setColorDepth(8); // Optionally set depth to 8 to halve RAM use
img.createSprite(IWIDTH, IHEIGHT);
img.fillSprite(TFT_BLACK);
}

void loop() {
targetTime = millis();

// First we test them with a background colour set
img.setTextSize(1);
img.fillSprite(TFT_BLACK);
img.setTextColor(TFT_GREEN, TFT_BLACK);

img.drawString(" !\"#$%&'()*+,-./0123456", 0, 0, 2);
img.drawString("789:;<=>?@ABCDEFGHIJKL", 0, 16, 2);
img.drawString("MNOPQRSTUVWXYZ[\\]^_`", 0, 32, 2);
img.drawString("abcdefghijklmnopqrstuvw", 0, 48, 2);

int xpos = 0;
xpos += img.drawString("xyz{|}~", 0, 64, 2);
img.drawChar(127, xpos, 64, 2);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.setTextColor(TFT_GREEN, TFT_BLACK);

img.drawString(" !\"#$%&'()*+,-.", 0, 0, 4);
img.drawString("/0123456789:;", 0, 26, 4);
img.drawString("<=>?@ABCDE", 0, 52, 4);
img.drawString("FGHIJKLMNO", 0, 78, 4);
img.drawString("PQRSTUVWX", 0, 104, 4);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.drawString("YZ[\\]^_`abc", 0, 0, 4);
img.drawString("defghijklmno", 0, 26, 4);
img.drawString("pqrstuvwxyz", 0, 52, 4);
xpos = 0;
xpos += img.drawString("{|}~", 0, 78, 4);
img.drawChar(127, xpos, 78, 4);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.setTextColor(TFT_BLUE, TFT_BLACK);

img.drawString("012345", 0, 0, 6);
img.drawString("6789", 0, 40, 6);
img.drawString("apm-:.", 0, 80, 6);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.setTextColor(TFT_RED, TFT_BLACK);

img.drawString("0123", 0, 0, 7);
img.drawString("4567", 0, 60, 7);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.drawString("890:.", 0, 0, 7);
img.drawString("", 0, 60, 7);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.setTextColor(TFT_YELLOW, TFT_BLACK);

img.drawString("01", 0, 0, 8);
img.pushSprite(0, 0); delay(WAIT);

img.drawString("23", 0, 0, 8);
img.pushSprite(0, 0); delay(WAIT);

img.drawString("45", 0, 0, 8);
img.pushSprite(0, 0); delay(WAIT);

img.drawString("67", 0, 0, 8);
img.pushSprite(0, 0); delay(WAIT);

img.drawString("89", 0, 0, 8);
img.pushSprite(0, 0); delay(WAIT);

img.drawString("0:.", 0, 0, 8);
img.pushSprite(0, 0); delay(WAIT);

img.setTextColor(TFT_WHITE);
img.drawNumber(millis() - targetTime, 0, 100, 4);
img.pushSprite(0, 0); delay(WAIT);
delay(4000);

// Now test them with transparent background
targetTime = millis();

img.setTextSize(1);
img.fillSprite(TFT_BLACK);
img.setTextColor(TFT_GREEN);

img.drawString(" !\"#$%&'()*+,-./0123456", 0, 0, 2);
img.drawString("789:;<=>?@ABCDEFGHIJKL", 0, 16, 2);
img.drawString("MNOPQRSTUVWXYZ[\\]^_`", 0, 32, 2);
img.drawString("abcdefghijklmnopqrstuvw", 0, 48, 2);
xpos = 0;
xpos += img.drawString("xyz{|}~", 0, 64, 2);
img.drawChar(127, xpos, 64, 2);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.setTextColor(TFT_GREEN);

img.drawString(" !\"#$%&'()*+,-.", 0, 0, 4);
img.drawString("/0123456789:;", 0, 26, 4);
img.drawString("<=>?@ABCDE", 0, 52, 4);
img.drawString("FGHIJKLMNO", 0, 78, 4);
img.drawString("PQRSTUVWX", 0, 104, 4);

img.pushSprite(0, 0); delay(WAIT);
img.fillSprite(TFT_BLACK);
img.drawString("YZ[\\]^_`abc", 0, 0, 4);
img.drawString("defghijklmno", 0, 26, 4);
img.drawString("pqrstuvwxyz", 0, 52, 4);
xpos = 0;
xpos += img.drawString("{|}~", 0, 78, 4);
img.drawChar(127, xpos, 78, 4);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.setTextColor(TFT_BLUE);

img.drawString("012345", 0, 0, 6);
img.drawString("6789", 0, 40, 6);
img.drawString("apm-:.", 0, 80, 6);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.setTextColor(TFT_RED);

img.drawString("0123", 0, 0, 7);
img.drawString("4567", 0, 60, 7);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.drawString("890:.", 0, 0, 7);
img.drawString("", 0, 60, 7);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.setTextColor(TFT_YELLOW);

img.drawString("0123", 0, 0, 8);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.drawString("4567", 0, 0, 8);
img.pushSprite(0, 0); delay(WAIT);

img.fillSprite(TFT_BLACK);
img.drawString("890:.", 0, 0, 8);
img.pushSprite(0, 0); delay(WAIT);

img.setTextColor(TFT_WHITE);

img.drawNumber(millis() - targetTime, 0, 100, 4);
img.pushSprite(0, 0); delay(WAIT);
delay(4000);;
}

0 comments on commit e9c60a5

Please sign in to comment.