Save 4D array in files on SD card

Hello! I am making a arduino project were i need to store data. The data doesn't have to be loaded in the program all the time, only one 4D array is needed at one time. I want the other 4D array's to be stored on a SD card and make the arduino open the right one when it is needed. (It knows the name of the files so i just open the correct file). My question, is there a easy way to store a 4D array to a file on a SD card and open it when needed? It is going to be a bool array of size [?][8][8][8]. If possible the ? would be left open and the SD card would have a nother file telling how long every array is. Since it is a bool array i would also be able to make a byte array of size [?][8][8] if that is easyer to impliment.
Someone knows how to do this?

The standard way would be to loop over each item (in this case three nested for-loops are needed when you choose to use bytes instead of booleans). Alternatively you could cast the array to a one dimensional byte array and write this to the SD.

The size of the array needs to be known at compile time. If this is not possible, then perhaps you can think about using vectors or use malloc/realloc/free to allocate the required memory dynamically.

[edit]

The following will probably work, but it may not work with all compilers.

size_t s = 10;
uint8_t arr[s][8][8];

What is the upper limit to this? Can it fit in memory?

I can fit one array in the memory, but in my project i use multiple. I only use 1 array at i time, so i need a function to load an array form a SD card

4 nested for loops could be used to write the data to the file and to read it back in the same in the same order.

The same destination array would be used for any source array and need to be large enough to hold the largest source array, but you don't have to use all of it for a smaller array once it is read back

I've put together some code which stores boolean values as if in a 4D array. It's stored as individual bits in an unsigned 8 bit integer to save memory, if more than 8 'w' values is necessary some changes should be made. The MyThing structure will always be the same size, but contains the size field to keep track of the amount of x values. The required memory will be 1 + MAX_X_VALUES * Y_VALUES * Z_VALUES.

#define MAX_X_VALUES 8
#define Y_VALUES 8
#define Z_VALUES 8
#define W_VALUES 8
#define OUT_OF_BOUNDS -1
#define OK 0

typedef uint8_t grid3_t[MAX_X_VALUES][Y_VALUES][Z_VALUES];

struct MyThing {
    uint8_t size;

    union {
        grid3_t grid;
        uint8_t data[sizeof(grid3_t)];
    };
};

static int getValue(struct MyThing *myThing, uint8_t x, uint8_t y, uint8_t z, uint8_t w) {

    if (x > myThing->size || y > Y_VALUES || z > Z_VALUES || w > W_VALUES) {
        return OUT_OF_BOUNDS;
    }

    return myThing->grid[x][y][z] & (1 << w);
}

static int setValue(struct MyThing *myThing, uint8_t x, uint8_t y, uint8_t z, uint8_t w, int value) {

    if (x > myThing->size || y > Y_VALUES || z > Z_VALUES || w > W_VALUES) {
        return OUT_OF_BOUNDS;
    }

    if (value) {
        myThing->grid[x][y][z] |= (1 << w);
    } else {
        myThing->grid[x][y][z] &= ~(1 << w);
    }

    return OK;
}

static int writeCard(struct MyThing *myThing) {
    // Write to card here, the raw data is accessible in myThing->data

    // If successful return true
    return 1;
}

static int readCard(struct MyThing *myThing) {
    // Read from card here into myThing->data
    // Put number of xValues into myThing->size

    // If successful return true
    return 1;
}

The code can be used as follows:

First read the array from the card (this you will have to implement yourself).

struct MyThing myThing;
if (readCard(&myThing)) {
	// readCard returned true, a valid array should be in 'myThing'
}

Set values in the array like this:

if (setValue(&myThing, 1, 0, 1, 0, 1) == OUT_OF_BOUNDS) {
	// Handle error here
}

Get values from the array like this:

int value = getValue(&myThing, 1, 0, 1, 0);
if (value != OUT_OF_BOUNDS) {
	// Do something with 'value'
}

Save the array like this:

if (!writeCard(&myThing)) {
	// Handle error
}

I don't know your experience level and I get that this might not be the most beginnerfriendly code (and written quickly), so let me know if you need any clarification.

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