Hi,
I have a need to have separate header files each with image data in them like so:
#define _bitmap_name "My Bitmap"
#define _bitmap_width 128
#define _bitmap_height 64
const uint8_t _bitmap[] PROGMEM = {
0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04
etc..
};
What I would like to do is pull the info from each tab (hopefully keeping the define/variable names the same in each tab) into a central array in the main file so I can loop through this array and pick out the data it contains, maybe using a struct, something like this:
struct ImgData {
char* ImgName;
int ImgHeight;
int ImgWidth;
int ImgData;
} imgArr[] = {
{_bitmap_name , _bitmap_height , _bitmap_width , _bitmap},
};
Ideally I would like to test for the existence of any img.h files and include the data into the main ImgData array.
Is this even possible?
You may have guessed I'm not an expert in C/C++
pwhitrow:
Ideally I would like to test for the existence of any img.h files and include the data into the main ImgData array.
Is this even possible?
I'll assume you are asking this with respect to the default installable Arduino IDE. Obviously other ways of building may vary, and of course everything can be scripted/customized with a little effort.
To my knowledge, wildcard #includes are not possible in C/C++.
However, a quirk of the way the Aduino IDE tooling builds your sketch is that it performs some preprocessing on your source files. One of those preprocessing actions is that it concatenates all .ino files in the sketch folder together into one big file.
So instead of calling your data files .h you could call them .ino and have them automatically added to the sketch.
The order of this concatenation is probably unspecified though, which makes things awkward for preprocessor tricks.
So, how to automatically include the data in the array... ponder
I should have stated, yes it's the Arduino IDE 1.8.2
I'm actually thinking that the best way forward would be to just have the files stored in SDcard and grab them from there.
But again, the issue I have yet to solve, in my limited knowledge thus far, is how to dynamically add to an array that's pre-declared/initialized in the main .ino file.
So for example, each "config" file, or rather I should call them image file, would have the same structure inside, eg:
_bitmap_name : "My Bitmap"
_bitmap_width : 128
_bitmap_height : 64
_bitmap_data :
0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04
etc..
or something like that. So I could then check for the existence of ImgData_1.cgf, ImgData_2.cfg (etc) and call/load each one of those files' data into the .ino array.
Sounds simple in theory!
Well, it all depends on what you need. But I wouldn't get stuck on all this "dynamically adding to array" stuff. Basically your options are to allocate memory statically in your program as your code snippets have shown; or allocate dynamically at runtime using malloc()/free() or new/delete.
If you allocate statically, you can use the PROGMEM directive if it makes sense in your use case; if dynamic, then not.
If static, you'd need to allocate enough space at the beginning for the maximum number of expected data items and allow the rest as wastage; if dynamic you could allocate just the right amount (but you'd still need to have the capability of handling the maximum amount, so no great advantage).
If you were to do a lot of dynamic memory allocations and deallocations, you could run into memory fragmentation too - something to be aware of.
Personally I'd probably decide on a reasonable maximum and do as much as possible statically.
Anyway, if an SDcard is an option, then you shouldn't really need to load all the stuff into memory at once anyway. When you need a file's data, just load it on demand. If necessary, read through all files at begin() time and create an index of the content.
arduarn:
Well, it all depends on what you need. But I wouldn't get stuck on all this "dynamically adding to array" stuff. Basically your options are to allocate memory statically in your program as your code snippets have shown; or allocate dynamically at runtime using malloc()/free() or new/delete.
If you allocate statically, you can use the PROGMEM directive if it makes sense in your use case; if dynamic, then not.
If static, you'd need to allocate enough space at the beginning for the maximum number of expected data items and allow the rest as wastage; if dynamic you could allocate just the right amount (but you'd still need to have the capability of handling the maximum amount, so no great advantage).
If you were to do a lot of dynamic memory allocations and deallocations, you could run into memory fragmentation too - something to be aware of.
Personally I'd probably decide on a reasonable maximum and do as much as possible statically.
Anyway, if an SDcard is an option, then you shouldn't really need to load all the stuff into memory at once anyway. When you need a file's data, just load it on demand. If necessary, read through all files at begin() time and create an index of the content.
Of course! You just jump started my brain. Funny how we get locked on a path until someone points out the blindingly obvious. Cheers dude.
MorganS:
Alphabetical order.
Thanks, I did find a third party website that suggested it was as follows, but I imagine it is more an implementation detail rather than something set in stone.
MySketch.ino
AFile1.ino
BFile2.ino
aFile3.ino
bFile4.ini
arduarn:
Thanks, I did find a third party website that suggested it was as follows, but I imagine it is more an implementation detail ...
yes, that isn't C++ that is an Arduino implementaton.
the better way is to create a header with the data and include it as you would normally
bitmaps.h:
#ifndef BITMAPS
#define BITMAPS
//here
#endif
myIno.ino
#include "bitmaps.h"
void setup(){}
void loop(){}
and never worry about that alphabetical order crap...