I’m using the EEPROM quite a lot, and was just wondering if there’s any trick I could use to make things more concise.
is anything like this possible to do?
EEPROM.get(10, a, 20, b, 30, c, 40, d, 50, d);
You could use a for loop and arrays I think.
You could create a struct that has a, b, c, d, and e as members if that makes sense for what those letters represent. You can then get and put them with one statement.
A related question re best practices for electron emulated eeprom...I have a couple of structs, each with many variables taking up about 120 bytes that i want to read from eeprom on startup. 3 of the variabes are a eeprom timestamp, and flags for "use defaults instead" and "save to eeprom".
The docs say
Unlike "true" EEPROM, flash doesn't suffer from write "wear" with each write to each individual address. Instead, the page suffers wear when it is filled.
Q1) Is writing the entire struct object to eeprom better or worse than writing individual member changes? In other words, if eeprom.put() checks for changes before writing, does it check each member or byte before writing and just write the changed bytes, or does it write the whole 120 byte struct if one byte has changed? If I understand the docs, that would make a big difference in how fast the page fills and eeeprom wears.
Q2) If whole-struct writes are worse, what is an efficient way to handle a hundred configuration variables of various types?
I also need to make the variables visible in modbus registers via a union with a uint16_t array, so I need the variables in known positions relative to the start address.
The thing with flash is that you can only clear individual bits but never set individual bits. So whenever you want to change a single bit in the whole struct from 0 to 1 the whole struct needs to be moved to a new location anyway.
So the most effective approach would be to collect all the changes and only write the whole set of changes at once.