• Oh I see - maybe they would be part of the subsequent "If..." section...?

  • @bertjerred,

    you mean something like this (code for 4 bits / 4 touch buttons):

    function onInit() {
      
    console.log("example where bit 0 changed to 1 and bit 3 to 0:");
    
    // event handler functions for 4 bits 0..3 - one for each change to 0 and 1 
    var f0 = // ...turned to 0
    [ () => { console.log("0:0"); } // bit 0 changed to 0 (untouched)
    , () => { console.log("1:0"); } // bit 1 changed to 0 (untouched)
    , () => { console.log("2:0"); } // bit 2 changed to 0 (untouched)
    , () => { console.log("3:0"); } // bit 3 changed to 0 (untouched)
    ];
    var f1 = // ...turned to 1
    [ () => { console.log("0:1"); } // bit 0 changed to 1 (touched)
    , () => { console.log("1:1"); } // bit 1 changed to 1 (touched)
    , () => { console.log("2:1"); } // bit 2 changed to 1 (touched)
    , () => { console.log("3:1"); } // bit 3 changed to 1 (touched)
    ];
    
    // emulating the previously read (old) bits:
    var oldBits = 0b1010; // 10 = 8+2 = 2^3+2^1 - previously read
    // emulating the newly read bits (in poll or sensor interrupt handler):
    var newBits = 0b0011; //  3 = 2+1 = 2^2+2^0 - newly read
    // if-section:
    var difBits = oldBits ^ newBits; // 'xor': 10 ^ 3 =  0b1001 = 9 = 2^3+2^0
    var bit = -1, val = 1;
    while (++bit<4) { // 1,2,4,8 = 4 bits 0..3
      if (difBits & val) (newBits & val) ? f1[bit]() : f0[bit](); // ignore warn
      val<<=1;
    }
    oldBits = newBits;
    
    } // /onInit()
    
    setTimeout(onInit,999);
    

    Ignore the IDE warning for line #.

    There are other options: composing an attribute name from the bit and the bit's value identifying the respective bit handling function in a functions object:

    ...
    var fs = // functions object with bit handler functions as attributes
    { f00: () => console.log("0:0")
    , f01: () => console.log("0:1")
    , f10: () => console.log("1:0")
    , f12: () => console.log("1:1")
    ...
    , f30: () => console.log("3:0")
    , f38: () => console.log("3:1")
    ...
    var bit = -1, val = 1;
    while (++bit<4) { if  (difBits & val) fs["f"+bit+(newBits & val)](); val<<=1; }
    }
    

    To use just the value of the bit - think one bit shifted to the left:

    ...
    var fs = // functions object with bit handler functions as attributes
    { f20: () => console.log("0:0")
    , f22: () => console.log("0:1")
    , f40: () => console.log("1:0")
    , f44: () => console.log("1:1")
    ...
    , f160: () => console.log("3:0")
    , f1616: () => console.log("3:1")
    };
    ...
    var oldBits = 0b1010<<1; // 10 = 8+2 = 2^3+2^1 - previously read
    // emulating the newly read bits (in poll or sensor interrupt handler):
    var newBits = 0b0011<<1; //  3 = 2+1 = 2^2+2^0 - newly read
    ...
    val = 1;
    while ((val<<=1)<32) if  (difBits & val) fs["f"+val+(newBits & val)](); 
    ...
    

    Would be interesting to have some timing stats on the variations to find the least processing overhead - least amount of time to figure what function to call and the calling of it.

    Remember, Espruino interprets on the source - no JIT or other byte code compile - and therefore less source code bytes / statements to process in a loop, the faster the loop and the more responsive the behavior of the device.

About

Avatar for bertjerred @bertjerred started