EEPROM library (AVR) and use of EERef Class is undocumented

Today I was digging into the EEPROM.h library to see if there’s a neat way of reading/writing a word instead of just a single byte - there appears not to be. However, what there DOES appear to be is a whole slew of assignment operators…

    //Assignment/write members.
    EERef &operator=( const EERef &ref ) { return *this = *ref; }
    EERef &operator=( uint8_t in )       { return eeprom_write_byte( (uint8_t*) index, in ), *this;  }
    EERef &operator +=( uint8_t in )     { return *this = **this + in; }
    EERef &operator -=( uint8_t in )     { return *this = **this - in; }
    EERef &operator *=( uint8_t in )     { return *this = **this * in; }
    EERef &operator /=( uint8_t in )     { return *this = **this / in; }
    EERef &operator ^=( uint8_t in )     { return *this = **this ^ in; }
    EERef &operator %=( uint8_t in )     { return *this = **this % in; }
    EERef &operator &=( uint8_t in )     { return *this = **this & in; }
    EERef &operator |=( uint8_t in )     { return *this = **this | in; }
    EERef &operator <<=( uint8_t in )    { return *this = **this << in; }
    EERef &operator >>=( uint8_t in )    { return *this = **this >> in; }
    
    EERef &update( uint8_t in )          { return  in != *this ? *this = in : *this; }
    
    /** Prefix increment/decrement **/
    EERef& operator++()                  { return *this += 1; }
    EERef& operator--()                  { return *this -= 1; }
    
    /** Postfix increment/decrement **/
    uint8_t operator++ (int){ 
        uint8_t ret = **this;
        return ++(*this), ret;
    }

    uint8_t operator-- (int){ 
        uint8_t ret = **this;
        return --(*this), ret;
    }

(edit: trimmed down the block so it’s more obvious for people to READ THE ABOVE CODE BLOCK as it’s the entire purpose of this thread)

It seems like the library has a setup to utilize EEPROM space similar to variables… but there aren’t any examples and I can’t understand these “structs” to figure out how to make use of this functionality.

For example, I’m guessing I can do something like this:

#include <EEPROM.h>

EEPtr eMyVariable(2);

void loop() {
  uint8_t spot;
  spot = eMyVariable;
  spot++;
  eMyVariable = spot;
  while (1) ;
}

… and without ever touching “EEPROM.read()” or “EEPROM.write()”, I’ve read the value, incremented it (with extra steps), and written it back to EEPROM. But I am completely making up that initial declaration syntax out of my arse because that’s the entire subject here - I don’t know how to call it up.

EEPtr is another part of it but that seems less useful :slight_smile:

Google gave me nothing but copies of the eeprom.h source file itself when I try looking for “EERef” so I think this is worth documenting somewhere. The Arduino docs - Arduino - EEPROM - say nothing about this functionality.

Do you have a question?

put() and get() methods work with any datatype.

gfvalvo:
Do you have a question?

I thought it was pretty obvious that I'm looking for documentation on this part of the EEPROM.h library that is beyond just "get and put".

These entire sections of the .h file aren't documented anywhere... no idea how they work, no examples on how to use them.

How do you use them?

More to the point, these appear to be ways of working with EEPROM values as if they were variables, so how does one declare such a variable to make use of this feature?

Today I was digging into the EEPROM.h library to see if there’s a neat way of reading/writing a word instead of just a single byte - there appears not to be.

I’m looking for documentation on this part of the EEPROM.h library that is beyond just “get and put”.

As has been pointed out, put() and get() work with any data type of any. Are you confusing them with write() and read() which only deal with single bytes ?

FalconFour:
How do you use them?

First two Google hits for "arduino eeprom library":

https://www.arduino.cc/en/Reference/EEPROMhttps://www.arduino.cc/en/Reference/EEPROM

avr-libc: <avr/eeprom.h>: EEPROM handling might have what you're looking for.

Wow, for the third time, I'm talking about an undocumented function of the library - and even in OP, I pasted the portion of EEPROM.h that is of interest. Please go back to the first post and read the contents of it. There is a code block in there that is exactly what I'm interested in.

"get() and put()" aren't even relevant here at all. Nothing involving function calls at all. What I'm trying to understand is the part of EEPROM.h that defines a bunch of operators, as if you can manipulate EEPROM values simply by assigning a variable.

