[Wiki] Using PokittoDisk (Saving & Loading)


#21

It don’t contains data. There’s a problem with the saving i guess.


#22

I think fileWriteBytes is broken. Better switch to SDFileSystem, if you can.


#23

Ok. I will try that.


#24

What is yhe data file like?


#25
#include "Pokitto.h"
#include "../Pokitto/POKITTO_LIBS/SDFileSystem/SDFileSystem.h"

Pokitto::Core game;

bool saved = false;
bool success = false;

uint16_t counter = 0;
uint16_t readByte = 0;
int reader = 0;
int helper = 0;

SDFileSystem sd(P0_9,P0_8, P0_6, P0_7,"sd");
FILE* fp;

int main()
{
    game.begin();

    if (!saved)
    {
        if (fp = fopen("/sd/data.txt", "wb")) success = true;
        while (counter <= 50)
        {
            fwrite(&counter, sizeof(uint16_t), 1, fp);
            counter++;
            game.display.print((int)counter);
            game.display.update();
        }
        fclose(fp);
        saved = true;
    }

    while (game.isRunning())
    {
        if (game.update())
        {
            if (game.buttons.released(BTN_A))
            {
                fp = fopen("/sd/data.txt", "rb");
                fseek(fp, reader * sizeof(uint16_t), SEEK_SET);
                fread(&readByte, sizeof(uint16_t), 1, fp);
                fclose(fp);
                reader++;
                helper++;
            }
            game.display.print(readByte);
            game.display.setCursor(0,20);
            game.display.print(reader);
            game.display.update();
        }
    }
    return 0;
}

Now using SDFileSystem. The display flickers and nothing happens, that’s not really better.


#26

Zuzu, start by telling what is it that you are trying to do. Then it is much easier to help.

STORYTIME !

When I was in school, I always went to the tool store keeper (a Mr. Malminen, great guy) to ask for tool X. Then he brought it to me. Before he handed it over the counter, he held on to it lingeringly and asked: “what are you trying to do?”. After I explained, he said “I know a better tool for doing that”, took tool X back to the shelf, brought me tool Y, and I went back to class with it. This happened dozens of times. And not only to me, I later heard the same story from all my classmates.


#27

not sure if this is helpfull but i recently did some saving for pokitbeast using pokittodisk

void save(){
    uint8_t px[2] = {player.x,player.y};
    if(!fileOpen("SAVE.TST", FILE_MODE_OVERWRITE)){
        fileWriteBytes(px,2);
        fileClose();
    }
}

void load(){
    uint8_t px[2];
    if(!fileOpen("SAVE.TST", FILE_MODE_READONLY)){
        fileReadBytes(px,2);
        player.x = px[0];
        player.y = px[1];
        fileClose();
    }
}

the file has to already exist on sd card since PokittoDisk cant create new files as far as i understand


#28

I’m trying to figure out how to write things to SD :joy:
I’ll need it later to safe game-data. This test program should put numbers on a file and read it afterwards.
It’s important that i can write every object data. Btw i’m testing the r/w speed also.


#29

There are 3 options for doing that:

  1. PetitFatFS (built in SD library, used in the loader)
  • fastest, smallest memory use
  • can not create, delete or resize files
  • save file must exist and be correct size (fill it zeroes or other dummy data)
  1. SDfileSystem (external mbed library)
  • can do pretty much anything including creating files
  • not as fast as PetitFatFS
  1. Pokitto::Cookie class (EEPROM cookies for small data, like highscores etc)
    -works without SD
    -very simple to use
    -data size is limited (I would say absolute max 512 bytes, if more use SD instead)

#30

I’m fine with the SDFileSystem, it’s perfect, if it would work for me. I’m don’t know why the code above does’nt work. I’ll debug it now. If anyone knows the issue please tell me :smile:


#31

First, check whether the file is actually being created on the SD card. Put it in an adapter and take a look at it. Use a hex editor to check the values in the file are what you intended.

Then begin debugging the other part


#32

The file is not created because it allready exists. It writes the bytes perfectly aswell. There’s a problem with the reading


#33

I don’t think this line is needed…


#34

@Zuzu36

Do a less extreme version of the reading test first. Before the isRunning loop, open the file once, read first 10 values into buffer. Dont seek, just read. Dont even close the filw

Then in the display update loop, println the 0…9 elements


#35

It seems like the pins at SDFileSystem sd(P0_9,P0_8, P0_6, P0_7,"sd"); are wrong. @jonne what are the exact pins for SD? These ones are somehow connected to the buttons…


#36

Did you also check this thread?

I found out that placing the SDFileSystem() command inside the main function solved the interference with the buttons…


#37

Thanks @sbmrgd !

Yes @Zuzu36 , this is the correct answer. Create the SDFileSystem.object after game.begin. The init process is interfering with the SDFileSystem object


#38

Thanks @jonne & @sbmrgd! Working fine now :blush: