http://qs1969.pair.com?node_id=1180093


in reply to Re: Is this a sane/safe way to pass an aref into a C function?
in thread Is this a sane/safe way to pass an aref into a C function?

Thanks Anonymonk, the bytes most definitely have individual meaning... MSByte is a control register (which actually consists of two 4-bit nibbles, but I dissect that later in the chain), and LSByte is the data register, both of a larger 16-bit command register, which gets sent to a piece of hardware over the Serial Peripheral Interface.

Example of what I was originally toying with:

# perl testing(65535, 4); ... // C #include <stdio.h> int testing(int n, int len){ unsigned char bytes[4]; bytes[3] = (n >> 24) & 0xFF; bytes[2] = (n >> 16) & 0xFF; bytes[1] = (n >> 8) & 0xFF; bytes[0] = n & 0xFF; printf("output should be 255, 255, 0, 0\n\n"); printf("%d, %d, %d, %d\n", bytes[0], bytes[1], bytes[2], bytes[3]) +; return 0; }

Works fine. The problem though is that the buf array is eventually sent to another C function that puts it on the SPI bus. The way I've done it in the above example limits me to accept/send only a single 4-byte array, so if a device requires say 6 bytes, I'd have to add in another param (at least that's how I saw it). I wanted my Perl code to be as compatible with the API I'm using as possible, and heck, I've always wanted to learn how to pass in an array/aref to a C function anyway, so it was a fun learning experience :)