Hi,
In the Intel Galileo Arduino IDE, there is a function, SPI.transferBuffer, which requires an array of the data you want to send. What I have been doing is this:
However, I'd like to save some time/space and was wondering if I can sort of create the array from the two bytes in the function, like one can use a byte cast, char cast, etc.
Is this possible?
Thanks
Um it is? The SPI.transferBuffer bit is irrelevant, I just want an way to cast an array temporarily. I don't want to have to actually declare a separate array at all.
indeed:
Hi,
In the Intel Galileo Arduino IDE, there is a function, SPI.transferBuffer, which requires an array of the data you want to send. What I have been doing is this:
byte xBuffer = {x, x};
SPI.transferBuffer(xBuffer, NULL, 2);
However, I'd like to save some time/space and was wondering if I can sort of create the array from the two bytes in the function, like one can use a byte cast, char cast, etc.
Is this possible?
Thanks
Which 2 bytes in the code snippet you posted do you want to create the array from. I assume the fact that there is no array in the code snippet is an error of transcription.
As usual, seeing a full example of what you are doing now would be helpful in suggesting possible solutions..
I'll edit that.
The reason I want to do this instead of just 2 SPI.transfers is that I heard it's quicker to use SPI.transferBuffer for multiple consecutive transfers (since the Galileo's SPI is pretty slow from the Arduino emulator). Whether that is true or not is sort of irrelevant since I'd like to know if what I'm asking is possible anyway.
indeed:
that approach would probably lead to a loss, and requires more code anyway...
Is there no way to do it inline?
I don't understand your objections. The approach you showed is a nice simple way to do it, and I don't see what 'loss' you're referring to or why the amount of code is excessive - it's a single line of code a couple of dozen characters long; how much shorter do you think you're going to get it?
It would be possible to work out the location of the two arguments in the calling frame and pass the address and byte count to your sending function, but it would be a horrible approach to take and just to avoid two byte assignments I don't see the justification.
Alright yes I know that would work, but when I said inline what I meant was like you can do with other types:
char var = 'h';
function((byte)var);
Which I guess technically does what I asked, but I just want to know if there's a default, built-in thing like the above for arrays, that requires no additional code, just like the above requires no additional code other than the standard libraries.
Anyway thank you for your input.
but I just want to know if there's a default, built-in thing like the above for arrays
No, there isn't. Think about what an array IS. It is a contiguous block of memory. A variable here and a variable there are not contiguous. Not amount of lying to the compiler (which is what casting is) will change that.