• I'd consider using ArrayBuffer.map. It's something that's on arrays in ES5, but has been extended to ArrayBuffers in ES6 (and Espruino :) ).

      LCD.reverse = function (a) {
        return h.map(function(i) { 
          return (i>>7|(i>>5&0x02)|(i>>3&0x04)|(i>>1&0x08)|(i<<1&0x10)|(i<<3&0x20)|(i<<5&0x40)|(i<<7&0x80));
        });
      }
    

    But for bit reversal you can be faster as well - there's a fun (and scary) website here full of tricks: http://graphics.stanford.edu/~seander/bithacks.html

      LCD.reverse = function (a) {
        return h.map(function(b) {return (((b*0x0802&0x22110)|(b*0x8020&0x88440))*0x10101>>16)&0xFF;});
      }
    

    You can do it even faster with 64 bit maths (which Espruino supports) but it's something that I may be removing soon to make Espruino faster, smaller, and more JS compliant so I won't post that here ;)

    Finally, you can use ArrayBuffers instead of Strings, and can predefine the code you want to add:

    var preamble = E.toArrayBuffer("DIM\x00\x00\x80\x40");
      LCD.flip = function () {
        var d = new Uint8Array(this.buffer.length+preamble.length);
        d.set(preamble,0);
        d.set(this.buffer,preamble.length);
        this.i2c.writeTo(0x27,d); 
      };  
    

    Hope that helps! I think I should probably add E.reverseByte as this comes up quite a lot. If that happens then you could be super fast and could just do: array.map(E.reverseByte)

    Of course there's always the inline assembler too ;)

About

Avatar for Gordon @Gordon started