Created
March 17, 2024 19:31
-
-
Save jones2126/90dda87dacecac81c32946fc23300b12 to your computer and use it in GitHub Desktop.
Two examples of using the 1.28" TFT LCD and an ESP32. The first example is extremely simple; However it flickers. The second one avoids the flicker by using the sprite function.
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
// First example - extremely simple | |
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip | |
#include <SPI.h> | |
TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h | |
unsigned long displayWeightLastTime = 0; | |
unsigned long displayWeightDelay = 1000; | |
unsigned long currentMillis = 0; | |
float weight = 123.99; | |
byte xpos = 40; | |
byte ypos = 90; | |
void setup(void) { | |
tft.init(); | |
tft.setRotation(0); | |
tft.fillScreen(TFT_BLACK); | |
tft.setTextColor(TFT_YELLOW, TFT_BLACK); // Note: the new fonts do not draw the background colour | |
} | |
void loop() { | |
currentMillis = millis(); | |
if (currentMillis - displayWeightLastTime >= displayWeightDelay) { | |
displayWeight(); | |
displayWeightLastTime = currentMillis; | |
} | |
} | |
void displayWeight() { | |
int fontWeight = 6; | |
tft.fillScreen(TFT_BLACK); // Optional: clear the screen to update the weight display | |
tft.setTextColor(TFT_ORANGE); // Set text color to orange | |
char buff[10]; // Buffer to hold the weight string | |
// Convert the float to a string with 1 decimal place | |
snprintf(buff, sizeof(buff), "%.2f", weight); | |
// Use drawString to display the weight at the specified position | |
tft.drawString(buff, xpos, ypos, fontWeight); // The last parameter is the font size, adjust as needed | |
} | |
// The second example begins here. It uses the 'sprite' approach to avoid flicker. | |
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip | |
#include <SPI.h> | |
TFT_eSPI tft = TFT_eSPI(); // Invoke TFT library | |
TFT_eSprite spr = TFT_eSprite(&tft); // Create sprite | |
int spriteWidth = 220; // Width of the sprite slightly larger than the circle's diameter | |
int spriteHeight = 220; // Height as well, to ensure the circle fits | |
float weight = 123.99; | |
void setup(void) { | |
tft.init(); | |
tft.setRotation(0); | |
tft.fillScreen(TFT_BLACK); | |
// Initialize sprite with enough space for the circle and text | |
spr.createSprite(spriteWidth, spriteHeight); | |
spr.setTextColor(TFT_YELLOW, TFT_BLACK); // Set sprite text color | |
} | |
void loop() { | |
// Display weight and update every 1 second | |
displayWeight(); | |
delay(1000); // Simple delay for demonstration, adjust as needed | |
} | |
void displayWeight() { | |
spr.fillSprite(TFT_BLACK); // Clear the sprite for fresh drawing | |
// Drawing the circle centered in the sprite | |
int circleDiameter = 215; | |
int circleRadius = circleDiameter / 2; | |
int circleX = spriteWidth / 2; // Center of the sprite width | |
int circleY = spriteHeight / 2; // Center of the sprite height | |
spr.drawCircle(circleX, circleY, circleRadius, TFT_RED); // Draw the circle centered in the sprite | |
spr.setTextSize(4); // Adjust size of the displayed weight as needed | |
// Adding an additional offset moves the text down within the circle | |
int offsetY = 16; // Adjust to lower the text below the center point | |
int textY = circleY + offsetY - spr.fontHeight() / 2; | |
// Display the weight text with adjusted y position | |
char buff[10]; | |
snprintf(buff, sizeof(buff), "%.2f", weight); | |
spr.setTextDatum(MC_DATUM); // Center text both horizontally and vertically | |
spr.drawString(buff, circleX, textY); // Draw weight text with new y position | |
// Push the sprite to the center of the screen | |
int spriteX = (240 - spriteWidth) / 2; // Adjust for a 240x240 display | |
int spriteY = (240 - spriteHeight) / 2; | |
spr.pushSprite(spriteX, spriteY); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment