Okay sorry but I am struggling with this. I tried to implement your advice in the first post - just because it was the most familiar to me. I decided to just run some test codes to see if I could get it working. I got it to run and it would return commands (printing through serial) but it would also output a lot of garbage.
const uint8_t maxNbofCommand = 20;
const uint8_t maxLengthofCommand = 63;
char commands[maxNbofCommand][maxLengthofCommand + 1];
uint8_t commandIndex = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if (millis() <= 5000){
strcpy(commands[commandIndex], "Ft_App_WrCoCmd_Buffer(phost, BEGIN(RECTS))");
commandIndex++;
strcpy(commands[commandIndex], "Ft_App_WrCoCmd_Buffer(phost, COLOR_RGB(0, 0, 0))");
commandIndex++;
strcpy(commands[commandIndex], "Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX, screenY))");
commandIndex++;
strcpy(commands[commandIndex], "Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX2, screenY2))");
commandIndex++;
strcpy(commands[commandIndex], "Ft_App_WrCoCmd_Buffer(phost, END())");
commandIndex++;
for(int i = 0; i < 100; i++)
{
Serial.println(commands[i]);
}
}
if (millis() >= 5000 && millis() <= 10000) {
strcpy(commands[commandIndex], "Ft_App_WrCoCmd_Buffer(phost, BEGIN(RECTS))");
commandIndex++;
strcpy(commands[commandIndex], "Ft_App_WrCoCmd_Buffer(phost, COLOR_RGB(255, 255, 255))");
commandIndex++;
strcpy(commands[commandIndex], "Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX, screenY))");
commandIndex++;
strcpy(commands[commandIndex], "Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX2, screenY2))");
commandIndex++;
strcpy(commands[commandIndex], "Ft_App_WrCoCmd_Buffer(phost, END())");
commandIndex++;
}
for(int i = 0; i < 100; i++)
{
Serial.println(commands[i]);
}
}
So I moved on to trying to use strdup() and malloc but I really am not familiar with these functions.
From my little understanding - strdup() duplicates a string and uses malloc to allocate memory for the string copy.
I'm not sure how I use strdup() to add the command to an array. This is the little test code I wrote so far.
const uint8_t maxNbofCommand = 256;
const char*commands[maxNbofCommand];
String rect = "Ft_App_WrCoCmd_Buffer(phost, BEGIN(RECTS))";
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
//void loop() {
char*strdup(const char *rect)
{
// Compute the required space.
size_t len_rect = strlen(rect) + 1;
// Allocate that amount of space, and check for failure.
char *mem_rect = malloc(len_rect);
if (!mem_rect) return NULL;
// Copy the data and return.
return memcpy(mem_rect, rect, len_rect);
Serial.println(rect);
}
//}
void loop() {}
Any further explanation on how to use strdup() to add the commands to the array would be greatly appreciated.
My end result is hopefully something like the code below where the commands for certain conditions are added to the array each time through the loop and then the array of commands is sent to the LCD.
char*screenArray = {};
int elementCount = sizeof(screenArray) / sizeof(char);
int previousX = 0;
int previousY = 0;
int screenX = 0;
int screenY = 0;
int screenX2 = 0;
int screenY2 = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
}
void loop() {
DisplayData();
showScreen();
}
void DisplayData() {
screenX = Xaverage * Xratio;
screenY = 7680 - (Yaverage * Yratio);
Serial.println(screenX);
Serial.println(screenY);
if (y >= 0 && screenY <= 7680 && x >= 0 && screenX <= 12800) {
if (previousX != screenX && previousY != screenY) {
previousX = screenX;
previousY = screenY;
screenX2 = screenX + 480;
screenY2 = screenY + 480;
Ft_Gpu_CoCmd_Dlstart(phost);
Ft_App_WrCoCmd_Buffer(phost, CLEAR(1, 1, 1));
if (average > 2000) {
char*screenArray[] = {
"Ft_App_WrCoCmd_Buffer(phost, BEGIN(RECTS))",
"Ft_App_WrCoCmd_Buffer(phost, COLOR_RGB(255, 255, 255))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX, screenY))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX2, screenY2))",
"Ft_App_WrCoCmd_Buffer(phost, END())"
};
}
if (1900 < average && average <= 2000) {
char*screenArray[] = {
"Ft_App_WrCoCmd_Buffer(phost, BEGIN(RECTS))",
"Ft_App_WrCoCmd_Buffer(phost, COLOR_RGB(255, 255, 25))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX, screenY))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX2, screenY2))",
"Ft_App_WrCoCmd_Buffer(phost, END())"
};
}
if (1600 < average && average <= 1900 ) {
char*screenArray[] = {
"Ft_App_WrCoCmd_Buffer(phost, BEGIN(RECTS))",
"Ft_App_WrCoCmd_Buffer(phost, COLOR_RGB(255, 125, 0))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX, screenY))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX2, screenY2))",
"Ft_App_WrCoCmd_Buffer(phost, END())"
};
}
if (1200 < average && average <= 1600) {
char*screenArray[] = {
"Ft_App_WrCoCmd_Buffer(phost, BEGIN(RECTS))",
"Ft_App_WrCoCmd_Buffer(phost, COLOR_RGB(205, 0, 0))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX, screenY))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX2, screenY2))",
"Ft_App_WrCoCmd_Buffer(phost, END())"
};
}
if (900 < average && average <= 1200) {
char*screenArray[] = {
"Ft_App_WrCoCmd_Buffer(phost, BEGIN(RECTS))",
"Ft_App_WrCoCmd_Buffer(phost, COLOR_RGB(50, 0, 100))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX, screenY))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX2, screenY2))",
"Ft_App_WrCoCmd_Buffer(phost, END())"
};
}
if (200 < average && average <= 900) {
char*screenArray[] = {
"Ft_App_WrCoCmd_Buffer(phost, BEGIN(RECTS))",
"Ft_App_WrCoCmd_Buffer(phost, COLOR_RGB(0, 0, 50))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX, screenY))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX2, screenY2))",
"Ft_App_WrCoCmd_Buffer(phost, END())"
};
}
if (average <= 200) {
char*screenArray[] = {
"Ft_App_WrCoCmd_Buffer(phost, BEGIN(RECTS))",
"Ft_App_WrCoCmd_Buffer(phost, COLOR_RGB(0, 0, 0))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX, screenY))",
"Ft_App_WrCoCmd_Buffer(phost, VERTEX2F(screenX2, screenY2))",
"Ft_App_WrCoCmd_Buffer(phost, END())"
};
}
}
}
}
void showScreen() {
for (int index = 0; index < elementCount; index++)
{
Ft_App_WrCoCmd_Buffer(phost, screenArray[0]);
Ft_App_WrCoCmd_Buffer(phost, screenArray[1]);
Ft_App_WrCoCmd_Buffer(phost, screenArray[2]);
Ft_App_WrCoCmd_Buffer(phost, screenArray[3]);
Ft_App_WrCoCmd_Buffer(phost, screenArray[4]);
Ft_App_WrCoCmd_Buffer(phost, DISPLAY());
Ft_Gpu_CoCmd_Swap(phost);
Ft_App_Flush_Co_Buffer(phost);
Ft_Gpu_Hal_WaitCmdfifo_empty(phost);
}
Thanks again