Introducing Pokitto::Cookie
What is it?
Pokitto::Cookie is a way to store game data (highscores) etc. into the EEPROM in a way that makes sure you do not interfere with other programs.
What is EEPROM?
EEPROM is a small (4kB) memory inside the Pokitto chip that can be used to store small things, like highscore tables and Pokitto settings.
- EEPROM is inside the chip and works also when there is no SD card!
- Saving / loading a file from SD card is a different thing!
Why do we need this system?
The problem with using EEPROM is that if you have many games and apps, they can accidentally write data on top of each other. A management system ensures other apps do not interfere with your appās data and vice versa.
How does it work?
Users can create their own Cookies which contain the stuff they want to store. When the Cookie is saved, the data is written into the EEPROM. When the program restarts, the data is retrieved automatically from the EEPROM.
Details:
- 48 cookies maximum at a time
- each cookie has a key of 8 characters that is stored in the keytable
- one program can create several cookies if needed
- each cookie reserves at least 1 block of 32 bytes
- a cookie can extend to as many blocks as needed to store the data, but:
- there are exactly 112 blocks available (3584 bytes of data)
The system is already working very well, some minor bugs probably will still need to be ironed out
Demo binary of the system
savehi.bin (41.3 KB)
Source code in GitHub (not yet in mbed)
https://github.com/pokitto/PokittoLib/tree/master/Examples/SaveHighscore
Minimal usage example
#include "Pokitto.h"
#include "PokittoCookie.h"
Pokitto::Core mygame;
class mycookie : public Pokitto::Cookie {
public:
char initials[4] = {'A','D','A',0};
int score=10;
};
/* create instance */
mycookie highscore;
int main () {
highscore.begin("HISCTEST",sizeof(highscore),(char*)&highscore); //initialize cookie
mygame.begin(); //start game, only AFTER cookie is initialized
while (mygame.isRunning()) {
if (mygame.update()) {
/* display the random data */
mygame.display.setFont(font5x7);
mygame.display.color=1;
mygame.display.println("Highscore Cookie test\n");
mygame.display.color=2;
mygame.display.println("Random \"data\":\n");
mygame.display.color=3;
mygame.display.setFont(fontZXSpec);
mygame.display.fontSize=2;
mygame.display.print((const char*)highscore.initials);
mygame.display.print(" ");
mygame.display.println((int)highscore.score);
mygame.display.println("\n");
mygame.display.fontSize=1;
mygame.display.color=1;
mygame.display.println("\n\nPress A to generate random highscore\nand store it ");
/*generate random data on A button press*/
if (mygame.buttons.pressed(BTN_A)) {
//generate a random, but higher new highscore
highscore.initials[0] = random(65,90);
highscore.initials[1] = random(65,90);
highscore.initials[2] = random(65,90);
highscore.score += random(10);
strcpy(highscore.rank,playerrank[random(0,3)]);
highscore.playtime = (long)random(0,0xFFFF)*(long)random(0,0xFFFF);
// save new highscore
highscore.saveCookie();
}
} //update
} //isrunning
return 0;
} // main