Saving and writing to eeprom

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Saving and writing to eeprom

Postby tele_player » Mon Feb 12, 2018 4:11 pm

Esp32 EEPROM is in flash. So is SPIFFS. SPIFFS moves writes around to level the wear, EEPROM library doesn’t.

SD has the same problem with wear, and the same solution: wear leveling. Same thing applies to SSD.

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: Saving and writing to eeprom

Postby Duhjoker » Mon Feb 12, 2018 9:57 pm

Not gonna worry about how many writes and wipes the eeprom can do before burning out. The esp32 is sold at a reasonable rate so I can just replace it if I need to. Though I doubt ill make it to a hundred thousand writes/wipes.

Meanwhile I have a couple more structures to save to eeprom. This didn't work for some reason when the board is powered off and repowered. As long as the board is powered though it works. Meaning it works til powered off. Do I need to add read or something to the get part of the code since I have to use commit to actually save it to eeprom correctly?

Code: Select all

void setup() {
  while (!Serial && (millis() < 4000)) ;
  Serial.begin(115200);
  tft.begin();
  tft.setRotation(3);
  tft.fillScreen(BLACK);
  //tft.setFrameRate(60);
  tft.persistence = false;
  ////////////////////////////////////////////////////////
  ////////////////////////////////////////////////////////
  if (!ss1.begin(0x49)) {
    Serial.println("ERROR!");
    while (1);
  }
  if (!ss2.begin(0x4a)) {
    Serial.println("ERROR!");
    while (1);
  }
  else {
    Serial.println("seesaw started");
    Serial.print("version: ");
    Serial.println(ss1.getVersion(), HEX);
  }
  /////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////
  ss1.pinModeBulk(button_mask, INPUT_PULLUP);
  ss1.setGPIOInterrupts(button_mask, 1);
  pinMode(IRQ_PIN1, INPUT);
  /////////////////////////////////////////////////////////
  ss2.pinModeBulk(button_mask2, INPUT_PULLUP);
  ss2.setGPIOInterrupts(button_mask2, 1);
  pinMode(IRQ_PIN2, INPUT);
  /////////////////////////////////////////////////////////
  /////////////////////////////////////////////////////////
  if (!EEPROM.begin(EEPROM_SIZE))
  {
    Serial.println("failed to initialise EEPROM"); delay(1000000);
  }
  Serial.println(" bytes read from Flash . Values are:");
  for (int i = 0; i < EEPROM_SIZE; i++)
  {
    Serial.print(byte(EEPROM.read(i))); Serial.print(" ");
  }
  Serial.println();
  Serial.println("writing random n. in memory");

  tft.useFrameBuffer(use_fb);
}


struct Player
{
  int player_x;
  int player_y;
  int w;
  int h;
  int room;
  int cameraX;
  int cameraY;
  int player_direction;
  int player_directionRepeat;
};

  Player player = { 160, 170, 16, 16, 3, -128, -254, 2, 0};
  
  struct Slot {
  int slot_x;
  int slot_y;
  int slot_w;
  int slot_h;
  uint8_t itemId;
  uint8_t quantity;
};

Slot slots[NUMBER_OF_SLOTS] = {
  { 180, 16,  81, 16, SLOT_AVAILABLE},
  { 180, 34,  81, 16, SLOT_AVAILABLE},
  { 180, 52,  81, 16, SLOT_AVAILABLE},
  { 180, 70,  81, 16, SLOT_AVAILABLE},
  { 180, 88,  81, 16, SLOT_AVAILABLE},
  { 180, 106, 81, 16, SLOT_AVAILABLE},
  { 180, 124, 81, 16, SLOT_AVAILABLE},
  { 180, 142, 81, 16, SLOT_AVAILABLE},
  { 180, 160, 81, 16, SLOT_AVAILABLE},
  { 180, 178, 81, 16, SLOT_AVAILABLE},
  { 180, 196, 81, 16, SLOT_AVAILABLE},
  { 180, 214, 81, 16, SLOT_AVAILABLE},
};



void save()
{
  EEPROM.put(0, player);
  EEPROM.put(36, slots);
  EEPROM.commit();
 }

void load()
{
    EEPROM.get(0, player);
    EEPROM.get(36, slots);
 }
Last edited by Duhjoker on Tue Feb 13, 2018 2:15 am, edited 1 time in total.
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Saving and writing to eeprom

Postby tele_player » Tue Feb 13, 2018 1:53 am

I can’t tell what you are doing incorrectly from the partial code you include here.

What you should do is write a test program to learn how to save and restore using EEPROM. If you can’t make it work, provide the complete code here, maybe somebody will debug it.

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: Saving and writing to eeprom

Postby Duhjoker » Tue Feb 13, 2018 2:07 am

That's the problem I don't have an example to show me how to restore from eeprom. The given example is for writing to eeprom with the promise of a different sketch to restore it. I've googled the H out of it. The regular Arduino sketch helps but esp32 does it differently.

Its committing to eeprom while powered but not when turned off. What you see of the code is what I have. The structures for player and Slots are included as well as the save and load functions.
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

Duhjoker
Posts: 85
Joined: Mon Mar 20, 2017 8:09 am

Re: Saving and writing to eeprom

Postby Duhjoker » Thu Feb 15, 2018 7:01 am

Seriously??? I'm not trying to be smart but how am I supposed to write a short program to save and restore with out a reference on how its done??? This is not the first time I have asked. Seems every body clams up the question. Where exactly is the sketch to restore????? Its been a couple days. I was hoping for some kind of answer. Am I asking for too much?
Fear is the mind killer.......

GameR the DIY iot gaming device that does more......
https://github.com/Duhjoker/GameR-Iot_ESP

tele_player
Posts: 90
Joined: Sun Jul 02, 2017 3:38 am

Re: Saving and writing to eeprom

Postby tele_player » Thu Feb 15, 2018 11:09 pm

Seriously???

Save example:

Code: Select all

#include <Arduino.h>
#include <EEPROM.h>

// I'm just guessing EEPROM SIZE here
#define EEPROM_SIZE 1024

typedef struct foo {
  char a[16];
  char b[16];
} FOO;

FOO foo;


void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("");
  
  EEPROM.begin(EEPROM_SIZE);

  // put some data in foo
  strcpy(foo.a, "some text");
  strcpy(foo.b, "some more text");

  //save foo to EEPROM
  EEPROM.put( 0, foo);
  EEPROM.commit();

  Serial.println("data saved to EEPROM");


}

void loop() {
  ;
}
Restore example:

Code: Select all

#include <Arduino.h>
#include <EEPROM.h>

// I'm just guessing EEPROM SIZE here
#define EEPROM_SIZE 1024

typedef struct foo {
  char a[16];
  char b[16];
} FOO;

FOO foo;

void setup() {
  Serial.begin(115200);
  delay(1000);
  Serial.println("");
  
  EEPROM.begin(EEPROM_SIZE);

  // print foo. Empty, because it isn't restored from EEPROM yet
  Serial.printf("before restore: foo.a='%s' foo.b='%s'\n", foo.a, foo.b);

  // read from EEPROM into foo
  EEPROM.get(0, foo);

  // print foo. Now it has values from EEPROM
  Serial.printf("after restore: foo.a='%s' foo.b='%s'\n", foo.a, foo.b);

}

void loop() {
  ;
}

Who is online

Users browsing this forum: No registered users and 147 guests