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 all 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
22 changes: 22 additions & 0 deletions hardware/esp8266com/esp8266/libraries/EEPROM/EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <stddef.h>
#include <stdint.h>
#include <string.h>

class EEPROMClass
{
Expand All @@ -35,6 +36,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;
memcpy(ptr, _data + address, sizeof(T));
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;
memcpy(_data + address, ptr, sizeof(T));
_dirty = true;
return t;
}

protected:
uint8_t* _data;
size_t _size;
Expand Down