Skip to content

Commit

Permalink
simplify begin()
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Nov 24, 2023
1 parent 54cf6be commit 172f294
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 76 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [1.8.0] - 2023-11-24 (breaking change)
- simplify **begin()**, remove setting Wire pins from library.
- add **getAddress()**
- update readme.md


## [1.7.4] - 2023-09-06
- solve #57 add support for WriteProtectPin
- add writeProtectPin as optional parameter in **begin()**
Expand All @@ -18,7 +24,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- update keywords.txt
- update readme.md


## [1.7.3] - 2023-05-10
- fix #55 ==> redo fix #53
- add test to detect **MBED** and **RP2040**
Expand Down
55 changes: 7 additions & 48 deletions I2C_eeprom.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: I2C_eeprom.cpp
// AUTHOR: Rob Tillaart
// VERSION: 1.7.4
// VERSION: 1.8.0
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
// URL: https://github.com/RobTillaart/I2C_EEPROM.git

Expand Down Expand Up @@ -53,56 +53,9 @@ I2C_eeprom::I2C_eeprom(const uint8_t deviceAddress, const uint32_t deviceSize, T
}


#if defined(ESP8266) || defined(ESP32)

bool I2C_eeprom::begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin)
{
// if (_wire == 0) Serial.println("zero"); // test #48
if ((sda < 255) && (scl < 255))
{
_wire->begin(sda, scl);
}
else
{
_wire->begin();
}
_lastWrite = 0;
_writeProtectPin = writeProtectPin;
if (_writeProtectPin >= 0)
{
pinMode(_writeProtectPin, OUTPUT);
preventWrite();
}
return isConnected();
}

#elif defined(ARDUINO_ARCH_RP2040) && !defined(__MBED__)

bool I2C_eeprom::begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin)
{
if ((sda < 255) && (scl < 255))
{
_wire->setSCL(scl);
_wire->setSDA(sda);
_wire->begin();
}
_lastWrite = 0;
_writeProtectPin = writeProtectPin;
if (_writeProtectPin >= 0)
{
pinMode(_writeProtectPin, OUTPUT);
preventWrite();
}
return isConnected();
}

#endif


bool I2C_eeprom::begin(int8_t writeProtectPin)
{
// if (_wire == 0) Serial.println("zero"); // test #48
_wire->begin();
_lastWrite = 0;
_writeProtectPin = writeProtectPin;
if (_writeProtectPin >= 0)
Expand All @@ -121,6 +74,12 @@ bool I2C_eeprom::isConnected()
}


uint8_t I2C_eeprom::getAddress()
{
return _deviceAddress;
}


/////////////////////////////////////////////////////////////
//
// WRITE SECTION
Expand Down
11 changes: 3 additions & 8 deletions I2C_eeprom.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: I2C_eeprom.h
// AUTHOR: Rob Tillaart
// VERSION: 1.7.4
// VERSION: 1.8.0
// PURPOSE: Arduino Library for external I2C EEPROM 24LC256 et al.
// URL: https://github.com/RobTillaart/I2C_EEPROM.git

Expand All @@ -11,7 +11,7 @@
#include "Wire.h"


#define I2C_EEPROM_VERSION (F("1.7.4"))
#define I2C_EEPROM_VERSION (F("1.8.0"))


#define I2C_DEVICESIZE_24LC512 65536
Expand Down Expand Up @@ -59,15 +59,10 @@ class I2C_eeprom
*/
I2C_eeprom(const uint8_t deviceAddress, const uint32_t deviceSize, TwoWire *wire = &Wire);


// MBED test ==> see #55, #53
#if defined(ESP8266) || defined(ESP32) || (defined(ARDUINO_ARCH_RP2040) && !defined(__MBED__))
// set the I2C pins explicitly (overrule)
bool begin(uint8_t sda, uint8_t scl, int8_t writeProtectPin = -1);
#endif
// use default I2C pins.
bool begin(int8_t writeProtectPin = -1);
bool isConnected();
uint8_t getAddress();


// writes a byte to memoryAddress
Expand Down
23 changes: 12 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,16 @@ One from "Earle F. Philhower" and an "MBED" one. See issues #53 and #55 for deta

In 1.7.3 defines are checked to select between these two and as far as tested this seems
to solve the issue #53 while being backwards compatible.
If a better solution is found, it will be implemented.
If a better solution is found, it will be implemented.


#### Breaking change

Version 1.8.0 introduced a breaking change.
You cannot set the pins in **begin()** any more.
This reduces the dependency of processor dependent Wire implementations.
The user has to call **Wire.begin()** and can optionally set the Wire pins
before calling **I2C_eeprom.begin()**.


