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

Converting to I2C #3

Open
wz2b opened this issue May 16, 2024 · 0 comments
Open

Converting to I2C #3

wz2b opened this issue May 16, 2024 · 0 comments

Comments

@wz2b
Copy link

wz2b commented May 16, 2024

I am trying to convert this library to work on an WS12850S that allegedly has the exact same software interface as the MFRC522 but using an I2C interface rather than SPI.

To do this, I replaced two functions:

    def _wreg(self, reg: int, val):
        data = bytearray([val])
        self.i2c.try_lock()
        self.i2c.writeto_then_readfrom(self.i2c_address, bytes([reg]), data)
        self.i2c.unlock()
        return data[0]

    def _rreg(self, reg: int):
        data = bytearray(1)
        self.i2c.try_lock()
        self.i2c.writeto_then_readfrom(self.i2c_address, bytes([reg]), data)
        self.i2c.unlock()
        return data[0]

Looks reasonable so far, for writing single bytes. I'm not sure the readback from _wreg is meaningful.

This kind of works, but I'm a little concerned about this:

    def _tocard(self, cmd: int, send):
       ...
        for c in send:
            self._wreg(0x09, c)

I think the intent there is to write a string of bytes (send) but in I2C I think I would want to do that as a single transaction, similar to what I did with my _wreg but taking an array of values rather than just a single byte. Does this make sense? Do you agree, and do you see other things that would need modification?

Finally ... if this works, is it worth abstracting out the physical interface so that we can re-use your code but select the interface (SPI or I2C) like:

	rdr = MFRC522( spiInterface )
	rdr = MFRC522( i2cInterface )

and both those interfaces include _wreg, _rreg, and possibly _tocard (and maybe more?)

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

1 participant