Skip to content

Commit

Permalink
Add update() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
JChristensen committed Jan 15, 2022
1 parent d418ccf commit 11562bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
26 changes: 22 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Arduino EEPROM Library
+++++# Arduino EEPROM Library
http://github.com/JChristensen/JC_EEPROM
README file

Expand All @@ -25,6 +25,8 @@ The **JC_EEPROM Library** has been tested with:
- Microchip 24LC256 (256k bit)
- Microchip 24FC1026 (1M bit, thanks to Gabriele B on the Arduino forum)
- ST Micro M24M02 (2M bit)
- Atmel AT24C256C (thanks to searobin)
- Microchip 24C16B, 24C32 (thanks to Diogko)

The **JC_EEPROM Library** will **NOT** work with Microchip 24xx1025 as its control byte does not conform to the following assumptions.

Expand Down Expand Up @@ -108,7 +110,7 @@ if ( i2cStat != 0 ) {
//there was a problem
}
```
### write(unsigned long addr, byte *values, unsigned int nBytes)
### write(unsigned long addr, byte* values, unsigned int nBytes)
##### Description
Write one or more bytes to external EEPROM.
##### Syntax
Expand Down Expand Up @@ -149,11 +151,11 @@ Same as multiple-byte write() above.
//write the value 16 to EEPROM location 314.
byte i2cStat = myEEPROM.write(314, 16);
```
### read(unsigned long addr, byte *values, unsigned int nBytes)
### read(unsigned long addr, byte* values, unsigned int nBytes)
##### Description
Reads one or more bytes from external EEPROM into an array supplied by the caller.
##### Syntax
`myEEPROM.read(unsigned long addr, byte *values, byte nBytes);`
`myEEPROM.read(unsigned long addr, byte* values, byte nBytes);`
##### Parameters
**addr** *(unsigned long)*: The beginning EEPROM location to read from.
**values** _(byte*)_: Pointer to an array to receive the data.
Expand Down Expand Up @@ -203,3 +205,19 @@ else {
//data read ok
}
```

### update(unsigned long addr, byte value)
##### Description
Updates a single byte in external EEPROM. Like `write(addr, value)` except first reads the location from EEPROM and only writes `value` if it differs from the current value stored at the given location, to reduce wear on the EEPROM.
##### Syntax
`myEEPROM.update(unsigned long addr, byte value);`
##### Parameters
**addr** *(unsigned long)*: The EEPROM location to update.
**values** _(byte)_: The value to write.
##### Returns
Same as multiple-byte write() above.
##### Example
```c++
// update the value in EEPROM location 314 to 16.
byte i2cStat = myEEPROM.update(314, 16);
```
8 changes: 6 additions & 2 deletions src/JC_EEPROM.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,22 @@ class JC_EEPROM
};

// I2C clock frequencies
enum twiClockFreq_t { twiClock100kHz = 100000, twiClock400kHz = 400000 };
enum twiClockFreq_t
{ twiClock100kHz = 100000, twiClock400kHz = 400000 };

// EEPROM addressing error, returned by write() or read() if
// upper address bound is exceeded
static const uint8_t EEPROM_ADDR_ERR {9};

JC_EEPROM(eeprom_size_t deviceCapacity, byte nDevice, unsigned int pageSize, byte eepromAddr = 0x50);
JC_EEPROM(eeprom_size_t deviceCapacity, byte nDevice,
unsigned int pageSize, byte eepromAddr = 0x50);
byte begin(twiClockFreq_t twiFreq = twiClock100kHz);
byte write(unsigned long addr, byte* values, unsigned int nBytes);
byte write(unsigned long addr, byte value);
byte read(unsigned long addr, byte* values, unsigned int nBytes);
int read(unsigned long addr);
byte update(unsigned long addr, byte value)
{return (read(addr) == value) ? 0 : write(addr, &value, 1); }

private:
uint8_t _eepromAddr; // eeprom i2c address
Expand Down

0 comments on commit 11562bd

Please sign in to comment.