I'm currently developing a library for an Arduino project, and I have a problem regarding file inclusion. In my project, I have a file named "images.h" located in the main sketch folder. However, I need this file to be accessible to my library as well. How can I achieve this?
Start by posting some code that demonstrates the issue.
The library is here. The images.h file is in the main sketch but the library should be able to access it.
If i compile the code like this, it throws an error saying that the images.h file is not found and it is because it is not in the same folder as the library itself.
Just off the top of my head, what about including images.h in the sketch before including the library? Something like:
#include "images.h"
#include <library.h>
void setup() {
.
.
.
}
void loop() {
.
.
.
}
I will try that right now
Nope, still the same problem.
Did you remove the #include "images.h" line from the library source?
Yes. Should I let it in the library?
No, it should be removed.
I think that should have worked. You may be looking at some other problem. Let me ponder.
Okay, I see. You're referencing the variables in images.h in the .cpp file, not the .h file.
Another approach: pass those arrays to the library either through the constructor or begin method. If all the arrays will be same size for different versions of images.h, it could be something like:
void begin(
Menu_icon_1,
Menu_icon_2,
Menu_icon_3,
Menu_icon_4,
Menu_icon_5,
Menu_icon_6,
Menu_icon_7,
Menu_icon_8,
Menu_icon_9,
bitmap_icons,
epd_bitmap_contour_selection_dashboard,
Boot_img);
If each array isn't always the same size, you could also pass sizeof Menu_icon_1, etc.
I'm not too sure to understand but how could I reference the variable in the .h file?
I had this whole convoluted idea of using getter functions defined in the .h file... but as I was typing it out I realized it won't work. When the .cpp file includes OpenMenuOS.h, it's not going to know about the images.h file because that was included in the sketch. One of those "it sounded good until I actually thought it through" ideas. Oops.
I think you'll have to pass the arrays to the constructor or the begin method.
Would it be something like that (Took it from ChatGPT) :
In Library
void begin(
const uint8_t* Menu_icon_1, size_t size_Menu_icon_1,
const uint8_t* Menu_icon_2, size_t size_Menu_icon_2,
const uint8_t* Menu_icon_3, size_t size_Menu_icon_3,
const uint8_t* Menu_icon_4, size_t size_Menu_icon_4,
const uint8_t* Menu_icon_5, size_t size_Menu_icon_5,
const uint8_t* Menu_icon_6, size_t size_Menu_icon_6,
const uint8_t* Menu_icon_7, size_t size_Menu_icon_7,
const uint8_t* Menu_icon_8, size_t size_Menu_icon_8,
const uint8_t* Menu_icon_9, size_t size_Menu_icon_9,
const uint8_t* bitmap_icons, size_t size_bitmap_icons,
const uint8_t* epd_bitmap_contour_selection_dashboard, size_t size_epd_bitmap_contour_selection_dashboard,
const uint8_t* Boot_img, size_t size_Boot_img);
and :
In main code
Menu.begin(
Menu_icon_1, sizeof(Menu_icon_1),
Menu_icon_2, sizeof(Menu_icon_2),
// Pass other arrays similarly
);
Yeah, something like that. Define class variables in the library, assign the passed values to the class variables and use them in the library.
Ok, Thanks! I'll check that out tommorow and see if that works!
In my main code, I put this function :
extern const uint8_t Menu_icon_1[];
extern const uint8_t Menu_icon_2[];
extern const uint8_t Menu_icon_3[];
extern const uint8_t Menu_icon_4[];
extern const uint8_t Menu_icon_5[];
extern const uint8_t Menu_icon_6[];
extern const uint8_t Menu_icon_7[];
extern const uint8_t Menu_icon_8[];
extern const uint8_t Menu_icon_9[];
extern const uint8_t* bitmap_icons[];
extern const uint8_t epd_bitmap_contour_selection_dashboard[];
extern const uint8_t Boot_img[];
menu.setImages(
Menu_icon_1, sizeof(Menu_icon_1),
Menu_icon_2, sizeof(Menu_icon_2),
Menu_icon_3, sizeof(Menu_icon_3),
Menu_icon_4, sizeof(Menu_icon_4),
Menu_icon_5, sizeof(Menu_icon_5),
Menu_icon_6, sizeof(Menu_icon_6),
Menu_icon_7, sizeof(Menu_icon_7),
Menu_icon_8, sizeof(Menu_icon_8),
Menu_icon_9, sizeof(Menu_icon_9),
bitmap_icons, sizeof(bitmap_icons),
epd_bitmap_contour_selection_dashboard, sizeof(epd_bitmap_contour_selection_dashboard),
Boot_img, sizeof(Boot_img)
);
And it is set like that in the library :
void OpenMenuOS::setImages(
const uint8_t* Menu_icon_1, size_t size_Menu_icon_1,
const uint8_t* Menu_icon_2, size_t size_Menu_icon_2,
const uint8_t* Menu_icon_3, size_t size_Menu_icon_3,
const uint8_t* Menu_icon_4, size_t size_Menu_icon_4,
const uint8_t* Menu_icon_5, size_t size_Menu_icon_5,
const uint8_t* Menu_icon_6, size_t size_Menu_icon_6,
const uint8_t* Menu_icon_7, size_t size_Menu_icon_7,
const uint8_t* Menu_icon_8, size_t size_Menu_icon_8,
const uint8_t* Menu_icon_9, size_t size_Menu_icon_9,
const uint8_t* bitmap_icons, size_t size_bitmap_icons,
const uint8_t* epd_bitmap_contour_selection_dashboard, size_t size_epd_bitmap_contour_selection_dashboard,
const uint8_t* Boot_img, size_t size_Boot_img){};
But i get these error message :
Compilation error: invalid application of 'sizeof' to incomplete type 'const uint8_t []' {aka 'const unsigned char []'}
What am I doing wrong??
How do you expect the compiler to know the size of the Menu_icon_x arrays?
With size_t I guess? I am only 14 y/o so I don't know a lot
(It is mostly code from ChatGPT)
It's too difficult to peering into what you're trying to do given the snippets you've posted. Please post a complete code, including all your source files and "images.h". Also, post GitHub to links any external libraries you may be using.
Most people here will tell you that any code you get from ChatGPT will be crap. As far as this thread goes, it's living up to that expectation.
And, I totally agree with them