• It is starting to make sense. Thanks again!

    Currently, the Espruino IDE is unhappy with the word, "function," as a 'reserved word,' and is giving red flags there and at the 100ms specification. I shall read up on setInterval and tinker more...

    Here are some bold (untested) modifications for my project, in case anyone wishes to see:

    I2C1.setup({scl:B8,sda:B9});
    I2C2.setup({scl:B10,sda:B11});
    
    var lastKeyPressed = 0;
    var lastBankSelected = 0;
    
    var mpr1 = require("MPR121").connect(I2C1, {
      //device 1 (12-key musical keyboard) is ready
      setInterval(function() {
        var keys = mpr1.touched();
        if (keys!=lastKeyPressed){
          for (var i=0;i<12;i++){
            var kbit = 1<<i;
            if ((keys&kbit) && !(lastKeyPressed&kbit))
              //do something, for example:
              console.log("Key: " + i);
          }
        }
        lastKeyPressed = keys;
      }, 100);
    });
                                         
    var mpr2 = require("MPR121").connect(I2C2, {
      //device 2 (12-position rotary switch) is ready
      setInterval(function() {
        var banks = mpr2.touched();
        if (banks!=lastBankSelected){
          for (var j=0;j<12;j++){
            var bbit = 1<<j;
            if ((banks&bbit) && !(lastBankSelected&bbit))
              //do something, for example:
              console.log("Bank: " + j);
          }
        }
        lastBankSelected = banks;
      }, 100);
    });
    
  • @bertjerred,

    two (2) issues there:

    • the .connect() arguments are not as expected - function object is expected - and
    • malformed themselves - not a proper object, that's why the syntax complain: no object property can be named function, because of function being a reserved word.

    The second argument of .connect() has to be a function() object and the code passes a malformed object { setInterval(... }.

    Small change fixes this.

    I2C1.setup({scl:B8,sda:B9});
    I2C2.setup({scl:B10,sda:B11});
    var mpr1;
    var mpr2;
    var lastKeyPressed = 0;
    var lastBankSelected = 0;
    
    function keyDev() {
      // device 1 (12-key musical keyboard) is ready
      setInterval(function() {
        var keys = mpr1.touched();
        if (keys!=lastKeyPressed){
          for (var i=0;i<12;i++){
            var kbit = 1<<i;
            if ((keys&kbit) && !(lastKeyPressed&kbit))
              // do something, for example:
              console.log("Key: " + i);
          }
        }
        lastKeyPressed = keys;
      }, 100);
    };
    
    funciton bankDev() {
      // device 2 (12-position rotary switch) is ready
      setInterval(function() {
        var banks = mpr2.touched();
        if (banks!=lastBankSelected){
          for (var j=0;j<12;j++){
            var bbit = 1<<j;
            if ((banks&bbit) && !(lastBankSelected&bbit))
              // do something, for example:
              console.log("Bank: " + j);
          }
        }
        lastBankSelected = banks;
      }, 100);
    };
    
    function onInit() {
      mpr1 = require("MPR121").connect(I2C1, keyDev);
      mpr2 = require("MPR121").connect(I2C2, bankDev);
    }
    
    setTimeout(onInit,999); // for dev; comment before save()
    

    If you do not like the extra ('external', non-anonymous, named) keyDev(){...} and bankDev(){}...} functions, you can define them inline as anonymous, just like in the setInterval(function(){...},...); function:

    I2C1.setup({scl:B8,sda:B9});
    I2C2.setup({scl:B10,sda:B11});
    var mpr1;
    var mpr2;
    var lastKeyPressed = 0;
    var lastBankSelected = 0;
    
    function onInit() {
      mpr1 = require("MPR121").connect(I2C1, function(){
        //device 1 (12-key musical keyboard) is ready
        setInterval(function() {
          var keys = mpr1.touched();
          if (keys!=lastKeyPressed){
            for (var i=0;i<12;i++){
              var kbit = 1<<i;
              if ((keys&kbit) && !(lastKeyPressed&kbit))
                //do something, for example:
                console.log("Key: " + i);
            }
          }
          lastKeyPressed = keys;
        }, 100);
      });
    
      mpr2 = require("MPR121").connect(I2C2, function() {
        //device 2 (12-position rotary switch) is ready
        setInterval(function() {
          var banks = mpr2.touched();
          if (banks!=lastBankSelected){
            for (var j=0;j<12;j++){
              var bbit = 1<<j;
              if ((banks&bbit) && !(lastBankSelected&bbit))
                //do something, for example:
                console.log("Bank: " + j);
            }
          }
          lastBankSelected = banks;
        }, 100);
      });
    }
    
    setTimeout(onInit,999); // for dev; comment before save()
    
About

Avatar for bertjerred @bertjerred started