I am not that great at Arduino and C++ yet, but I'll give this a shot:
I have a char array that looks like this:
char PROGMEM bitmap[] = {0xD7,0xED,0xEF,0xFF,0xF6,0xEF,0xFE};
However, I receive this char array from a string, which is basically:
String input = "0xD7,0xED,0xEF,0xFF,0xF6,0xEF,0xFE";
How do I turn my input
variable with a comma separated char array into the actual array? I need it to be fast and not use a lot of memory. I haven't been able to find a use case like this, because it's very unique.
I feel like this should be super simple to do, but I can't wrap my head around it.
Things I've tried and didn't understand that much:
1:
char delimiter[] = ",";
char* ptr = strtok_r(text, delimiter);
while (ptr != NULL) {
Serial.println(ptr);
ptr = strtok_r(NULL, delimiter);
}
cannot convert 'String' to 'char*' for argument '1' to 'char* strtok(char*, const char*)'
2:
char buf[4800];
text.toCharArray(buf, 4800);
char *p = buf;
char *str;
int i = 0;
char bitmap[115];
while ((str = strtok_r(p, ",", &p)) != NULL) {
bitmap[i] = str;
i++;
}
Serial.println(bitmap);
(the total length of my characters are 4799 and the total length of hexadecimals are 114, so I'm just trying something here)
Error: Apparently you can't use =
?
3:
char buf[4800];
text.toCharArray(buf, 4800);
char *p = buf;
char *str;
while ((str = strtok_r(p, ",", &p)) != NULL) {
Serial.println(str);
}
This actually prints the strings to the console, but trying to add them to a char array is still a mystery I am trying to solve right as I write this.
4:
char buf[4800];
text.toCharArray(buf, 4800);
char *p = buf;
char *str;
char *ptr;
char bitmap[115];
int i = 0;
while ((str = strtok_r(p, ",", &p)) != NULL) {
bitmap[i] = strtol(str, &ptr, 16);
i++;
}
Serial.println(bitmap);
I'm getting a lot closer now. strtol(str, &ptr, 16)
actually gives me the right integers I need, but it seems like my Arduino crashes when I run this code. It simply reboots it. Error:
assertion "heap != NULL && "free() target pointer is outside heap areas"" failed: file "/Users/ficeto/Desktop/ESP32/ESP32/esp-idf-public/components/heap/./heap_caps.c", line 171, function: heap_caps_free abort() was called at PC 0x4011c173 on core 1