olehs / arduino-edb

Automatically exported from code.google.com/p/arduino-edb
0 stars 0 forks source link

Error when loading data after reset #1

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

I modified the sample code somewhat to show what I was getting in my 
own sketch.  Below is the updated sample.

/*
 EDB_Simple.pde
 Extended Database Library + Internal Arduino EEPROM Demo Sketch

 The Extended Database library project page is here:

 */
#include "WProgram.h"
#include <EDB.h>

// Use the Internal Arduino EEPROM as storage
#include <EEPROM.h>

// Uncomment the line appropriate for your platform
#define TABLE_SIZE 512 // Arduino 168 or greater

// The number of demo records that should be created.  This should be 
less
// than (TABLE_SIZE - sizeof(EDB_Header)) / sizeof(LogEvent).  If it is 
higher,
// operations will return EDB_OUT_OF_RANGE for all records outside the 
usable range.
#define RECORDS_TO_CREATE 10

// Arbitrary record definition for this table.  
// This should be modified to reflect your record needs.
struct LogEvent {
  char something[50];
  char something2[8];
  int id;
  int temperature;
}
logEvent;

// The read and write handlers for using the EEPROM Library
void writer(unsigned long address, byte data)
{
  EEPROM.write(address, data);
}

byte reader(unsigned long address)
{
  return EEPROM.read(address);
}

// Create an EDB object with the appropriate write and read handlers
EDB db(&writer, &reader);

void setup()
{
  Serial.begin(9600);
  Serial.println("Extended Database Library + Arduino Internal EEPROM 
Demo");
  Serial.println();

  //db.create(0, TABLE_SIZE, sizeof(logEvent));
  db.open(0);
  Serial.print("Record Count: "); Serial.println(db.count());

  Serial.println("Creating Records...");
  int recno;

  Serial.print("Record Count: "); Serial.println(db.count());
  for (recno = 1; recno < RECORDS_TO_CREATE; recno++)
  {
    db.readRec(recno, EDB_REC logEvent);
    Serial.print("ID: "); Serial.println(logEvent.id);
    Serial.print("Temp: "); Serial.println(logEvent.temperature);  
    Serial.print("1: "); Serial.println(logEvent.something);
    Serial.print("2: "); Serial.println(logEvent.something2);  
  }
  db.clear();
  db.create(0, TABLE_SIZE, sizeof(logEvent));
  for (recno = 1; recno <= RECORDS_TO_CREATE; recno++)
  {
    logEvent.id = recno;
    logEvent.temperature = recno * 2;
    strcpy(logEvent.something, "some test data that is long");
    strcpy(logEvent.something2, "test2");
    db.appendRec(EDB_REC logEvent);
  }
  Serial.print("Record Count: "); Serial.println(db.count());
  for (recno = 1; recno < RECORDS_TO_CREATE; recno++)
  {
    db.readRec(recno, EDB_REC logEvent);
    Serial.print("ID: "); Serial.println(logEvent.id);
    Serial.print("Temp: "); Serial.println(logEvent.temperature);  
    Serial.print("1: "); Serial.println(logEvent.something);
    Serial.print("2: "); Serial.println(logEvent.something2);  
  }
}

void loop()
{
}

What is the expected output? What do you see instead?

Output for the first run:

Extended Database Library + Arduino Internal EEPROM Demo

Record Count: 0
Creating Records...
Record Count: 0
ID: 0
Temp: 0
1:
2:
ID: 0
Temp: 0
1:
2:
ID: 0
Temp: 0
1:
2:
ID: 0
Temp: 0
1:
2:
ID: 0
Temp: 0
1:
2:
ID: 0
Temp: 0
1:
2:
ID: 0
Temp: 0
1:
2:
ID: 0
Temp: 0
1:
2:
ID: 0
Temp: 0
1:
2:
Record Count: 8
ID: 1
Temp: 2
1: some test data that is long
2: test2
ID: 2
Temp: 4
1: some test data that is long
2: test2
ID: 3
Temp: 6
1: some test data that is long
2: test2
ID: 4
Temp: 8
1: some test data that is long
2: test2
ID: 5
Temp: 10
1: some test data that is long
2: test2
ID: 6
Temp: 12
1: some test data that is long
2: test2
ID: 7
Temp: 14
1: some test data that is long
2: test2
ID: 8
Temp: 16
1: some test data that is long
2: test2
ID: 8
Temp: 16
1: some test data that is long
2: test2

Output after the Reset is pressed.

Extended Database Library + Arduino Internal EEPROM Demo

Record Count: 8
Creating Records...
Record Count: 8
ID: 0
Temp: 25972
1:
2:
ID: 0
Temp: 25972
1: st2
2:
ID: 0
Temp: 25972
1: st2
2:
ID: 0
Temp: 25972
1: st2
2:
ID: 0
Temp: 25972
1: st2
2:
ID: 0
Temp: 25972
1: st2
2:
ID: 0
Temp: 25972
1: st2
2:
ID: 0
Temp: 25972
1: st2
2:
ID: 0
Temp: 25972
1: st2
2:
Record Count: 8
ID: 1
Temp: 2
1: some test data that is long
2: test2
ID: 2
Temp: 4
1: some test data that is long
2: test2
ID: 3
Temp: 6
1: some test data that is long
2: test2
ID: 4
Temp: 8
1: some test data that is long
2: test2
ID: 5
Temp: 10
1: some test data that is long
2: test2
ID: 6
Temp: 12
1: some test data that is long
2: test2
ID: 7
Temp: 14
1: some test data that is long
2: test2
ID: 8
Temp: 16
1: some test data that is long
2: test2
ID: 8
Temp: 16
1: some test data that is long
2: test2

What version of the product are you using? On what operating system?

I am on an at328 with 1k EEPROM.

Original issue reported on code.google.com by barryruf...@gmail.com on 20 Mar 2010 at 9:52

GoogleCodeExporter commented 9 years ago
In had the same problem and figured out how to fix it:

The function readHead() should also update the EDB_table_ptr, otherwise this 
one will be wrong and all record access after that has the wrong address:

// reads EDB_Header
void EDB::readHead()
{
  edbRead(EDB_head_ptr, EDB_REC EDB_head, (unsigned long)sizeof(EDB_Header));
  EDB_table_ptr = sizeof(EDB_Header) + EDB_head_ptr;
}

Like this it is the reverse of the code in create().

Original comment by martinha...@gmail.com on 8 Jun 2011 at 10:24

GoogleCodeExporter commented 9 years ago
On second thought, it should probably be the open function that be fixed. A 
patch is attached.

Original comment by martinha...@gmail.com on 9 Jun 2011 at 8:00

Attachments:

GoogleCodeExporter commented 9 years ago
Patch fixes the issue.

Original comment by mi...@nowlive.ro on 24 Dec 2011 at 6:08