Serial.write(X) will always write 8 bits for numbers - but if you supply a string or array it'll write each element separately as 8 bits.
To do it 'nicely', I'd use Typed Arrays. The code would look a bit like:
var buffer = new ArrayBuffer(16); // 8 bytes
var i32 = new Int32Array(buffer);
var i16 = new Int16Array(buffer);
var i8 = new Int8Array(buffer);
i8 = 255;
// buffer = [0,0,0,0,255,0,0,0];
i16 = 0x0102;
// buffer = [0,0,0,0,2,1,0,0];
i32 = 0x01020304;
// buffer = [0,0,0,0,4,3,2,1];
You don't have to be 'aligned' either. You can do:
var x32 = new Int32Array(buffer,2 /* 2 bytes offset*/);
x32 = 0x01020304;
// buffer = [0,0,4,3,2,1,0,0];
Hope that helps!
© Espruino, powered by microcosm.
Report a problem