Skip to content

Add get and put functions to EEPROM #80

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 19, 2015
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,27 @@ class EEPROMClass
void commit();
void end();

template<typename T> T &get(int address, T &t)
{
if (address < 0 || address + sizeof(T) > _size)
return t;

uint8_t *ptr = (uint8_t*) &t;
for(int count = 0; count < sizeof(T); ++count) *ptr++ = _data[address + count];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we just use memcpy(ptr, _data + address, count); here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated accordingly.

return t;
}

template<typename T> const T &put(int address, const T &t)
{
if (address < 0 || address + sizeof(T) > _size)
return t;

const uint8_t *ptr = (const uint8_t*) &t;
for(int count = 0; count < sizeof(T); ++count) _data[address + count] = *ptr++;
_dirty = true;
return t;
}

protected:
uint8_t* _data;
size_t _size;
Expand Down