SDFileSystem not working on EmBitz? [SOLVED]


#1

Hey everyone -
I’ve been working on adding save functionality to Blockitto and I think I might’ve stumbled upon a bug. When I try to save and load save data nothing seems to be saved. Either there’s a bug in the library or a bug in EmBitz, as I’m fairly certain my code isn’t faulty seeing as it works on PC.

Here’s my code for saving and loading in case it’s of any use, ignore the strange indentation as that’s just because the original code is indented in a function (POK_HW is defined in the build options for my EmBitz because I thought it would fix the bug, will remove it later):

Save
std::cout << "Saved chunk at x=" << x << ", y=" << y << std::endl;
    #ifdef POK_SIM
        std::string sdpath = "C:/Users/kaden/Documents/Pokitto/My Projects/Blockitto/PokittoLib/Games/Blockitto/sd/";
    #elif defined POK_HW
        std::string sdpath = "/sd/";
    #endif // POK_SIM
    std::string cdir = sdpath + "blockitto";
    if (opendir(cdir.c_str())==0) {
        #ifdef POK_SIM
            mkdir(cdir.c_str());
        #elif defined POK_HW
            mkdir(cdir.c_str(),0777);
        #endif // POK_SIM
    };
    std::string filepath = sdpath + "blockitto/chunk" + patch::to_string(x) + "x" + patch::to_string(y) + ".data";
    FILE *fout = fopen(filepath.c_str(), "wb");
    if (fout == NULL) {// error

    } else {
        // success, write!
        int8_t surfaceBuffer[30];
        for (int p1=0; p1<5; p1++) {
            for (int p2=0; p2<6; p2++) {
                surfaceBuffer[p1*6+p2] = ground[p1][p2];
            }
        }
        fwrite (surfaceBuffer,sizeof(int8_t),30,fout);
        int32_t TESize[1] = {tients.size()};
        fwrite (TESize,sizeof(int32_t),1,fout);
        int32_t theTETrinity[3];
        for (int i=0; i<tients.size(); i++) {
            theTETrinity[0] = tients[i].getX();
            theTETrinity[1] = tients[i].getY();
            theTETrinity[2] = tients[i].getType();
            fwrite (theTETrinity,sizeof(int32_t),3,fout);
        }
        fclose(fout);
    }
Load
bool loadThis = false;
    #ifdef POK_SIM
        std::string sdpath = "C:/Users/kaden/Documents/Pokitto/My Projects/Blockitto/PokittoLib/Games/Blockitto/sd/";
    #elif defined POK_HW
        std::string sdpath = "/sd/";
    #endif // POK_SIM
    std::string cdir = sdpath + "blockitto";
    if (opendir(cdir.c_str())==0) {
        loadThis = false;
    } else {
        std::string filepath = sdpath + "blockitto/chunk" + patch::to_string(x) + "x" + patch::to_string(y) + ".data";
        FILE *fload = fopen(filepath.c_str(), "rb");
        if (fload == NULL) {
            loadThis = false;
        } else {
            loadThis = true;
            int8_t initBlocks[30];
            fread(initBlocks,sizeof(int8_t),30,fload);
            for (int p1=0; p1<5; p1++) {
                for (int p2=0; p2<6; p2++) {
                    ground[p1][p2] = initBlocks[p1*6+p2]; // load surface blocks
                }
            }
            int32_t numTEs[1];
            fread(numTEs,sizeof(int32_t),1,fload); // store number of tile entities
            int32_t inputTETrinity[3];
            for (int i=0; i<numTEs[0]; i++) {
                fread(inputTETrinity,sizeof(int32_t),3,fload);
                tients.emplace_back(inputTETrinity[0],inputTETrinity[1],inputTETrinity[2]);
            }
            fclose(fload);
        }
    }
    if (!loadThis) {
        // chunk not saved yet (new chunk), use procgen to make it
        int ax, ay;
        int tempTE;
        for (int i=0; i<5; i++) {
            for (int j=0; j<6; j++) {
                ax = x*6+j;
                ay = y*5+i;
                ground[i][j] = TerrainGen::getGroundAt(ax,ay); // same generation system as default constructor
                tempTE = TerrainGen::getTEAt(ax,ay);
                if (!(tempTE == -1)) {
                    tients.emplace_back(ax,ay,tempTE);
                }
            }
        }
    }

Unless the file read/write functions aren’t going to the correct path I have no idea why it isn’t working. Tested on both the current text loader and the WIP graphical loader.


#2
#include "SDFileSystem.h"
//init SDFileSystem
SDFileSystem sd(P0_9, P0_8, P0_6, P0_7, "sd");

mkdir("/sd/blockitto", 0777); //no clue what t the 777 means but it has to be there to work

FILE *fp = fopen("/sd/blockitto/FILENAME.TXT", "r+b");

this is code from my old games but i hit a bug where after loading the file a couple times the hole system just locks up, i do not recomend using SDFileSystem and just stick with pokittoDisk instead

also not sure if this helps, but i spoted this sd.disk_sync();in my old save and load code no idea if it does anything


#3

Well at least until @FManga fixes the SDFS I know he was looking into it because of some card compatibility issues


#4

I guess I wasn’t initializing SDFileSystem, that must be it. Thanks


#5

oh lol yea says that on mbed you should do that XD

also have a look at the header file of SDFileSystem there some things there you might find usefull like:

    SDFileSystem(PinName mosi, PinName miso, PinName sclk, PinName cs, const char* name);
    int disk_initialize();
    int disk_status();
    int disk_read(uint8_t* buffer, uint32_t block_number, uint32_t count);
    int disk_write(const uint8_t* buffer, uint32_t block_number, uint32_t count);
    int disk_sync();
    uint32_t disk_sectors();


#6

#7

I understand this is a bug in mbed, not SDFileSystem. To get around it, don’t use fopen/fclose/fread, call the equivalent functions directly in SDFileSystem.


#8

Huh, wait Wich is equivalent to fopen on SdFilrSystem?


#9

Have a look here for some commonly-used equivalents.