Reopening files and memory managment

I am trying to implement a library which supports drawing different types of bitmaps, and later may have the functionality to draw compressed format images. The most simple example of a class which represents the Bitmap structure may look like this:

Bitmap.h

#include <SD.h>
class Bitmap{
public:
  Bitmap(const String& path);
   void fillBuffer();
private:
 File _root;
 //bitmap params(size, width, height etc...)
}

Bitmap.cpp

Bitmap::Bitmap(const String& path){
 _root = SD.open(path);
//read image header and instantiate parameters...
_root.close();
}

void Bitmap::fillBuffer(){

}

The problem is that now the _root file is closed after the bitmap header scan (for each Bitmap object the scan has to be automatic) , and there is no way that I know which let's me reopen the file exclude using SD.open(), but this function works with a file path and not the file itself, that means that SD.open(_root.name()) will not work if the file is not in a root directory(the "path/to/filename" will become just "filename" in this case).

of course I can store the path to the file separately but in this case there will be useless memory duplication in File member "name" and the path and the library results unoptimized.

any suggestions about this topic?

See if that helps:

P.S.: Sorry, I saw that it's not Arduino code.

Maybe this one can help:

that's a crude example on how you would copy that file into memory

This is C code, and I really can't understand what is going on there))

Still, thank you.

this example shows the base case of the algorithm - if the file size is less than the required number, than copy it, but if not do nothing. I want to make predefined boundaries and copy the bitmap by parts into small buffers, load the buffer to a display and than load the other parts of the bitmap as so.

Why do you close the file if you still using it?

I am closing it only when the I instantiated the parameters of the bitmap - in the object declaration

Bitmap bmp1(path1);
Bitmap bmp2(path2);

I do it because you can't have many files opened.

after it i have to draw them each to a display.

display.drawBitmap(bmp1, 0, 0) // cordinates
display.drawBitmap(bmp2, 50, 60) 

when I draw the bitmap i don't want the file to close until I finished drawing each individual bitmap

Maybe it's better to use some operating system, or maybe a board that already has an operating system, like Raspberry Pi.

From what I understand of SD card in Arduino, you have to open the file, read or write, and close. What you said about 'Instantiate' sounds to me like you're trying to create a pointer address that can be accessed directly without opening the file, and I believe it doesn't work that way.

Pi would be a great solution but my sd card fits in a tft shield display which I think fits on arduino and not on Pi.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.