If you can tell us what these structs are being used for in the original C
code, we might be able to point you to a suitable way to implement the same
thing efficiently in Python.
Stefan
>
> If you can tell us what these structs are being used for in the original C
> code, we might be able to point you to a suitable way to implement the
> same
> thing efficiently in Python.
>
> Stefan
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
It's a test program for testing the DSP (Digital Signal Processor).
Basically what it does is that it sends commands to the DSP via a driver and
waits for a response from the DSP. The structures represent the data that I
might receive from the DSP. For example if I send command A, DSP will send
data in the format of structa to me, so I will cast the data I received to
structa, and print the struct field values on screen. Each field in the
structures represent some status or info about the DSP, different bits in an
unsigned char or unsigned short field might represent different DSP status.
I just found out that there's a ctypes module
(http://docs.python.org/library/ctypes.html) in python that might solve my
problem. I understand that it is already included in Python 2.5 & I'm using
Python 2.6.2. However when I tried "from ctypes import *", I got "No module
named _ctypes". What am I missing here?
In Python, we cannot directly name bitfields within an int. However, we can
*read, set, and flip bits with the bit operators and bit masks
*define a dict that maps bitfield names to bit indexes
*define named functions that use the above.
*wrap or subclass int with a class that has attributes that map to a
bitfield.
I am pretty sure there is public code that does all of the above.
Searching pypi.python.org for 'bitfield', I found
BitDecoder 0.5.1
Decode bit-fields to human readable description
This program (and Python module) will decode a value as per a bitfield
definition. This is very useful for hardware registers that have
meanings to parts of the bits in seperate.
Google for more.
--
Terry Jan Reedy
Python has the "struct" module for formatting binary data.
http://docs.python.org/library/struct.html
But it doesn't handle bit fields. Its lowest resolution is
one byte.
John Nagle
That's really an excellent find. Thanks for bringing it up.
Geremy Condra