Go Down

Topic: EEPROM - reads and writes key value pair (Read 339 times) previous topic - next topic

Diego67

Hello!

I am old but an Arduino beginner.

I wrote a header file to allow the insertion and search in EEPROM of values using a numeric key or a char array key. The key is written together with the value in a B-Tree structure. In this way, bytes are wasted, but in some situations it may be useful.

Char arrays are stored in the EEPROM up to the value '\ 0' even if they are longer.

  the main commands are:
       DS_put (key, value); Writes a key / value pair
     DS_get (key, value); reads the value by supplying the key

the key can be a 16-bit integer or a char array
the value must be a char array

      DS_format ();  It deletes the EEPROM  and writes the first usable address to position 0.

I provide below a test code and enclose the header file.

Code: [Select]


#include <stdio.h>
#include "DS_EEProm_New.h"

void setup() {
  Serial.begin(9600); 
}


void ReadStringFromSerial(char searchedString[]){
  while(!Serial.available()){delay(20);}
  byte j=0;
   while(Serial.available())
    {
      searchedString[j] = Serial.read();
      ++j;
      searchedString[j] = '\0'; 
    }
   //return searchedString;
  }


void loop() {

  char searchedString[100];
  char keyString[50];
  char valueString[100];
  char command[5];
 
  Serial.println("-----------------Insert command-----------------");
  Serial.println("Format EEPROM                           ----->  f");
  Serial.println("List EEPROM byte                        ----->  l");
  Serial.println("Write int Key int and *char Value       ----->  wi");
  Serial.println("Write *char Key int and *char Value     ----->  ws");
  Serial.println("Search Value with int Key               ----->  ri");
  Serial.println("Search Value with *char Key             ----->  rs");

  ReadStringFromSerial(command);
  if (strcmp(command,"f")==0)
    {   
      Serial.println("Format begin");
      DS_format();
      Serial.println("Format end");
    }
  else if (strcmp(command,"l")==0)
    {
      uint8_t val;
      for (int cont=0; cont<100; cont++)
      {
        val=eeprom_read_byte(cont);
        Serial.print(cont,DEC);
        Serial.print("-->");
        Serial.println(val,DEC);
      }
    }
  else if (strcmp(command,"wi")==0)   
    {
      Serial.println("Insert Key");
      ReadStringFromSerial(keyString);
      Serial.println("Insert Value");
      ReadStringFromSerial(valueString);
      DS_put(atoi(keyString),valueString);       
    }
  else if (strcmp(command,"ws")==0)   
    {
      Serial.println("Insert Key");
      ReadStringFromSerial(keyString);
      Serial.println("Insert Value");
      ReadStringFromSerial(valueString);
      DS_put(keyString,valueString);             
    }
  else if (strcmp(command,"ri")==0)   
    {
      Serial.println("Insert Key");
      ReadStringFromSerial(keyString);     
      if (DS_get(atoi(keyString),searchedString)==1)
        {
          Serial.print(atoi(keyString));
          Serial.print(" ------------> ");
          Serial.println(searchedString);
        }     
    }
  else if (strcmp(command,"rs")==0)   
    {
      Serial.println("Insert Key");
      ReadStringFromSerial(keyString);     
      if (DS_get(keyString,searchedString)==1)
        {
          Serial.print(keyString);
          Serial.print(" ------------> ");
          Serial.println(searchedString);
        }         
     
    }     

  delay(1000);               
               
}


Diego67

I would like to understand why I can not find comments:

Is it useless?
Is the process unclear?
Does it already exist done better?
Is everything wrong?

Thanks for collaboration!

GolamMostafa

#2
May 20, 2018, 07:51 pm Last Edit: May 20, 2018, 07:53 pm by GolamMostafa
I read your post on the very first day of the post; however, the contents of the post was not clear to me. If so, why did I not inquire you that day -- this is one of the serious difficulties of a human beings that he is not always inspired! The following things are not clear to me:

1.  Are you talking about internal EEPROM of the ATmega328P or external EEPROM line 24C32/24C512?

2.  If you are talking about the internal EEPROM, what is the problem/limitation of the current Arduino's EEPROM.h Library that you have to come forward with something new/better?



Diego67

#3
May 20, 2018, 09:22 pm Last Edit: May 21, 2018, 08:58 am by Diego67 Reason: error write
First of all thanks for your post GolamMostafa.

1. Yes, I'm talking about the internal EEPROM of ATmega328P.
2. The purpose of my work is to eliminate both writing and reading the use of physical addresses and to use char arrays or int(16bit) as search keys.

For example, I would like to use it to dynamically store in the eeprom, strings read from the serial and then display on a display depending on the particular conditions of the Arduino inputs.
Different solution for this problem would be the use of EEMEM, but we must know how many will be and also must have a length, defined at the beginning, such as to contain the longest of the strings. If the strings are shorter, the space is wasted.

Go Up