Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Big block of macros to access EEPROM data
#include <avr/eeprom.h>
#define eeprom_read_to(dst_p, eeprom_field, dst_size) eeprom_read_block(dst_p, (void *)offsetof(__eeprom_data, eeprom_field), MIN(dst_size, sizeof((__eeprom_data*)0)->eeprom_field))
#define eeprom_read(dst, eeprom_field) eeprom_read_to(&dst, eeprom_field, sizeof(dst))
#define eeprom_write_from(src_p, eeprom_field, src_size) eeprom_write_block(src_p, (void *)offsetof(__eeprom_data, eeprom_field), MIN(src_size, sizeof((__eeprom_data*)0)->eeprom_field))
#define eeprom_write(src, eeprom_field) { typeof(src) x = src; eeprom_write_from(&x, eeprom_field, sizeof(x)); }
#define MIN(x,y) ( x > y ? y : x )
#define KEYPAD_SIZE 5
struct __eeprom_data {
int saved_keypad[KEYPAD_SIZE];
};
void setup()
{
int local_keypad[KEYPAD_SIZE] = {0};
// Load it up
eeprom_read(local_keypad, saved_keypad);
// Do some stuff, work with local_keypad as a local variable
// Save it back whenever you need to
eeprom_write_from(local_keypad, saved_keypad, sizeof(local_keypad));
}
@projectgus
Copy link
Author

projectgus commented Jul 29, 2010

(This code compiles but I haven't tested it, sorry!)

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment