hello, this might be a bit complicated, bear with me.
i have been building an "emulator" that takes arduino code inside of a string and runs it on the arduino board. You could use this to send code to the arduino board over serial monitor and it would run, or store lots of arduino sketches inside an sd card and have the board run each one!
pretty cool? as you might imagine, this emulator uses so much space, and i havent even gotten half the arduino language built in yet, just for loop functions, if statement, variable creation/manipulation and String class functions. I have been running the emulator on my esp32 board(its a small wifi board with heaps of space) but for certain reasons i need to now run it on an arduino uno. the sketch is 2x the storage space of the uno, is there even the slightest possibility of reducing the code by half? code below. Try it out, put one of the example codes inside the String code.
//String code = "String mystr = \"hello world\"; if(mystr.charAt(1) == \"e\"){ if(mystr.charAt(2) == \"l\"){for(int i = 0;i < 10;i = i+1){Serial.prntln(\"charat 2 is e\");}}}";
//PARAMETERS
//String code = "String mystr = \"hello\"; int x = 5;";
//String code = "String mystr = \"hello\"; int start = 0; int end = start+4; if(mystr.substring(start,end) == \"hell\"){ }";
//String code = "int x = 0; int y = 0; void setup{ tft.setTextFont(2); tft.setTextColor(0xffff,0x049f);} void loop{ tft.getTouch(x,y); tft.setCursor(30,90); tft.fillRect(30,90,40,20,0x0000); tft.print(x); tft.setCursor(30,50); tft.fillRect(30,50,40,20,0x0000); tft.print(y)}";
String code = "void loop() {int x = 0; int y = 0; int us = 0; int ue = 0;if(tft.getTouch(us,ue)){tft.fillRect(x-4,y-4,8,8,0x0000);tft.getTouch(x,y);tft.fillRect(x-4,y-4,8,8,0xffff);} tft.fillRect(x-4,y-4,8,8,0xffff);}";
//WORK ON THE TEXT CUSOR FUNCTION AND PRINTING THE X PRESS AND Y PRESS COORDS! GETTING PRESS COORDS IS WORKING!!!!!!!WOOOO
//String code = "int x = 0; int y = 0; int z = 0; if(x==0){if(y==0){if(z==0){for(int a = 0;a<10;a=a+1){if(x==0){prnt(\"xworks\");}if(y==0){prnt(\"yworks\");}}}}}";
/*
examples:
this example shows comparing things in the if statement. it also uses the string class. if the substring(0,4) of mystr equals hell then it should say in serial monitor.
String code = "String mystr = \"hello\"; int start = 0; int end = 4; if(myst.substring(start,end) == \"hell\"){ }";
bugs:
strings arent being made properly by compiler which is shitting on everything[FIXED]
what needs to be worked on:
setting variables. make x = x+1 work[WORKING]
the for loop. must be able to pull all inputs except first input, which is performed by the compile[WORKING]
////////////////////////////////
notes:
all variables are global, no matter where they are declared
string functions like indexOf('a',2) cannot contain nested string functions
allowed:
mystr.indexOf('a',2);
not allowed:
mystr.indexOf('a',mystr.indexOf('p',0));
if you want to do the above just do
int a = mystr.indexOf('p',0);
mystr.indexOf('a',p);
emulator code notes for me, the programmer:
in void compile() variables made should be removed from the code.
*/
const int vs = 200;//change this if needed. its how many variables peoples code can have
String strs[vs] = {"1"};
String ints[vs] = {"1"};
String chars[vs] = {"1"};
#include "SPI.h"
#include "TFT_eSPI.h"
#include <stdlib.h>
TFT_eSPI tft = TFT_eSPI();
void setup() {
Serial.begin(115200);
tft.init();
touch_calibrate();
compile();///////////////////////here the code inside the String code is "compiled" where all spaces except inside quotes are removed. all variables are also created
runcode(code.indexOf("{",code.indexOf("voidsetup"))+1);
}
void loop() {
runcode(code.indexOf("{",code.indexOf("voidloop"))+1);
}
String varget(String type, String id) {
String result = "-1";
if (type == "String") {
String varcount = strs[0];
if (varcount.toInt() != 0) {
for (int a = 0; a < varcount.toInt(); a++) {
String found = strs[a];
if (found.indexOf(type + id) > -1) {
result = found.substring(found.indexOf("=") + 1, found.indexOf(";"));
}
}
}
}
else if (type == "char") {
String varcount = chars[0];
if (varcount.toInt() != 0) {
for (int a = 0; a < varcount.toInt(); a++) {
String found = chars[a];
if (found.indexOf(type + id) > -1) {
result = found.substring(found.indexOf("=") + 1, found.indexOf(";"));
}
}
}
}
else if (type == "int") {
String varcount = ints[0];//number of variables of type listed in above if statement.
if (varcount.toInt() != 0) {
for (int a = 0; a < varcount.toInt(); a++) {
String found = ints[a];
if (found.indexOf(type + id) > -1) {
result = pull(found.substring(found.indexOf("=") + 1, found.indexOf(";")));
}
}
}
}
return result;
}
String pull(String input) { // you can input anything, from variables to string functions and stuff, and this function will return its value
String result = input;
if(input.substring(0,12) == "tft.getTouch"){
uint16_t ux,uy;
if(tft.getTouch(&ux,&uy)){
return "true";
}
else{
return "false";
}
}
if (input.indexOf("<") > -1 || input.indexOf(">") > -1 || input.indexOf("==") > -1 || input.indexOf("!=") > -1) {
String part1;
String part2;
if (input.indexOf("<") > -1) {
part1 = pull(input.substring(0, input.indexOf("<")));
part2 = pull(input.substring(input.indexOf("<") + 1, input.length()));
if (part1.toInt() < part2.toInt()) {
result = "true";
}
else {
result = "false";
}
}
else if (input.indexOf(">") > -1) {
part1 = pull(input.substring(0, input.indexOf(">")));
part2 = pull(input.substring(input.indexOf(">") + 1, input.length()));
if (part1.toInt() > part2.toInt()) {
result = "true";
}
else {
result = "false";
}
}
else if (input.indexOf("==") > -1) {
part1 = pull(input.substring(0, input.indexOf("=")));
part2 = pull(input.substring(input.indexOf("==") + 2, input.length()));
if (part1 == part2) {
result = "true";
}
else {
result = "false";
}
}
else if (input.indexOf("!=") > -1) {
part1 = pull(input.substring(0, input.indexOf("!")));
part2 = pull(input.substring(input.indexOf("=") + 1, input.length()));
if (part1 != part2) {
result = "true";
}
else {
result = "false";
}
}
}
else if (input.indexOf("=") > -1) {
String var = input.substring(0, input.indexOf("="));
String set = input.substring(input.indexOf("=") + 1, input.length());
if (varget("int", var) > "-1") {
String start = ints[0];
for (int x = 1; x < start.toInt(); x++) {
String current = ints[x];
if (current.indexOf(var) > -1) {
current.remove(current.indexOf("=") + 1, current.length() - current.indexOf("="));
current.concat(pull(set));
current.concat(";");
ints[x] = current;
}
}
}
else if (varget("String", var) > "-1") {
String start = strs[0];
for (int x = 1; x < start.toInt(); x++) {
String current = strs[x];
if (current.indexOf(var) > -1) {
current.remove(current.indexOf("=") + 1, current.length() - current.indexOf("="));
current.concat(pull(set));
current.concat(";");
strs[x] = current;
}
}
}
else if (varget("char", var) > "-1") {
String start = chars[0];
for (int x = 1; x < start.toInt(); x++) {
String current = chars[x];
if (current.indexOf(var) > -1) {
current.remove(current.indexOf("=") + 1, current.length() - current.indexOf("="));
current.concat(pull(set));
current.concat(";");
chars[x] = current;
}
}
}
}
else if (input.indexOf("-") > -1 || input.indexOf("+") > -1 || input.indexOf("*") > -1 || input.indexOf("/") > -1) {
char type;
if (input.indexOf("-") > -1) {
type = '-';
}
else if (input.indexOf("+") > -1) {
type = '+';
}
else if (input.indexOf("*") > -1) {
type = '*';
}
else if (input.indexOf("/") > -1) {
type = '/';
}
int partwithnum = 0;
String p1 = pull(input.substring(0, input.indexOf(type)));
String p2 = pull(input.substring(input.indexOf(type) + 1, input.length()));
if (type == '-') {
result = p1.toInt() - p2.toInt();
}
else if (type == '+') {
result = p1.toInt() + p2.toInt();
}
else if (type == '*') {
result = p1.toInt() * p2.toInt();
}
else if (type == '/') {
result = p1.toInt() / p2.toInt();
}
}
else if (varget("String", input) != "-1") {
result = varget("String", input);
}
else if (varget("char", input) != "-1") {
result = varget("char", input);
}
else if (varget("int", input) != "-1") {
result = varget("int", input);
}
else if (strfunction(input) != "-1") {
result = strfunction(input);
}
else if (!isDigit(input.charAt(0))) {
result.remove(0, 1);
result.remove(result.length() - 1, 1);
}
return result;
}
String strfunction(String datin) { // inputs examples: str1.indexOf("hello",2) , st1.substring(0,5) etc
String res = "-1";
String strdat = varget("String", datin.substring(0, datin.indexOf('.')));
String function = datin.substring(datin.indexOf('.') + 1, datin.indexOf("("));
if (function == "indexOf") {
String functdata = datin.substring(datin.indexOf("("), datin.lastIndexOf(")"));
String p1 = functdata.substring(1, functdata.indexOf(',', 0));
String p2 = functdata.substring(functdata.indexOf(",") + 1, functdata.indexOf(")", functdata.indexOf(",")));
p1 = pull(p1);
p2 = pull(p2);
res = strdat.indexOf(p1, p2.toInt());
}
else if (function == "substring") {
String functdata = datin.substring(datin.indexOf("("), datin.lastIndexOf(")"));
String p1 = functdata.substring(1, functdata.indexOf(',', 0));
String p2 = functdata.substring(functdata.indexOf(",") + 1, functdata.indexOf(")", functdata.indexOf(",")));
p1 = pull(p1);
p2 = pull(p2);
res = strdat.substring(p1.toInt(), p2.toInt());
}
else if (function == "length") {
res = strdat.length();
}
else if (function == "charAt") {
String functdata = datin.substring(datin.indexOf("(") + 1, datin.lastIndexOf(")"));
String p1 = functdata;
p1 = pull(p1);
res = strdat.charAt(p1.toInt());
}
else if (function == "lastIndexOf") {
String functdata = datin.substring(datin.indexOf("("), datin.lastIndexOf(")"));
String p1 = functdata.substring(1, functdata.indexOf(',', 0));
String p2 = functdata.substring(functdata.indexOf(",") + 1, functdata.indexOf(")", functdata.indexOf(",")));
p1 = pull(p1);
p2 = pull(p2);
res = strdat.lastIndexOf(p1.toInt(), p2.toInt());
}
return res;
}
void runcode(int codestart) {
int codeend;
for (int i = codestart; i < code.length(); i++) {
if (code.charAt(i) == '{') {
codeend = i;
i = code.length() + 1;
}
else if(code.charAt(i) == '}'){
codeend = i;
i = code.length() + 1;
}
}
Serial.println("going to run: ");
for(int a = codestart; a < codeend;a++){
Serial.print(code.charAt(a));
}
Serial.println(" ");
for (int x = codestart; x < codeend; x++) {
////////////////////////////////THE SECTION BELOW CONTAINS BUILTIN COMMANDS. THE NEXT SECTION CONTAINS CUSTOM LIBRARY COMMANDS////////
if(code.charAt(x) == '='){
int start = -1;
for(int n = x; n > 0; n--){
if(code.charAt(n) == '('){//make sure its not a maths f unction inside a parameter.
n = -1;
start = -1;
break;
}
if(code.charAt(n) == ';' || code.charAt(n) == '{' || code.charAt(n) == '}'){
start = n+1;
n = -1;
}
}
if(start != -1){
Serial.println("doing maths!: " + code.substring(start,code.indexOf(";",x)));
pull(code.substring(start,code.indexOf(";",x)));
}
}
else if (code.substring(x, x + 4) == "prnt") {
Serial.println("print: " + pull(code.substring(code.indexOf("(", x) + 1, code.indexOf(";", x) - 1)));
}
/////////////////////////////////////////////////////////////FOR:///////////////////
else if (code.substring(x, x + 3) == "for") {
String main = code.substring(code.indexOf("(", x) + 1, code.indexOf("{", x));
String param2 = main.substring(1, main.lastIndexOf(";"));
String param3 = main.substring(main.lastIndexOf(";") + 1, main.indexOf(")"));
for (int useless = 0; pull(param2) == "true"; pull(param3)) {
runcode(code.indexOf("{", x) + 1);
}
int endpoint = 0;
int brcount = 0;
for (int i = code.indexOf("{", x) + 1; i < code.length(); i++) {
if (code.charAt(i) == '}' && brcount == 0) {
endpoint = i + 1;
i = code.length() + 1;
}
if (code.charAt(i) == '{') {
brcount = brcount + 1;
}
if (code.charAt(i) == '}') {
brcount = brcount - 1;
}
}
runcode(endpoint);//continue running code after! this should be in every functions that could possibly be blocking.
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////IF:
else if (code.substring(x, x + 2) == "if") {
String main = code.substring(code.indexOf("(", x) + 1, code.indexOf("{", x) - 1);
if (pull(main) == "true") {
//Serial.println("match between two variables. running code: " + code.substring(code.indexOf("{",x)+1,code.length()));
runcode(code.indexOf("{", x) + 1);
}
int endpoint = 0;
int brcount = 0;
for (int i = code.indexOf("{", x) + 1; i < code.length(); i++) {
if (code.charAt(i) == '}' && brcount == 0) {
endpoint = i + 1;
i = code.length() + 1;
}
if (code.charAt(i) == '{') {
brcount = brcount + 1;
}
if (code.charAt(i) == '}') {
brcount = brcount - 1;
}
}
runcode(endpoint);//continue running code after! this should be in every functions that could possibly be blocking.
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////LIBRARY COMMANDS:
//////////////////////////////////////////////////////TFT ESPI
else if(code.substring(x,x+9) == "tft.drawPixel"){
String p1 = pull(code.substring(code.indexOf("(",x),code.indexOf(",",x)));
String p2 = pull(code.substring(code.indexOf(",",x),code.indexOf(")",x)));
String p3 = pull(code.substring(code.indexOf(",",code.indexOf(",",x)+1)+1,code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)));
uint16_t color = strtol(p3.c_str(), NULL, 0);
tft.drawPixel(p1.toInt(),p2.toInt(),color);
}
else if(code.substring(x,x+12) == "tft.drawRect"){
Serial.println("drawsrect");
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(",",x)));
String p2 = pull(code.substring(code.indexOf(",",x)+1,code.indexOf(",",code.indexOf(",",x)+1)));
String p3 = pull(code.substring(code.indexOf(",",code.indexOf(",",x)+1)+1,code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)));
String p4 = pull(code.substring(code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)+1,code.lastIndexOf(",",code.indexOf(")",x))));
String p5 = pull(code.substring(code.lastIndexOf(",",code.indexOf(")",x))+1,code.indexOf(")",x)));
uint16_t color = strtol(p5.c_str(), NULL, 0);
tft.drawRect(p1.toInt(),p2.toInt(),p3.toInt(),p4.toInt(),color);
}
else if(code.substring(x,x+12) == "tft.fillRect"){
Serial.println("drawsrect");
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(",",x)));
String p2 = pull(code.substring(code.indexOf(",",x)+1,code.indexOf(",",code.indexOf(",",x)+1)));
String p3 = pull(code.substring(code.indexOf(",",code.indexOf(",",x)+1)+1,code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)));
String p4 = pull(code.substring(code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)+1,code.lastIndexOf(",",code.indexOf(")",x))));
String p5 = pull(code.substring(code.lastIndexOf(",",code.indexOf(")",x))+1,code.indexOf(")",x)));
uint16_t color = strtol(p5.c_str(), NULL, 0);
tft.fillRect(p1.toInt(),p2.toInt(),p3.toInt(),p4.toInt(),color);
}
else if(code.substring(x,x+17) == "tft.drawRoundRect"){
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(",",x)));
String p2 = pull(code.substring(code.indexOf(",",x)+1,code.indexOf(",",code.indexOf(",",x)+1)));
String p3 = pull(code.substring(code.indexOf(",",code.indexOf(",",x)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)));
String p4 = pull(code.substring(code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1)));
String p5 = pull(code.substring(code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)+1,code.lastIndexOf(",",code.indexOf(")",x))));
String p6 = pull(code.substring(code.lastIndexOf(",",code.indexOf(")",x))+1,code.indexOf(")",x)));
uint16_t color = strtol(p6.c_str(), NULL, 0);
tft.drawRoundRect(p1.toInt(),p2.toInt(),p3.toInt(),p4.toInt(),p5.toInt(),color);
}
else if(code.substring(x,x+17) == "tft.fillRoundRect"){
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(",",x)));
String p2 = pull(code.substring(code.indexOf(",",x)+1,code.indexOf(",",code.indexOf(",",x)+1)));
String p3 = pull(code.substring(code.indexOf(",",code.indexOf(",",x)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)));
String p4 = pull(code.substring(code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1)));
String p5 = pull(code.substring(code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)+1,code.lastIndexOf(",",code.indexOf(")",x))));
String p6 = pull(code.substring(code.lastIndexOf(",",code.indexOf(")",x))+1,code.indexOf(")",x)));
uint16_t color = strtol(p6.c_str(), NULL, 0);
tft.fillRoundRect(p1.toInt(),p2.toInt(),p3.toInt(),p4.toInt(),p5.toInt(),color);
}
else if(code.substring(x,x+14) == "tft.drawCircle"){
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(",",x)));
String p2 = pull(code.substring(code.indexOf(",",x)+1,code.indexOf(",",code.indexOf(",",x)+1)));
String p3 = pull(code.substring(code.indexOf(",",code.indexOf(",",x)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)));
String p4 = pull(code.substring(code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1)));
uint16_t color = strtol(p4.c_str(), NULL, 0);
tft.drawCircle(p1.toInt(),p2.toInt(),p3.toInt(),color);
}
else if(code.substring(x,x+14) == "tft.fillCircle"){
Serial.println("Drawing circle");
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(",",x)));
String p2 = pull(code.substring(code.indexOf(",",x)+1,code.indexOf(",",code.indexOf(",",x)+1)));
String p3 = pull(code.substring(code.indexOf(",",code.indexOf(",",x)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)));
String p4 = pull(code.substring(code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1)));
uint16_t color = strtol(p4.c_str(), NULL, 0);
tft.fillCircle(p1.toInt(),p2.toInt(),p3.toInt(),color);
}
else if(code.substring(x,x+16) == "tft.drawTriangle"){
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(",",x)));
String p2 = pull(code.substring(code.indexOf(",",x)+1,code.indexOf(",",code.indexOf(",",x)+1)));
String p3 = pull(code.substring(code.indexOf(",",code.indexOf(",",x)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)));
String p4 = pull(code.substring(code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1)));
String p5 = pull(code.substring(code.lastIndexOf(",",code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)-1)+1,code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)));
String p6 = pull(code.substring(code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)+1,code.lastIndexOf(",",code.indexOf(")",x))));
String p7 = pull(code.substring(code.lastIndexOf(",",code.indexOf(")",x))+1,code.indexOf(")",x)));
uint16_t color = strtol(p7.c_str(), NULL, 0);
tft.drawTriangle(p1.toInt(),p2.toInt(),p3.toInt(),p4.toInt(),p5.toInt(),p6.toInt(),color);
}
else if(code.substring(x,x+16) == "tft.fillTriangle"){
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(",",x)));
String p2 = pull(code.substring(code.indexOf(",",x)+1,code.indexOf(",",code.indexOf(",",x)+1)));
String p3 = pull(code.substring(code.indexOf(",",code.indexOf(",",x)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)));
String p4 = pull(code.substring(code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1,code.indexOf(",",code.indexOf(",",code.indexOf(",",code.indexOf(",",x)+1)+1)+1)));
String p5 = pull(code.substring(code.lastIndexOf(",",code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)-1)+1,code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)));
String p6 = pull(code.substring(code.lastIndexOf(",",code.lastIndexOf(",",code.indexOf(")",x))-1)+1,code.lastIndexOf(",",code.indexOf(")",x))));
String p7 = pull(code.substring(code.lastIndexOf(",",code.indexOf(")",x))+1,code.indexOf(")",x)));
uint16_t color = strtol(p7.c_str(), NULL, 0);
tft.fillTriangle(p1.toInt(),p2.toInt(),p3.toInt(),p4.toInt(),p5.toInt(),p6.toInt(),color);
}
else if(code.substring(x,x+12) == "tft.getTouch"){
Serial.println("touch");
String p1 = code.substring(code.indexOf("(",x)+1,code.indexOf(",",x));
String p2 = code.substring(code.indexOf(",",x)+1,code.indexOf(")",x));
uint16_t txp,typ;
tft.getTouch(&txp,&typ);
if(txp < 240 && typ < 320){
p1.concat("=" + String(txp));
p2.concat("=" + String(typ));
pull(p1);
pull(p2);
}
}
else if(code.substring(x,x+13) == "tft.setCursor"){
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(",",x)));
String p2 = pull(code.substring(code.indexOf(",",x)+1,code.indexOf(")",x)));
tft.setCursor(p1.toInt(),p2.toInt());
}
else if(code.substring(x,x+11) == "tft.println"){
Serial.println("run println");
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(")",x)));
}
else if(code.substring(x,x+9) == "tft.print"){
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(")",x)));
tft.print(p1);
Serial.println("printed on screen: " +p1);
}
//////////////////////////////////////////////////////////////
else if(code.substring(x,x+16) == "tft.setTextColor"){
int a = code.indexOf(")",x);
int b = code.indexOf(",",x);
if(b == -1 || b > a){//must only have a single parameter
String param1 = code.substring(code.indexOf("(",x)+1,code.indexOf(")",x));
uint16_t color1 = strtol(param1.c_str(), NULL, 0);
tft.setTextColor(color1);
}
else if(b < a){
String param1 = code.substring(code.indexOf("(",x)+1,code.indexOf(",",x));
String param2 = code.substring(code.indexOf(",",x)+1,code.indexOf(")",x));
uint16_t color1 = strtol(param1.c_str(), NULL, 0);
uint16_t color2 = strtol(param2.c_str(), NULL, 0);
tft.setTextColor(color1,color2);
}
}
else if(code.substring(x,x+15) == "tft.setTextFont"){
String p1 = pull(code.substring(code.indexOf("(",x)+1,code.indexOf(")",x)));
tft.setTextFont(p1.toInt());
}
}
}
void compile() { //ran on startup. makes the code easier for this emulator to read, and also creates all variables;
String current = code;
code = ";";
code.concat(current);
bool inquotes = false;
for (int f = 0; f < code.length(); f++) {
if (inquotes == true) {
if (code.charAt(f) == '"') {
inquotes = false;
f = f + 1;
}
}
else if (inquotes == false) {
if (code.charAt(f) == ' ') {
code.remove(f, 1);
}
if (code.charAt(f) == '"') {
inquotes = true;
f = f + 1;
}
}
}
for (int s = 0; s < code.length(); s++) {
if (code.substring(s, s + 6) == "String") {
String count = strs[0];
strs[count.toInt()] = code.substring(s, code.indexOf('=', s) + 1) + pull(code.substring(code.indexOf('=', s) + 1, code.indexOf(';', s))) + ";";
strs[0] = String(count.toInt() + 1);
}
else if (code.substring(s, s + 3) == "int" && code.substring(s-2,s)!= "pr") {
String count = ints[0];
ints[count.toInt()] = String(code.substring(s, code.indexOf('=', s) + 1) + pull(code.substring(code.indexOf('=', s) + 1, code.indexOf(';', s))) + ";");
ints[0] = String(count.toInt() + 1);
code.remove(s, code.indexOf(";", s) - s);
}
else if (code.substring(s, s + 4) == "char") {
String count = chars[0];
chars[count.toInt()] = String(code.substring(s, code.indexOf('=', s) + 1) + pull(code.substring(code.indexOf('=') + 1, code.indexOf(';'))) + ";");
chars[0] = String(count.toInt() + 1);
}
}
}
void touch_calibrate()
{
uint16_t calData[5];
uint8_t calDataOK = 0;
tft.fillScreen(TFT_BLACK);
tft.setCursor(20, 0);
tft.setTextFont(2);
tft.setTextSize(1);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.println("Touch corners as indicated");
tft.setTextFont(1);
tft.println();
tft.calibrateTouch(calData, TFT_MAGENTA, TFT_BLACK, 15);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.println("Calibration complete!");
tft.fillScreen(TFT_BLACK);
}
update:
thanks for all the options
a little irelivant but might clear some stuff up about using esp32
yes, esp32 is awesome however there is an issue with using it:
i recently bought a 320x480 UNO tft ili9486 shield, which unfortunately currently doesnt work with my esp32, however it does work with my arduino uno, which has wifi capabilities because it is wired to an esp-01 module.
I have a better display on its way which is 320x480 SPI so it can be wired to my esp32, but it would still be nice to have an UNO version because the atmega328p is quite a compact chip and low cost