Skip to content

Instantly share code, notes, and snippets.

@projectgus
Created July 29, 2010 23:44
Show Gist options
  • Save projectgus/499528 to your computer and use it in GitHub Desktop.
Save projectgus/499528 to your computer and use it in GitHub Desktop.
// 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

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

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