Take color for TFT from JSON?

Is it possible somehow to take color1=ST7735_GREEN from JSON and insert it to tft.setTextColor(color1)???

Ive tried:

uint16_t color_1 = doc["color_1"];
tft.setTextColor(color_1);

But result is using of previous color.

Not like that. color1=ST7735_GREEN is a text in json, tft.setTextColor() more than likely expects a numeric value (or RGB array). So you must find a way to translate.

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problems with (nor for advice on) your project :wink: See About the Installation & Troubleshooting category.

The easiest method would be to just use the numeric value of the ST7735_GREEN constant in your JSON file. Glancing at the ST7735.h file

// Color definitions
#define	ST7735_BLACK   0x0000
#define	ST7735_BLUE    0x001F
#define	ST7735_RED     0xF800
#define	ST7735_GREEN   0x07E0
#define ST7735_CYAN    0x07FF
#define ST7735_MAGENTA 0xF81F
#define ST7735_YELLOW  0xFFE0  
#define ST7735_WHITE   0xFFFF

so you could make your JSON be color_1 = 2016 and read the integer.

If you use the actual hex value, you will have to read that as a string and convert it to an number using strol().

If you want to use text such as "GREEN" or "RED" ..., then you need to read in the text as a string and set up a serial of if/then/else statements to do the actual assignment

uint16_t color = ST7735_BLACK;
char * colorString = doc["color_1"];
if ( strcmp(colorString, "GREEN") == 0 ) color = ST7735_GREEN;
elseif ( strcmp(colorString, "RED") == 0 ) color = ST7735_RED;
elseif ( ... )
1 Like

Thx, its working!

int set_color(const char * colorString){ 
  uint16_t color = ST7735_BLACK;
  if ( strcmp(colorString , "ST7735_GREEN") == 0 ) return color = ST7735_GREEN;
  else if ( strcmp(colorString , "ST7735_RED") == 0 ) return color = ST7735_RED;
  else if ( strcmp(colorString , "ST7735_BLUE") == 0 ) return color = ST7735_BLUE;
  else if ( strcmp(colorString , "ST7735_BLACK") == 0 ) return color = ST7735_BLACK;
  else if ( strcmp(colorString , "ST7735_CYAN") == 0 ) return color = ST7735_CYAN;
  else if ( strcmp(colorString , "ST7735_MAGENTA") == 0 ) return color = ST7735_MAGENTA;
  else if ( strcmp(colorString , "ST7735_YELLOW") == 0 ) return color = ST7735_YELLOW;
  else if ( strcmp(colorString , "ST7735_WHITE") == 0 ) return color = ST7735_WHITE;
  else if ( strcmp(colorString , "ST7735_ORANGE") == 0 ) return color = ST7735_ORANGE;
}

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