In my case my Puck is notifying a .js app of a change of a float32 value.
I tried using print() but this converts a float32 into a ascii byte-string. This is sort of OK but rather messy for the app to have to convert back to a Float, not to mention potential loss of precision.
Is there a way to send the float in its natural 4-byte form so that converting back to float32 can be easily done by the app?
Yes, there are 2 easyish ways - Float32Array and DataView.
DataView is the nicest though:
var arr = new Uint8Array(4);
d = new DataView(arr.buffer) ;
print(arr) // it contains [68, 154, 64, 0]
You can pack lots of different types of data (double,float,int,short,byte,etc) with different endianness, all in the same array. Check out the DataView reference for the various methods: http://www.espruino.com/Reference#DataView
Don't worry about formatting, just type in the text and we'll take care of making sense of it. We will auto-convert links, and if you put asterisks around words we will make them bold.
For a full reference visit the Markdown syntax.
© Espruino, powered by microcosm.
Report a problem