lsgunth / pyft232

Python bindings to d2xx and libftdi to access FT232 chips with the same interface as pyserial
GNU Lesser General Public License v2.1
11 stars 11 forks source link

SPI functions #12

Closed JulianS-Uni closed 5 years ago

JulianS-Uni commented 5 years ago

In a future project i will need the SPI. I will use d2xx since im on a windows machine. I will have to write some SPI functions then which i would like to contribute if everything goes well. Should i implement them like the cbus functions or better subclass the ftdi class and create a SPI class?

lsgunth commented 5 years ago

That would be cool. I don't really know the answer to your question. I guess if the SPI devices are special unlike other devices, then a subclass might make sense. On the other hand, if the subclass is only adding a couple functions, it's probably unnecessary.

Also, the real point behind this library is to allow for compatibility between d2xx and libftdi, so it's pretty important to have SPI implementations for both.

Thanks,

Logan

JulianS-Uni commented 5 years ago

I started coding some of my MPSSE and SPI stuff. What do you think about changing the ft232.write()function like this:

    def write(self, s):
        s = str(s)
        buf = c.create_string_buffer(s)
        written = c.c_ulong()
        status = d2xx.FT_Write(self.handle, buf, len(s), c.byref(written))
        if status != FT_OK: raise D2XXException(status)
        return written.value

So that write can be given a bytearray() or a bytes() list. I think this shouldn't even break implementations of other functions.

lsgunth commented 5 years ago

Well, that looks like it would be broken on python3 where c.create_string_buffer expects a bytes type, not a string type. And in python3, bytes types don't trivially convert to strings like that. Also, this is inline with what python-serial accepts.

You should really be developing on python3 as 2.7 is long dead.

Logan

JulianS-Uni commented 5 years ago

Ah I just got that your code is inline with python-serial. From python-serial it seems that I can directly pass a bytearray to the write() function.

Write the bytes data to the port. This should be of type bytes (or compatible such as bytearray or memoryview). Unicode strings must be encoded (e.g. 'hello'.encode('utf-8').

I actually am on python 3.7. I just can't test my code yet, because im still waiting for my FT232H module and the d2xx driver lib seems to only install with a detected ft2xx chip connected to the PC. I am pretty new to Python and and I never did anything with serial connections before. So bear with me when i ask simple questions. But I hope in the end I can be helpfull to this project.