KrisKasprzak / FlickerFreePrint

Library to intelligently repaint old text before drawing new--eliminates drawing drafillRect()
17 stars 6 forks source link

Character after dynamic value #8

Closed maharzan closed 6 months ago

maharzan commented 6 months ago

I am trying to use this for my small arduino project with TFT 9341 display. It works fantastically. My issue is I am using this to show the State of Charge of a battery so I need the value + % sign. So, I am trying to do this

Data1.print(stateOfCharge, 0);
Data2.setTextColor(C_WHITE, C_BLACK);
Data2.print("%");

It seems to work fine until the digits change.. like from 99 to 10 it works great. But as soon as it hits 9, the extra % from the previous render is not erased so I see like 9%%. Same thing when it goes from 100% to 99%%. If I start from 100, when it goes to 9 it becomes 9%%%.

I tried to check the values and add spaces but it doesn't seem to work.

Is there any way to use flickerfree to cleanly transition these extra characters after the value?

Thanks.

KrisKasprzak commented 6 months ago

I'll test this case and see what the issue is. to be clear you are trying something this correct?

Data1.print(stateOfCharge, 0); Data2.setTextColor(C_WHITE, C_BLACK); Data2.print("%"); ... Data2.print("99%"); //and you get 99% ... Data2.print("9%"); //and you get 9%%

very odd, lib should not leave trailing stuff, let me take a look.

maharzan commented 6 months ago

I am only trying to print a variable value along with a fixed character, it could be %, V, A, C, W, Ah, as I need them. I am not sure how to concat variable value with a constant in this. So, I am printing the value first and then the constant. maybe something like this would be a good example.

Data1.setTextColor(C_WHITE, C_BLACK);
Data1.print(i);
Data2.setTextColor(C_WHITE, C_BLACK);
Data2.print("%");
i++;
delay(500);

I want to print a simple 1%, 10%, 100% without any artifacts (clear previous values/characters).

Here is what I am getting IMG_20240229_101515

This is the full program I am using

#include "SPI.h"
#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include <FlickerFreePrint.h>
#include <Fonts/FreeSansBold12pt7b.h>

#define TFT_CS        10
#define TFT_DC        9
#define TFT_RST       8

// setup some colors
#define  C_BLACK      0x0000
#define C_WHITE       0xFFFF

// create the display object
Adafruit_ILI9341 Display(TFT_CS, TFT_DC, TFT_RST);

// create a flicker free pnject for each data to be printed with the flicker free option
// the library used template scheme so you need to pass the data type in <>
FlickerFreePrint<Adafruit_ILI9341> Data1(&Display, C_WHITE, C_BLACK);
FlickerFreePrint<Adafruit_ILI9341> Data2(&Display, C_WHITE, C_BLACK);
FlickerFreePrint<Adafruit_ILI9341> Data3(&Display, C_WHITE, C_BLACK);

// setup some variables
int i;

void setup() {

  Serial.begin(9600);

  pinMode(TFT_CS,   OUTPUT);
  pinMode(TFT_DC,   OUTPUT);

  Display.begin();
  Display.setRotation(1);
  Display.fillScreen(C_BLACK);

}

void loop() {

  i++;

  Display.setFont(&FreeSansBold12pt7b);
  Display.setCursor(10, 30);
  Data1.setTextColor(C_WHITE, C_BLACK);
  Data1.print(i);
  Data2.print("%");

  delay(100);

}
maharzan commented 6 months ago

It seems that the only solution to the TFT displays for now as I have seen in numerous examples is to render the % sign far away from the varying text. It does look very odd when there is a single digit or values vary a lot but maybe thats why TFT displays are cheap. :)

void loop() {

  i++;

  Display.setFont(&FreeSansBold12pt7b);

  Display.setCursor(10, 30);
  Data1.setTextColor(C_WHITE, C_BLACK);
  Data1.print(i);

  Display.setCursor(53, 30);
  Data2.setTextColor(C_WHITE, C_BLACK);
  Data2.print("%");

  delay(200);

}
KrisKasprzak commented 6 months ago

Oh, now I see what you’re trying to do. You’ll definitely want to print everything at the same time and in the same flicker, free print object. Sprintf is the way to go. I’ll work up an example.

maharzan commented 6 months ago

Ah I got the hang of it. Thanks for the heads up. This seems to work.

sprintf(buffer, "%d%s", soc, "%");
Data1.print(buffer);
KrisKasprzak commented 6 months ago

That is what I would have suggested. Since all is working, closing.

maharzan commented 6 months ago

Thanks. This is wht I have come up with, nicely rendering... IMG_20240229_203213

BJPatriotRacing commented 6 months ago

Nice work!

KrisKasprzak commented 6 months ago

Yeah, the display looks great