I'm a C developer that need to access a FTDI device using the pyftdi library and as I try to manipulate the remote registers of the slaves. I found that it is impossible to execute bitwise operations (bitshift, NOT, AND and OR) on things other than int
in Python but I found the NumPy library that has data types that enable such functionality. My problem is that NumPy is very heavy on resources and I wonder if there is an alternative to such a heavy library.
I've tried the native BitField
of PyFtdi which doesn't have such functionalities and the ctypes library which neither has.
This is the kind of code where I would like not to use NumPy for:
def set_bit(variable, bit_ID):
variable |= np.ubyte(1 << bit_ID)
return variable
Again, the main issue is not that it doesn't work, it is that it works but is very heavy on ressources and I need the functionalities of a 8-bit variables with the bitwise operators without always switching data types.
I need that kind of variable to avoid casting multiple times my variables using pyftdi's functions:
Acquiring data from it in the Python-native bytes()
and converting it to int
.
Then using the bitwise operator and restricting to 8 bits the outputs.
Then converting back to bytes()
to send them back via the I2C API of PYFTDI.
Finally, I have resorted to cast each time the data types in one or another data type. It's not the most elegant answer to such a problem but it'll do for now. If someone ever has a miracle solution, fell free to post it ;)