#### Links
Expand Down Expand Up @@ -76,13 +85,8 @@ Furthermore it checks if the deviceAddress is available on the I2C bus.
Returns true if deviceAddress is found on the bus, false otherwise.
Optionally one can set the **WP** writeProtect pin. (see section below).
If the **WP** pin is defined the default will be to **not** allow writing.
- **bool begin(uint8_t sda, uint8_t scl, uint8_t writeProtectPin = -1)** for ESP32 / ESP8266 / RP2040 and alike.
Initializes the I2C bus with the specified pins, thereby overruling the default pins.
Furthermore it checks if the deviceAddress is available on the I2C bus.
Returns true if deviceAddress is found on the bus, false otherwise.
Optionally one can set the **WP** writeProtect pin. (see section below).
If the **WP** pin is defined the default will be to **not** allow writing.
- **bool isConnected()** test to see if deviceAddress is found on the bus.
- **uint8_t getAddress()** returns device address set in the constructor.


### Write functions
Expand Down Expand Up @@ -195,7 +199,7 @@ than 5 milliseconds which is the minimum.
- **void setExtraWriteCycleTime(uint8_t ms)** idem
- **uint8_t getExtraWriteCycleTime()** idem

Since 1.7.2 it is also possible to adjust the **I2C_WRITEDELAY** in the .h file
It is also possible to adjust the **I2C_WRITEDELAY** in the .h file
or overrule the define on the command line.


Expand Down Expand Up @@ -252,21 +256,18 @@ See examples
- write functions should return bytes written or so.
- make deviceSize explicit in examples?


#### Could

- investigate smarter strategy for **updateBlock()**
=> find first and last changed position could possibly result in less writes.
- can **setBlock()** use strategies from **updateBlock()**


#### Wont

- investigate the print interface?
- circular buffer? (see FRAM library)
- dump function?


## Support

If you appreciate my libraries, you can support the development and maintenance.
Expand Down
26 changes: 20 additions & 6 deletions README_cyclic_store.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,37 @@
[![Arduino CI](https://github.com/RobTillaart/I2C_EEPROM/workflows/Arduino%20CI/badge.svg)](https://github.com/marketplace/actions/arduino_ci)
[![Arduino-lint](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/arduino-lint.yml/badge.svg)](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/arduino-lint.yml)
[![JSON check](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/jsoncheck.yml/badge.svg)](https://github.com/RobTillaart/I2C_EEPROM/actions/workflows/jsoncheck.yml)
[![GitHub issues](https://img.shields.io/github/issues/RobTillaart/I2C_EEPROM.svg)](https://github.com/RobTillaart/I2C_EEPROM/issues)

[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](https://github.com/RobTillaart/I2C_EEPROM/blob/master/LICENSE)
[![GitHub release](https://img.shields.io/github/release/RobTillaart/I2C_EEPROM.svg?maxAge=3600)](https://github.com/RobTillaart/I2C_EEPROM/releases)
[![PlatformIO Registry](https://badges.registry.platformio.org/packages/robtillaart/library/I2C_EEPROM.svg)](https://registry.platformio.org/libraries/robtillaart/I2C_EEPROM)


# I2C_eeprom_cyclic_store

Utility class for storing data using cyclic writes to eeprom memory
Utility class for storing data using cyclic writes to eeprom memory.


## Description

In order to maximize the life expectancy of an eeprom this class implements a method to spread the write operations across as much of the eeprom as possible, rather than writing to the same fixed location.
In order to maximize the life expectancy of an eeprom this class implements
a method to spread the write operations across as much of the eeprom as possible,
rather than writing to the same fixed location.

This is suitable for a situation where a single data structure (buffer), such as settings, configuration or metric data, is written to the eeprom.
This is suitable for a situation where a single data structure (buffer),
such as settings, configuration or metric data, is written to the eeprom.

It operates by partitioning the eeprom into slots large enough to hold the declared buffer (and header) and then writing each new version of the data to the next slot, overwriting any older version already in there. As it reaches the end of the alotted region of the eeprom it wraps around and starts writing from the start of the memory again. When initializing an instance it scans the eeprom to find the last written version and continues from that.
It operates by partitioning the eeprom into slots large enough to hold the
declared buffer (and header) and then writing each new version of the data
to the next slot, overwriting any older version already in there.
As it reaches the end of the alotted region of the eeprom it wraps around
and starts writing from the start of the memory again.
When initializing an instance it scans the eeprom to find the last written
version and continues from that.

In order to use an eeprom that already has data (and if the structure of the buffer changes) the eeprom has to be prepared by formatting the indexes.
In order to use an eeprom that already has data (and if the structure of the
buffer changes) the eeprom has to be prepared by formatting the indexes.

The interface is pretty straightforward

Expand All @@ -32,7 +45,8 @@ The interface is pretty straightforward

## Limitation

The class does not handle changes in buffer size or structure, nor does it detect an eeprom that has data that wasn't written using the class.
The class does not handle changes in buffer size or structure, nor does
it detect an eeprom that has data that wasn't written using the class.


## Operational
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/I2C_EEPROM.git"
},
"version": "1.7.4",
"version": "1.8.0",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=I2C_EEPROM
version=1.7.4
version=1.8.0
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Library for I2C EEPROMS
Expand Down

0 comments on commit 172f294

Please sign in to comment.