Switching between dev/standalone mode

Posted on
  • Hi all,

    I have trouble to understand when I would call save() load() and reset(). I've declared a few global variables at the top of my espruino code:

    var channel, led2State, debug;

    These variables are initialized in an onInit() function, that I typically call manually once I have moved the code over to the device. The onInit() function is called once the device starts up - e.g. after a reset button click, right?

    Now, I could also make the onIinit() call at the bottom of the code - once I flash the code over, it would execute the onInit() once, - just like after a reset. But a reset - if I understand correctly - will not execute the onInit() twice, as the call to onInit() is done by the system.

    Are these assumptions correct?
    Is it OK to declare global variables -what happens to these once I do a save()?


  • Hi, what you're doing is fine, however you may not have to set your global variables on onInit()...

    • If you declare a global variable, it will be saved (along with its value) just like a function or object definition would be - so declaring them is absolutely fine.
    • Also, any setInterval or setWatch that was created will be remembered
    • onInit() will just be called once by the system at startup, after a hard reset or a call to load or save.
    • reset will reset the device but won't load the saved code (so won't execute onInit).

    If you just send your code over to the device then you'll have to call onInit. However if you send your code to the device and then type save(), onInit will be called automatically

  • So the habit / pattern I developed now is this

    • change code in the IDE, transfer it over
    • in the console, call save() - then it is saved for "standalone" and I don't have to call init.

    --> so reset() - I would do this to get a "blank" espruino to play with. But it would not clear the persistent memory, so after a power on/off or reset button press, it would be back as it loads again.


  • Yes, that's it...

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

Switching between dev/standalone mode

Posted by Avatar for hansamann @hansamann