Page 3 of 3

Re: Saving and writing to eeprom

Posted: Mon Feb 12, 2018 4:11 pm
by tele_player
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.

Re: Saving and writing to eeprom

Posted: Mon Feb 12, 2018 9:57 pm
by Duhjoker
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);
 }

Re: Saving and writing to eeprom

Posted: Tue Feb 13, 2018 1:53 am
by tele_player
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.

Re: Saving and writing to eeprom

Posted: Tue Feb 13, 2018 2:07 am
by Duhjoker
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.

Re: Saving and writing to eeprom

Posted: Thu Feb 15, 2018 7:01 am
by Duhjoker
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?

Re: Saving and writing to eeprom

Posted: Thu Feb 15, 2018 11:09 pm
by tele_player
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() {
  ;
}