Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only write if byte is different #9

Open
b-blake opened this issue Dec 26, 2021 · 8 comments
Open

Only write if byte is different #9

b-blake opened this issue Dec 26, 2021 · 8 comments

Comments

@b-blake
Copy link

b-blake commented Dec 26, 2021

Only Write to a byte if it is different. Read the byte and compare before writing the byte.

@FoamyGuy
Copy link
Collaborator

I do like this idea. It seems like it should save a little bit of wear on the device by not writing unless necessary.

Are you interested in submitting a PR with this change perhaps?

@b-blake
Copy link
Author

b-blake commented Dec 27, 2021

What is a PR? How do I submit one? Do I get and Brownie Points?

It will have the biggest impact when erasing an EEPROM.

@FoamyGuy
Copy link
Collaborator

@b-blake PR is Pull Request. Essentially it's proposing a change in the library that then can get reviewed by project maintainers and merged in if accepted.

There is a guide here: https://learn.adafruit.com/contribute-to-circuitpython-with-git-and-github/create-your-pull-request that gives a more in depth description and covers the process. That whole guide contains great information to get you started contributing to CircuitPython libraries.

@b-blake
Copy link
Author

b-blake commented Dec 27, 2021

@FoamyGuy OK, I have not modified your code to add this feature. I looked and am not sure where in the code a byte is actually written to the EEPROM. So I guess I will have to leave it as an idea the code manager considers for implementation. Thank you for the offer.

@FoamyGuy
Copy link
Collaborator

FoamyGuy commented Dec 28, 2021

@b-blake It writes to the EEPROM ultimately from here:

def __setitem__(self, address, value):
"""Write the value at the given starting index.
.. code-block:: python
# write single index
fram[0] = 1
# write values 0 thru 4 with a list
fram[0:4] = [0,1,2,3]
"""
if self.write_protected:
raise RuntimeError("FRAM currently write protected.")
if isinstance(address, int):
if not isinstance(value, int):
raise ValueError("Data stored in an address must be an integer 0-255")
if not 0 <= address < self._max_size:
raise ValueError(
"Address '{0}' out of range. It must be 0 <= address < {1}.".format(
address, self._max_size
)
)
self._write(address, value, self._wraparound)

The line near the end does the writing specifically. It could be put inside of an if statement that checks the address / value and then only calls _write if they aren't the same already.

@b-blake
Copy link
Author

b-blake commented Dec 28, 2021

FG,

I found that line/part. Where is a byte read?
I have yet to figure out how to read the byte that is about to be written.
I don't find a self._read(address) or similar that I can call and test on.
I figure I am 50% fluent in Python/CircuitPython. ;-)

BBB

@FoamyGuy
Copy link
Collaborator

I think you can read a value using square brackets just like it's done in the examples.

self[address] should read the value at the given address.

@b-blake
Copy link
Author

b-blake commented Dec 28, 2021

Thank you for the tip, however whoever wrote the code is smarter than me.
The self._write() call only handle's individual bytes. bytearrays are handled someplace else.
buffer = self[address] did not work. The code stopped at the new line of code, no error thrown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants