Spin to C Translation Assistance, Please [SOLVED]
JonnyMac
Posts: 9,161
I have a Spin program that can extract five 16-bit Little Endian values from a byte array that is returned by a sensor. As Spin aligns variables of the same size in the order they're declared, I can update the five word variables with a single line.
Am I correct in thinking that a C struct maintains the alignment of the member variables as they're declared, in other words, can I do this and now that the memory layout is variable1.lowbyte, variable1.highbyte, variable2.lowbyte, ...
Thanks.
bytemove(@value1, @array[6], 10)
Am I correct in thinking that a C struct maintains the alignment of the member variables as they're declared, in other words, can I do this and now that the memory layout is variable1.lowbyte, variable1.highbyte, variable2.lowbyte, ...
struct block { uint16_t variable1; uint16_t variable2; uint16_t variable3; uint16_t variable4; uint16_t variable5; };And if that is the case, how can I move 10 bytes from array[6] to the structure? I have the K&R on my desk and am experimenting, but would appreciate some coaching from those with experience.
Thanks.
Comments
Mike
This if for the Pixy2 camera (used to be called CMU cam. It responds with a variable length array of bytes. In the case of a block (tracked target), the response will be six bytes (header) plus 14 bytes of block information -- I only care about the first 10 bytes which are unsigned, 16-bit values. Note that if the Pixy returns two targets, the stream will be 24 bytes: header (6) + block (14) + block (14), etc., up to the maximum number of blocks allowed to be returned in one call.
In another case, I pass two pointers to a function and update the variables like this:
This works great. I could duplicate this and pass five pointers for the block values, but that would make the function call unwieldy. I want to grab 10 consecutive bytes out of the stream based on the block (if multiples) that I want to extract.
In my demo app I have these variables defined:
This is the function that I'm attempting to use but it's giving me grief.
The program tells me I have blocks, but the data always comes back as 0.
Note that the compiler complains, too -- this is the output.
I doesn't like what I'm doing with memcpy, but I don't understand the error messages vis-a-vis type arguments.
Again, C is not my normal language -- please be patient and gentle with me.
Edit: You also need & in front of response[ofs] to get the address of that array element.
Do I need to change the way I'm defining my block variables in C? ATM, they are consecutive words -- just as in my Spin program.
I put the block variables into a struct to force their order in memory:
Here's the updated function that moves the values from in receive buffer to the block variables. Again, a buffer can contain more than one block, hence the use of an index.
Here's the little test loop that requests and displays blocks. All seems well now.