Did some adjustments and tested code in post #6. The main advantages of using pattern in post #6 vs. the other ones is that the creation of (view) objects happens only once and only indexes are used afterwards... 1st one works only when working all the time on the same piece of memory. Latter one can be combined with the pattern @Gordon a d @asez73 suggest. In real app, I would anyway hide these things in an StructXArray 'class' and implement methods as needed rather than just functions. Doing so, multiple structured objects can then coexist in a very easy to read form, as @TomWS points out. Final verdict regarding performance always depends on the implementation of Espruino, though,...
Below is the test code (html in browser), followed by output. The output visualizes nicely what is going on with the memory locations. In addition to the actually used Uint32 and Uint16 array, also the Uint8 array of the same memory is shown. I attached the arrayOfStrict.html. You can click on it and it runs directly in your browser and should show the same output...
<html>
<head>
<script>
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;
}
// 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]
} );
}
</script>
</head>
<body>
<h3>10 x Structuer w/ 32+16+16 bit</h3>
test / visualization:
<script>
document.write('<br><b>storeVals(0,65536,255,256); // @ index 0</b>');
storeVals(0,65536,255,256);
document.write('<br>--->store32:['+store32+']');
document.write('<br>--->store16:['+store16+']');
document.write('<br>--->store08:['+store08+']');
document.write('<br><b>storeObj(3,{time:65535,temp1:128,temp2:1040}); // @ index 3</b>');
storeObj(3,{time:65535,temp1:128,temp2:1040});
document.write('<br>--->store08:['+store08+']');
document.write('<br>--->store32:['+store32+']');
document.write('<br>--->store16:['+store16+']');
document.write('<br><b>obj1 = retrObj(0); // @ index 0</b>')
var obj1 = retrObj(0);
document.write('<br>---> {time:'+obj1.time+',temp1:'+obj1.temp1+',temp2:'+obj1.temp2+'}');
document.write('<br><b>obj2 = retrObj(3); // @ index 3</b>')
var obj2 = retrObj(3);
document.write('<br>---> {time:'+obj2.time+',temp1:'+obj2.temp1+',temp2:'+obj2.temp2+'}');
document.write('<br><b>obj3 = retrObj(9); // @ index 9</b>')
var obj3 = retrObj(9);
document.write('<br>---> {time:'+obj3.time+',temp1:'+obj3.temp1+',temp2:'+obj3.temp2+'}');
</script>
</body>
</html>
And the output:
10 x Structuer w/ 32+16+16 bit
test / visualization:
storeVals(0,65536,255,256); // @ index 0
--->store32:[65536,16777471,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
--->store16:[0,1,255,256,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
--->store08:[0,0,1,0,255,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
storeObj(3,{time:65535,temp1:128,temp2:1040}); // @ index 3
--->store08:[0,0,1,0,255,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,255,255,0,0,128,0,16,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
--->store32:[65536,16777471,0,0,0,0,65535,68157568,0,0,0,0,0,0,0,0,0,0,0,0]
--->store16:[0,1,255,256,0,0,0,0,0,0,0,0,65535,0,128,1040,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
obj1 = retrObj(0); // @ index 0
---> {time:65536,temp1:255,temp2:256}
obj2 = retrObj(3); // @ index 3
---> {time:65535,temp1:128,temp2:1040}
obj3 = retrObj(9); // @ index 9
---> {time:0,temp1:0,temp2:0}
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.
Did some adjustments and tested code in post #6. The main advantages of using pattern in post #6 vs. the other ones is that the creation of (view) objects happens only once and only indexes are used afterwards... 1st one works only when working all the time on the same piece of memory. Latter one can be combined with the pattern @Gordon a d @asez73 suggest. In real app, I would anyway hide these things in an StructXArray 'class' and implement methods as needed rather than just functions. Doing so, multiple structured objects can then coexist in a very easy to read form, as @TomWS points out. Final verdict regarding performance always depends on the implementation of Espruino, though,...
Below is the test code (html in browser), followed by output. The output visualizes nicely what is going on with the memory locations. In addition to the actually used Uint32 and Uint16 array, also the Uint8 array of the same memory is shown. I attached the arrayOfStrict.html. You can click on it and it runs directly in your browser and should show the same output...
And the output:
2 Attachments