Reading file data from flash mem

Status
Not open for further replies.

Projectitis

Well-known member
Hi all,

I have a large amount of read-only data that I want available to my code at runtime (T_3.6). If it was included in my code as a byte array (or equivalent) it would be larger than the available RAM, so I can’t just create an instance of the byte array and access it. T_3.6 has 1M flash available, so let’s use it :D

How can I:
1) automatically #include (or similar) any file as an array of bytes into my code so that it is compiled with my sketch. E.g. /data/datafile.bin
2) read arbitrary parts of that file from flash mem (knowing the offset)?

Cheers,
Peter
 
Simply define the data as const array in a .h file and include it in the .ino. Thus, it will be automatically in the flash memory and not in RAM.
 
ok, great.
Any clues about (1)? In other languages I've used I can do something like this:
Code:
static const uint8_t databytes[] =
[include "data/datafile.bin" as byte]
Anything similar in c/c++?
 
No... first, create a file "datafile.h" in your sketch directory in which you simply write:
Code:
static const uint8_t databytes[]={/*put here all the bytes you want, comma separated*/};
Second, in your sketch .ino file insert simply before setup() :
Code:
#include "datafile.h"

From there on, you might directly use in setup() or loop() or both for example
Code:
uint8_t mybyte = databytes[4711]; //to get the 4712th byte in the array ;)
 
Search for bin2h (google) - it converts binary files to c header files. Depending on the version, you may have to add "const" (and perhaps remove that annoying, not needed "PROGMEM" from the resulting file, if added)
 
Status
Not open for further replies.
Back
Top