I'm storing a large snippet of text in three separate PROGMEM char arrays:
const char script1[] PROGMEM = "....";
const char script2[] PROGMEM = "....";
const char script3[] PROGMEM = "....";
The first two are 32766 characters, and the last one is 23867 characters (the whole string is 89400 characters in total). I store these in PROGMEM on an Arduino Mega 2560 (which then uses about 90K of 256K of available program memory).
I stored these three strings in a table, together with their lengths:
const int TABLE_SIZE = 3;
const char * const scriptTable[] PROGMEM =
{
script1,
script2,
script3
};
const int scriptLengths[] {32767, 32767, 23867};
I have some issues trying to chunk up the string into 10-byte buffers, and processng it that way. I want to iterate through the table, process the first part in 10-byte chunks first, then move on to the second part, and finally the last part.
For a single string (code
), the following works fine:
const char code[] PROGMEM = "....";
// Chunk up large code string in PROGMEM.
const int CHUNKSIZE = 10; // CHUNKSIZE *must* be an even number!!
char workingSpace[CHUNKSIZE + 1];
int baseIndex = 0;
while (baseIndex < sizeof(code)) {
int chunk;
if (baseIndex + CHUNKSIZE > sizeof(code)) {
chunk = sizeof(code) - baseIndex;
}
else {
chunk = CHUNKSIZE;
}
memcpy_PF(workingSpace, ((uint_farptr_t)code) + baseIndex, chunk);
workingSpace[chunk] = '\0';
// for debugging
Serial.print("#");
Serial.print(baseIndex);
Serial.print("– Got a chunk [");
Serial.print(workingSpace);
Serial.println("]");
baseIndex += CHUNKSIZE;
However, I'm having some difficulties doing the memcpy_PF call using the table.
int tableIndex = 0;
// for each of the three script strings
for (tableIndex = 0; tableIndex < TABLE_SIZE; tableIndex++) {
// Chunk up large script strings in PROGMEM.
const int CHUNKSIZE = 10; // CHUNKSIZE *must* be an even number!!
char workingSpace[CHUNKSIZE + 1];
int baseIndex = 0;
// we can only use sizeof if it's a static var.
// strlen should only be calculated once, expensive operation (loop until we encounter '\0').
// now stored statically in scriptLengths table.
Serial.println("tableIndex: ");
Serial.println(tableIndex);
int strLength = scriptLengths[tableIndex];
Serial.print("length script string: ");
Serial.println(strLength);
while (baseIndex < strLength) {
int chunk;
if (baseIndex + CHUNKSIZE > strLength) {
chunk = strLength - baseIndex;
}
else {
chunk = CHUNKSIZE;
}
// do memcpy_PF call here
I have tried several options here, mostly having difficulties with the second parameter (how to address the string stored in the table, and increase its indexing parameter). I read that the way to get a string out of a string table is to use pgm_read_word. However, with both of these below, I either get gibberish in the buffer string, or it seems to start in an incorrect location in the string.
memcpy_PF(workingSpace, ((uint_farptr_t) &scriptTable[tableIndex][baseIndex]), chunk);
memcpy_PF(workingSpace, ((uint_farptr_t) (char*)pgm_read_word(&(scriptTable[tableIndex])) + baseIndex), chunk);
Any help would be greatly appreciated!
Thanks!