Arduino Library for I2C EEPROM - 24LC256/64/32/16/08/04/02/01.
This library allows to interact with external I2C EEPROM in the style of the ESP32 Preferences.h library, avoiding the user having to manage the memory addresses.
#include <EEPROM_Preferences.h>
EEPROM_Preferences preferences(0x50, EEPROM_24LC256);
void setup() {
Serial.begin(115200);
preferences.begin();
preferences.writeBool("bool", true);
Serial.println(preferences.getBool("bool", false));
preferences.writeString("welcome", "Hello word");
Serial.println(preferences.getString("welcome", ""));
}
void loop() {
}
3.3V and 5V can be used!
EEPROM_Preferences(const uint8_t deviceAddress, const uint16_t deviceSize, TwoWire *wire = &Wire);
- Initializes the eeprom and configures the write protection pin if necessary
void begin(int8_t writeProtectPin = -1);
- Obtains a string through a key, if the key is not found in memory, it will return the default value indicated in the second parameter
char* getString(const char* key, const char* defaultValue);
- Obtains an int through a key, if the key is not found in memory, it will return the default value indicated in the second parameter
int getInt(const char* key, int defaultValue);
- Obtains an uint through a key, if the key is not found in memory, it will return the default value indicated in the second parameter
int getUInt(const char* key, int defaultValue);
- Obtains a float through a key, if the key is not found in memory, it will return the default value indicated in the second parameter
float getFloat(const char* key, float defaultValue);
- Obtains a bool through a key, if the key is not found in memory, it will return the default value indicated in the second parameter
bool getBool(const char* key, bool defaultValue);
- Writes a string to memory using the specified key
StatusCode writeString(const char* key, const char* value);
- Writes an int to memory using the specified key
StatusCode writeInt(const char* key, int32_t value);
- Writes an uint to memory using the specified key
StatusCode writeUInt(const char* key, uint32_t value);
- Writes a float to memory using the specified key
StatusCode writeFloat(const char* key, float value);
- Writes a bool to memory using the specified key
StatusCode writeBool(const char* key, bool value);
- Removes string record by value from memory and reallocate memory
StatusCode removeString(const char* key);
- Removes int record by value from memory and reallocate memory
StatusCode removeInt(const char* key);
- Removes uint record by value from memory and reallocate memory
StatusCode removeUInt(const char* key);
- Removes bool record by value from memory and reallocate memory
StatusCode removeBool(const char* key);
- Removes float record by value from memory and reallocate memory
StatusCode removeFloat(const char* key);
- Enable write protection mode on the memory
void enableWrite();
- Disable write protection mode on the memory
void disableWrite();
- Completely clears the memory (it takes some time to perform this operation)
bool freeEEPROM();
- Dumps register data from EEPROM, address should be multiple of 32
const char* dumpRecord(uint16_t address);
Code | Text |
---|---|
0 | No errors found |
1 | Memory is full |
2 | Key not found (only on remove) |
3 | Key length overflow (only on write) |
4 | Data length overflow (only on write) |
5 | Memory is write protected |
- Add support to all 24LCXXX memories (only tested with 24LC256)
Inspired by RobTillaart's I2C_EEPROM library (https://github.com/RobTillaart/I2C_EEPROM) and ESP32 Preferences library