I need to load a text file into my Electrons, then read the data in the files into arrays. How do I do this in the Particle Workbench?
How much text?
You can statically compile it into a header file. That way it’s stored into flash. If you’re loading it all into memory, you’ll run out of space quick.
const char data = "Your text here."
You may have to write some pre-processing code to generate this file. You may be able to find something similar on the internet somewhere. (It really depends on what’s inside the text file)
How is the file formatted? One index per line? CSV?
You’ll probably need to make a script that will let your computer send the file in pieces to the Electron over serial. (I’d do this with python or bash.)
You’ll also need to allow your application to receive the pieces over serial and then save the data into the array however you want.
If the file doesn’t get changed much I’d go with what @jaredwolff has suggested, but it depends on your application.
If you really need to load new data onto the Electron without compiling it in, it might be worth it to create a script that lets the Electron load in data from serial.
Hmm, not sure this frames it correctly.
Whether it’s in a header file or not does not have any bearing on whether the strings would be stored in flash or not.
The thing that actually makes it live in flash is the
const modifier on global variables.
Having the data in a header file improves readability of the code due to the separation between the static data and the actual code tho’ - hence the advice as such is a fair one.
BTW, it probably should be
const char data = "Your text here." // or (not my preferred alternative) const char* data = "Your text here."
@astroyam, have you considered using an SD card to hold the files and read the data from there?
My thinking was that if it was a lot of data, then it would be taking up a ton of space. Purely for organizational reasons to put it into a header file.
Thanks for clarifying @ScruffR!
Thanks everyone. Great ideas.
The original problem that I had was to bring arrays of data from python code into my Electron code. I thought a good way was to push the data to a text file then read it in to the C code. Turns out a friend of mine found a way to have Python directly create .c and .h files with the data, which I can directly drop into my C code on the Electron.
(Let me know if you want to see some of that.)