• Surprised about duplicate setup in line 14 in post #7. I think that line should be removed...

    Same for the duplicate setup in post #8: line 19 should be removed.

    The time killer loops in lines 31..33, 39..41 and 51..53 cause problems because they hog the cpu and nothing else (js-wise) can happen during that time. Wait by burning cycles does not exist / does not work in Espruino... to the contrary, it will break the execution... 'Waiting' has to be done with 'deferred / timed-out resume (w/ setTimeout(...,...); ).

    Does the module have a reset command? If so, I would send that before sending the command for turning on the oscillator.

    Try something like that - my ugly chained steps with retries on timeouts...

    var ini = function() { // first
      require("Font4x6").add(Graphics);
      clearInterval(); // not sure you need that, only w/ arg
      clearWatch(); // not sure you need that, only w/ arg
      // initialisation I2C
      console.log("setup I2C");
      I2C1.setup({scl: B6,sda: B7});
      // pinMode(B6, 'opendrain'); // even though I suggested it, remove it
      /* rajouter les init de connexion pour le display */
      console.log("init programme affichage matrice v2.2 - éric choisy ©2019");
      console.log("mém. utilisée: "+Math.round(process.memory().usage/proc­ess.memory().total*100)+"%");
    }  
    , res = function(tries) { if (tries>0) { // second w/ retries
      try { console.log("reset");
        // I2C1.writeTo(0x70, 0x??); // reset
        setTimeout(osc,250,2);
      } catch(e) { console.log(e);
        setTimeout(res,250,--tries);
      } } else trow "Unable to reset";
    }
    , osc = function(tries) { if (tries>0) { // third w/ retries
      try { console.log("turn OSC ON");
        I2C1.writeTo(0x70, 0x21); // turn on oscillator
        setTimeout(don,250,2)
      } catch(e) { console.log(e);
        setTimeout(osc,250,--tries);
      } } else trow "Unable to turn osc on";
    }
    , don = function(tries) { if (tries>0) { // fourth w/ retries
      try { console.log("turn display ON");
        I2C1.writeTo(0x70, 0x81); // display on
        setTimeout(lum,250,2)
      } catch(e) { console.log(e);
        setTimeout(don,250,--tries);
      } } else trow "Unable to turn display ON";
    }
    , lum = function(tries) { if (tries>0) { // fifth w/ retries
      try { console.log("set brightness");
        I2C1.writeTo(0x70, 0xE0 | 0); // luminosité 0-15
        setTimeout(envoieMatrice, 1000);
      } catch(e) { console.log(e);
        setTimeout(lum,250,--tries);
      } } else throw "Unable to set brightness";
    }
    ;
    
    const onInit = function() {
      ini();
      res(2); // starts the chain \:
    };
    

    Another - better looking approach would be nested/chained promises w/ (global) retries that is set / reset at the right spots.

    PS: Code not tested (yet)

About

Avatar for allObjects @allObjects started