TFT Display: conversion of string for printf()

Hi

I am trying to display my string variable (train_id) on a ST7735 TFT Module. I learnt that I need to convert the string to char array first as string is not supported by the TFT Library. I used the code provided in this forum but how can I then display the result on the TFT display using the printf() Function?

Thank you for your help.

Remo

String str = train_id;

// Length (with one extra character for the null terminator)
int str_len = str.length() + 1;

// Prepare the character array (the buffer)
char char_array[str_len];

// Copy it over
(str.toCharArray(char_array, str_len));

tft.printf(? ? ? ?);

Please post your entire sketch using code tags and described here:

Read this before posting a programming question ...

I learnt that I need to convert the string to char array first as string is not supported by the TFT Library.

Why is train_id a String in the first place rather than a zero terminated array of chars ?

answer is

tft.printf(char_array);

what is train_id's type? if it's a number, then

tft.printf(train_id);

might just work as the ST7735 library inherits from Adafruit_GFX which supports Print

The chain of inheritance is a little long:

class Adafruit_ST7735 : public Adafruit_ST77xx {
class Adafruit_ST77xx : public Adafruit_SPITFT {
class Adafruit_SPITFT : public Adafruit_GFX
class Adafruit_GFX : public Print {

So, yes, Adafruit_ST7735 ultimately inherits from Print. Print knows how to Print an object of the String class. Therefore, I see no basis for OP's original statement:

patschifig123:
I learnt that I need to convert the string to char array first as string is not supported by the TFT Library.

exactly :slight_smile:

Is OP using the Adafruit library?

fair question. Maybe not

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.