Espruino Pico onInit problem

Posted on
  • I think a my be doing something wrong, when run the code from the IDE, everything runs fine. But, when i try to save the code, wrapping the initialization code in a function and calling it from the onInit function does not run unless I call it manually with load(). When I run manually load() everything runs fine too.

    The code is uggly, but it's working when i run it manually.

    //All VAR are declared globally
    function initializeall() {
      require("Font6x8").add(Graphics);
      require("Font8x16").add(Graphics);
      i2c = new I2C();
      i2c.setup({ scl : B6, sda: B7, bitrate: 1000000  });
      g = require("SSD1306").connect(i2c, start, { height : 32 });
      dht = require("DHT22").connect(B5);
      Start_read();
      pinMode(B15,'input_pullup');
      pinMode(B14,'input_pullup');
      pinMode(B13,'input_pullup');
      setWatch(selection, B15, { repeat: true, edge:'falling', debounce:20});
      setWatch(BtnUP, B14, { repeat: true, edge:'falling', debounce:20});
      setWatch(BtnDown, B13, { repeat: true, edge:'falling', debounce:20});
    
    }
    
    function onInit() {
      USB.setConsole(true);
      setTimeout(function () {
                    initializeall();
                    }, 
          1000);
    }
    

    Does anyone has a hint?

    Thanks

  • After upload, did you a) run it and then save it? - or b) save it right away?

    Save will after completion call onInit, can you verify that?

    I 'm sure you already worked through the Troubleshooting section...

  • After upload, did you a) run it and then save it? - or b) save it right away?
    I have tried both options.

    Save will after completion call onInit, can you verify that?
    Yes it does, in fact while connected it works very well. The problem happens when I connect it without a console, but in the code I do not have any call to write to the console.

    Compacting Flash...
    Calculating Size...
    Writing..
    Compressed 81600 bytes to 19967
    Running onInit()...
    

    if force the running mode with load(), works ok...

    >load()
    =undefined
    Loading 20034 bytes from flash...
    Running onInit()...
    

    I 'm sure you already worked through the Troubleshooting section...
    Yes I did but without success.

    Thanks for your help.

  • This is the full code


    1 Attachment

  • Looking further, The problem apparently lies in the initialization of SSD1306.

  • if I plug the USB, connect the IDE and execute "g = require("SSD1306").connect(i2c, start, { height : 32 });" from the console the display update all the information. Looking further to understand why...

  • OK, i found it. This SSD1306 module, apparently, need at least 1.5 seconds to stabilize.

  • Ic... you try to use it too early and then it fails.... this 1.5+ secs is probably to initialize all its components.

  • OK, i found it. This SSD1306 module, apparently, need at least 1.5 seconds to stabilize.

    Glad it's working now - so was the issue that you were calling g.flip before the SSD1306 had initialised? Or that you actually had to wait a few seconds before even initialising the SSD1306?

  • Tried to look at your uploaded compete code 'Fermentadora'... but don't kow how to look at it.

    I though did lookup the SSD1306 module and I found that the .connect(...) accepts a callback function as second argument... and when using this callback to call your code that has to be executed afterwards, it should time just right! (The module does internally already a lot of setTimeout(...) to manage timing... for you... when using the callback. If it still does not work, the times used in these setTimeout(...) should be adjusted in the module.)

    PS: ...could it be that start in connect should be something like the Start_read()?

    Without knowing all the details, I would give this restructured code a shot:

    var dht, i2c, g;
    function initializeall() {
      pinMode(B15,'input_pullup');
      pinMode(B14,'input_pullup');
      pinMode(B13,'input_pullup');
      dht = require("DHT22").connect(B5);
      require("Font6x8").add(Graphics);
      require("Font8x16").add(Graphics);
      i2c = new I2C();
      i2c.setup({ scl : B6, sda: B7, bitrate: 1000000  });
      g = require("SSD1306").connect(i2c, start, { height : 32 });
    }
    
    function start() {
      setWatch(selection, B15, { repeat: true, edge:'falling', debounce:20});
      setWatch(BtnUP    , B14, { repeat: true, edge:'falling', debounce:20});
      setWatch(BtnDown  , B13, { repeat: true, edge:'falling', debounce:20});
      Start_read();
    }
    
    // .....
    
    function onInit() {
      USB.setConsole(true);
      setTimeout(function () {
                    initializeall();
                    }, 
          1000);
    }
    
  • I will try to clean the code, and trace the callĀ“s order, and come back again tomorrow. But basically the call to start is in the require(ssd1306) callback.

    Thanks for your help.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Espruino Pico onInit problem

Posted by Avatar for Pinnchus @Pinnchus

Actions