#include <Adafruit_ILI9341.h>
#include <ArduinoJson.h>
#define TFT_CS 9
#define TFT_DC 10
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define WIDTH 320
#define HEIGHT 240
void JSONGUI(String JSON_TEXT) {
// Parse the incoming JSON data
DynamicJsonDocument doc(1024);
DeserializationError error = deserializeJson(doc, JSON_TEXT);
// Check for parsing errors
if (error) {
Serial.print("deserializeJson() failed: ");
Serial.println(error.c_str());
return;
}
// Function to convert hexadecimal color string to RGB color value
auto hexToRgb = [](String hexColor) {
// Check if the hexColor starts with '#', if so, remove it
if (hexColor.startsWith("#")) {
hexColor = hexColor.substring(1);
}
// Convert the hexColor string to a long integer
long rgb = strtol(hexColor.c_str(), NULL, 16);
// Extract RGB components (red, green, blue)
uint8_t red = (rgb >> 16) & 0xFF;
uint8_t green = (rgb >> 8) & 0xFF;
uint8_t blue = rgb & 0xFF;
return tft.color565(red, green, blue);
};
// Iterate over each key-value pair in the JSON object
for (JsonPair pair : doc.as<JsonObject>()) {
// Get the name of the field (main field)
String elementType = String(pair.key().c_str());
// Draw GUI element based on type
if (elementType == "text") {
JsonObject element = pair.value();
int x = element["x"];
int y = element["y"];
String text = element["text"];
String color = element.containsKey("rgb") ? element["rgb"].as<String>() : "FFFFFF";
int scale = element.containsKey("scale") ? element["scale"] : 1;
tft.setTextColor(hexToRgb(color));
tft.setTextSize(scale);
tft.setCursor(x, y);
tft.print(text);
} else if (elementType == "fill") {
tft.fillScreen(hexToRgb(pair.value().as<String>()));
} else if (elementType == "line") {
JsonObject element = pair.value();
int startX = element["startX"];
int startY = element["startY"];
int endX = element["endX"];
int endY = element["endY"];
String color = element.containsKey("rgb") ? element["rgb"].as<String>() : "FFFFFF";
tft.drawLine(startX, startY, endX, endY, hexToRgb(color));
} else if (elementType == "drawRect") {
JsonObject element = pair.value();
int x = element["x"];
int y = element["y"];
int width = element["width"];
int height = element["height"];
String color = element.containsKey("rgb") ? element["rgb"].as<String>() : "FFFFFF";
tft.drawRect(x, y, width, height, hexToRgb(color));
} else if (elementType == "fillRect") {
JsonObject element = pair.value();
int x = element["x"];
int y = element["y"];
int width = element["width"];
int height = element["height"];
String color = element.containsKey("rgb") ? element["rgb"].as<String>() : "FFFFFF";
tft.fillRect(x, y, width, height, hexToRgb(color));
} else if (elementType == "drawCircle") {
JsonObject element = pair.value();
int x = element["x"];
int y = element["y"];
int radius = element["r"];
String color = element.containsKey("rgb") ? element["rgb"].as<String>() : "FFFFFF";
tft.drawCircle(x, y, radius, hexToRgb(color));
} else if (elementType == "fillCircle") {
JsonObject element = pair.value();
int x = element["x"];
int y = element["y"];
int radius = element["r"];
String color = element.containsKey("rgb") ? element["rgb"].as<String>() : "FFFFFF";
tft.fillCircle(x, y, radius, hexToRgb(color));
}
// Add more conditions for other GUI element types as needed
}
}
void setup(void) {
Serial.begin(115200);
tft.begin();
tft.fillScreen(ILI9341_BLACK); // Clear screen
tft.setTextColor(ILI9341_WHITE); // Set text color
tft.setRotation(1);
JSONGUI("{\"fill\":\"FFFFFF\",\"fillCircle\":{\"x\":20,\"y\":20,\"r\":55,\"rgb\":\"0000FF\"}}");
}
void loop() {
}