I am writing a sketch in which I would like to print out some numerical values on the screen of a TFT display screen. Is there a simple way to do this using the code in my sketch to change or select the size of the printed output on the TFT display screen, say 12 14 or 16 pt?
Which library are you using in the sketch ?
Please post your full sketch using code tags when you do
check the "keywords.txt" in your library map (libraries\TFT-something\keywords.txt) and see if you can find something like char-size ... then you can take a look in the .cpp file to see what parameters you have to apply..
It will be something like:
display.setTextSize(1);
But what exactly will depend on the mystery library you are using.
You will likely also need to adjust positioning of the text.
UKHeliBob; Thank you for responding to my question.
I am using Bodmer's TFT_eSPI Library. I am writing this code to display an analog meter face
on an MSP2807 TFT Display, driven by an ILI9341 IC.
This is the meter face shown on the MSP2807 display:
My objective is to increase the font size associated with the digital printout in the red area in the lower left hand corner of the meter display.
My apologies for omitting the sketch which I now attach:
// ########################################################################
// #
// # animated analogue meters sketch using a ILI9341 TFT LCD screen
// #
// # It appears that this sketch uses the 30-Pin DOIT ESP32 Module
// #
// # This sketch worked on 2-1-22
// #
// # Originally downloaded from ElectroPeak on 12-28-21
// #
// # This sketch had Floris's User Setup File
// #
// # I added my User Setup File on 2-1-22
// #
// # Needs Font 2 (also Font 4 if using large scale label)
// #
// # Comm Port: COM3
// #
// ########################################################################
// ########################################################################
// ########################################################################
// #
// # Begin Internal Sketch User Setup Statements
// #
// ########################################################################
// ########################################################################
#define TFT_RGB_ORDER TFT_BGR // Colour order Blue-Green-Red
// ########################################################################
//
// Section 2. Define the pins that are used to interface with the display
//
// #######################################################################
// #define TFT_BL 32 // LED back-light control pin
// #define TFT_BACKLIGHT_ON HIGH // Level to turn ON back-light (HIGH or LOW)
// Added pin connections, if desired, from connection drawing
// #define TFT_SD_CS 5
// #define TFT_SD_MOSI 23
// #define TFT_SD_MISO 19
// #define TFT_SD_SCK 18
// #define T_CS 22
// ##################################################################################
// ##################################################################################
// #
// # End Internal Sketch User Setup Statements
// #
// ##################################################################################
// ##################################################################################
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
#include <User_Setup_Select.h> // User Setup File
#define TFT_GREY 0x5AEB
#define LOOP_PERIOD 35 // Display updates every 35 ms
float ltx = 0; // Saved x coord of bottom of needle
uint16_t osx = 120, osy = 120; // Saved x & y coords
uint32_t updateTime = 0; // time for next update
int old_analog = -999; // Value last displayed
int old_digital = -999; // Value last displayed
int value[6] = {0, 0, 0, 0, 0, 0};
int old_value[6] = { -1, -1, -1, -1, -1, -1};
int d = 0;
// ###############################################################
// #
// # Define LH and RH meter rectangle pixel value coordinates
// # as integer variables
// #
// ###############################################################
int MWX = 239; // Meter rectangle width
int MHY = 126; // Meter rectangle height
int RHM_x0 = 0;
int RHM_y0 = 0;
int RHM_xl = 140;
int RHM_yl = 20;
// ###############################################################
// #
// # Now let's try to move the meter over 30 pixels to the right
// #
// ###############################################################
// int x_del = 0; // define number of right shift x - pixels
int x_del = 20; // define number of right shift x - pixels
int y_del = 0; // define number of down shift y - pixels
int LHM_x0 = x_del; // define meter rectangle upper LH corner x - coordinant in pixels plus x_del
int LHM_y0 = y_del; // define meter rectangle upper LH corner y - coordinant in pixels plus x_del
int LHM_xL = LHM_x0 + MWX; // define meter rectangle far corner x coordinant pixel location
int LHM_yL = LHM_y0 + MHY; // define meter rectangle far corner y coordinant pixel location
// ###############################################################
// #
// # Define pointer pixel value coordinates as integer variables
// #
// ###############################################################
// ###############################################################
// #
// # The pointer pixel value coordinates below specify the
// # location of the pointer lower line point
// #
// # If we shift the meter rectangle by x_del and dy_del pixels
// # this set of coordinants will have to be shifted also
// #
// ###############################################################
int p_x0 = ( MWX / 2 + 1) + x_del;
int p_x0_del = 20;
int p_y0 = MHY + 14 + y_del;
int p_y0_del = 20;
void setup(void) {
tft.init();
tft.setRotation(3); // set rotation to place top of meter rectangle
// along the TFT display long edhe under the
// ESP32 Dev Mod USB jack
Serial.begin(115200); // Enable print out to Serial Monitor for debug
tft.fillScreen(TFT_BLACK);
analogMeter(); // Draw the big analog meter
byte d = 40;
updateTime = millis(); // Next update time
}
void loop() {
if (updateTime <= millis()) {
updateTime = millis() + LOOP_PERIOD;
d += 4; if (d >= 360) d = 0;
//value[0] = map(analogRead(A0), 0, 1023, 0, 100); // Test with value from Analogue 0
// Create a Sine wave for testing - these are y-coord values for the top end of the pointer line
value[0] = 50 + 50 * sin((d + 0) * 0.0174532925);
value[1] = 50 + 50 * sin((d + 60) * 0.0174532925);
value[2] = 50 + 50 * sin((d + 120) * 0.0174532925);
value[3] = 50 + 50 * sin((d + 180) * 0.0174532925);
value[4] = 50 + 50 * sin((d + 240) * 0.0174532925);
value[5] = 50 + 50 * sin((d + 300) * 0.0174532925);
//unsigned long t = millis();
// plotPointer();
plotNeedle(value[0], 0); // this value for plot needle is for needle begin and end
//Serial.println(millis()-t); // Print time taken for meter update
}
}
// #########################################################################
//
// Draw the analog meter on the screen
//
// #########################################################################
void analogMeter()
{
// Meter rectangle outline
// Left Meter
tft.fillRect(LHM_x0, LHM_y0, MWX, LHM_yL, TFT_YELLOW);
tft.fillRect(LHM_x0 + 5, LHM_y0 + 3, MWX - 9, LHM_yL - 7 , TFT_WHITE);
// Right Meter
// tft.fillRect(155, 0, 300, 126 + mydely, TFT_GREY);
// tft.fillRect(160, 3, 295, 119 + mydely, TFT_WHITE);
tft.setTextColor(TFT_BLACK); // Text color
// Draw ticks every 5 degrees from -50 to +50 degrees (100 deg. FSD swing)
for (int i = -50; i < 51; i += 5) {
// Long scale tick length
int tl = 15;
// Coodinates of tick to draw
float sx = cos((i - 90) * 0.0174532925);
float sy = sin((i - 90) * 0.0174532925);
uint16_t x0 = sx * (100 + tl) + p_x0; // p_x0 = 120
uint16_t y0 = sy * (100 + tl) + p_y0; // p_y0 = 140
uint16_t x1 = sx * 100 + p_x0; // p_x0 = 120
uint16_t y1 = sy * 100 + p_y0; // p_y0 = 140
// Coordinates of next tick for zone fill these are linked
// to variable specified locations, for p_x0 and p_y0
// including any specified x_del or y_del shift values
float sx2 = cos((i + 5 - 90) * 0.0174532925);
float sy2 = sin((i + 5 - 90) * 0.0174532925);
int x2 = sx2 * (100 + tl) + p_x0; // p_x0 = 120
int y2 = sy2 * (100 + tl) + p_y0; // p_y0 = 140
int x3 = sx2 * 100 + p_x0; // p_x0 = 120
int y3 = sy2 * 100 + p_y0; // p_y0 = 140
// Yellow zone limits
if (i >= -50 && i < 0) {
tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_YELLOW);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_YELLOW);
}
// Green zone limits
if (i >= 0 && i < 25) {
tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_GREEN);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_GREEN);
}
// Orange zone limits
if (i >= 25 && i < 50) {
tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_ORANGE);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_ORANGE);
}
// Short scale tick length
if (i % 25 != 0) tl = 8;
// Recalculate coords in case tick length changed
x0 = sx * (100 + tl) + p_x0; // p_x0 = 120
y0 = sy * (100 + tl) + p_y0; // p_y0 = 140
x1 = sx * 100 + p_x0; // p_x0 = 120
y1 = sy * 100 + p_y0; // p_y0 = 140
// Draw tick, either short or long
tft.drawLine(x0, y0, x1, y1, TFT_BLACK);
// Check if labels on the meter face arc should be drawn,
// with position tweaks
if (i % 25 == 0) {
// Calculate scale label positions
x0 = sx * (100 + tl + 10) + p_x0;
y0 = sy * (100 + tl + 10) + p_y0;
switch (i / 25) {
case -2: tft.drawCentreString("0", x0, y0 - 12, 2); break;
case -1: tft.drawCentreString("25", x0, y0 - 9, 2); break;
case 0: tft.drawCentreString("50", x0, y0 - 6, 2); break;
case 1: tft.drawCentreString("75", x0, y0 - 9, 2); break;
case 2: tft.drawCentreString("100", x0, y0 - 12, 2); break;
}
}
// Now draw the arc of the scale
sx = cos((i + 5 - 90) * 0.0174532925);
sy = sin((i + 5 - 90) * 0.0174532925);
x0 = sx * 100 + p_x0;
y0 = sy * 100 + p_y0;
// Draw scale arc, don't draw the last part
if (i < 50) tft.drawLine(x0, y0, x1, y1, TFT_BLACK);
}
// #############################################################
// #
// # Draw labels on the meter face
// #
// #############################################################
Serial.println("Meter Width and Height");
Serial.println(MWX);
Serial.println(MHY);
Serial.println(" ");
Serial.println("Offsets");
Serial.println(x_del);
Serial.println(y_del);
Serial.println(" ");
Serial.println("Near Corner");
Serial.println(LHM_x0);
Serial.println(LHM_y0);
Serial.println(" ");
Serial.println("Far Corner");
Serial.println(LHM_xL);
Serial.println(LHM_yL);
Serial.println(" ");
Serial.println("Bezel");
Serial.println(MWX-9);
Serial.println(MHY-7);
tft.drawString("%RH", 5 + x_del + MWX -1 - 40, MHY - 7 - 20, 2); // %RH at bottom right
tft.drawCentreString("%RH", p_x0 + x_del, 70 + y_del, 4); // Comment out to avoid font 4
Serial.println(" ");
Serial.println("Bezel Far RH Corner");
Serial.println(LHM_xL);
Serial.println(" ");
Serial.println("Meter Far RH Corner #2");
Serial.println(LHM_xL);
tft.drawRect(LHM_x0 + 0, LHM_y0 + 0, LHM_x0 + MWX - x_del, LHM_y0 + MHY, TFT_RED); // Draw bezel line Bezel - OK
plotNeedle(0, 0); // Put meter needle at 0
}
// #########################################################################
//
// Update needle position
//
// This function is blocking while needle moves, time depends on ms_delay
// 10ms minimises needle flicker if text is drawn within needle sweep area
//
// Smaller values OK if text not in sweep area, zero for instant movement
// but does not look realistic...
// (note: 100 increments for full scale deflection)
//
// #########################################################################
//
void plotNeedle(int value, byte ms_delay)
{
tft.setTextColor(TFT_WHITE, TFT_RED);
char buf[8]; dtostrf(value, 4, 0, buf);
tft.drawRightString(buf, LHM_x0 + 40, MHY - 27, 2); //
if (value < -10) value = -10; // Limit value to emulate needle end stops
if (value > 110) value = 110;
// Move the needle until new value reached
while (!(value == old_analog)) {
if (old_analog < value) old_analog++;
else old_analog--;
if (ms_delay == 0) old_analog = value; //Update immediately if delay is 0
float sdeg = map(old_analog, -10, 110, -150, -30); // Map value to angle
// Calculate tip of needle cords
float sx = cos(sdeg * 0.0174532925);
float sy = sin(sdeg * 0.0174532925);
// Calculate x delta of needle start (does not start at pivot point)
float tx = tan((sdeg + 90) * 0.0174532925);
// Erase old needle image
tft.drawLine(p_x0 + p_x0_del * ltx - 1, p_y0 - p_y0_del, osx - 1, osy, TFT_WHITE);
tft.drawLine(p_x0 + p_x0_del * ltx, p_y0 - p_y0_del, osx, osy, TFT_WHITE);
tft.drawLine(p_x0 + p_x0_del * ltx + 1, p_y0 - p_y0_del, osx + 1, osy, TFT_WHITE);
// Re-plot text under needle
tft.setTextColor(TFT_BLACK);
tft.drawCentreString("%RH", p_x0, 70, 4); // Comment out to avoid font 4
// Store new needle end coords for next erase
ltx = tx;
osx = sx * 98 + p_x0;
osy = sy * 98 + p_y0;
// Draw the needle in the new postion, magenta makes needle a bit bolder
// draw 3 lines to thicken needle
tft.drawLine(p_x0 + p_x0_del * ltx - 1, p_y0 - p_y0_del, osx - 1, osy, TFT_RED);
tft.drawLine(p_x0 + p_x0_del * ltx, p_y0 - p_y0_del, osx, osy, TFT_MAGENTA);
tft.drawLine(p_x0 + p_x0_del * ltx + 1, p_y0 - p_y0_del, osx + 1, osy, TFT_RED);
// Slow needle down slightly as it approaches new postion
if (abs(old_analog - value) < 10) ms_delay += ms_delay / 5;
// Wait before next update
delay(ms_delay);
}
}
Have you looked at the examples that come with the library ?
Yes I have looked at the TFT_eSPI Font Library and found a solution to my problem. See attached photo of screen showing the font added in the lower left hand corner:
I attach the sketch with corrections added for reference:
// ########################################################################
// #
// # animated analogue meters sketch using a ILI9341 TFT LCD screen
// #
// # It appears that this sketch uses the 30-Pin DOIT ESP32 Module
// #
// # This sketch worked on 2-1-22
// #
// # Originally downloaded from ElectroPeak on 12-28-21
// #
// # This sketch had Floris's User Setup File
// #
// # I added my User Setup File on 2-1-22
// #
// # Needs Font 2 (also Font 4 if using large scale label)
// #
// # Comm Port: COM3
// #
// ########################################################################
// ########################################################################
// ########################################################################
// #
// # Begin Internal Sketch User Setup Statements
// #
// ########################################################################
// ########################################################################
#define TFT_RGB_ORDER TFT_BGR // Colour order Blue-Green-Red
// ########################################################################
//
// Section 2. Define the pins that are used to interface with the display
//
// #######################################################################
// #define TFT_BL 32 // LED back-light control pin
// #define TFT_BACKLIGHT_ON HIGH // Level to turn ON back-light (HIGH or LOW)
// Added pin connections, if desired, from connection drawing
// #define TFT_SD_CS 5
// #define TFT_SD_MOSI 23
// #define TFT_SD_MISO 19
// #define TFT_SD_SCK 18
// #define T_CS 22
// ##################################################################################
// ##################################################################################
// #
// # End Internal Sketch User Setup Statements
// #
// ##################################################################################
// ##################################################################################
#include <TFT_eSPI.h> // Hardware-specific library
#include <SPI.h>
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
#include <User_Setup_Select.h> // User Setup File
//######################################################################################################
//######################################################################################################
// Specify desired font from TFT_eSPI GFXFF Font Library
// Stock font and GFXFF reference handle
#define GFXFF 1
#define FF18 &FreeSans12pt7b
// Custom are fonts added to library "TFT_eSPI\Fonts\Custom" folder
// a #include must also be added to the "User_Custom_Fonts.h" file
// in the "TFT_eSPI\User_Setups" folder. See example entries.
#define CF_OL24 &Orbitron_Light_24
#define CF_OL32 &Orbitron_Light_32
#define CF_RT24 &Roboto_Thin_24
#define CF_S24 &Satisfy_24
#define CF_Y32 &Yellowtail_32
//######################################################################################################
//######################################################################################################
#define TFT_GREY 0x5AEB
#define LOOP_PERIOD 35 // Display updates every 35 ms
float ltx = 0; // Saved x coord of bottom of needle
uint16_t osx = 120, osy = 120; // Saved x & y coords
uint32_t updateTime = 0; // time for next update
int old_analog = -999; // Value last displayed
int old_digital = -999; // Value last displayed
int value[6] = {0, 0, 0, 0, 0, 0};
int old_value[6] = { -1, -1, -1, -1, -1, -1};
int d = 0;
// ###############################################################
// #
// # Define LH and RH meter rectangle pixel value coordinates
// # as integer variables
// #
// ###############################################################
int MWX = 239; // Meter rectangle width
int MHY = 126; // Meter rectangle height
int RHM_x0 = 0;
int RHM_y0 = 0;
int RHM_xl = 140;
int RHM_yl = 20;
// ###############################################################
// #
// # Now let's try to move the meter over 30 pixels to the right
// #
// ###############################################################
// int x_del = 0; // define number of right shift x - pixels
int x_del = 20; // define number of right shift x - pixels
int y_del = 0; // define number of down shift y - pixels
int LHM_x0 = x_del; // define meter rectangle upper LH corner x - coordinant in pixels plus x_del
int LHM_y0 = y_del; // define meter rectangle upper LH corner y - coordinant in pixels plus x_del
int LHM_xL = LHM_x0 + MWX; // define meter rectangle far corner x coordinant pixel location
int LHM_yL = LHM_y0 + MHY; // define meter rectangle far corner y coordinant pixel location
// ###############################################################
// #
// # Define pointer pixel value coordinates as integer variables
// #
// ###############################################################
// ###############################################################
// #
// # The pointer pixel value coordinates below specify the
// # location of the pointer lower line point
// #
// # If we shift the meter rectangle by x_del and dy_del pixels
// # this set of coordinants will have to be shifted also
// #
// ###############################################################
int p_x0 = ( MWX / 2 + 1) + x_del;
int p_x0_del = 20;
int p_y0 = MHY + 14 + y_del;
int p_y0_del = 20;
void setup(void) {
tft.init();
tft.setRotation(3); // set rotation to place top of meter rectangle
// along the TFT display long edhe under the
// ESP32 Dev Mod USB jack
Serial.begin(115200); // Enable print out to Serial Monitor for debug
tft.fillScreen(TFT_BLACK);
analogMeter(); // Draw the big analog meter
byte d = 40;
updateTime = millis(); // Next update time
}
void loop() {
if (updateTime <= millis()) {
updateTime = millis() + LOOP_PERIOD;
d += 4; if (d >= 360) d = 0;
//value[0] = map(analogRead(A0), 0, 1023, 0, 100); // Test with value from Analogue 0
// Create a Sine wave for testing - these are y-coord values for the top end of the pointer line
value[0] = 50 + 50 * sin((d + 0) * 0.0174532925);
value[1] = 50 + 50 * sin((d + 60) * 0.0174532925);
value[2] = 50 + 50 * sin((d + 120) * 0.0174532925);
value[3] = 50 + 50 * sin((d + 180) * 0.0174532925);
value[4] = 50 + 50 * sin((d + 240) * 0.0174532925);
value[5] = 50 + 50 * sin((d + 300) * 0.0174532925);
//unsigned long t = millis();
// plotPointer();
plotNeedle(value[0], 0); // this value for plot needle is for needle begin and end
//Serial.println(millis()-t); // Print time taken for meter update
}
}
// #########################################################################
//
// Draw the analog meter on the screen
//
// #########################################################################
void analogMeter()
{
// Meter rectangle outline
// Left Meter
tft.fillRect(LHM_x0, LHM_y0, MWX, LHM_yL, TFT_YELLOW);
tft.fillRect(LHM_x0 + 5, LHM_y0 + 3, MWX - 9, LHM_yL - 7 , TFT_WHITE);
// Right Meter
// tft.fillRect(155, 0, 300, 126 + mydely, TFT_GREY);
// tft.fillRect(160, 3, 295, 119 + mydely, TFT_WHITE);
tft.setTextColor(TFT_BLACK); // Text color
// Draw ticks every 5 degrees from -50 to +50 degrees (100 deg. FSD swing)
for (int i = -50; i < 51; i += 5) {
// Long scale tick length
int tl = 15;
// Coodinates of tick to draw
float sx = cos((i - 90) * 0.0174532925);
float sy = sin((i - 90) * 0.0174532925);
uint16_t x0 = sx * (100 + tl) + p_x0; // p_x0 = 120
uint16_t y0 = sy * (100 + tl) + p_y0; // p_y0 = 140
uint16_t x1 = sx * 100 + p_x0; // p_x0 = 120
uint16_t y1 = sy * 100 + p_y0; // p_y0 = 140
// Coordinates of next tick for zone fill these are linked
// to variable specified locations, for p_x0 and p_y0
// including any specified x_del or y_del shift values
float sx2 = cos((i + 5 - 90) * 0.0174532925);
float sy2 = sin((i + 5 - 90) * 0.0174532925);
int x2 = sx2 * (100 + tl) + p_x0; // p_x0 = 120
int y2 = sy2 * (100 + tl) + p_y0; // p_y0 = 140
int x3 = sx2 * 100 + p_x0; // p_x0 = 120
int y3 = sy2 * 100 + p_y0; // p_y0 = 140
// Yellow zone limits
if (i >= -50 && i < 0) {
tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_YELLOW);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_YELLOW);
}
// Green zone limits
if (i >= 0 && i < 25) {
tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_GREEN);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_GREEN);
}
// Orange zone limits
if (i >= 25 && i < 50) {
tft.fillTriangle(x0, y0, x1, y1, x2, y2, TFT_ORANGE);
tft.fillTriangle(x1, y1, x2, y2, x3, y3, TFT_ORANGE);
}
// Short scale tick length
if (i % 25 != 0) tl = 8;
// Recalculate coords in case tick length changed
x0 = sx * (100 + tl) + p_x0; // p_x0 = 120
y0 = sy * (100 + tl) + p_y0; // p_y0 = 140
x1 = sx * 100 + p_x0; // p_x0 = 120
y1 = sy * 100 + p_y0; // p_y0 = 140
// Draw tick, either short or long
tft.drawLine(x0, y0, x1, y1, TFT_BLACK);
// Check if labels on the meter face arc should be drawn,
// with position tweaks
if (i % 25 == 0) {
// Calculate scale label positions
x0 = sx * (100 + tl + 10) + p_x0;
y0 = sy * (100 + tl + 10) + p_y0;
switch (i / 25) {
case -2: tft.drawCentreString("0", x0, y0 - 12, 2); break;
case -1: tft.drawCentreString("25", x0, y0 - 9, 2); break;
case 0: tft.drawCentreString("50", x0, y0 - 6, 2); break;
case 1: tft.drawCentreString("75", x0, y0 - 9, 2); break;
case 2: tft.drawCentreString("100", x0, y0 - 12, 2); break;
}
}
// Now draw the arc of the scale
sx = cos((i + 5 - 90) * 0.0174532925);
sy = sin((i + 5 - 90) * 0.0174532925);
x0 = sx * 100 + p_x0;
y0 = sy * 100 + p_y0;
// Draw scale arc, don't draw the last part
if (i < 50) tft.drawLine(x0, y0, x1, y1, TFT_BLACK);
}
// #############################################################
// #
// # Draw labels on the meter face
// #
// #############################################################
tft.drawString("%RH", 5 + x_del + MWX -1 - 40, MHY - 7 - 20, 2); // %RH at bottom right
//tft.drawRoundRect(5 + x_del + MWX -1 - 40, MHY - 7 - 20)
tft.drawCentreString("%RH", p_x0 + x_del, 70 + y_del, 4); // Comment out to avoid font 4
tft.drawRect(LHM_x0 + 0, LHM_y0 + 0, LHM_x0 + MWX - x_del, LHM_y0 + MHY, TFT_RED); // Draw bezel line Bezel - OK
plotNeedle(0, 0); // Put meter needle at 0
}
// #########################################################################
//
// Update needle position
//
// This function is blocking while needle moves, time depends on ms_delay
// 10ms minimises needle flicker if text is drawn within needle sweep area
//
// Smaller values OK if text not in sweep area, zero for instant movement
// but does not look realistic...
// (note: 100 increments for full scale deflection)
//
// #########################################################################
//
void plotNeedle(int value, byte ms_delay)
{
// #######################################################################
// #
// # Font selection from TFT_eSPI GFXFF Font Library
// # FF18 - FreeSans12pt7b
// #
// #######################################################################
tft.setTextColor(TFT_BLACK, TFT_WHITE);
tft.setFreeFont(FF18); // Select the font
char buf[8]; dtostrf(value, 4, 0, buf);
tft.drawRightString(buf, LHM_x0 + 40, MHY - 27, GFXFF); //
if (value < -10) value = -10; // Limit value to emulate needle end stops
if (value > 110) value = 110;
// Move the needle until new value reached
while (!(value == old_analog)) {
if (old_analog < value) old_analog++;
else old_analog--;
if (ms_delay == 0) old_analog = value; //Update immediately if delay is 0
float sdeg = map(old_analog, -10, 110, -150, -30); // Map value to angle
// Calculate tip of needle cords
float sx = cos(sdeg * 0.0174532925);
float sy = sin(sdeg * 0.0174532925);
// Calculate x delta of needle start (does not start at pivot point)
float tx = tan((sdeg + 90) * 0.0174532925);
// Erase old needle image
tft.drawLine(p_x0 + p_x0_del * ltx - 1, p_y0 - p_y0_del, osx - 1, osy, TFT_WHITE);
tft.drawLine(p_x0 + p_x0_del * ltx, p_y0 - p_y0_del, osx, osy, TFT_WHITE);
tft.drawLine(p_x0 + p_x0_del * ltx + 1, p_y0 - p_y0_del, osx + 1, osy, TFT_WHITE);
// Re-plot text under needle
int xrr = (MWX/2) + 1 - 20;
int yrr = MHY - 60;
tft.fillRoundRect(xrr, yrr, 95, 50, 20, TFT_WHITE);
tft.setTextColor(TFT_BLACK);
tft.drawCentreString("%RH", p_x0, 70, 4); // Comment out to avoid font 4
// Store new needle end coords for next erase
ltx = tx;
osx = sx * 98 + p_x0;
osy = sy * 98 + p_y0;
// Draw the needle in the new postion, magenta makes needle a bit bolder
// draw 3 lines to thicken needle
tft.drawLine(p_x0 + p_x0_del * ltx - 1, p_y0 - p_y0_del, osx - 1, osy, TFT_RED);
tft.drawLine(p_x0 + p_x0_del * ltx, p_y0 - p_y0_del, osx, osy, TFT_MAGENTA);
tft.drawLine(p_x0 + p_x0_del * ltx + 1, p_y0 - p_y0_del, osx + 1, osy, TFT_RED);
// Slow needle down slightly as it approaches new postion
if (abs(old_analog - value) < 10) ms_delay += ms_delay / 5;
// Wait before next update
delay(ms_delay);
}
}
I have slowly been teaching myself how to use and program the ESP32 MCU. It took me some time to learn how to place the analog meter face onto the MSP2807 TFT display at a specified location. Now I have to learn about the fonts that I want to use on the display. I have started this process and look to master it in the coming weeks.
Thank you UKHeliBob
This looks wrong to me
Did you remove the number that was there before printing a different one or is 36 meant to look like that ?
Yes I removed the earlier number. I photographed the screen while the meter pointer was swinging. In this photo, the number printed out was changing to a new value.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.