Can y'all please read OP before jumping to "hurr durr do you not know how EEPROM.h works? you just eeprom.read() and eeprom.write() euhhh" like I'm a freaking moron?

dougp:
avr-libc: <avr/eeprom.h>: EEPROM handling might have what you're looking for.

No, it doesn't, because those are all avr-libc references and EEPROM.h is Arduino specific. Nothing I'm asking about is a function call.

gfvalvo:
First two Google hits for "arduino eeprom library":
https://forum.arduino.cc/index.php?topic=312645.0
https://www.arduino.cc/en/Reference/EEPROMhttps://www.arduino.cc/en/Reference/EEPROM

Not reading anything I wrote, eh? Might want to apply to work for eBay or Amazon customer service, they take people with bad reading comprehension like that. You really think I need help finding the Arduino EEPROM reference page, which I said DOES NOT HAVE documentation for these functions in the code?

Jesus, I thought this would be a quick and easy answer for someone that could read the code, but just finding anyone that can even read the EEPROM.h file seems to be hard to find...

FalconFour:
Today I was digging into the EEPROM.h library to see if there's a neat way of reading/writing a word instead of just a single byte - there appears not to be.

You might want to edit that bit as well, most people reading the start of your original message will be thinking why is put() not a neat way of writing a word with the EEPROM.h library ?

the description was explained higher up on the code:

/***
    EERef class.

    This object references an EEPROM cell.
    Its purpose is to mimic a typical byte of RAM, however its storage is the EEPROM.
    This class has an overhead of two bytes, similar to storing a pointer to an EEPROM cell.
***/

struct EERef{

    EERef( const int index )
        : index( index )                 {}

    //Access/read members.
    uint8_t operator*() const            { return eeprom_read_byte( (uint8_t*) index ); }
    operator const uint8_t() const       { return **this; }

If you want to use the 'EERef' and 'EEPtr' structs outside of .put() and .get(), study the way those two functions use them.".

gfvalvo:
If you want to use the 'EERef' and 'EEPtr' structs outside of .put() and .get(), study the way those two functions use them.".

A simple example. Note how the printed values increment every time the board is reset.

#include "Arduino.h"
#include "EEPROM.h"

void setup() {

 EEPtr ptr1(0);
 EERef var1(1);

 Serial.begin(115200);
 delay(1000);

 Serial.print("Value stored at EEPROM Location 0 = ");
 Serial.println(*ptr1);
 (*ptr1)++;

 Serial.print("Value stored at EEPROM Location 1 = ");
 Serial.println(var1);
 var1++;
}

void loop() {
}

srnet:
You might want to edit that bit as well, most people reading the start of your original message will be thinking why is put() not a neat way of writing a word with the EEPROM.h library ?

More apropos advice - Take the chip off your shoulder (i.e. lose the bad attitude) when asking for free help.

gfvalvo:
A simple example. Note how the printed values increment every time the board is reset.

#include "Arduino.h"

#include "EEPROM.h"

void setup() {

EEPtr ptr1(0);
EERef var1(1);

Serial.begin(115200);
delay(1000);

Serial.print("Value stored at EEPROM Location 0 = ");
Serial.println(*ptr1);
(*ptr1)++;

Serial.print("Value stored at EEPROM Location 1 = ");
Serial.println(var1);
var1++;
}

void loop() {
}

Lordy, thank you. This is the subject of the post, nothing else. Every reply prior to this had ignored this subject or mis-diagnosed the clearly stated case. I return respect when it's given, and I mirror disrespect the same. Earlier replies were very disrespectful, thinking I'm stupid for not reading (non-existent) docs on this subject. They might notice that EERef and EEPtr are entirely undocumented... but they couldn't read past "EEPROM.h" and thinking it's durr-durr basic.

The declaration syntax, "EEPtr ptr1(0)" and "EERef var1(1)" is what I didn't know about.

So, you can declare EEPROM variables with this neat new system, that will automatically read and write EEPROM as if it were a standard RAM variable. That's pretty nifty.

Thank you. :grin:

Earlier replies were very disrespectful, thinking I’m stupid for not reading (non-existent) docs on this subject.

At least one earlier reply pointed out that your statement

I was digging into the EEPROM.h library to see if there’s a neat way of reading/writing a word instead of just a single byte - there appears not to be.

was wrong which I felt important to point out whatever the subject of the thread