[Wiki] Using PokittoDisk (Saving & Loading)

#include "Pokitto.h"

Pokitto::Core game;

bool saved = false;
bool success = false;

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

int main()
{
    game.begin();

    while (game.isRunning())
    {
        if (game.update())
        {
            if (!saved)
            {
                if (fileOpen("data.txt", FILE_MODE_READWRITE)) success = true;
                while (counter <= 50)
                {
                    fileSetPosition(sizeof(uint16_t) * counter);
                    fileWriteBytes((uint8_t*)&counter, sizeof(uint16_t));
                    counter++;
                    game.display.print((int)counter);
                    game.display.update();
                }
                fileClose();
                saved = true;
            }
            else
            {
                if (game.buttons.released(BTN_A))
                {
                    fileOpen("data.txt", FILE_MODE_READONLY);
                    fileSetPosition(sizeof(uint16_t) * reader);
                    fileReadBytes((uint8_t*)&readByte, sizeof(uint16_t));
                    fileClose();
                    reader++;
                    helper++;
                }
                game.display.print(readByte);
                game.display.setCursor(0,20);
                game.display.print(success);
            }
        }
    }
    return 0;
}

I can’t figure out what mistake I made. It’s just showing a 0 every time. Any ideas?

Zero bytes in reading? Have you checked that the file in sd contains data?

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

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

Ok. I will try that.

What is yhe data file like?

#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.

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.

1 Like

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

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.

1 Like

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)
2 Likes

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:

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

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

I don’t think this line is needed…

@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

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…

Did you also check this thread?

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

3 Likes

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

1 Like

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

2 Likes