Hi,
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.
Serial.write(X)
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[4] = 255; // buffer = [0,0,0,0,255,0,0,0]; i16[2] = 0x0102; // buffer = [0,0,0,0,2,1,0,0]; i32[1] = 0x01020304; // buffer = [0,0,0,0,4,3,2,1]; // Finally: Serial1.write(buffer);
You don't have to be 'aligned' either. You can do:
var x32 = new Int32Array(buffer,2 /* 2 bytes offset*/); x32[0] = 0x01020304; // buffer = [0,0,4,3,2,1,0,0];
Hope that helps!
@Gordon started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
Hi,
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:
You don't have to be 'aligned' either. You can do:
Hope that helps!