Created
March 11, 2024 21:02
-
-
Save jones2126/e0f5552eafbd3c9419a99385192b3c08 to your computer and use it in GitHub Desktop.
Example of rotating sprites using the TFT_eSPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Example code for 1.28 inch, 240*240 TFT LCD Display which uses the GC9A01 driver. The code rotates a needle | |
around a pivot point (think compass needle). Thanks to "Volos Projects" on YouTube. | |
Original code ref: https://github.com/VolosR/RotateSpritesTutorial/blob/main/RotateSpritesTutorial.ino | |
YouTube ref: https://www.youtube.com/watch?v=oqBa_ptBmLU | |
I adapted the example code for my LCD that is 240x240 and used some variables in the process | |
*/ | |
#include <TFT_eSPI.h> | |
TFT_eSPI tft = TFT_eSPI(); | |
TFT_eSprite back = TFT_eSprite(&tft); | |
TFT_eSprite needle = TFT_eSprite(&tft); | |
// Screen dimensions | |
const int SCREEN_WIDTH = 240; | |
const int SCREEN_HEIGHT = 240; | |
// Calculate pivot point based on screen size, you could add an offset if you did not want it in the center. | |
const int PIVOT_X = SCREEN_WIDTH / 2; | |
const int PIVOT_Y = SCREEN_HEIGHT / 2; | |
// Sprite dimensions and positions can also be adjusted relative to screen size | |
const int NEEDLE_WIDTH = 20; | |
const int NEEDLE_HEIGHT = SCREEN_HEIGHT / 2; | |
const int BACK_DIAMETER = min(SCREEN_WIDTH, SCREEN_HEIGHT) * 0.7; // Ensure the sprite fits within the screen | |
void setup() { | |
tft.init(); | |
tft.fillScreen(TFT_BLACK); | |
tft.setPivot(PIVOT_X, PIVOT_Y); | |
needle.createSprite(NEEDLE_WIDTH, NEEDLE_HEIGHT); | |
back.createSprite(BACK_DIAMETER, BACK_DIAMETER); | |
} | |
int angle = 0; | |
void loop() { | |
back.fillSprite(TFT_BLACK); | |
back.fillCircle(BACK_DIAMETER / 2, BACK_DIAMETER / 2, BACK_DIAMETER / 2, TFT_SILVER); | |
needle.fillSprite(TFT_BLACK); | |
needle.drawWedgeLine(NEEDLE_WIDTH / 2, 0, NEEDLE_WIDTH / 2, NEEDLE_HEIGHT / 2, 1, NEEDLE_WIDTH / 2, TFT_RED); | |
needle.drawWedgeLine(NEEDLE_WIDTH / 2, NEEDLE_HEIGHT / 2, NEEDLE_WIDTH / 2, NEEDLE_HEIGHT, NEEDLE_WIDTH / 2, 1, TFT_BLUE); | |
needle.fillCircle(NEEDLE_WIDTH / 2, NEEDLE_HEIGHT / 2, NEEDLE_WIDTH / 2, TFT_WHITE); | |
needle.pushRotated(&back, angle, TFT_BLACK); | |
back.pushSprite((SCREEN_WIDTH - BACK_DIAMETER) / 2, (SCREEN_HEIGHT - BACK_DIAMETER) / 2); // Center the sprite on the screen | |
angle++; | |
if(angle == 360) | |
angle = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment