Using SPI to talk to a ShiftBrites

I’ve just started dabbling with getting my imp to control a string of three shiftbrite 2.0 LEDs and I’m having a bit of a problem understanding how to convert my arduino code into squirrel.

The Shiftbrite 2.0 documentation is here: http://docs.macetech.com/doku.php/shiftbrite_2.0

It shifts in the bits for each led using spi, on my arduino I used the same function as shown in the tutorial code to do this:
`
void SB_SendPacket() {

if (SB_CommandMode == B01) {
 SB_RedCommand = 120;
 SB_GreenCommand = 100;
 SB_BlueCommand = 100;
}

SPDR = SB_CommandMode << 6 | SB_BlueCommand>>4;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_BlueCommand<<4 | SB_RedCommand>>6;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_RedCommand << 2 | SB_GreenCommand>>8;
while(!(SPSR & (1<<SPIF)));
SPDR = SB_GreenCommand;
while(!(SPSR & (1<<SPIF)));

}
`

However, i’m not sure how I can implement this on the imp. I started off by trying to something like this:

local tosend = blob(('B00' << 6) | (blue>>4)); tosend += blob((blue<<4)|(red>>6)); tosend += blob((red << 2 | (green >> 8)); tosend += blob(green); hardware.spi257.write(tosend );

But this did not work at all (Unsurprisingly I have very little experience with bit shifting and SPI in general!). I haven’t been able to find anything that helps me convert this type of bit shifting to squirrel so I was wondering if anyone has done something similar or knows of some imp functionality that could help me achieve this?

Thanks!

So, something like:

`
// assumes red, green, blue are 10 bit values, ie 0-1023.
local tosend = blob(4); // 4 byte blob = one packet to A6281 controller

// Pack values into 32 bits and write it to the blob. Think this writes little-endian though, which isn’t what we want
tosend.writen((green<<22)|(red<<12)|(blue<<2), ‘i’);

// swap endianness of blob so that red gets sent first. This would only need to be done once before sending
tosend.swap4();

// send it
hardware.spi257.write(tosend);`

Every packet is 32 bits, ie 4 bytes, and the addressing is done in the second-to-last bit sent.

Try that?

I think I’ll be able to get it working with your code, Thanks for your help!