#include <Adafruit_ILI9341.h>
#define TFT_CS 10
#define TFT_RST 9
#define TFT_DC 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
#define _id ":\"value\";"
#define _img ":[w,h];"
#define _imgbeg_2 ":{(r,g,b)};"
String functionID[] = {"_id","_img","_imgbeg"};
String img_beg = "_id:\"12\";_img:[7,7];_imgbeg:{(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255),(255,255,255)};";
String subImg_beg1;
String subImg_beg2;
int fromIndex_begin = 0;
int carrier = 0;
int poscarryon[2];
int info[3];
int* img = (int*)malloc(0 * sizeof(int));
int* imgR = (int*)malloc(0 * sizeof(int));
int* imgG = (int*)malloc(0 * sizeof(int));
int* imgB = (int*)malloc(0 * sizeof(int));
bool isInteger(String str) {
int value = str.toInt();
String strBack = String(value);
return strBack.equals(str);
}
void setup() {
tft.begin();
tft.fillScreen(ILI9341_BLACK);
fromIndex_begin = img_beg.indexOf("_",fromIndex_begin);
int fromIndex_end = img_beg.indexOf(";",fromIndex_begin);
subImg_beg1 = img_beg.substring(fromIndex_begin,fromIndex_end+1);
fromIndex_end = img_beg.indexOf(":",fromIndex_begin);
subImg_beg2 = img_beg.substring(fromIndex_begin,fromIndex_end);
fromIndex_begin = img_beg.indexOf(";",fromIndex_begin);
Serial.begin(9600);
}
void loop() {
if(subImg_beg2 == functionID[0]){
String macro = _id;
int index = macro.indexOf("value");
int index_end = macro.indexOf("\"",index);
macro.remove(index, index_end-2);
String subMacro1 = macro;
macro.remove(0,macro.length()-2);
String subMacro2 = macro;
subMacro1.remove(index, subMacro1.length());
int ind = subImg_beg1.indexOf(subMacro1);
int ind_end = subImg_beg1.indexOf(subMacro2);
subImg_beg2 = subImg_beg1.substring(ind+2,ind_end);
if(isInteger(subImg_beg2)){
carrier = subImg_beg2.toInt();
info[0] = carrier;
}
else{
Serial.println("Id invalida, utilize int");
return;
}
fromIndex_begin = img_beg.indexOf("_",fromIndex_begin);
int fromIndex_end = img_beg.indexOf(";",fromIndex_begin);
subImg_beg1 = img_beg.substring(fromIndex_begin,fromIndex_end+1);
fromIndex_end = img_beg.indexOf(":",fromIndex_begin);
subImg_beg2 = img_beg.substring(fromIndex_begin,fromIndex_end);
fromIndex_begin = img_beg.indexOf(";",fromIndex_begin);
}
else if(subImg_beg2 == functionID[1]){
String macro = _img;
int index = macro.indexOf("w");
int index_end = macro.indexOf("]");
macro.remove(index, index_end-2);
String subMacro1 = macro;
macro.remove(0,macro.length()-2);
String subMacro2 = macro;
subMacro1.remove(index, subMacro1.length());
int ind = subImg_beg1.indexOf(subMacro1);
int ind_end = subImg_beg1.indexOf(subMacro2);
subImg_beg2 = subImg_beg1.substring(ind+2,ind_end);
index = subImg_beg2.indexOf(",");
subImg_beg1 = subImg_beg2.substring(0,index);
subImg_beg2.remove(0,index+1);
carrier = subImg_beg1.toInt();
info[1] = carrier;
carrier = subImg_beg2.toInt();
info[2] = carrier;
fromIndex_begin = img_beg.indexOf("_",fromIndex_begin);
int fromIndex_end = img_beg.indexOf(";",fromIndex_begin);
subImg_beg1 = img_beg.substring(fromIndex_begin,fromIndex_end+1);
fromIndex_end = img_beg.indexOf(":",fromIndex_begin);
subImg_beg2 = img_beg.substring(fromIndex_begin,fromIndex_end);
fromIndex_begin = img_beg.indexOf(";",fromIndex_begin);
}
else if(subImg_beg2 == functionID[2]){
String macro = _imgbeg_2;
int index = macro.indexOf("{(");
int index_end = macro.indexOf(")}");
macro.remove(index, index_end-2);
String subMacro1 = macro;
macro.remove(0,macro.length()-2);
String subMacro2 = macro;
subMacro1.remove(index, subMacro1.length());
int ind = subImg_beg1.indexOf(subMacro1);
int ind_end = subImg_beg1.indexOf(subMacro2);
subImg_beg2 = subImg_beg1.substring(ind+2,ind_end);
index = 0;
index_end = 0;
free(img);
img = (int*)malloc((info[1]*info[2]) * sizeof(int));
int count = 0;
for(int i = 0; i<(info[1]*info[2]); i++){
index = subImg_beg2.indexOf("(",index_end);
index_end = subImg_beg2.indexOf(")",index);
img[i] = index;
count += 1;
}
free(imgR);
imgR = (int*)malloc((info[1]*info[2]) * sizeof(int));
free(imgG);
imgG = (int*)malloc((info[1]*info[2]) * sizeof(int));
free(imgB);
imgB = (int*)malloc((info[1]*info[2]) * sizeof(int));
for (int i = 0; i < (info[1] * info[2]); i++) {
int startR = img[i] + 1;
int endR = subImg_beg2.indexOf(",", img[i]);
String subImg_beg3 = subImg_beg2.substring(startR, endR);
imgR[i] = subImg_beg3.toInt();
index_end = endR;
int startG = index_end + 1;
int endG = subImg_beg2.indexOf(",", startG);
subImg_beg3 = subImg_beg2.substring(startG, endG);
imgG[i] = subImg_beg3.toInt();
index_end = endG;
int startB = index_end + 1;
int endB = subImg_beg2.indexOf(",", startB);
subImg_beg3 = subImg_beg2.substring(startB, endB);
imgB[i] = subImg_beg3.toInt();
}
}
drawImage(imgR,imgG,imgB,info[1],info[2]);
}
void drawImage(int* imgr, int* imgg, int* imgb, int width, int height) {
for (int i = 0; i < width * height; i++) {
uint16_t color = tft.color565(imgr[i], imgg[i], imgb[i]);
int x = i % width;
int y = i / width;
tft.drawPixel(x, y, color);
poscarryon[0] = x;
poscarryon[0] = y;
}
}