@Gordon 's solution is nicely oo... I was not thinking to go that far if not needed... two (or 3) functions with upfront storage definitions would have done the job for me, as something like this or variations of it:
var storage = new ArrayBuffer(80) // 10 x 32+16+16d bits 10 x 8 bytes
, store08 = new Uint8Array(storage) // 80 x 8 bits - just for display
, store32 = new Uint32Array(storage) // 20 x 32 bits
, store16 = new Uint16Array(storage) // 40 x 16 bits
;
// store time(ui32),temp1(u16),temp2(u16) Values
function storeVals(idx,time,temp1,temp2) {
store32[idx <<= 1] = time;
store32[++idx] = (temp2<<16) | temp1; // ...or analog to retrObj()
}
// store {time(u32),temp1(u16),temp2(u16)} Object
function storeObj(idx, obj) {
storeVals(idx,obj.time,obj.temp1,obj.temp2);
}
// retrieve as {time(ui32),temp1(u16),temp2(u16)} Object
function retrObj(idx) {
return (
{ time: store32[idx <<= 1]
, temp1: store16[idx = ++idx << 1]
, temp2: store16[++idx]
} );
}
To have even more convenience methods for retrieving is adding functions like getTim(idx), getTemp1(idx) and getTemp2(idx) with idx adjusting to respective store32 and store16.
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.
@Gordon 's solution is nicely oo... I was not thinking to go that far if not needed... two (or 3) functions with upfront storage definitions would have done the job for me, as something like this or variations of it:
To have even more convenience methods for retrieving is adding functions like getTim(idx), getTemp1(idx) and getTemp2(idx) with idx adjusting to respective store32 and store16.
PS: tested: see post #11