You are reading a single comment by @Gordon and its replies. Click here to read the full conversation.
  • 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.

About

Avatar for Gordon @Gordon started