The issue is that Int8Array/etc are 'typed' arrays. They point to a fixed size chunk of memory. As a result, things like shift/unshift/push/pop that alter the length can't be implemented efficiently - in fact they're not even part of normal JavaScript for those arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8Array
Normal arrays let you do all that stuff, but then you pay for that flexibility with inefficient storage.
So, the solution is either to keep an index in the array and move around (shown on that Data Collection page), or to actually move all elements of the array backwards each time, and shove your data onto the end.
function log() {
ruuvi.setEnvOn(true);
// move data back 2 at a time
records.set(new Int8Array(records.buffer, 2/*bytes*/));
// add new data
records[records.length-2] = getTime();
records[records.length-1] = ruuvi.getEnvData().temp;
ruuvi.setEnvOn(false);
}
Also, since you have an array of bytes you can't just push the object {time:getTime(),temp:ruuvi.getEnvData().temp} into the array - you need to split it down into individual bytes manually as I have above.
At that point, getTime() won't be too useful since you can only store values -128..127, but the temperature will be fine. You could always use new Int32Array(length) to store bigger values though.
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.
The issue is that
Int8Array
/etc are 'typed' arrays. They point to a fixed size chunk of memory. As a result, things likeshift/unshift/push/pop
that alter the length can't be implemented efficiently - in fact they're not even part of normal JavaScript for those arrays: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int8ArrayNormal arrays let you do all that stuff, but then you pay for that flexibility with inefficient storage.
So, the solution is either to keep an index in the array and move around (shown on that Data Collection page), or to actually move all elements of the array backwards each time, and shove your data onto the end.
Also, since you have an array of bytes you can't just push the object
{time:getTime(),temp:ruuvi.getEnvData().temp}
into the array - you need to split it down into individual bytes manually as I have above.At that point,
getTime()
won't be too useful since you can only store values -128..127, but the temperature will be fine. You could always usenew Int32Array(length)
to store bigger values though.